Guest User

Untitled

a guest
Aug 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. //csv.cpp
  2. #include "csv.h"
  3.  
  4. #include <stdio.h>
  5.  
  6. static void _split(std::string str, std::string d, std::vector<std::string> &dest)
  7. {
  8. int cut;
  9.  
  10. while((cut = str.find_first_of(d)) != str.npos)
  11. {
  12. if(cut > 0)
  13. dest.push_back(str.substr(0, cut));
  14. str = str.substr(cut + 1);
  15. }
  16. if(str.length() > 0)
  17. dest.push_back(str);
  18. }
  19.  
  20. void CSV::populate()
  21. {
  22. std::ifstream ifs(fname);
  23.  
  24. //Get column names.
  25. std::string line;
  26. if(ifs.is_open() && ifs.good())
  27. std::getline(ifs, line);
  28.  
  29. //Split it into col_names.
  30. _split(line, ";", col_names);
  31.  
  32. //Read rows and split them.
  33. int i;
  34. for(i = 0; ifs.good(); i++)
  35. {
  36. getline(ifs, line);
  37. std::vector<std::string> row;
  38. _split(line, ";", row);
  39. rows.push_back(row);
  40. }
  41. }
  42.  
  43. CSV::CSV(std::string _fname)
  44. : changed(false)
  45. {
  46. fname = _fname;
  47.  
  48. //Populate buffer.
  49. populate();
  50. }
  51.  
  52. CSV::~CSV()
  53. {
  54. //Write buffer back.
  55. writeback();
  56. }
  57.  
  58. void CSV::writeback()
  59. {
  60. //Only do a writeback if the data changed.
  61. if(changed == false)
  62. return;
  63.  
  64. std::ofstream ofs(fname);
  65. int i, j;
  66.  
  67. //Write column names.
  68. for(i = 0; i < col_names.size(); i++)
  69. {
  70. ofs << col_names[i];
  71. if(i < col_names.size() - 1)
  72. ofs << ";";
  73. }
  74. ofs << std::endl;
  75.  
  76. //Write rows.
  77. for(i = 0; i < rows.size(); i++)
  78. {
  79. //Write row.
  80. for(j = 0; j < rows[i].size(); j++)
  81. {
  82. ofs << rows[i][j];
  83. if(j < rows[i].size() - 1)
  84. ofs << ";";
  85. }
  86. if(i < rows.size() - 1)
  87. ofs << std::endl;
  88. }
  89. }
  90.  
  91. std::vector<std::string> &CSV::getColNames()
  92. {
  93. return col_names;
  94. }
  95.  
  96. std::vector<std::vector<std::string>> &CSV::getRows()
  97. {
  98. return rows;
  99. }
  100.  
  101. bool CSV::setCell(int row, int col, std::string value)
  102. {
  103. //Test if row and col are in range.
  104. if(row > rows.size() - 1)
  105. return false;
  106. if(col > rows[row].size() - 1)
  107. return false;
  108.  
  109. //Set value.
  110. rows[row][col] = value;
  111. changed = true;
  112.  
  113. return true;
  114. }
  115.  
  116. std::string CSV::getCell(int row, int col)
  117. {
  118. //Test if row and col are in range.
  119. if(row > rows.size() - 1)
  120. return "";
  121. if(col > rows[row].size() - 1)
  122. return "";
  123.  
  124. return rows[row][col];
  125. }
  126.  
  127. bool CSV::deleteRow(int row)
  128. {
  129. //Test if row is in range.
  130. if(row > rows.size() - 1)
  131. return false;
  132.  
  133. //Erase it.
  134. rows.erase(rows.begin() + row);
  135.  
  136. return true;
  137. }
  138.  
  139. bool CSV::isChanged()
  140. {
  141. return changed;
  142. }
  143.  
  144. //csv.h
  145. #pragma once
  146.  
  147. #include <iostream>
  148. #include <fstream>
  149. #include <string>
  150. #include <vector>
  151.  
  152. class CSV
  153. {
  154. private:
  155. //Filename.
  156. std::string fname;
  157. //Column names.
  158. std::vector<std::string> col_names;
  159. //Row buffers.
  160. std::vector<std::vector<std::string>> rows;
  161. //If this is true, the data has changed.
  162. bool changed;
  163.  
  164. //Populate buffer.
  165. void populate();
  166.  
  167. public:
  168. CSV(std::string _fname);
  169. ~CSV();
  170.  
  171. //Write buffer back to file.
  172. void writeback();
  173. //Get column names.
  174. std::vector<std::string> &getColNames();
  175. //Get rows.
  176. std::vector<std::vector<std::string>> &getRows();
  177. //Set cell value.
  178. bool setCell(int row, int col, std::string value);
  179. //Get cell value.
  180. std::string getCell(int row, int col);
  181. //Delete row.
  182. bool deleteRow(int row);
  183. //Get changed status.
  184. bool isChanged();
  185. };
Add Comment
Please, Sign In to add comment