ConfigFile.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibCore/ConfigFile.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/StandardPaths.h>
  30. #include <pwd.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. namespace Core {
  34. NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
  35. {
  36. String home_path = StandardPaths::home_directory();
  37. auto path = String::format("%s/%s.ini", home_path.characters(), app_name.characters());
  38. return adopt(*new ConfigFile(path));
  39. }
  40. NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
  41. {
  42. auto path = String::format("/etc/%s.ini", app_name.characters());
  43. return adopt(*new ConfigFile(path));
  44. }
  45. NonnullRefPtr<ConfigFile> ConfigFile::open(const String& path)
  46. {
  47. return adopt(*new ConfigFile(path));
  48. }
  49. ConfigFile::ConfigFile(const String& file_name)
  50. : m_file_name(file_name)
  51. {
  52. reparse();
  53. }
  54. ConfigFile::~ConfigFile()
  55. {
  56. sync();
  57. }
  58. void ConfigFile::reparse()
  59. {
  60. m_groups.clear();
  61. auto file = File::construct(m_file_name);
  62. if (!file->open(IODevice::OpenMode::ReadOnly))
  63. return;
  64. HashMap<String, String>* current_group = nullptr;
  65. while (file->can_read_line()) {
  66. auto line = file->read_line(BUFSIZ);
  67. auto* cp = (const char*)line.data();
  68. while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
  69. ++cp;
  70. switch (*cp) {
  71. case '\0': // EOL...
  72. case '#': // Comment, skip entire line.
  73. case ';': // -||-
  74. continue;
  75. case '[': { // Start of new group.
  76. StringBuilder builder;
  77. ++cp; // Skip the '['
  78. while (*cp && (*cp != ']'))
  79. builder.append(*(cp++));
  80. current_group = &m_groups.ensure(builder.to_string());
  81. break;
  82. }
  83. default: { // Start of key{
  84. StringBuilder key_builder;
  85. StringBuilder value_builder;
  86. while (*cp && (*cp != '='))
  87. key_builder.append(*(cp++));
  88. ++cp; // Skip the '='
  89. while (*cp && (*cp != '\n'))
  90. value_builder.append(*(cp++));
  91. if (!current_group) {
  92. // We're not in a group yet, create one with the name ""...
  93. current_group = &m_groups.ensure("");
  94. }
  95. current_group->set(key_builder.to_string(), value_builder.to_string());
  96. }
  97. }
  98. }
  99. }
  100. String ConfigFile::read_entry(const String& group, const String& key, const String& default_value) const
  101. {
  102. if (!has_key(group, key)) {
  103. const_cast<ConfigFile&>(*this).write_entry(group, key, default_value);
  104. return default_value;
  105. }
  106. auto it = m_groups.find(group);
  107. auto jt = it->value.find(key);
  108. return jt->value;
  109. }
  110. int ConfigFile::read_num_entry(const String& group, const String& key, int default_value) const
  111. {
  112. if (!has_key(group, key)) {
  113. const_cast<ConfigFile&>(*this).write_num_entry(group, key, default_value);
  114. return default_value;
  115. }
  116. bool ok;
  117. int value = read_entry(group, key).to_int(ok);
  118. if (!ok)
  119. return default_value;
  120. return value;
  121. }
  122. bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
  123. {
  124. return read_entry(group, key, default_value ? "1" : "0") == "1";
  125. }
  126. void ConfigFile::write_entry(const String& group, const String& key, const String& value)
  127. {
  128. m_groups.ensure(group).ensure(key) = value;
  129. m_dirty = true;
  130. }
  131. void ConfigFile::write_num_entry(const String& group, const String& key, int value)
  132. {
  133. write_entry(group, key, String::number(value));
  134. }
  135. void ConfigFile::write_bool_entry(const String& group, const String& key, bool value)
  136. {
  137. write_entry(group, key, value ? "1" : "0");
  138. }
  139. void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
  140. {
  141. write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha()));
  142. }
  143. bool ConfigFile::sync()
  144. {
  145. if (!m_dirty)
  146. return true;
  147. FILE* fp = fopen(m_file_name.characters(), "wb");
  148. if (!fp)
  149. return false;
  150. for (auto& it : m_groups) {
  151. fprintf(fp, "[%s]\n", it.key.characters());
  152. for (auto& jt : it.value)
  153. fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters());
  154. fprintf(fp, "\n");
  155. }
  156. fclose(fp);
  157. m_dirty = false;
  158. return true;
  159. }
  160. void ConfigFile::dump() const
  161. {
  162. for (auto& it : m_groups) {
  163. printf("[%s]\n", it.key.characters());
  164. for (auto& jt : it.value)
  165. printf("%s=%s\n", jt.key.characters(), jt.value.characters());
  166. printf("\n");
  167. }
  168. }
  169. Vector<String> ConfigFile::groups() const
  170. {
  171. return m_groups.keys();
  172. }
  173. Vector<String> ConfigFile::keys(const String& group) const
  174. {
  175. auto it = m_groups.find(group);
  176. if (it == m_groups.end())
  177. return {};
  178. return it->value.keys();
  179. }
  180. bool ConfigFile::has_key(const String& group, const String& key) const
  181. {
  182. auto it = m_groups.find(group);
  183. if (it == m_groups.end())
  184. return {};
  185. return it->value.contains(key);
  186. }
  187. bool ConfigFile::has_group(const String& group) const
  188. {
  189. return m_groups.contains(group);
  190. }
  191. void ConfigFile::remove_group(const String& group)
  192. {
  193. m_groups.remove(group);
  194. m_dirty = true;
  195. }
  196. void ConfigFile::remove_entry(const String& group, const String& key)
  197. {
  198. auto it = m_groups.find(group);
  199. if (it == m_groups.end())
  200. return;
  201. it->value.remove(key);
  202. m_dirty = true;
  203. }
  204. }