ConfigFile.cpp 5.4 KB

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