CSVparser.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //#include "stdafx.h"
  2. #include <fstream>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include "CSVparser.h"
  6. namespace csv {
  7. Parser::Parser(const std::string &data, const DataType &type, char sep)
  8. : _type(type), _sep(sep)
  9. {
  10. std::string line;
  11. if (type == eFILE)
  12. {
  13. _file = data;
  14. std::ifstream ifile(_file.c_str());
  15. if (ifile.is_open())
  16. {
  17. while (ifile.good())
  18. {
  19. getline(ifile, line);
  20. if (line != "")
  21. _originalFile.push_back(line);
  22. }
  23. ifile.close();
  24. if (_originalFile.size() == 0)
  25. throw Error(std::string("No Data in ").append(_file));
  26. parseHeader();
  27. parseContent();
  28. }
  29. else
  30. throw Error(std::string("Failed to open ").append(_file));
  31. }
  32. else
  33. {
  34. std::istringstream stream(data);
  35. while (std::getline(stream, line))
  36. if (line != "")
  37. _originalFile.push_back(line);
  38. if (_originalFile.size() == 0)
  39. throw Error(std::string("No Data in pure content"));
  40. parseHeader();
  41. parseContent();
  42. }
  43. }
  44. Parser::~Parser(void)
  45. {
  46. std::vector<Row *>::iterator it;
  47. for (it = _content.begin(); it != _content.end(); it++)
  48. delete *it;
  49. }
  50. void Parser::parseHeader(void)
  51. {
  52. std::stringstream ss(_originalFile[0]);
  53. std::string item;
  54. while (std::getline(ss, item, _sep))
  55. _header.push_back(item);
  56. }
  57. void Parser::parseContent(void)
  58. {
  59. std::vector<std::string>::iterator it;
  60. it = _originalFile.begin();
  61. it++; // skip header
  62. for (; it != _originalFile.end(); it++)
  63. {
  64. bool quoted = false;
  65. int tokenStart = 0;
  66. unsigned int i = 0;
  67. Row *row = new Row(_header);
  68. for (; i != it->length(); i++)
  69. {
  70. if (it->at(i) == '"')
  71. quoted = ((quoted) ? (false) : (true));
  72. else if (it->at(i) == ',' && !quoted)
  73. {
  74. row->push(it->substr(tokenStart, i - tokenStart));
  75. tokenStart = i + 1;
  76. }
  77. }
  78. //end
  79. row->push(it->substr(tokenStart, it->length() - tokenStart));
  80. // if value(s) missing
  81. if (row->size() != _header.size())
  82. throw Error("corrupted data !");
  83. _content.push_back(row);
  84. }
  85. }
  86. Row &Parser::getRow(unsigned int rowPosition) const
  87. {
  88. if (rowPosition < _content.size())
  89. return *(_content[rowPosition]);
  90. throw Error("can't return this row (doesn't exist)");
  91. }
  92. Row &Parser::operator[](unsigned int rowPosition) const
  93. {
  94. return Parser::getRow(rowPosition);
  95. }
  96. unsigned int Parser::rowCount(void) const
  97. {
  98. return _content.size();
  99. }
  100. unsigned int Parser::columnCount(void) const
  101. {
  102. return _header.size();
  103. }
  104. std::vector<std::string> Parser::getHeader(void) const
  105. {
  106. return _header;
  107. }
  108. const std::string Parser::getHeaderElement(unsigned int pos) const
  109. {
  110. if (pos >= _header.size())
  111. throw Error("can't return this header (doesn't exist)");
  112. return _header[pos];
  113. }
  114. bool Parser::deleteRow(unsigned int pos)
  115. {
  116. if (pos < _content.size())
  117. {
  118. delete *(_content.begin() + pos);
  119. _content.erase(_content.begin() + pos);
  120. return true;
  121. }
  122. return false;
  123. }
  124. bool Parser::addRow(unsigned int pos, const std::vector<std::string> &r)
  125. {
  126. Row *row = new Row(_header);
  127. for (auto it = r.begin(); it != r.end(); it++)
  128. row->push(*it);
  129. if (pos <= _content.size())
  130. {
  131. _content.insert(_content.begin() + pos, row);
  132. return true;
  133. }
  134. return false;
  135. }
  136. void Parser::sync(void) const
  137. {
  138. if (_type == DataType::eFILE)
  139. {
  140. std::ofstream f;
  141. f.open(_file, std::ios::out | std::ios::trunc);
  142. // header
  143. unsigned int i = 0;
  144. for (auto it = _header.begin(); it != _header.end(); it++)
  145. {
  146. f << *it;
  147. if (i < _header.size() - 1)
  148. f << ",";
  149. else
  150. f << std::endl;
  151. i++;
  152. }
  153. for (auto it = _content.begin(); it != _content.end(); it++)
  154. f << **it << std::endl;
  155. f.close();
  156. }
  157. }
  158. const std::string &Parser::getFileName(void) const
  159. {
  160. return _file;
  161. }
  162. /*
  163. ** ROW
  164. */
  165. Row::Row(const std::vector<std::string> &header)
  166. : _header(header) {}
  167. Row::~Row(void) {}
  168. unsigned int Row::size(void) const
  169. {
  170. return _values.size();
  171. }
  172. void Row::push(const std::string &value)
  173. {
  174. _values.push_back(value);
  175. }
  176. bool Row::set(const std::string &key, const std::string &value)
  177. {
  178. std::vector<std::string>::const_iterator it;
  179. int pos = 0;
  180. for (it = _header.begin(); it != _header.end(); it++)
  181. {
  182. if (key == *it)
  183. {
  184. _values[pos] = value;
  185. return true;
  186. }
  187. pos++;
  188. }
  189. return false;
  190. }
  191. const std::string Row::operator[](unsigned int valuePosition) const
  192. {
  193. if (valuePosition < _values.size())
  194. return _values[valuePosition];
  195. throw Error("can't return this value (doesn't exist)");
  196. }
  197. const std::string Row::operator[](const std::string &key) const
  198. {
  199. std::vector<std::string>::const_iterator it;
  200. int pos = 0;
  201. for (it = _header.begin(); it != _header.end(); it++)
  202. {
  203. if (key == *it)
  204. return _values[pos];
  205. pos++;
  206. }
  207. throw Error("can't return this value (doesn't exist)");
  208. }
  209. std::ostream &operator<<(std::ostream &os, const Row &row)
  210. {
  211. for (unsigned int i = 0; i != row._values.size(); i++)
  212. os << row._values[i] << " | ";
  213. return os;
  214. }
  215. std::ofstream &operator<<(std::ofstream &os, const Row &row)
  216. {
  217. for (unsigned int i = 0; i != row._values.size(); i++)
  218. {
  219. os << row._values[i];
  220. if (i < row._values.size() - 1)
  221. os << ",";
  222. }
  223. return os;
  224. }
  225. }