123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- #include "AlgorithmDB.h"
- #include <fstream>
- #include <sstream>
- using namespace std;
- void AlgorithmDB::InitAlgorithmDB(memepp::native_string& folderPath)
- {
- //TODO: to be native_string
- #if MG_OS__WIN_AVAIL
- m_filename = "C:/AlgotithmInfo.db";
- m_filename_tmp = "C:/AlgotithmInfo_tmp.db";
- #else
- m_filename = folderPath + "/AlgotithmInfo.db";
- m_filename = folderPath + "/AlgotithmInfo_tmp.db";
- #endif
- }
- void AlgorithmDB::AddRecord(const Record& record)
- {
- ofstream file(m_filename, ios::app);
- if (file.is_open())
- {
- file << record.strAlgorithmId << "," << record.strEquipId << ","
- << record.strThresholdFormula << "," << record.strNewThresholdFormula;
- for (const auto& collectPoint : record.collectPointVec)
- {
- file << "," << collectPoint;
- }
- file << "\n";
- file.close();
- cout << "Record added successfully.\n";
- }
- else {
- cerr << "Unable to open file.\n";
- }
- }
- vector<AlgorithmDB::Record> AlgorithmDB::GetAllRecords()
- {
- vector<Record> records;
- ifstream file(m_filename);
- if (file.is_open())
- {
- string line;
- while (getline(file, line))
- {
- size_t pos = line.find(",");
- string algorithmId = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string equipId = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string thresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string newThresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- vector<string> collectPointVec;
- while ((pos = line.find(",")) != string::npos)
- {
- string collectPoint = line.substr(0, pos);
- collectPointVec.push_back(collectPoint);
- line = line.substr(pos + 1);
- }
- collectPointVec.push_back(line);
- Record record{ algorithmId, equipId, thresholdFormula, newThresholdFormula, collectPointVec };
- records.push_back(record);
- }
- file.close();
- }
- else {
- cerr << "GetAllRecords:Unable to open file.\n";
- }
- return records;
- }
- void AlgorithmDB::DeleteRecord(const string& algorithmId)
- {
- ifstream fileIn(m_filename);
- ofstream fileOut(m_filename_tmp);
- int delcount = 0;
- if (fileIn.is_open() && fileOut.is_open())
- {
- string line;
- while (getline(fileIn, line))
- {
- size_t pos = line.find(",");
- string recordAlgorithmId = line.substr(0, pos);
- if (recordAlgorithmId != algorithmId)
- {
- fileOut << line << endl;
- }
- else
- {
- delcount++;
- }
- }
- fileIn.close();
- fileOut.close();
- remove(m_filename.c_str());
- rename(m_filename_tmp.c_str(), m_filename.c_str());
- if(delcount > 0 )
- std::cout << "Record deleted "<< delcount <<"successfully.\n";
- else
- std::cout << "Delete:No find Record " << algorithmId << ".\n";
- }
- else {
- cerr << "Unable to open file.\n";
- }
- }
- vector<AlgorithmDB::Record> AlgorithmDB::SearchRecord(const string& algorithmId)
- {
- vector<AlgorithmDB::Record> foundRecords;
- ifstream file(m_filename);
- if (file.is_open())
- {
- string line;
- while (getline(file, line))
- {
- size_t pos = line.find(",");
- string recordAlgorithmId = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string equipId = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string thresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string newThresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- vector<string> collectPointVec;
- while ((pos = line.find(",")) != string::npos)
- {
- string collectPoint = line.substr(0, pos);
- collectPointVec.push_back(collectPoint);
- line = line.substr(pos + 1);
- }
- collectPointVec.push_back(line);
- if (recordAlgorithmId == algorithmId) {
- Record record{ recordAlgorithmId, equipId, thresholdFormula, newThresholdFormula, collectPointVec };
- foundRecords.push_back(record);
- }
- }
- file.close();
- }
- else {
- cerr << "Unable to open file.\n";
- }
- return foundRecords;
- }
- void AlgorithmDB::UpdateRecord(const string& algorithmId, const Record& newRecord)
- {
- ifstream fileIn(m_filename);
- ofstream fileOut(m_filename_tmp);
- int updateCount = 0;
- if (fileIn.is_open() && fileOut.is_open())
- {
- string line;
- while (getline(fileIn, line)) {
- size_t pos = line.find(",");
- string recordAlgorithmId = line.substr(0, pos);
- if (recordAlgorithmId == algorithmId)
- {
- line = line.substr(pos + 1);
- pos = line.find(",");
- string equipId = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string thresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- pos = line.find(",");
- string newThresholdFormula = line.substr(0, pos);
- line = line.substr(pos + 1);
- vector<string> collectPointVec;
- while ((pos = line.find(",")) != string::npos)
- {
- string collectPoint = line.substr(0, pos);
- collectPointVec.push_back(collectPoint);
- line = line.substr(pos + 1);
- }
- collectPointVec.push_back(line);
- fileOut << newRecord.strAlgorithmId << "," << newRecord.strEquipId << ","
- << newRecord.strThresholdFormula << "," << newRecord.strNewThresholdFormula;
- for (const auto& collectPoint : newRecord.collectPointVec) {
- fileOut << "," << collectPoint;
- }
- fileOut << "\n";
- updateCount++;
- }
- else
- {
- fileOut << line << endl;
- }
- }
- fileIn.close();
- fileOut.close();
- remove(m_filename.c_str());
- rename(m_filename_tmp.c_str(), m_filename.c_str());
- if (updateCount > 0)
- std::cout << "Record updated " << updateCount << "successfully.\n";
- else
- std::cout << "Updata:No find Record " << algorithmId << ".\n";
- }
- else
- {
- cerr << "Unable to open file.\n";
- }
- }
|