ConfigFile.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(const String& 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. auto* cp = line.characters();
  64. while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
  65. ++cp;
  66. switch (*cp) {
  67. case '\0': // EOL...
  68. case '#': // Comment, skip entire line.
  69. case ';': // -||-
  70. continue;
  71. case '[': { // Start of new group.
  72. StringBuilder builder;
  73. ++cp; // Skip the '['
  74. while (*cp && (*cp != ']'))
  75. builder.append(*(cp++));
  76. current_group = &m_groups.ensure(builder.to_string());
  77. break;
  78. }
  79. default: { // Start of key{
  80. StringBuilder key_builder;
  81. StringBuilder value_builder;
  82. while (*cp && (*cp != '='))
  83. key_builder.append(*(cp++));
  84. ++cp; // Skip the '='
  85. while (*cp && (*cp != '\n'))
  86. value_builder.append(*(cp++));
  87. if (!current_group) {
  88. // We're not in a group yet, create one with the name ""...
  89. current_group = &m_groups.ensure("");
  90. }
  91. auto value_string = value_builder.to_string();
  92. current_group->set(key_builder.to_string(), value_string.trim_whitespace(TrimMode::Right));
  93. }
  94. }
  95. }
  96. }
  97. String ConfigFile::read_entry(String const& group, String const& key, String const& default_value) const
  98. {
  99. if (!has_key(group, key)) {
  100. return default_value;
  101. }
  102. auto it = m_groups.find(group);
  103. auto jt = it->value.find(key);
  104. return jt->value;
  105. }
  106. int ConfigFile::read_num_entry(String const& group, String const& key, int default_value) const
  107. {
  108. if (!has_key(group, key)) {
  109. return default_value;
  110. }
  111. return read_entry(group, key).to_int().value_or(default_value);
  112. }
  113. bool ConfigFile::read_bool_entry(String const& group, String const& key, bool default_value) const
  114. {
  115. auto value = read_entry(group, key, default_value ? "1" : "0");
  116. if (value == "1" || value.to_lowercase() == "true")
  117. return 1;
  118. return 0;
  119. }
  120. void ConfigFile::write_entry(String const& group, String const& key, String const& value)
  121. {
  122. m_groups.ensure(group).ensure(key) = value;
  123. m_dirty = true;
  124. }
  125. void ConfigFile::write_num_entry(String const& group, String const& key, int value)
  126. {
  127. write_entry(group, key, String::number(value));
  128. }
  129. void ConfigFile::write_bool_entry(String const& group, String const& key, bool value)
  130. {
  131. write_entry(group, key, value ? "1" : "0");
  132. }
  133. void ConfigFile::write_color_entry(String const& group, String const& key, Color value)
  134. {
  135. write_entry(group, key, String::formatted("{},{},{},{}", value.red(), value.green(), value.blue(), value.alpha()));
  136. }
  137. bool ConfigFile::sync()
  138. {
  139. if (!m_dirty)
  140. return true;
  141. m_file->truncate(0);
  142. m_file->seek(0);
  143. for (auto& it : m_groups) {
  144. m_file->write(String::formatted("[{}]\n", it.key));
  145. for (auto& jt : it.value)
  146. m_file->write(String::formatted("{}={}\n", jt.key, jt.value));
  147. m_file->write("\n");
  148. }
  149. m_dirty = false;
  150. return true;
  151. }
  152. void ConfigFile::dump() const
  153. {
  154. for (auto& it : m_groups) {
  155. outln("[{}]", it.key);
  156. for (auto& jt : it.value)
  157. outln("{}={}", jt.key, jt.value);
  158. outln();
  159. }
  160. }
  161. Vector<String> ConfigFile::groups() const
  162. {
  163. return m_groups.keys();
  164. }
  165. Vector<String> ConfigFile::keys(String const& group) const
  166. {
  167. auto it = m_groups.find(group);
  168. if (it == m_groups.end())
  169. return {};
  170. return it->value.keys();
  171. }
  172. bool ConfigFile::has_key(String const& group, String const& key) const
  173. {
  174. auto it = m_groups.find(group);
  175. if (it == m_groups.end())
  176. return {};
  177. return it->value.contains(key);
  178. }
  179. bool ConfigFile::has_group(String const& group) const
  180. {
  181. return m_groups.contains(group);
  182. }
  183. void ConfigFile::remove_group(String const& group)
  184. {
  185. m_groups.remove(group);
  186. m_dirty = true;
  187. }
  188. void ConfigFile::remove_entry(String const& group, String const& key)
  189. {
  190. auto it = m_groups.find(group);
  191. if (it == m_groups.end())
  192. return;
  193. it->value.remove(key);
  194. m_dirty = true;
  195. }
  196. }