You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.3 KiB

12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
  1. #include "zcsv.hpp"
  2. using namespace std;
  3. using namespace iflytop;
  4. ZCSV::ZCSV() {}
  5. bool ZCSV::parseCSV(string filename) {
  6. csvData.clear();
  7. ifstream file(filename);
  8. string line;
  9. if (!file.is_open()) {
  10. return false;
  11. }
  12. int rowNum = -1;
  13. while (getline(file, line)) {
  14. rowNum = rowNum + 1;
  15. stringstream linestream(line);
  16. string cell;
  17. ZCSVCell csvCell;
  18. csvCell.rowNum = rowNum;
  19. int colNum = -1;
  20. while (getline(linestream, cell, ',')) {
  21. colNum = colNum + 1;
  22. csvCell.colNum = colNum;
  23. csvCell.data = cell;
  24. csvData.push_back(csvCell);
  25. }
  26. csvData.push_back(csvCell);
  27. }
  28. // 找到最大行数
  29. int maxRowNum = -1;
  30. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  31. if (it->rowNum > maxRowNum) {
  32. maxRowNum = it->rowNum;
  33. }
  34. }
  35. // 找到最大列数
  36. int maxColNum = -1;
  37. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  38. if (it->colNum > maxColNum) {
  39. maxColNum = it->colNum;
  40. }
  41. }
  42. m_maxRowNum = maxRowNum;
  43. m_maxColNum = maxColNum;
  44. return true;
  45. }
  46. ZCSVCell* ZCSV::findCell(int rowNum, int colNum) {
  47. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  48. if (it->rowNum == rowNum && it->colNum == colNum) {
  49. return &(*it);
  50. }
  51. }
  52. return NULL;
  53. }
  54. void ZCSV::setdata(int rowNum, int colNum, string data) {
  55. ZCSVCell* cell = findCell(rowNum, colNum);
  56. if (cell != NULL) {
  57. cell->data = data;
  58. } else {
  59. ZCSVCell newCell;
  60. newCell.rowNum = rowNum;
  61. newCell.colNum = colNum;
  62. newCell.data = data;
  63. if (rowNum > m_maxRowNum) {
  64. m_maxRowNum = rowNum;
  65. }
  66. if (colNum > m_maxColNum) {
  67. m_maxColNum = colNum;
  68. }
  69. csvData.push_back(newCell);
  70. }
  71. }
  72. string ZCSV::getdata(int rowNum, int colNum) {
  73. ZCSVCell* cell = findCell(rowNum, colNum);
  74. if (cell != NULL) {
  75. return cell->data;
  76. } else {
  77. return "";
  78. }
  79. }
  80. int ZCSV::findCol(string key) {
  81. int colNum = -1;
  82. for (int i = 0; i <= m_maxColNum; i++) {
  83. ZCSVCell* cell = findCell(0, i);
  84. if (cell != NULL && cell->data == key) {
  85. colNum = i;
  86. break;
  87. }
  88. }
  89. return colNum;
  90. }
  91. bool ZCSV::isColExist(string key) {
  92. int colNum = findCol(key);
  93. if (colNum != -1) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. string ZCSV::getData(string key, int rowNum) {
  99. int colNum = findCol(key);
  100. if (colNum == -1) {
  101. return "";
  102. }
  103. return getdata(rowNum, colNum);
  104. }
  105. vector<string> ZCSV::getRowKeys() {
  106. vector<string> keys;
  107. for (int i = 0; i <= m_maxColNum; i++) {
  108. ZCSVCell* cell = findCell(0, i);
  109. if (cell != NULL) {
  110. keys.push_back(cell->data);
  111. }
  112. }
  113. return keys;
  114. }
  115. int ZCSV::forceFindRow(string name) {
  116. for (int i = 0; i <= m_maxRowNum; i++) {
  117. ZCSVCell* cell = findCell(i, 0);
  118. if (cell != NULL && cell->data == name) {
  119. return i;
  120. }
  121. }
  122. // 强制添加一行
  123. int targetRow = m_maxRowNum + 1;
  124. if (targetRow == 0) targetRow = 1;
  125. setdata(targetRow, 0, name);
  126. return targetRow;
  127. }
  128. int ZCSV::forceFindCol(string name) {
  129. for (int i = 0; i <= m_maxColNum; i++) {
  130. ZCSVCell* cell = findCell(0, i);
  131. if (cell != NULL && cell->data == name) {
  132. return i;
  133. }
  134. }
  135. // 强制添加一列
  136. int targetCol = m_maxColNum + 1;
  137. if (targetCol == 0) targetCol = 1;
  138. setdata(0, targetCol, name);
  139. return targetCol;
  140. }
  141. void ZCSV::setData(string rowName, string colName, string data) {
  142. int rowNum = forceFindRow(rowName);
  143. int colNum = forceFindCol(colName);
  144. setdata(rowNum, colNum, data);
  145. }
  146. void ZCSV::dumpCSV(string filename) {
  147. ofstream file;
  148. file.open(filename, ios::out | ios::trunc);
  149. // 找到最大行数
  150. int maxRowNum = -1;
  151. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  152. if (it->rowNum > maxRowNum) {
  153. maxRowNum = it->rowNum;
  154. }
  155. }
  156. // 找到最大列数
  157. int maxColNum = -1;
  158. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  159. if (it->colNum > maxColNum) {
  160. maxColNum = it->colNum;
  161. }
  162. }
  163. // 足个点插入数据
  164. for (int i = 0; i <= maxRowNum; i++) {
  165. for (int j = 0; j <= maxColNum; j++) {
  166. ZCSVCell* cell = findCell(i, j);
  167. if (cell != NULL) {
  168. file << cell->data;
  169. }
  170. file << ",";
  171. }
  172. file << endl;
  173. }
  174. }