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.

130 lines
2.7 KiB

12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 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. }