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