Skip to content

Commit 7a311a2

Browse files
quote CSV strings containing separators, and double quote quotes within. For #1743.
1 parent eaae240 commit 7a311a2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

engine/variableValue.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ namespace minsky
428428
}
429429
of<<quoted("RavelHypercube=["+os.str()+"]")<<endl;
430430
for (const auto& i: hypercube().xvectors)
431-
of<<"\""<<i.name<<"\",";
431+
of<<CSVQuote(i.name,',')<<",";
432432
of<<"value$\n";
433433

434434
auto idxv=index();

model/minsky.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ namespace minsky
9090
redrawAllGodleyTables();
9191
return m_multipleEquities;
9292
}
93-
93+
9494
void Minsky::openLogFile(const string& name)
9595
{
9696
outputDataFile.reset(new ofstream(name));
9797
*outputDataFile<< "#time";
9898
for (auto& v: variableValues)
9999
if (logVarList.contains(v.first))
100-
*outputDataFile<<" "<<v.second->name;
100+
*outputDataFile<<" "<<CSVQuote(v.second->name,' ');
101101
*outputDataFile<<endl;
102102
}
103103

str.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ namespace minsky
152152
s.seekg(0); //rewind input stream
153153
}
154154

155+
/// quotes a string if it contains a separator character, and double quotes quotes
156+
inline std::string CSVQuote(const std::string& x, char sep)
157+
{
158+
std::string r;
159+
bool needsQuoting=false;
160+
for (auto c: x)
161+
{
162+
r+=c;
163+
if (c=='"') {r+='"'; needsQuoting=true;}
164+
if (c==sep) needsQuoting=true;
165+
}
166+
if (needsQuoting) return "\""+r+"\"";
167+
return r;
168+
}
155169

156170
}
157171
#endif

0 commit comments

Comments
 (0)