ConfigFile.cpp 6.8 KB

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