Debian Codegraph @

tools/krcfile.vala

raw
1 /* vim:set et sts=4 sw=4:
2 *
3 * ibus - The Input Bus
4 *
5 * Copyright(c) 2024 Takao Fujiwara <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23 struct KRcFileKeyValuePair {
24 public string? key;
25 public string? value;
26 }
27
28 struct KRcFileGroup {
29 public string? name;
30 public unowned GLib.List<KRcFileKeyValuePair?> key_value_pairs;
31 }
32
33 class KRcFile {
34 // Make sure KRcFileGroup is a pointer for add_key_value_pair()
35 private unowned KRcFileGroup? m_current_group;
36 private GLib.List<KRcFileGroup?> m_groups;
37
38 public KRcFile() {
39 KRcFileGroup? group = KRcFileGroup();
40 m_groups.prepend(group);
41 // no name group for header comments
42 // Make sure m_current_group does not duplicate the memory.
43 m_current_group = (KRcFileGroup?)m_groups.data;
44 }
45
46 private bool is_comment(string line) {
47 unichar ch = line.get_char(0);
48 return ch == '#' || ch == '\0' || ch == '\n';
49 }
50
51 private bool is_group(string line) {
52 if (line.get_char(0) != '[')
53 return false;
54 int i = 1;
55 int length = line.char_count();
56 while (i < length) {
57 unichar ch = line.get_char(i);
58 if (ch == ']')
59 return true;
60 i++;
61 }
62 return false;
63 }
64
65 // "[Tiling][12345678-9012-3456-7890-123456789012]" is supported.
66 private bool is_group_name(string name) {
67 int i = 0;
68 int length = name.char_count();
69 unichar ch = 0;
70 while (i < length) {
71 ch = name.get_char(i);
72 if (ch == '\0' || ch.iscntrl())
73 break;
74 i++;
75 }
76 if (i != 0 && (i == length || ch == '\0'))
77 return true;
78 return false;
79 }
80
81 private bool is_key_value_pair(string line) {
82 int pos = line.index_of_char('=');
83 if (pos == -1 || pos == 0)
84 return false;
85 return true;
86 }
87
88 // Assume key name is ASCII only
89 private bool is_key_name(string line, size_t length) {
90 long i = 0;
91 // Cast size_t to long.
92 assert(length < Posix.Limits.SSIZE_MAX);
93 while (i < length) {
94 char ch = line.get(i);
95 if (ch == '=' || ch == '[' || ch == ']')
96 break;
97 if (ch == '\0') {
98 i = (long)length;
99 break;
100 }
101 i++;
102 }
103 if (i == 0)
104 return false;
105 if (line.get(0) == ' ' || line.get(i - 1) == ' ')
106 return false;
107 if (i < length && line.get(i) == '[') {
108 i++;
109 while (i < length) {
110 assert(Posix.Limits.SSIZE_MAX > length - i);
111 unichar ch =
112 line.offset(i).get_char_validated((ssize_t)length - i);
113 if (ch <= 0)
114 break;
115 // kwinrc file supports "$e" language.
116 if (!ch.isalnum() && ch != '-' && ch != '_' && ch != '.'
117 && ch != '@' && ch != '$') {
118 break;
119 }
120 i++;
121 }
122 if (i == length)
123 return false;
124 if (line.get(i) != ']')
125 return false;
126 // Now line[i] is ']'
127 i++;
128 }
129 if (i < length)
130 return false;
131 return true;
132 }
133
134 private static int find_group(KRcFileGroup group1, KRcFileGroup group2) {
135 if (group1.name == null) {
136 if (group2.name == null)
137 return 0;
138 return -1;
139 }
140 if (group2.name == null)
141 return 1;
142 return group1.name.ascii_casecmp(group2.name);
143 }
144
145 private unowned GLib.List<KRcFileGroup?>
146 get_group_node_with_name(string group_name,
147 bool if_throw_error=false)
148 throws GLib.KeyFileError {
149 var tmp = KRcFileGroup();
150 tmp.name = group_name;
151 unowned GLib.List<KRcFileGroup?> group_node =
152 m_groups.find_custom(tmp,
153 (GLib.CompareFunc)find_group);
154 if (group_node == null && if_throw_error) {
155 throw new GLib.KeyFileError.GROUP_NOT_FOUND(
156 "Key file does not have group %s", group_name);
157 }
158 return group_node;
159 }
160
161 private void add_key_value_pair(KRcFileGroup? group,
162 KRcFileKeyValuePair pair,
163 GLib.List sibling) {
164 group.key_value_pairs.insert_before(sibling, pair);
165 }
166
167 private void add_key(KRcFileGroup? group,
168 string? key,
169 string value) {
170 KRcFileKeyValuePair pair = KRcFileKeyValuePair();
171 pair.key = key;
172 pair.value = value;
173 unowned GLib.List<KRcFileKeyValuePair?> lp = group.key_value_pairs;
174 while (lp != null && lp.data.key == null)
175 lp = lp.next;
176 add_key_value_pair(group, pair, lp);
177 }
178
179 private void parse_comment(string line, long length) {
180 KRcFileKeyValuePair pair = KRcFileKeyValuePair();
181 pair.key = null;
182 pair.value = line.substring(0, length);
183 m_current_group.key_value_pairs.prepend(pair);
184 }
185
186 private void parse_group(string line,
187 long length) throws GLib.KeyFileError {
188 int l = (int)length;
189 if (l < 0) {
190 throw new GLib.KeyFileError.PARSE(
191 "Too long group name %s against IMT_MAX", line);
192 }
193 assert(l > 2);
194 unichar ch;
195 while (line.get_prev_char(ref l, out ch)) {
196 if (ch != ']')
197 break;
198 }
199 string group_name = line.substring(1, l);
200 if (!is_group_name(group_name)) {
201 throw new GLib.KeyFileError.PARSE(
202 "Invalid group name %s", group_name);
203 }
204 unowned GLib.List<KRcFileGroup?> group_node =
205 get_group_node_with_name(group_name);
206 if (group_node != null) {
207 m_current_group = (KRcFileGroup?)group_node.data;
208 return;
209 }
210 KRcFileGroup group = KRcFileGroup();
211 group.name = group_name;
212 m_groups.prepend(group);
213 m_current_group = (KRcFileGroup?)m_groups.data;
214 }
215
216 private void parse_key_value_pair(string line,
217 long length) throws GLib.KeyFileError {
218 if (m_current_group.name == null) {
219 throw new GLib.KeyFileError.GROUP_NOT_FOUND(
220 "Key file does not start with a group");
221 }
222 int key_end = line.index_of_char('=');
223 int value_start = key_end;
224 warn_if_fail(key_end != -1);
225 key_end--;
226 value_start++;
227 string key = line.substring(0, key_end + 1);
228 key = key.chomp();
229 if (!is_key_name(line, key_end + 1)) {
230 throw new GLib.KeyFileError.PARSE(
231 "Invalid key name: %s", key);
232 }
233 string value = line.substring(value_start, line.length - value_start);
234 value = value.chug();
235 assert(m_current_group.name != null);
236 if (key == "Encoding" && value != "UTF-8") {
237 throw new GLib.KeyFileError.UNKNOWN_ENCODING(
238 "Key file contains unsupported encoding %s", value);
239 }
240 var pair = KRcFileKeyValuePair();
241 pair.key = key;
242 pair.value = value;
243 add_key_value_pair(m_current_group,
244 pair,
245 m_current_group.key_value_pairs);
246 }
247
248 private void parse_line(string line, long length) throws GLib.KeyFileError {
249 // unowned to owned string
250 string _line = line;
251 _line = _line.chug();
252 if (is_comment(_line))
253 parse_comment(line, length);
254 if (is_group(_line))
255 parse_group(_line, length - (length - _line.length));
256 if (is_key_value_pair(_line))
257 parse_key_value_pair(_line, length - (length - _line.length));
258 }
259
260 // Use data.get() instead of data.get_char() and support UTF-8 only with
261 // parse_key_value_pair()
262 private bool parse_data(string data, size_t length) {
263 long i = 0;
264 long offset = 0;
265 while (i < length) {
266 char ch = data.get(i);
267 if (ch == '\n') {
268 long sub_length = i - offset;
269 string sub = data.substring(offset, sub_length);
270 offset = i + 1;
271 if (sub.length > 0 && sub.get(sub.length - 1) == '\r')
272 sub = sub.substring(0, --sub_length);
273 try {
274 if (sub.length == 0)
275 parse_comment("", 0);
276 else
277 parse_line(sub, sub_length);
278 } catch (GLib.KeyFileError e) {
279 warning("Failed to load data: %s", e.message);
280 return false;
281 }
282 }
283 i++;
284 }
285 return true;
286 }
287
288 public bool load_from_file(string path,
289 GLib.KeyFileFlags flags)
290 throws GLib.KeyFileError,
291 GLib.FileError {
292 if (!GLib.FileUtils.test(path, GLib.FileTest.EXISTS |
293 GLib.FileTest.IS_REGULAR)) {
294 throw new GLib.KeyFileError.PARSE(
295 "Not a regular file %s", path);
296 }
297 string contents = null;
298 size_t length = 0;
299 if (!GLib.FileUtils.get_contents(path, out contents, out length)) {
300 warning("Failed to load %s", path);
301 return false;
302 }
303 return parse_data(contents, length);
304 }
305
306 public bool has_group(string group_name) {
307 try {
308 if (get_group_node_with_name(group_name) != null)
309 return true;
310 } catch (GLib.KeyFileError e) {
311 assert_not_reached();
312 }
313 return false;
314 }
315
316 public string[] get_keys(string group_name) throws GLib.KeyFileError {
317 string[] keys = {};
318 unowned GLib.List<KRcFileGroup?> group_node =
319 get_group_node_with_name(group_name, true);
320 unowned GLib.List<KRcFileKeyValuePair?> tmp =
321 group_node.data.key_value_pairs;
322 while (tmp != null) {
323 KRcFileKeyValuePair pair = tmp.data;
324 if (pair.key != null)
325 keys += pair.key;
326 tmp = tmp.next;
327 }
328 return keys;
329 }
330
331 public bool has_key(string group_name,
332 string key) throws GLib.KeyFileError {
333 unowned GLib.List<KRcFileGroup?> group_node =
334 get_group_node_with_name(group_name, true);
335 unowned GLib.List<KRcFileKeyValuePair?> tmp =
336 group_node.data.key_value_pairs;
337 while (tmp != null) {
338 KRcFileKeyValuePair pair = tmp.data;
339 if (pair.key == key)
340 return true;
341 tmp = tmp.next;
342 }
343 return false;
344 }
345
346 public bool remove_group(string group_name) throws GLib.KeyFileError {
347 unowned GLib.List<KRcFileGroup?> group_node =
348 get_group_node_with_name(group_name, true);
349 unowned GLib.List<KRcFileKeyValuePair?> tmp =
350 group_node.data.key_value_pairs;
351 while (tmp != null) {
352 tmp.data = null;
353 tmp.delete_link(tmp);
354 }
355 group_node.data = null;
356 m_groups.delete_link(group_node);
357 return true;
358 }
359
360 public bool remove_key(string group_name,
361 string key) throws GLib.KeyFileError {
362 unowned GLib.List<KRcFileGroup?> group_node =
363 get_group_node_with_name(group_name, true);
364 unowned GLib.List<KRcFileKeyValuePair?> tmp =
365 group_node.data.key_value_pairs;
366 while (tmp != null) {
367 KRcFileKeyValuePair? pair = tmp.data;
368 if (pair.key == key)
369 break;
370 tmp = tmp.next;
371 }
372 if (tmp == null) {
373 throw new GLib.KeyFileError.KEY_NOT_FOUND(
374 "Key file does not have key %s in group %s",
375 key, group_name);
376 }
377 tmp.data = null;
378 tmp.delete_link(tmp);
379 return true;
380 }
381
382 public string to_data(out ssize_t length) {
383 var data_string = new GLib.StringBuilder();
384 for (unowned GLib.List<KRcFileGroup?> group_node = m_groups.last();
385 group_node != null;
386 group_node = group_node.prev) {
387 KRcFileGroup? group = group_node.data;
388 if (group.name != null)
389 data_string.append_printf("[%s]\n", group.name);
390 for (unowned GLib.List<KRcFileKeyValuePair?> key_file_node =
391 group.key_value_pairs.last();
392 key_file_node != null;
393 key_file_node = key_file_node.prev) {
394 KRcFileKeyValuePair pair = key_file_node.data;
395 if (pair.key != null)
396 data_string.append_printf("%s=%s\n", pair.key, pair.value);
397 else
398 data_string.append_printf("%s\n", pair.value);
399 }
400 }
401 length = data_string.len;
402 return data_string.str;
403 }
404
405 public bool save_to_file(string filename) throws GLib.FileError {
406 ssize_t length = 0;
407 string contents = to_data(out length);
408 bool retval = GLib.FileUtils.set_contents(filename, contents, length);
409 return retval;
410 }
411
412 public string? get_value(string group_name,
413 string key) throws GLib.KeyFileError {
414 unowned GLib.List<KRcFileGroup?> group_node =
415 get_group_node_with_name(group_name, true);
416 unowned GLib.List<KRcFileKeyValuePair?> tmp =
417 group_node.data.key_value_pairs;
418 while (tmp != null) {
419 KRcFileKeyValuePair? pair = tmp.data;
420 if (pair.key == key)
421 return pair.value;
422 tmp = tmp.next;
423 }
424 throw new GLib.KeyFileError.KEY_NOT_FOUND(
425 "Key file does not have key %s in group %s",
426 key, group_name);
427 }
428
429 public void set_value(string group_name,
430 string key,
431 string value) {
432 return_if_fail(is_group_name(group_name));
433 return_if_fail(is_key_name(key, key.length));
434 unowned GLib.List<KRcFileGroup?> group_node;
435 try {
436 group_node = get_group_node_with_name(group_name);
437 } catch (GLib.KeyFileError e) {
438 assert_not_reached();
439 }
440 if (group_node == null) {
441 unowned KRcFileGroup? last_group = m_groups.data;
442 if (last_group.key_value_pairs != null) {
443 if (last_group.key_value_pairs.data.key != null)
444 parse_comment("", 0);
445 }
446 KRcFileGroup group = KRcFileGroup();
447 group.name = group_name;
448 m_groups.prepend(group);
449 m_current_group = (KRcFileGroup?)m_groups.data;
450 add_key(m_current_group, key, value);
451 } else {
452 unowned GLib.List<KRcFileKeyValuePair?> tmp =
453 group_node.data.key_value_pairs;
454 while (tmp != null) {
455 KRcFileKeyValuePair? pair = tmp.data;
456 if (pair.key == key)
457 break;
458 tmp = tmp.next;
459 }
460 if (tmp != null)
461 tmp.data.value = value;
462 else
463 add_key(group_node.data, key, value);
464 }
465 }
466
467 }