csv.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <util/generic/yexception.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/generic/vector.h>
  5. #include <util/stream/input.h>
  6. /*
  7. Split string by rfc4180
  8. */
  9. namespace NCsvFormat {
  10. class TLinesSplitter {
  11. private:
  12. IInputStream& Input;
  13. const char Quote;
  14. public:
  15. TLinesSplitter(IInputStream& input, const char quote = '"')
  16. : Input(input)
  17. , Quote(quote) {
  18. }
  19. TString ConsumeLine();
  20. };
  21. class CsvSplitter {
  22. public:
  23. CsvSplitter(TString& data, const char delimeter = ',', const char quote = '"')
  24. // quote = '\0' ignores quoting in values and words like simple split
  25. : Delimeter(delimeter)
  26. , Quote(quote)
  27. , Begin(data.begin())
  28. , End(data.end())
  29. {
  30. }
  31. bool Step() {
  32. if (Begin == End) {
  33. return false;
  34. }
  35. ++Begin;
  36. return true;
  37. }
  38. TStringBuf Consume();
  39. explicit operator TVector<TString>() {
  40. TVector<TString> ret;
  41. do {
  42. TStringBuf buf = Consume();
  43. ret.push_back(TString{buf});
  44. } while (Step());
  45. return ret;
  46. }
  47. private:
  48. const char Delimeter;
  49. const char Quote;
  50. TString::iterator Begin;
  51. const TString::const_iterator End;
  52. TString CustomString;
  53. TVector<TStringBuf> CustomStringBufs;
  54. };
  55. }