ConfigFile.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_lib(const String& lib_name)
  35. {
  36. String directory = StandardPaths::config_directory();
  37. auto path = String::format("%s/lib/%s.ini", directory.characters(), lib_name.characters());
  38. return adopt(*new ConfigFile(path));
  39. }
  40. NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
  41. {
  42. String directory = StandardPaths::config_directory();
  43. auto path = String::format("%s/%s.ini", directory.characters(), app_name.characters());
  44. return adopt(*new ConfigFile(path));
  45. }
  46. NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
  47. {
  48. auto path = String::format("/etc/%s.ini", app_name.characters());
  49. return adopt(*new ConfigFile(path));
  50. }
  51. NonnullRefPtr<ConfigFile> ConfigFile::open(const String& path)
  52. {
  53. return adopt(*new ConfigFile(path));
  54. }
  55. ConfigFile::ConfigFile(const String& file_name)
  56. : m_file_name(file_name)
  57. {
  58. reparse();
  59. }
  60. ConfigFile::~ConfigFile()
  61. {
  62. sync();
  63. }
  64. void ConfigFile::reparse()
  65. {
  66. m_groups.clear();
  67. auto file = File::construct(m_file_name);
  68. if (!file->open(IODevice::OpenMode::ReadOnly))
  69. return;
  70. HashMap<String, String>* current_group = nullptr;
  71. while (file->can_read_line()) {
  72. auto line = file->read_line(BUFSIZ);
  73. auto* cp = (const char*)line.data();
  74. while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
  75. ++cp;
  76. switch (*cp) {
  77. case '\0': // EOL...
  78. case '#': // Comment, skip entire line.
  79. case ';': // -||-
  80. continue;
  81. case '[': { // Start of new group.
  82. StringBuilder builder;
  83. ++cp; // Skip the '['
  84. while (*cp && (*cp != ']'))
  85. builder.append(*(cp++));
  86. current_group = &m_groups.ensure(builder.to_string());
  87. break;
  88. }
  89. default: { // Start of key{
  90. StringBuilder key_builder;
  91. StringBuilder value_builder;
  92. while (*cp && (*cp != '='))
  93. key_builder.append(*(cp++));
  94. ++cp; // Skip the '='
  95. while (*cp && (*cp != '\n'))
  96. value_builder.append(*(cp++));
  97. if (!current_group) {
  98. // We're not in a group yet, create one with the name ""...
  99. current_group = &m_groups.ensure("");
  100. }
  101. current_group->set(key_builder.to_string(), value_builder.to_string());
  102. }
  103. }
  104. }
  105. }
  106. String ConfigFile::read_entry(const String& group, const String& key, const String& default_value) const
  107. {
  108. if (!has_key(group, key)) {
  109. return default_value;
  110. }
  111. auto it = m_groups.find(group);
  112. auto jt = it->value.find(key);
  113. return jt->value;
  114. }
  115. int ConfigFile::read_num_entry(const String& group, const String& key, int default_value) const
  116. {
  117. if (!has_key(group, key)) {
  118. return default_value;
  119. }
  120. return read_entry(group, key).to_int().value_or(default_value);
  121. }
  122. bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
  123. {
  124. auto value = read_entry(group, key, default_value ? "1" : "0");
  125. if (value == "1" || value.to_lowercase() == "true")
  126. return 1;
  127. return 0;
  128. }
  129. void ConfigFile::write_entry(const String& group, const String& key, const String& value)
  130. {
  131. m_groups.ensure(group).ensure(key) = value;
  132. m_dirty = true;
  133. }
  134. void ConfigFile::write_num_entry(const String& group, const String& key, int value)
  135. {
  136. write_entry(group, key, String::number(value));
  137. }
  138. void ConfigFile::write_bool_entry(const String& group, const String& key, bool value)
  139. {
  140. write_entry(group, key, value ? "1" : "0");
  141. }
  142. void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
  143. {
  144. write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha()));
  145. }
  146. bool ConfigFile::sync()
  147. {
  148. if (!m_dirty)
  149. return true;
  150. FILE* fp = fopen(m_file_name.characters(), "wb");
  151. if (!fp)
  152. return false;
  153. for (auto& it : m_groups) {
  154. fprintf(fp, "[%s]\n", it.key.characters());
  155. for (auto& jt : it.value)
  156. fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters());
  157. fprintf(fp, "\n");
  158. }
  159. fclose(fp);
  160. m_dirty = false;
  161. return true;
  162. }
  163. void ConfigFile::dump() const
  164. {
  165. for (auto& it : m_groups) {
  166. printf("[%s]\n", it.key.characters());
  167. for (auto& jt : it.value)
  168. printf("%s=%s\n", jt.key.characters(), jt.value.characters());
  169. printf("\n");
  170. }
  171. }
  172. Vector<String> ConfigFile::groups() const
  173. {
  174. return m_groups.keys();
  175. }
  176. Vector<String> ConfigFile::keys(const String& group) const
  177. {
  178. auto it = m_groups.find(group);
  179. if (it == m_groups.end())
  180. return {};
  181. return it->value.keys();
  182. }
  183. bool ConfigFile::has_key(const String& group, const String& key) const
  184. {
  185. auto it = m_groups.find(group);
  186. if (it == m_groups.end())
  187. return {};
  188. return it->value.contains(key);
  189. }
  190. bool ConfigFile::has_group(const String& group) const
  191. {
  192. return m_groups.contains(group);
  193. }
  194. void ConfigFile::remove_group(const String& group)
  195. {
  196. m_groups.remove(group);
  197. m_dirty = true;
  198. }
  199. void ConfigFile::remove_entry(const String& group, const String& key)
  200. {
  201. auto it = m_groups.find(group);
  202. if (it == m_groups.end())
  203. return;
  204. it->value.remove(key);
  205. m_dirty = true;
  206. }
  207. }