RunDataLite.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "RunDataLite.h"
  2. #include <regex>
  3. #include <iostream>
  4. RunDataLite::RunDataLite(std::string filePath): fileStream(filePath), filePath(filePath)
  5. {
  6. std::string file = filePath.substr(filePath.find_last_of("/\\") + 1);
  7. name = file.substr(0, file.rfind("."));
  8. if (!fileStream.is_open())
  9. {
  10. //Cant open file
  11. badFileFlag = true;
  12. std::cout << "Cannot open File.";
  13. return;
  14. }
  15. /* Start extracting */
  16. while (fileStream.peek() != EOF) {
  17. std::string buffer;
  18. getLine(buffer);
  19. SolutionPointData sol;
  20. std::regex regexIter("i:([\\+\\-]?\\d+)");
  21. std::regex regexObjectiveFunction("of:([\\+\\-]?\\d+\\.\\d+(?:E[\\+\\-]\\d+)?)");
  22. std::smatch match;
  23. if (!std::regex_search(buffer, match, regexIter)) {
  24. std::cout << "Bad formatatted Line[" << actualLine << "].";
  25. std::cout << "Failed to matched:";
  26. std::cout << buffer;
  27. return;
  28. }
  29. sol.iteration = std::stoi(match[1]);
  30. if (!std::regex_search(buffer, match, regexObjectiveFunction)) {
  31. std::cout << "Bad formatatted Line[" << actualLine << "].";
  32. std::cout << "Failed to matched:";
  33. std::cout << buffer;
  34. return;
  35. }
  36. sol.objectiveFunction = std::stod(match[1]);
  37. std::regex regexParticleNumber("pN:([\\+\\-]?\\d+)");
  38. if (!std::regex_search(buffer, match, regexParticleNumber)) {
  39. std::cout << "Bad formatatted Line[" << actualLine << "].";
  40. std::cout << "Failed to matched:";
  41. std::cout << buffer;
  42. return;
  43. }
  44. sol.particleNumber = std::stoi(match[1]);
  45. std::regex regexBitVec("b:([01]+)");
  46. if (!std::regex_search(buffer, match, regexBitVec)) {
  47. std::cout << "Bad formatatted Line[" << actualLine << "].";
  48. std::cout << "Failed to matched:";
  49. std::cout << buffer;
  50. return;
  51. }
  52. sol.bitVec.resize(match[1].length());
  53. int count = 0;
  54. std::string str = match[1];
  55. for (std::string::size_type i = 0; i < str.size(); ++i) {
  56. sol.bitVec[i] = (str[i] == '1');
  57. }
  58. solutionVec.push_back(sol);
  59. }
  60. fileStream.close();
  61. }
  62. bool RunDataLite::badFile()
  63. {
  64. return badFileFlag;
  65. }
  66. void RunDataLite::getLine(std::string& bufferString)
  67. {
  68. std::getline(fileStream, bufferString);
  69. actualLine++;
  70. }