INI.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Created by xcbosa on 2023/1/30.
  3. //
  4. #include "INI.h"
  5. #include "utils.h"
  6. #include "strop.h"
  7. namespace xc {
  8. namespace utils {
  9. INISection::INISection(): title(), data() { }
  10. INISection::INISection(string title, map<string, string> data): title(title), data(data) { }
  11. string INISection::get(string key) {
  12. return this->data[key];
  13. }
  14. bool INISection::has(string key) {
  15. return this->data.count(key) == 1;
  16. }
  17. void INISection::set(string key, string value) {
  18. this->data[key] = value;
  19. }
  20. string INISection::getTitle() {
  21. return this->title;
  22. }
  23. void INISection::setTitle(string title) {
  24. this->title = title;
  25. }
  26. INI::INI(): data() { }
  27. INI::INI(string str) {
  28. vector<string> lines = split(str, "\n");
  29. string sectionName = "";
  30. map<string, string> currentSection;
  31. for (string it : lines) {
  32. string line = it;
  33. replace_all(line, "\r", "");
  34. if (line.length() == 0) { continue; }
  35. if (line[0] == '[') {
  36. if (sectionName.length() > 0 || !currentSection.empty()) {
  37. this->data.push_back(INISection(sectionName, currentSection));
  38. sectionName.clear();
  39. currentSection.clear();
  40. }
  41. ostringstream oss;
  42. for (string::iterator it = line.begin() + 1; it != line.end() && *it != ']'; it++) {
  43. oss << *it;
  44. }
  45. sectionName = oss.str();
  46. continue;
  47. }
  48. ostringstream left, right;
  49. bool isRight = false;
  50. for (int i = 0; i < line.length(); i++) {
  51. if (isRight) {
  52. right << line[i];
  53. continue;
  54. }
  55. if (line[i] == '=') {
  56. isRight = true;
  57. continue;
  58. }
  59. left << line[i];
  60. }
  61. string leftStr = left.str();
  62. string rightStr = right.str();
  63. trim(leftStr);
  64. trim(rightStr);
  65. currentSection[leftStr] = rightStr;
  66. }
  67. if (sectionName.length() > 0 || !currentSection.empty()) {
  68. this->data.push_back(INISection(sectionName, currentSection));
  69. sectionName.clear();
  70. currentSection.clear();
  71. }
  72. }
  73. INI::INI(vector<INISection> data): data(data) { }
  74. INI readFromFile(string filePath) {
  75. return INI(contentsOfTextFile(filePath));
  76. }
  77. bool INI::has(string header) {
  78. return this->get(header) != nullptr;
  79. }
  80. INISection* INI::get(string header) {
  81. for (auto it = this->data.begin(); it != this->data.end(); it++) {
  82. if (it->title == header) {
  83. return &*it;
  84. }
  85. }
  86. return nullptr;
  87. }
  88. INISection *INI::getMust(string header) {
  89. INISection *section = this->get(header);
  90. if (section == nullptr) {
  91. this->data.push_back(INISection(header, map<string, string>()));
  92. return this->getMust(header);
  93. }
  94. return section;
  95. }
  96. void INI::remove(string header) {
  97. std::remove_if(this->data.begin(), this->data.end(), [&header](auto it) {
  98. return it.title == header;
  99. });
  100. }
  101. string INI::getINIString() {
  102. ostringstream oss;
  103. for (INISection section : this->data) {
  104. oss << '[';
  105. oss << section.title;
  106. oss << "]\n";
  107. for (auto it : section.data) {
  108. oss << it.first;
  109. oss << " = ";
  110. oss << it.second;
  111. oss << '\n';
  112. }
  113. oss << '\n';
  114. }
  115. return oss.str();
  116. }
  117. INIFile::INIFile(string filePath): INI(contentsOfTextFile(filePath)) {
  118. this->filePath = filePath;
  119. }
  120. void INIFile::save() {
  121. saveTextFile(this->filePath, this->getINIString());
  122. }
  123. } // xc
  124. } // utils