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 <class T> void stringsToString(const T& tokens, string& s) { for (typename T::const_iterator it = tokens.begin(); it != tokens.end(); it++) { bool hasblanks = false; if (it->find_first_of(" \t\n") != string::npos) { hasblanks = true; } if (it != tokens.begin()) { s.append(1, ' '); } if (hasblanks) { s.append(1, '"'); } for (unsigned int i = 0; i < it->length(); i++) { char car = it->at(i); if (car == '"') { s.append(1, '\\'); s.append(1, car); } else { s.append(1, car); } } if (hasblanks) { s.append(1, '"'); } } }