fork(2) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <iterator>
  8.  
  9. using namespace std;
  10.  
  11. istream& get_cell(istream& is, string& s)
  12. {
  13. char c;
  14. is >> c; // skips ws
  15. is.unget();
  16.  
  17. if (c == '\'')
  18. return is >> quoted(s, '\'', '\\');
  19. else
  20. return getline(is, s, ','), is.unget();
  21. }
  22.  
  23. vector<string> get(const string& s)
  24. {
  25. istringstream iss{ s };
  26. string cell;
  27. vector<string> r;
  28. while (get_cell(iss, cell))
  29. {
  30. r.push_back(cell);
  31. char comma;
  32. iss >> comma;
  33. if (comma != ',')
  34. break;
  35. }
  36.  
  37. char c;
  38. if (iss >> c)
  39. throw "ill formed";
  40.  
  41. return r;
  42. }
  43.  
  44. int main()
  45. {
  46. string s = "1, 10, 'abc', 'test, 1'";
  47. try
  48. {
  49. auto v = get(s);
  50. copy (v.begin(), v.end(), ostream_iterator<string>(cout,"\n"));
  51. }
  52. catch (const char* e)
  53. {
  54. cout << e;
  55. }
  56. }
Success #stdin #stdout 0s 4888KB
stdin
Standard input is empty
stdout
1
10
abc
test, 1