ConfigFile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/UserInfo.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 = get_current_user_home_path();
  37. if (home_path == "/")
  38. home_path = String::format("/tmp");
  39. auto path = String::format("%s/%s.ini", home_path.characters(), app_name.characters());
  40. return adopt(*new ConfigFile(path));
  41. }
  42. NonnullRefPtr<ConfigFile> ConfigFile::get_for_system(const String& app_name)
  43. {
  44. auto path = String::format("/etc/%s.ini", app_name.characters());
  45. return adopt(*new ConfigFile(path));
  46. }
  47. NonnullRefPtr<ConfigFile> ConfigFile::open(const String& path)
  48. {
  49. return adopt(*new ConfigFile(path));
  50. }
  51. ConfigFile::ConfigFile(const String& file_name)
  52. : m_file_name(file_name)
  53. {
  54. reparse();
  55. }
  56. ConfigFile::~ConfigFile()
  57. {
  58. sync();
  59. }
  60. void ConfigFile::reparse()
  61. {
  62. m_groups.clear();
  63. auto file = File::construct(m_file_name);
  64. if (!file->open(IODevice::OpenMode::ReadOnly))
  65. return;
  66. HashMap<String, String>* current_group = nullptr;
  67. while (file->can_read_line()) {
  68. auto line = file->read_line(BUFSIZ);
  69. auto* cp = (const char*)line.data();
  70. while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
  71. ++cp;
  72. switch (*cp) {
  73. case '\0': // EOL...
  74. case '#': // Comment, skip entire line.
  75. case ';': // -||-
  76. continue;
  77. case '[': { // Start of new group.
  78. StringBuilder builder;
  79. ++cp; // Skip the '['
  80. while (*cp && (*cp != ']'))
  81. builder.append(*(cp++));
  82. current_group = &m_groups.ensure(builder.to_string());
  83. break;
  84. }
  85. default: { // Start of key{
  86. StringBuilder key_builder;
  87. StringBuilder value_builder;
  88. while (*cp && (*cp != '='))
  89. key_builder.append(*(cp++));
  90. ++cp; // Skip the '='
  91. while (*cp && (*cp != '\n'))
  92. value_builder.append(*(cp++));
  93. if (!current_group) {
  94. // We're not in a group yet, create one with the name ""...
  95. current_group = &m_groups.ensure("");
  96. }
  97. current_group->set(key_builder.to_string(), value_builder.to_string());
  98. }
  99. }
  100. }
  101. }
  102. String ConfigFile::read_entry(const String& group, const String& key, const String& default_value) const
  103. {
  104. if (!has_key(group, key)) {
  105. const_cast<ConfigFile&>(*this).write_entry(group, key, default_value);
  106. return default_value;
  107. }
  108. auto it = m_groups.find(group);
  109. auto jt = it->value.find(key);
  110. return jt->value;
  111. }
  112. int ConfigFile::read_num_entry(const String& group, const String& key, int default_value) const
  113. {
  114. if (!has_key(group, key)) {
  115. const_cast<ConfigFile&>(*this).write_num_entry(group, key, default_value);
  116. return default_value;
  117. }
  118. bool ok;
  119. int value = read_entry(group, key).to_uint(ok);
  120. if (!ok)
  121. return default_value;
  122. return value;
  123. }
  124. bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
  125. {
  126. return read_entry(group, key, default_value ? "1" : "0") == "1";
  127. }
  128. void ConfigFile::write_entry(const String& group, const String& key, const String& value)
  129. {
  130. m_groups.ensure(group).ensure(key) = value;
  131. m_dirty = true;
  132. }
  133. void ConfigFile::write_num_entry(const String& group, const String& key, int value)
  134. {
  135. write_entry(group, key, String::number(value));
  136. }
  137. void ConfigFile::write_bool_entry(const String& group, const String& key, bool value)
  138. {
  139. write_entry(group, key, value ? "1" : "0");
  140. }
  141. void ConfigFile::write_color_entry(const String& group, const String& key, Color value)
  142. {
  143. write_entry(group, key, String::format("%d,%d,%d,%d", value.red(), value.green(), value.blue(), value.alpha()));
  144. }
  145. bool ConfigFile::sync()
  146. {
  147. if (!m_dirty)
  148. return true;
  149. FILE* fp = fopen(m_file_name.characters(), "wb");
  150. if (!fp)
  151. return false;
  152. for (auto& it : m_groups) {
  153. fprintf(fp, "[%s]\n", it.key.characters());
  154. for (auto& jt : it.value)
  155. fprintf(fp, "%s=%s\n", jt.key.characters(), jt.value.characters());
  156. fprintf(fp, "\n");
  157. }
  158. fclose(fp);
  159. m_dirty = false;
  160. return true;
  161. }
  162. void ConfigFile::dump() const
  163. {
  164. for (auto& it : m_groups) {
  165. printf("[%s]\n", it.key.characters());
  166. for (auto& jt : it.value)
  167. printf("%s=%s\n", jt.key.characters(), jt.value.characters());
  168. printf("\n");
  169. }
  170. }
  171. Vector<String> ConfigFile::groups() const
  172. {
  173. return m_groups.keys();
  174. }
  175. Vector<String> ConfigFile::keys(const String& group) const
  176. {
  177. auto it = m_groups.find(group);
  178. if (it == m_groups.end())
  179. return {};
  180. return it->value.keys();
  181. }
  182. bool ConfigFile::has_key(const String& group, const String& key) const
  183. {
  184. auto it = m_groups.find(group);
  185. if (it == m_groups.end())
  186. return {};
  187. return it->value.contains(key);
  188. }
  189. bool ConfigFile::has_group(const String& group) const
  190. {
  191. return m_groups.contains(group);
  192. }
  193. void ConfigFile::remove_group(const String& group)
  194. {
  195. m_groups.remove(group);
  196. m_dirty = true;
  197. }
  198. void ConfigFile::remove_entry(const String& group, const String& key)
  199. {
  200. auto it = m_groups.find(group);
  201. if (it == m_groups.end())
  202. return;
  203. it->value.remove(key);
  204. m_dirty = true;
  205. }
  206. }