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.

126 lines
2.7 KiB

1 year 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 = 0;
  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 = 0;
  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 = 0;
  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 = 0;
  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. void ZCSV::dumpCSV(string filename) {
  81. ofstream file;
  82. file.open(filename, ios::out | ios::trunc);
  83. // 找到最大行数
  84. int maxRowNum = 0;
  85. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  86. if (it->rowNum > maxRowNum) {
  87. maxRowNum = it->rowNum;
  88. }
  89. }
  90. // 找到最大列数
  91. int maxColNum = 0;
  92. for (list<ZCSVCell>::iterator it = csvData.begin(); it != csvData.end(); it++) {
  93. if (it->colNum > maxColNum) {
  94. maxColNum = it->colNum;
  95. }
  96. }
  97. // 足个点插入数据
  98. for (int i = 1; i <= maxRowNum; i++) {
  99. for (int j = 1; j <= maxColNum; j++) {
  100. ZCSVCell* cell = findCell(i, j);
  101. if (cell != NULL) {
  102. file << cell->data;
  103. }
  104. file << ",";
  105. }
  106. file << endl;
  107. }
  108. }