ConfigFile.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibCore/ConfigFile.h>
  9. #include <LibCore/File.h>
  10. #include <LibCore/StandardPaths.h>
  11. #include <pwd.h>
  12. #include <stdio.h>
  13. namespace Core {
  14. NonnullRefPtr<ConfigFile> ConfigFile::open_for_lib(String const& lib_name, AllowWriting allow_altering)
  15. {
  16. String directory = StandardPaths::config_directory();
  17. auto path = String::formatted("{}/lib/{}.ini", directory, lib_name);
  18. return adopt_ref(*new ConfigFile(path, allow_altering));
  19. }
  20. NonnullRefPtr<ConfigFile> ConfigFile::open_for_app(String const& app_name, AllowWriting allow_altering)
  21. {
  22. String directory = StandardPaths::config_directory();
  23. auto path = String::formatted("{}/{}.ini", directory, app_name);
  24. return adopt_ref(*new ConfigFile(path, allow_altering));
  25. }
  26. NonnullRefPtr<ConfigFile> ConfigFile::open_for_system(String const& app_name, AllowWriting allow_altering)
  27. {
  28. auto path = String::formatted("/etc/{}.ini", app_name);
  29. return adopt_ref(*new ConfigFile(path, allow_altering));
  30. }
  31. NonnullRefPtr<ConfigFile> ConfigFile::open(String const& filename, AllowWriting allow_altering)
  32. {
  33. return adopt_ref(*new ConfigFile(filename, allow_altering));
  34. }
  35. NonnullRefPtr<ConfigFile> ConfigFile::open(String const& filename, int fd)
  36. {
  37. return adopt_ref(*new ConfigFile(filename, fd));
  38. }
  39. ConfigFile::ConfigFile(String const& filename, AllowWriting allow_altering)
  40. : m_file(File::construct(filename))
  41. {
  42. if (!m_file->open(allow_altering == AllowWriting::Yes ? OpenMode::ReadWrite : OpenMode::ReadOnly))
  43. return;
  44. reparse();
  45. }
  46. ConfigFile::ConfigFile(String const& filename, int fd)
  47. : m_file(File::construct(filename))
  48. {
  49. if (!m_file->open(fd, OpenMode::ReadWrite, File::ShouldCloseFileDescriptor::Yes))
  50. return;
  51. reparse();
  52. }
  53. ConfigFile::~ConfigFile()
  54. {
  55. sync();
  56. }
  57. void ConfigFile::reparse()
  58. {
  59. m_groups.clear();
  60. HashMap<String, String>* current_group = nullptr;
  61. while (m_file->can_read_line()) {
  62. auto line = m_file->read_line();
  63. if (line.is_null()) {
  64. m_groups.clear();
  65. return;
  66. }
  67. auto* cp = line.characters();
  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. auto value_string = value_builder.to_string();
  96. current_group->set(key_builder.to_string(), value_string.trim_whitespace(TrimMode::Right));
  97. }
  98. }
  99. }
  100. }
  101. String ConfigFile::read_entry(String const& group, String const& key, String const& default_value) const
  102. {
  103. if (!has_key(group, key)) {
  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(String const& group, String const& key, int default_value) const
  111. {
  112. if (!has_key(group, key)) {
  113. return default_value;
  114. }
  115. return read_entry(group, key).to_int().value_or(default_value);
  116. }
  117. bool ConfigFile::read_bool_entry(String const& group, String const& key, bool default_value) const
  118. {
  119. auto value = read_entry(group, key, default_value ? "true" : "false");
  120. return value == "1" || value.equals_ignoring_case("true"sv);
  121. }
  122. void ConfigFile::write_entry(String const& group, String const& key, String const& value)
  123. {
  124. m_groups.ensure(group).ensure(key) = value;
  125. m_dirty = true;
  126. }
  127. void ConfigFile::write_num_entry(String const& group, String const& key, int value)
  128. {
  129. write_entry(group, key, String::number(value));
  130. }
  131. void ConfigFile::write_bool_entry(String const& group, String const& key, bool value)
  132. {
  133. write_entry(group, key, value ? "true" : "false");
  134. }
  135. void ConfigFile::write_color_entry(String const& group, String const& key, Color value)
  136. {
  137. write_entry(group, key, String::formatted("{},{},{},{}", value.red(), value.green(), value.blue(), value.alpha()));
  138. }
  139. bool ConfigFile::sync()
  140. {
  141. if (!m_dirty)
  142. return true;
  143. m_file->truncate(0);
  144. m_file->seek(0);
  145. for (auto& it : m_groups) {
  146. m_file->write(String::formatted("[{}]\n", it.key));
  147. for (auto& jt : it.value)
  148. m_file->write(String::formatted("{}={}\n", jt.key, jt.value));
  149. m_file->write("\n");
  150. }
  151. m_dirty = false;
  152. return true;
  153. }
  154. void ConfigFile::dump() const
  155. {
  156. for (auto& it : m_groups) {
  157. outln("[{}]", it.key);
  158. for (auto& jt : it.value)
  159. outln("{}={}", jt.key, jt.value);
  160. outln();
  161. }
  162. }
  163. Vector<String> ConfigFile::groups() const
  164. {
  165. return m_groups.keys();
  166. }
  167. Vector<String> ConfigFile::keys(String const& group) const
  168. {
  169. auto it = m_groups.find(group);
  170. if (it == m_groups.end())
  171. return {};
  172. return it->value.keys();
  173. }
  174. bool ConfigFile::has_key(String const& group, String const& key) const
  175. {
  176. auto it = m_groups.find(group);
  177. if (it == m_groups.end())
  178. return {};
  179. return it->value.contains(key);
  180. }
  181. bool ConfigFile::has_group(String const& group) const
  182. {
  183. return m_groups.contains(group);
  184. }
  185. void ConfigFile::remove_group(String const& group)
  186. {
  187. m_groups.remove(group);
  188. m_dirty = true;
  189. }
  190. void ConfigFile::remove_entry(String const& group, String const& key)
  191. {
  192. auto it = m_groups.find(group);
  193. if (it == m_groups.end())
  194. return;
  195. it->value.remove(key);
  196. m_dirty = true;
  197. }
  198. }