restable: add menu entry to save table as csv file

This commit is contained in:
Jean-Francois Dockes
2011-08-18 19:02:39 +02:00
parent 4318891b48
commit 9c5886c7c4
4 changed files with 110 additions and 5 deletions
+40 -2
View File
@@ -320,6 +320,36 @@ template void stringsToString<list<string> >(const list<string> &, string &);
template void stringsToString<vector<string> >(const vector<string> &,string &);
template void stringsToString<set<string> >(const set<string> &, string &);
template <class T> void stringsToCSV(const T &tokens, string &s,
char sep)
{
s.erase();
for (typename T::const_iterator it = tokens.begin();
it != tokens.end(); it++) {
bool needquotes = false;
if (it->empty() ||
it->find_first_of(string(1, sep) + "\"\n") != string::npos)
needquotes = true;
if (it != tokens.begin())
s.append(1, sep);
if (needquotes)
s.append(1, '"');
for (unsigned int i = 0; i < it->length(); i++) {
char car = it->at(i);
if (car == '"') {
s.append(2, '"');
} else {
s.append(1, car);
}
}
if (needquotes)
s.append(1, '"');
}
}
template void stringsToCSV<list<string> >(const list<string> &, string &, char);
template void stringsToCSV<vector<string> >(const vector<string> &,string &,
char);
void stringToTokens(const string& str, vector<string>& tokens,
const string& delims, bool skipinit)
{
@@ -1045,7 +1075,7 @@ int main(int argc, char **argv)
cerr << "[" << *it << "] ";
cerr << endl;
exit(0);
#elif 1
#elif 0
if (argc <=0 ) {
cerr << "Usage: smallut <dateinterval>" << endl;
exit(1);
@@ -1126,7 +1156,15 @@ int main(int argc, char **argv)
in = "a: %a title: %(title) pcpc: %% %";
pcSubst(in, out, substs);
cout << "After map clear: " << in << " => " << out << endl;
#elif 1
list<string> tokens;
tokens.push_back("");
tokens.push_back("a,b");
tokens.push_back("simple value");
tokens.push_back("with \"quotes\"");
string out;
stringsToCSV(tokens, out);
cout << "CSV line: [" << out << "]" << endl;
#endif
}
+7
View File
@@ -93,6 +93,13 @@ template <class T> bool stringToStrings(const string& s, T &tokens,
*/
template <class T> void stringsToString(const T &tokens, string &s);
/**
* Strings to CSV string. tokens containing the separator are quoted (")
* " inside tokens is escaped as "" ([word "quote"] =>["word ""quote"""]
*/
template <class T> void stringsToCSV(const T &tokens, string &s,
char sep = ',');
/**
* Split input string. No handling of quoting
*/