Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <map>
  6. #include <vector>
  7.  
  8.  
  9. void Tokenize(const string& str,
  10. vector<string>& tokens,
  11. const string& delimiters = " ")
  12. {
  13. // Skip delimiters at beginning.
  14. string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  15. // Find first "non-delimiter".
  16. string::size_type pos = str.find_first_of(delimiters, lastPos);
  17.  
  18. while (string::npos != pos || string::npos != lastPos)
  19. {
  20. // Found a token, add it to the vector.
  21. tokens.push_back(str.substr(lastPos, pos - lastPos));
  22. // Skip delimiters. Note the "not_of"
  23. lastPos = str.find_first_not_of(delimiters, pos);
  24. // Find next "non-delimiter"
  25. pos = str.find_first_of(delimiters, lastPos);
  26. }
  27. }
  28.  
  29.  
  30.  
  31. class csvReader;
  32.  
  33. class csvRow {
  34. public:
  35. csvRow(std::vector<std::string> _data, std::map<std::string, int> *_header) {data = _data;header = _header; empty = false;}
  36. csvRow(bool _empty = false) {empty = _empty;}
  37. ~csvRow() {}
  38.  
  39. // TODO: extend below function to report failure when a col_name not present in headers is used.
  40. std::string operator[](const std::string &_col_name) const {return data[header->operator[](_col_name)];}
  41. std::string operator[](int _col_index) const {return data[_col_index];}
  42. bool isEmpty() {return empty;}
  43. private:
  44. bool empty;
  45. std::map<std::string, int> *header;
  46. std::vector<std::string> data;
  47. friend class csvReader;
  48. };
  49.  
  50. class csvReader {
  51. public:
  52. csvReader(std::string _filename);
  53. ~csvReader() {}
  54. csvRow next();
  55. private:
  56. std::ifstream ifs;
  57. std::map<std::string, int> header;
  58. };
  59.  
  60. csvReader::csvReader(std::string _filename) {
  61. ifs.open(_filename.c_str());
  62. std::vector<std::string> header_row = next().data;
  63. std::vector<std::string>::iterator ii;
  64. int i;
  65. for (ii = header_row.begin(), i = 0;
  66. ii != header_row.end();
  67. ii++, i++) {
  68. header[*ii] = i;
  69. }
  70. }
  71.  
  72. csvRow csvReader::next() {
  73.  
  74. std::vector<std::string> result;
  75. std::string line;
  76.  
  77. if(!ifs.eof())
  78. {
  79. std::getline(ifs, line);
  80. std::cout << line << endl;
  81. Tokenize(line,result,",");
  82. }
  83. else
  84. {
  85. ifs.close();
  86. }
  87.  
  88. // 1) Make the function exit gracefully when there are no more rows in the csv.
  89. // (i.e. end of file is reached.)
  90. // 2) Each row is read into 'line' var. Split it by ',' and insert to result vector, so that the following insert would make sense.
  91.  
  92. return csvRow(result, &header);
  93. }
  94.  
  95.  
  96. int main(void) {
  97. csvReader rdr("test.csv");
  98. csvRow current_row = rdr.next();
  99. while (!current_row.isEmpty()) {
  100. std::cout << "User: " << current_row["user"] << std::endl
  101. << "Pass: " << current_row["password"] << std::endl;
  102. current_row = rdr.next();
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement