CSVReader.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "CSVReader.h"
  2. #include <sstream>
  3. #include <algorithm>
  4. // Erstellt den Leser
  5. // path: Der Pfad der CSV Datei
  6. CSVReader::CSVReader( std::string path )
  7. : stream( path )
  8. {
  9. stream.seekg( 0, std::ios_base::end );
  10. byteCount = stream.tellg();
  11. stream.seekg( 0, std::ios_base::beg );
  12. }
  13. // Gibt die nächste Zeile der CSV Datei zurück
  14. std::vector< std::string > CSVReader::getNextRow()
  15. {
  16. std::string line;
  17. int tmp = stream.tellg();
  18. std::getline( stream, line );
  19. line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
  20. byteCount -= (int)stream.tellg() - tmp;
  21. std::stringstream lineStream( line );
  22. std::vector< std::string > result;
  23. std::string cell;
  24. while(std::getline(lineStream,cell, ';'))
  25. result.push_back(cell);
  26. return result;
  27. }
  28. // Gibt true zurück, falls es noch eine Zeile gibt
  29. bool CSVReader::hasNext() const
  30. {
  31. return stream && !stream.eof() && byteCount > 0;
  32. }
  33. // Gibt den Fortschritt des lesens in Prozent zurück
  34. int CSVReader::getProgress()
  35. {
  36. return (int)(((int)stream.tellg() / ((int)stream.tellg() + (double)byteCount)) * 100);
  37. }