configFile.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <string.h>
  2. #include "Helper.h"
  3. #include "configFile.h"
  4. ConfigFile::ConfigFile(std::string filePath)
  5. {
  6. std::string config = FormatFileName(filePath);
  7. pFile = OpenFileAndWait(config.c_str(), "r");
  8. }
  9. ConfigFile::~ConfigFile()
  10. {
  11. fclose(pFile);
  12. }
  13. bool ConfigFile::isNewParagraph(std::string input)
  14. {
  15. if ((input[0] == '[') || ((input[0] == ';') && (input[1] == '[')))
  16. {
  17. return true;
  18. }
  19. return false;
  20. }
  21. bool ConfigFile::GetNextParagraph(std::string& aktparamgraph, bool &disabled, bool &eof)
  22. {
  23. while (getNextLine(&aktparamgraph, disabled, eof) && !isNewParagraph(aktparamgraph));
  24. if (isNewParagraph(aktparamgraph))
  25. return true;
  26. return false;
  27. }
  28. bool ConfigFile::getNextLine(std::string *rt, bool &disabled, bool &eof)
  29. {
  30. eof = false;
  31. char zw[1024];
  32. if (pFile == NULL)
  33. {
  34. *rt = "";
  35. return false;
  36. }
  37. fgets(zw, 1024, pFile);
  38. printf("%s", zw);
  39. if ((strlen(zw) == 0) && feof(pFile))
  40. {
  41. *rt = "";
  42. eof = true;
  43. return false;
  44. }
  45. *rt = zw;
  46. *rt = trim(*rt);
  47. while ((zw[0] == ';' || zw[0] == '#' || (rt->size() == 0)) && !(zw[1] == '[')) // Kommentarzeilen (; oder #) und Leerzeilen überspringen, es sei denn es ist ein neuer auskommentierter Paragraph
  48. {
  49. fgets(zw, 1024, pFile);
  50. printf("%s", zw);
  51. if (feof(pFile))
  52. {
  53. *rt = "";
  54. eof = true;
  55. return false;
  56. }
  57. *rt = zw;
  58. *rt = trim(*rt);
  59. }
  60. disabled = ((*rt)[0] == ';');
  61. return true;
  62. }
  63. std::vector<string> ConfigFile::ZerlegeZeile(std::string input, std::string delimiter)
  64. {
  65. std::vector<string> Output;
  66. // std::string delimiter = " =,";
  67. input = trim(input, delimiter);
  68. size_t pos = findDelimiterPos(input, delimiter);
  69. std::string token;
  70. while (pos != std::string::npos) {
  71. token = input.substr(0, pos);
  72. token = trim(token, delimiter);
  73. Output.push_back(token);
  74. input.erase(0, pos + 1);
  75. input = trim(input, delimiter);
  76. pos = findDelimiterPos(input, delimiter);
  77. }
  78. Output.push_back(input);
  79. return Output;
  80. }