configFile.cpp 1.6 KB

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