ConfigFile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibCore/ConfigFile.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/StandardPaths.h>
  30. #include <pwd.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. namespace Core {
  34. NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
  35. {
  36. String home_path = StandardPaths::home_directory();
  37. auto path = String::format("%s/%s.ini", home_path.characters(), app_name.characters());
  38. return adopt(*new ConfigFile(path));
  39. }
  40. NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
  41. {
  42. auto path = String::format("/etc/%s.ini", app_name.characters());
  43. return adopt(*new ConfigFile(path));
  44. }
  45. NonnullRefPtr<ConfigFile> ConfigFile::open(const String& path)
  46. {
  47. return adopt(*new ConfigFile(path));
  48. }
  49. ConfigFile::ConfigFile(const String& file_name)
  50. : m_file_name(file_name)
  51. {
  52. reparse();
  53. }
  54. ConfigFile::~ConfigFile()
  55. {
  56. sync();
  57. }
  58. void ConfigFile::reparse()
  59. {
  60. m_groups.clear();
  61. auto file = File::construct(m_file_name);
  62. if (!file->open(IODevice::OpenMode::ReadOnly))
  63. return;
  64. HashMap<String, String>* current_group = nullptr;
  65. while (file->can_read_line()) {
  66. auto line = file->read_line(BUFSIZ);
  67. auto* cp = (const char*)line.data();
  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. current_group->set(key_builder.to_string(), value_builder.to_string());
  96. }
  97. }
  98. }
  99. }
  100. String ConfigFile::read_entry(const String& group, const String& key, const String& default_value) const
  101. {
  102. if (!has_key(group, key)) {
  103. const_cast<ConfigFile&>(*this).write_entry(group, key, default_value);
  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(const String& group, const String& key, int default_value) const
  111. {
  112. if (!has_key(group, key)) {
  113. const_cast<ConfigFile&>(*this).write_num_entry(group, key, default_value);
  114. return default_value;
  115. }
  116. bool ok;
  117. int value = read_entry(group, key).to_int(ok);
  118. if (!ok)
  119. return default_value;
  120. return value;
  121. }
  122. bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
  123. {
  124. auto value = read_entry(group, key, default_value ? "1" : "0");
  125. if (value == "1" || value.to_lowercase() == "true")
  126. return 1;
  127. return 0;
  128. }
  129. void ConfigFile::write_entry(const String& group, const String& key, const String& value)
  130. {
  131. m_groups.ensure(group).ensure(key) = value;
  132. m_dirty = true;
  133. }
  134. void ConfigFile::write_num_entry(const String& group, const String& key, int value)
  135. {
  136. write_entry(group, key, String::number(value));
  137. }
  138. void ConfigFile::write_bool_entry(const String& group, const String& key, bool value)
  139. {
  140. write_entry(group, key, value ? "1" : "0");
  141. }
  142. void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
  143. {
  144. write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha()));
  145. }
  146. bool ConfigFile::sync()
  147. {
  148. if (!m_dirty)
  149. return true;
  150. FILE* fp = fopen(m_file_name.characters(), "wb");
  151. if (!fp)
  152. return false;
  153. for (auto& it : m_groups) {
  154. fprintf(fp, "[%s]\n", it.key.characters());
  155. for (auto& jt : it.value)
  156. fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters());
  157. fprintf(fp, "\n");
  158. }
  159. fclose(fp);
  160. m_dirty = false;
  161. return true;
  162. }
  163. void ConfigFile::dump() const
  164. {
  165. for (auto& it : m_groups) {
  166. printf("[%s]\n", it.key.characters());
  167. for (auto& jt : it.value)
  168. printf("%s=%s\n", jt.key.characters(), jt.value.characters());
  169. printf("\n");
  170. }
  171. }
  172. Vector<String> ConfigFile::groups() const
  173. {
  174. return m_groups.keys();
  175. }
  176. Vector<String> ConfigFile::keys(const String& group) const
  177. {
  178. auto it = m_groups.find(group);
  179. if (it == m_groups.end())
  180. return {};
  181. return it->value.keys();
  182. }
  183. bool ConfigFile::has_key(const String& group, const String& key) const
  184. {
  185. auto it = m_groups.find(group);
  186. if (it == m_groups.end())
  187. return {};
  188. return it->value.contains(key);
  189. }
  190. bool ConfigFile::has_group(const String& group) const
  191. {
  192. return m_groups.contains(group);
  193. }
  194. void ConfigFile::remove_group(const String& group)
  195. {
  196. m_groups.remove(group);
  197. m_dirty = true;
  198. }
  199. void ConfigFile::remove_entry(const String& group, const String& key)
  200. {
  201. auto it = m_groups.find(group);
  202. if (it == m_groups.end())
  203. return;
  204. it->value.remove(key);
  205. m_dirty = true;
  206. }
  207. }