AlgorithmDB.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "AlgorithmDB.h"
  2. #include <fstream>
  3. #include <sstream>
  4. using namespace std;
  5. void AlgorithmDB::InitAlgorithmDB(memepp::native_string& folderPath)
  6. {
  7. //TODO: to be native_string
  8. #if MG_OS__WIN_AVAIL
  9. m_filename = "C:/AlgotithmInfo.db";
  10. m_filename_tmp = "C:/AlgotithmInfo_tmp.db";
  11. #else
  12. m_filename = folderPath + "/AlgotithmInfo.db";
  13. m_filename = folderPath + "/AlgotithmInfo_tmp.db";
  14. #endif
  15. }
  16. void AlgorithmDB::AddRecord(const Record& record)
  17. {
  18. ofstream file(m_filename, ios::app);
  19. if (file.is_open())
  20. {
  21. file << record.strAlgorithmId << "," << record.strEquipId << ","
  22. << record.strThresholdFormula << "," << record.strNewThresholdFormula;
  23. for (const auto& collectPoint : record.collectPointVec)
  24. {
  25. file << "," << collectPoint;
  26. }
  27. file << "\n";
  28. file.close();
  29. cout << "Record added successfully.\n";
  30. }
  31. else {
  32. cerr << "Unable to open file.\n";
  33. }
  34. }
  35. vector<AlgorithmDB::Record> AlgorithmDB::GetAllRecords()
  36. {
  37. vector<Record> records;
  38. ifstream file(m_filename);
  39. if (file.is_open())
  40. {
  41. string line;
  42. while (getline(file, line))
  43. {
  44. size_t pos = line.find(",");
  45. string algorithmId = line.substr(0, pos);
  46. line = line.substr(pos + 1);
  47. pos = line.find(",");
  48. string equipId = line.substr(0, pos);
  49. line = line.substr(pos + 1);
  50. pos = line.find(",");
  51. string thresholdFormula = line.substr(0, pos);
  52. line = line.substr(pos + 1);
  53. pos = line.find(",");
  54. string newThresholdFormula = line.substr(0, pos);
  55. line = line.substr(pos + 1);
  56. vector<string> collectPointVec;
  57. while ((pos = line.find(",")) != string::npos)
  58. {
  59. string collectPoint = line.substr(0, pos);
  60. collectPointVec.push_back(collectPoint);
  61. line = line.substr(pos + 1);
  62. }
  63. collectPointVec.push_back(line);
  64. Record record{ algorithmId, equipId, thresholdFormula, newThresholdFormula, collectPointVec };
  65. records.push_back(record);
  66. }
  67. file.close();
  68. }
  69. else {
  70. cerr << "GetAllRecords:Unable to open file.\n";
  71. }
  72. return records;
  73. }
  74. void AlgorithmDB::DeleteRecord(const string& algorithmId)
  75. {
  76. ifstream fileIn(m_filename);
  77. ofstream fileOut(m_filename_tmp);
  78. int delcount = 0;
  79. if (fileIn.is_open() && fileOut.is_open())
  80. {
  81. string line;
  82. while (getline(fileIn, line))
  83. {
  84. size_t pos = line.find(",");
  85. string recordAlgorithmId = line.substr(0, pos);
  86. if (recordAlgorithmId != algorithmId)
  87. {
  88. fileOut << line << endl;
  89. }
  90. else
  91. {
  92. delcount++;
  93. }
  94. }
  95. fileIn.close();
  96. fileOut.close();
  97. remove(m_filename.c_str());
  98. rename(m_filename_tmp.c_str(), m_filename.c_str());
  99. if(delcount > 0 )
  100. std::cout << "Record deleted "<< delcount <<"successfully.\n";
  101. else
  102. std::cout << "Delete:No find Record " << algorithmId << ".\n";
  103. }
  104. else {
  105. cerr << "Unable to open file.\n";
  106. }
  107. }
  108. vector<AlgorithmDB::Record> AlgorithmDB::SearchRecord(const string& algorithmId)
  109. {
  110. vector<AlgorithmDB::Record> foundRecords;
  111. ifstream file(m_filename);
  112. if (file.is_open())
  113. {
  114. string line;
  115. while (getline(file, line))
  116. {
  117. size_t pos = line.find(",");
  118. string recordAlgorithmId = line.substr(0, pos);
  119. line = line.substr(pos + 1);
  120. pos = line.find(",");
  121. string equipId = line.substr(0, pos);
  122. line = line.substr(pos + 1);
  123. pos = line.find(",");
  124. string thresholdFormula = line.substr(0, pos);
  125. line = line.substr(pos + 1);
  126. pos = line.find(",");
  127. string newThresholdFormula = line.substr(0, pos);
  128. line = line.substr(pos + 1);
  129. vector<string> collectPointVec;
  130. while ((pos = line.find(",")) != string::npos)
  131. {
  132. string collectPoint = line.substr(0, pos);
  133. collectPointVec.push_back(collectPoint);
  134. line = line.substr(pos + 1);
  135. }
  136. collectPointVec.push_back(line);
  137. if (recordAlgorithmId == algorithmId) {
  138. Record record{ recordAlgorithmId, equipId, thresholdFormula, newThresholdFormula, collectPointVec };
  139. foundRecords.push_back(record);
  140. }
  141. }
  142. file.close();
  143. }
  144. else {
  145. cerr << "Unable to open file.\n";
  146. }
  147. return foundRecords;
  148. }
  149. void AlgorithmDB::UpdateRecord(const string& algorithmId, const Record& newRecord)
  150. {
  151. ifstream fileIn(m_filename);
  152. ofstream fileOut(m_filename_tmp);
  153. int updateCount = 0;
  154. if (fileIn.is_open() && fileOut.is_open())
  155. {
  156. string line;
  157. while (getline(fileIn, line)) {
  158. size_t pos = line.find(",");
  159. string recordAlgorithmId = line.substr(0, pos);
  160. if (recordAlgorithmId == algorithmId)
  161. {
  162. line = line.substr(pos + 1);
  163. pos = line.find(",");
  164. string equipId = line.substr(0, pos);
  165. line = line.substr(pos + 1);
  166. pos = line.find(",");
  167. string thresholdFormula = line.substr(0, pos);
  168. line = line.substr(pos + 1);
  169. pos = line.find(",");
  170. string newThresholdFormula = line.substr(0, pos);
  171. line = line.substr(pos + 1);
  172. vector<string> collectPointVec;
  173. while ((pos = line.find(",")) != string::npos)
  174. {
  175. string collectPoint = line.substr(0, pos);
  176. collectPointVec.push_back(collectPoint);
  177. line = line.substr(pos + 1);
  178. }
  179. collectPointVec.push_back(line);
  180. fileOut << newRecord.strAlgorithmId << "," << newRecord.strEquipId << ","
  181. << newRecord.strThresholdFormula << "," << newRecord.strNewThresholdFormula;
  182. for (const auto& collectPoint : newRecord.collectPointVec) {
  183. fileOut << "," << collectPoint;
  184. }
  185. fileOut << "\n";
  186. updateCount++;
  187. }
  188. else
  189. {
  190. fileOut << line << endl;
  191. }
  192. }
  193. fileIn.close();
  194. fileOut.close();
  195. remove(m_filename.c_str());
  196. rename(m_filename_tmp.c_str(), m_filename.c_str());
  197. if (updateCount > 0)
  198. std::cout << "Record updated " << updateCount << "successfully.\n";
  199. else
  200. std::cout << "Updata:No find Record " << algorithmId << ".\n";
  201. }
  202. else
  203. {
  204. cerr << "Unable to open file.\n";
  205. }
  206. }