ConfigFile.cpp 5.5 KB

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