configFile.cpp 1.5 KB

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