ConfigFile.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. current_group->set(key_builder.to_string(), value_builder.to_string());
  92. }
  93. }
  94. }
  95. }
  96. String ConfigFile::read_entry(String const& group, String const& key, String const& default_value) const
  97. {
  98. if (!has_key(group, key)) {
  99. return default_value;
  100. }
  101. auto it = m_groups.find(group);
  102. auto jt = it->value.find(key);
  103. return jt->value;
  104. }
  105. int ConfigFile::read_num_entry(String const& group, String const& key, int default_value) const
  106. {
  107. if (!has_key(group, key)) {
  108. return default_value;
  109. }
  110. return read_entry(group, key).to_int().value_or(default_value);
  111. }
  112. bool ConfigFile::read_bool_entry(String const& group, String const& key, bool default_value) const
  113. {
  114. auto value = read_entry(group, key, default_value ? "1" : "0");
  115. if (value == "1" || value.to_lowercase() == "true")
  116. return 1;
  117. return 0;
  118. }
  119. void ConfigFile::write_entry(String const& group, String const& key, String const& value)
  120. {
  121. m_groups.ensure(group).ensure(key) = value;
  122. m_dirty = true;
  123. }
  124. void ConfigFile::write_num_entry(String const& group, String const& key, int value)
  125. {
  126. write_entry(group, key, String::number(value));
  127. }
  128. void ConfigFile::write_bool_entry(String const& group, String const& key, bool value)
  129. {
  130. write_entry(group, key, value ? "1" : "0");
  131. }
  132. void ConfigFile::write_color_entry(String const& group, String const& key, Color value)
  133. {
  134. write_entry(group, key, String::formatted("{},{},{},{}", value.red(), value.green(), value.blue(), value.alpha()));
  135. }
  136. bool ConfigFile::sync()
  137. {
  138. if (!m_dirty)
  139. return true;
  140. m_file->truncate(0);
  141. m_file->seek(0);
  142. for (auto& it : m_groups) {
  143. m_file->write(String::formatted("[{}]\n", it.key));
  144. for (auto& jt : it.value)
  145. m_file->write(String::formatted("{}={}\n", jt.key, jt.value));
  146. m_file->write("\n");
  147. }
  148. m_dirty = false;
  149. return true;
  150. }
  151. void ConfigFile::dump() const
  152. {
  153. for (auto& it : m_groups) {
  154. outln("[{}]", it.key);
  155. for (auto& jt : it.value)
  156. outln("{}={}", jt.key, jt.value);
  157. outln();
  158. }
  159. }
  160. Vector<String> ConfigFile::groups() const
  161. {
  162. return m_groups.keys();
  163. }
  164. Vector<String> ConfigFile::keys(String const& group) const
  165. {
  166. auto it = m_groups.find(group);
  167. if (it == m_groups.end())
  168. return {};
  169. return it->value.keys();
  170. }
  171. bool ConfigFile::has_key(String const& group, String const& key) const
  172. {
  173. auto it = m_groups.find(group);
  174. if (it == m_groups.end())
  175. return {};
  176. return it->value.contains(key);
  177. }
  178. bool ConfigFile::has_group(String const& group) const
  179. {
  180. return m_groups.contains(group);
  181. }
  182. void ConfigFile::remove_group(String const& group)
  183. {
  184. m_groups.remove(group);
  185. m_dirty = true;
  186. }
  187. void ConfigFile::remove_entry(String const& group, String const& key)
  188. {
  189. auto it = m_groups.find(group);
  190. if (it == m_groups.end())
  191. return;
  192. it->value.remove(key);
  193. m_dirty = true;
  194. }
  195. }