Branch data Line data Source code
1 : : /* gkeyfile.c - key file parser
2 : : *
3 : : * Copyright 2004 Red Hat, Inc.
4 : : * Copyright 2009-2010 Collabora Ltd.
5 : : * Copyright 2009 Nokia Corporation
6 : : *
7 : : * Written by Ray Strode <rstrode@redhat.com>
8 : : * Matthias Clasen <mclasen@redhat.com>
9 : : *
10 : : * SPDX-License-Identifier: LGPL-2.1-or-later
11 : : *
12 : : * This library is free software; you can redistribute it and/or
13 : : * modify it under the terms of the GNU Lesser General Public
14 : : * License as published by the Free Software Foundation; either
15 : : * version 2.1 of the License, or (at your option) any later version.
16 : : *
17 : : * This library is distributed in the hope that it will be useful,
18 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 : : * Lesser General Public License for more details.
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * along with this library; if not, see <http://www.gnu.org/licenses/>.
24 : : */
25 : :
26 : : #include "config.h"
27 : :
28 : : #include "gkeyfile.h"
29 : : #include "gutils.h"
30 : :
31 : : #include <errno.h>
32 : : #include <fcntl.h>
33 : : #include <locale.h>
34 : : #include <string.h>
35 : : #include <stdio.h>
36 : : #include <stdlib.h>
37 : : #include <sys/types.h>
38 : : #include <sys/stat.h>
39 : : #ifdef G_OS_UNIX
40 : : #include <unistd.h>
41 : : #endif
42 : : #ifdef G_OS_WIN32
43 : : #include <io.h>
44 : :
45 : : #undef fstat
46 : : #define fstat(a,b) _fstati64(a,b)
47 : : #undef stat
48 : : #define stat _stati64
49 : :
50 : : #ifndef S_ISREG
51 : : #define S_ISREG(mode) ((mode)&_S_IFREG)
52 : : #endif
53 : :
54 : : #endif /* G_OS_WIN23 */
55 : :
56 : : #ifndef O_CLOEXEC
57 : : #define O_CLOEXEC 0
58 : : #endif
59 : :
60 : : #include "gconvert.h"
61 : : #include "gdataset.h"
62 : : #include "gerror.h"
63 : : #include "gfileutils.h"
64 : : #include "ghash.h"
65 : : #include "glibintl.h"
66 : : #include "glist.h"
67 : : #include "gslist.h"
68 : : #include "gmem.h"
69 : : #include "gmessages.h"
70 : : #include "gstdio.h"
71 : : #include "gstring.h"
72 : : #include "gstrfuncs.h"
73 : : #include "gutils.h"
74 : :
75 : :
76 : : /**
77 : : * GKeyFile:
78 : : *
79 : : * `GKeyFile` parses .ini-like config files.
80 : : *
81 : : * `GKeyFile` lets you parse, edit or create files containing groups of
82 : : * key-value pairs, which we call ‘key files’ for lack of a better name.
83 : : * Several freedesktop.org specifications use key files. For example, the
84 : : * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/)
85 : : * and the [Icon Theme Specification](https://specifications.freedesktop.org/icon-theme-spec/latest/).
86 : : *
87 : : * The syntax of key files is described in detail in the
88 : : * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/),
89 : : * here is a quick summary: Key files consists of groups of key-value pairs, interspersed
90 : : * with comments.
91 : : *
92 : : * ```txt
93 : : * # this is just an example
94 : : * # there can be comments before the first group
95 : : *
96 : : * [First Group]
97 : : *
98 : : * Name=Key File Example\tthis value shows\nescaping
99 : : *
100 : : * # localized strings are stored in multiple key-value pairs
101 : : * Welcome=Hello
102 : : * Welcome[de]=Hallo
103 : : * Welcome[fr_FR]=Bonjour
104 : : * Welcome[it]=Ciao
105 : : *
106 : : * [Another Group]
107 : : *
108 : : * Numbers=2;20;-200;0
109 : : *
110 : : * Booleans=true;false;true;true
111 : : * ```
112 : : *
113 : : * Lines beginning with a `#` and blank lines are considered comments.
114 : : *
115 : : * Groups are started by a header line containing the group name enclosed
116 : : * in `[` and `]`, and ended implicitly by the start of the next group or
117 : : * the end of the file. Each key-value pair must be contained in a group.
118 : : *
119 : : * Key-value pairs generally have the form `key=value`, with the exception
120 : : * of localized strings, which have the form `key[locale]=value`, with a
121 : : * locale identifier of the form `lang_COUNTRY@MODIFIER` where `COUNTRY`
122 : : * and `MODIFIER` are optional. As a special case, the locale `C` is associated
123 : : * with the untranslated pair `key=value` (since GLib 2.84). Space before and
124 : : * after the `=` character is ignored. Newline, tab, carriage return and
125 : : * backslash characters in value are escaped as `\n`, `\t`, `\r`, and `\\\\`,
126 : : * respectively. To preserve leading spaces in values, these can also be escaped
127 : : * as `\s`.
128 : : *
129 : : * Key files can store strings (possibly with localized variants), integers,
130 : : * booleans and lists of these. Lists are separated by a separator character,
131 : : * typically `;` or `,`. To use the list separator character in a value in
132 : : * a list, it has to be escaped by prefixing it with a backslash.
133 : : *
134 : : * This syntax is obviously inspired by the .ini files commonly met
135 : : * on Windows, but there are some important differences:
136 : : *
137 : : * - .ini files use the `;` character to begin comments,
138 : : * key files use the `#` character.
139 : : *
140 : : * - Key files do not allow for ungrouped keys meaning only
141 : : * comments can precede the first group.
142 : : *
143 : : * - Key files are always encoded in UTF-8.
144 : : *
145 : : * - Key and Group names are case-sensitive. For example, a group called
146 : : * `[GROUP]` is a different from `[group]`.
147 : : *
148 : : * - .ini files don’t have a strongly typed boolean entry type,
149 : : * they only have `GetProfileInt()`. In key files, only
150 : : * `true` and `false` (in lower case) are allowed.
151 : : *
152 : : * Note that in contrast to the
153 : : * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/),
154 : : * groups in key files may contain the same key multiple times; the last entry wins.
155 : : * Key files may also contain multiple groups with the same name; they are merged
156 : : * together. Another difference is that keys and group names in key files are not
157 : : * restricted to ASCII characters.
158 : : *
159 : : * Here is an example of loading a key file and reading a value:
160 : : *
161 : : * ```c
162 : : * g_autoptr(GError) error = NULL;
163 : : * g_autoptr(GKeyFile) key_file = g_key_file_new ();
164 : : *
165 : : * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
166 : : * {
167 : : * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
168 : : * g_warning ("Error loading key file: %s", error->message);
169 : : * return;
170 : : * }
171 : : *
172 : : * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
173 : : * if (val == NULL &&
174 : : * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
175 : : * {
176 : : * g_warning ("Error finding key in key file: %s", error->message);
177 : : * return;
178 : : * }
179 : : * else if (val == NULL)
180 : : * {
181 : : * // Fall back to a default value.
182 : : * val = g_strdup ("default-value");
183 : : * }
184 : : * ```
185 : : *
186 : : * Here is an example of creating and saving a key file:
187 : : *
188 : : * ```c
189 : : * g_autoptr(GKeyFile) key_file = g_key_file_new ();
190 : : * const gchar *val = …;
191 : : * g_autoptr(GError) error = NULL;
192 : : *
193 : : * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
194 : : *
195 : : * // Save as a file.
196 : : * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
197 : : * {
198 : : * g_warning ("Error saving key file: %s", error->message);
199 : : * return;
200 : : * }
201 : : *
202 : : * // Or store to a GBytes for use elsewhere.
203 : : * gsize data_len;
204 : : * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
205 : : * if (data == NULL)
206 : : * {
207 : : * g_warning ("Error saving key file: %s", error->message);
208 : : * return;
209 : : * }
210 : : * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
211 : : * ```
212 : : */
213 : :
214 : : /**
215 : : * G_KEY_FILE_ERROR:
216 : : *
217 : : * Error domain for key file parsing.
218 : : *
219 : : * Errors in this domain will be from the [enum@GLib.KeyFileError] enumeration.
220 : : *
221 : : * See [struct@GLib.Error] for information on error domains.
222 : : */
223 : :
224 : : /**
225 : : * GKeyFileError:
226 : : * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
227 : : * an unknown encoding
228 : : * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
229 : : * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
230 : : * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
231 : : * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
232 : : * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
233 : : *
234 : : * Error codes returned by key file parsing.
235 : : */
236 : :
237 : : /**
238 : : * GKeyFileFlags:
239 : : * @G_KEY_FILE_NONE: No flags, default behaviour
240 : : * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
241 : : * (possibly modified) contents of the key file back to a file;
242 : : * otherwise all comments will be lost when the key file is
243 : : * written back.
244 : : * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
245 : : * (possibly modified) contents of the key file back to a file;
246 : : * otherwise only the translations for the current language will be
247 : : * written back.
248 : : *
249 : : * Flags which influence the parsing.
250 : : */
251 : :
252 : : /**
253 : : * G_KEY_FILE_DESKTOP_GROUP:
254 : : *
255 : : * The name of the main group of a desktop entry file, as defined in the
256 : : * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/).
257 : : *
258 : : * Consult the specification for more
259 : : * details about the meanings of the keys below.
260 : : *
261 : : * Since: 2.14
262 : : */
263 : :
264 : : /**
265 : : * G_KEY_FILE_DESKTOP_KEY_TYPE:
266 : : *
267 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
268 : : * giving the type of the desktop entry.
269 : : *
270 : : * Usually [const@GLib.KEY_FILE_DESKTOP_TYPE_APPLICATION],
271 : : * [const@GLib.KEY_FILE_DESKTOP_TYPE_LINK], or
272 : : * [const@GLib.KEY_FILE_DESKTOP_TYPE_DIRECTORY].
273 : : *
274 : : * Since: 2.14
275 : : */
276 : :
277 : : /**
278 : : * G_KEY_FILE_DESKTOP_KEY_VERSION:
279 : : *
280 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
281 : : * giving the version of the Desktop Entry Specification used for
282 : : * the desktop entry file.
283 : : *
284 : : * Since: 2.14
285 : : */
286 : :
287 : : /**
288 : : * G_KEY_FILE_DESKTOP_KEY_NAME:
289 : : *
290 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a localized
291 : : * string giving the specific name of the desktop entry.
292 : : *
293 : : * Since: 2.14
294 : : */
295 : :
296 : : /**
297 : : * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
298 : : *
299 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a localized
300 : : * string giving the generic name of the desktop entry.
301 : : *
302 : : * Since: 2.14
303 : : */
304 : :
305 : : /**
306 : : * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
307 : : *
308 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a boolean
309 : : * stating whether the desktop entry should be shown in menus.
310 : : *
311 : : * Since: 2.14
312 : : */
313 : :
314 : : /**
315 : : * G_KEY_FILE_DESKTOP_KEY_COMMENT:
316 : : *
317 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a localized
318 : : * string giving the tooltip for the desktop entry.
319 : : *
320 : : * Since: 2.14
321 : : */
322 : :
323 : : /**
324 : : * G_KEY_FILE_DESKTOP_KEY_ICON:
325 : : *
326 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a localized
327 : : * string giving the name of the icon to be displayed for the desktop
328 : : * entry.
329 : : *
330 : : * Since: 2.14
331 : : */
332 : :
333 : : /**
334 : : * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
335 : : *
336 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a boolean
337 : : * stating whether the desktop entry has been deleted by the user.
338 : : *
339 : : * Since: 2.14
340 : : */
341 : :
342 : : /**
343 : : * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
344 : : *
345 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a list of
346 : : * strings identifying the environments that should display the
347 : : * desktop entry.
348 : : *
349 : : * Since: 2.14
350 : : */
351 : :
352 : : /**
353 : : * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
354 : : *
355 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a list of
356 : : * strings identifying the environments that should not display the
357 : : * desktop entry.
358 : : *
359 : : * Since: 2.14
360 : : */
361 : :
362 : : /**
363 : : * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
364 : : *
365 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
366 : : * giving the file name of a binary on disk used to determine if the
367 : : * program is actually installed.
368 : : *
369 : : * It is only valid for desktop entries with the `Application` type.
370 : : *
371 : : * Since: 2.14
372 : : */
373 : :
374 : : /**
375 : : * G_KEY_FILE_DESKTOP_KEY_EXEC:
376 : : *
377 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
378 : : * giving the command line to execute.
379 : : *
380 : : * It is only valid for desktop entries with the `Application` type.
381 : : *
382 : : * Since: 2.14
383 : : */
384 : :
385 : : /**
386 : : * G_KEY_FILE_DESKTOP_KEY_PATH:
387 : : *
388 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
389 : : * containing the working directory to run the program in.
390 : : *
391 : : * It is only valid for desktop entries with the `Application` type.
392 : : *
393 : : * Since: 2.14
394 : : */
395 : :
396 : : /**
397 : : * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
398 : : *
399 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a boolean
400 : : * stating whether the program should be run in a terminal window.
401 : : *
402 : : * It is only valid for desktop entries with the `Application` type.
403 : : *
404 : : * Since: 2.14
405 : : */
406 : :
407 : : /**
408 : : * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
409 : : *
410 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a list
411 : : * of strings giving the MIME types supported by this desktop entry.
412 : : *
413 : : * Since: 2.14
414 : : */
415 : :
416 : : /**
417 : : * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
418 : : *
419 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a list
420 : : * of strings giving the categories in which the desktop entry
421 : : * should be shown in a menu.
422 : : *
423 : : * Since: 2.14
424 : : */
425 : :
426 : : /**
427 : : * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
428 : : *
429 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a boolean
430 : : * stating whether the application supports the
431 : : * [Startup Notification Protocol Specification](https://specifications.freedesktop.org/startup-notification-spec/latest/).
432 : : *
433 : : * Since: 2.14
434 : : */
435 : :
436 : : /**
437 : : * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
438 : : *
439 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is string
440 : : * identifying the WM class or name hint of a window that the application
441 : : * will create, which can be used to emulate
442 : : * [Startup Notification](https://specifications.freedesktop.org/startup-notification-spec/latest/)
443 : : * with older applications.
444 : : *
445 : : * Since: 2.14
446 : : */
447 : :
448 : : /**
449 : : * G_KEY_FILE_DESKTOP_KEY_URL:
450 : : *
451 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string
452 : : * giving the URL to access.
453 : : *
454 : : * It is only valid for desktop entries with the `Link` type.
455 : : *
456 : : * Since: 2.14
457 : : */
458 : :
459 : : /**
460 : : * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
461 : : *
462 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a boolean
463 : : * set to true if the application is D-Bus activatable.
464 : : *
465 : : * Since: 2.38
466 : : */
467 : :
468 : : /**
469 : : * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
470 : : *
471 : : * A key under [const@GLib.KEY_FILE_DESKTOP_GROUP], whose value is a string list
472 : : * giving the available application actions.
473 : : *
474 : : * Since: 2.38
475 : : */
476 : :
477 : : /**
478 : : * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
479 : : *
480 : : * The value of the [const@GLib.KEY_FILE_DESKTOP_KEY_TYPE], key for desktop
481 : : * entries representing applications.
482 : : *
483 : : * Since: 2.14
484 : : */
485 : :
486 : : /**
487 : : * G_KEY_FILE_DESKTOP_TYPE_LINK:
488 : : *
489 : : * The value of the [const@GLib.KEY_FILE_DESKTOP_KEY_TYPE], key for desktop
490 : : * entries representing links to documents.
491 : : *
492 : : * Since: 2.14
493 : : */
494 : :
495 : : /**
496 : : * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
497 : : *
498 : : * The value of the [const@GLib.KEY_FILE_DESKTOP_KEY_TYPE], key for desktop
499 : : * entries representing directories.
500 : : *
501 : : * Since: 2.14
502 : : */
503 : :
504 : : typedef struct _GKeyFileGroup GKeyFileGroup;
505 : :
506 : : struct _GKeyFile
507 : : {
508 : : GList *groups;
509 : : GHashTable *group_hash;
510 : :
511 : : GKeyFileGroup *start_group;
512 : : GKeyFileGroup *current_group;
513 : :
514 : : GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
515 : :
516 : : gchar list_separator;
517 : :
518 : : GKeyFileFlags flags;
519 : :
520 : : gboolean checked_locales; /* TRUE if @locales has been initialised */
521 : : gchar **locales; /* (nullable) */
522 : :
523 : : gint ref_count; /* (atomic) */
524 : : };
525 : :
526 : : typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
527 : :
528 : : struct _GKeyFileGroup
529 : : {
530 : : const gchar *name; /* NULL for above first group (which will be comments) */
531 : :
532 : : GList *key_value_pairs;
533 : :
534 : : /* Used in parallel with key_value_pairs for
535 : : * increased lookup performance
536 : : */
537 : : GHashTable *lookup_map;
538 : : };
539 : :
540 : : struct _GKeyFileKeyValuePair
541 : : {
542 : : gchar *key; /* NULL for comments */
543 : : gchar *value;
544 : : };
545 : :
546 : : static gint find_file_in_data_dirs (const gchar *file,
547 : : const gchar **data_dirs,
548 : : gchar **output_file,
549 : : GError **error);
550 : : static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
551 : : gint fd,
552 : : GKeyFileFlags flags,
553 : : GError **error);
554 : : static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
555 : : const gchar *group_name);
556 : : static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
557 : : const gchar *group_name);
558 : :
559 : : static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
560 : : GKeyFileGroup *group,
561 : : const gchar *key);
562 : : static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
563 : : GKeyFileGroup *group,
564 : : const gchar *key);
565 : :
566 : : static void g_key_file_remove_group_node (GKeyFile *key_file,
567 : : GList *group_node);
568 : : static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
569 : : GKeyFileGroup *group,
570 : : GList *pair_node);
571 : :
572 : : static void g_key_file_add_key_value_pair (GKeyFile *key_file,
573 : : GKeyFileGroup *group,
574 : : GKeyFileKeyValuePair *pair,
575 : : GList *sibling);
576 : : static void g_key_file_add_key (GKeyFile *key_file,
577 : : GKeyFileGroup *group,
578 : : const gchar *key,
579 : : const gchar *value);
580 : : static void g_key_file_add_group (GKeyFile *key_file,
581 : : const gchar *group_name,
582 : : gboolean created);
583 : : static gboolean g_key_file_is_group_name (const gchar *name);
584 : : static gboolean g_key_file_is_key_name (const gchar *name,
585 : : gsize len);
586 : : static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
587 : : static gboolean g_key_file_line_is_comment (const gchar *line);
588 : : static gboolean g_key_file_line_is_group (const gchar *line);
589 : : static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
590 : : static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
591 : : const gchar *value,
592 : : GSList **separators,
593 : : GError **error);
594 : : static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
595 : : const gchar *string,
596 : : gboolean escape_separator);
597 : : static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
598 : : const gchar *value,
599 : : GError **error);
600 : : static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
601 : : gint value);
602 : : static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
603 : : const gchar *value,
604 : : GError **error);
605 : : static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
606 : : const gchar *value,
607 : : GError **error);
608 : : static const gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
609 : : gboolean value);
610 : : static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
611 : : const gchar *value,
612 : : gboolean is_final_line);
613 : : static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
614 : : const gchar *comment);
615 : : static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
616 : : const gchar *line,
617 : : gsize length,
618 : : GError **error);
619 : : static void g_key_file_parse_comment (GKeyFile *key_file,
620 : : const gchar *line,
621 : : gsize length,
622 : : GError **error);
623 : : static void g_key_file_parse_group (GKeyFile *key_file,
624 : : const gchar *line,
625 : : gsize length,
626 : : GError **error);
627 : : static const gchar *key_get_locale (const gchar *key,
628 : : gsize *len_out);
629 : : static void g_key_file_parse_data (GKeyFile *key_file,
630 : : const gchar *data,
631 : : gsize length,
632 : : GError **error);
633 : : static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
634 : : GError **error);
635 : :
636 : 10922 : G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
637 : :
638 : : /* Wrapper needed to match GDestroyNotify prototype */
639 : : static void
640 : 32 : clear_fd (void *fd)
641 : : {
642 : 32 : g_clear_fd ((int *) fd, NULL);
643 : 32 : }
644 : :
645 : : static void
646 : 4509 : g_key_file_init (GKeyFile *key_file)
647 : : {
648 : 4509 : key_file->current_group = g_new0 (GKeyFileGroup, 1);
649 : 4509 : key_file->groups = g_list_prepend (NULL, key_file->current_group);
650 : 4509 : key_file->group_hash = NULL;
651 : 4509 : key_file->start_group = NULL;
652 : 4509 : key_file->parse_buffer = NULL;
653 : 4509 : key_file->list_separator = ';';
654 : 4509 : key_file->flags = 0;
655 : 4509 : }
656 : :
657 : : static void
658 : 4509 : g_key_file_clear (GKeyFile *key_file)
659 : : {
660 : : GList *tmp, *group_node;
661 : :
662 : 4509 : if (key_file->locales)
663 : : {
664 : 728 : g_strfreev (key_file->locales);
665 : 728 : key_file->locales = NULL;
666 : 4 : }
667 : 4509 : key_file->checked_locales = FALSE;
668 : :
669 : 4509 : if (key_file->parse_buffer)
670 : : {
671 : 1551 : g_string_free (key_file->parse_buffer, TRUE);
672 : 1551 : key_file->parse_buffer = NULL;
673 : 81 : }
674 : :
675 : 4509 : tmp = key_file->groups;
676 : 11014 : while (tmp != NULL)
677 : : {
678 : 6505 : group_node = tmp;
679 : 6505 : tmp = tmp->next;
680 : 6505 : g_key_file_remove_group_node (key_file, group_node);
681 : : }
682 : :
683 : 4509 : if (key_file->group_hash != NULL)
684 : : {
685 : 1603 : g_hash_table_destroy (key_file->group_hash);
686 : 1603 : key_file->group_hash = NULL;
687 : 84 : }
688 : :
689 : 4509 : g_warn_if_fail (key_file->groups == NULL);
690 : 4509 : }
691 : :
692 : :
693 : : /**
694 : : * g_key_file_new:
695 : : *
696 : : * Creates a new empty [struct@GLib.KeyFile] object.
697 : : *
698 : : * Use [method@GLib.KeyFile.load_from_file],
699 : : * [method@GLib.KeyFile.load_from_data], [method@GLib.KeyFile.load_from_dirs] or
700 : : * [method@GLib.KeyFile.load_from_data_dirs] to
701 : : * read an existing key file.
702 : : *
703 : : * Returns: (transfer full): an empty [struct@GLib.KeyFile].
704 : : *
705 : : * Since: 2.6
706 : : **/
707 : : GKeyFile *
708 : 2938 : g_key_file_new (void)
709 : : {
710 : : GKeyFile *key_file;
711 : :
712 : 2938 : key_file = g_new0 (GKeyFile, 1);
713 : 2938 : key_file->ref_count = 1;
714 : 2938 : g_key_file_init (key_file);
715 : :
716 : 2938 : return key_file;
717 : : }
718 : :
719 : : /**
720 : : * g_key_file_set_list_separator:
721 : : * @key_file: a key file
722 : : * @separator: the separator
723 : : *
724 : : * Sets the character which is used to separate values in lists.
725 : : *
726 : : * Typically `;` or `,` are used as separators. The default list separator
727 : : * is `;`.
728 : : *
729 : : * Since: 2.6
730 : : */
731 : : void
732 : 6 : g_key_file_set_list_separator (GKeyFile *key_file,
733 : : gchar separator)
734 : : {
735 : 6 : g_return_if_fail (key_file != NULL);
736 : :
737 : 6 : key_file->list_separator = separator;
738 : 3 : }
739 : :
740 : :
741 : : /* Iterates through all the directories in *dirs trying to
742 : : * open file. When it successfully locates and opens a file it
743 : : * returns the file descriptor to the open file. It also
744 : : * outputs the absolute path of the file in output_file.
745 : : */
746 : : static gint
747 : 3 : find_file_in_data_dirs (const gchar *file,
748 : : const gchar **dirs,
749 : : gchar **output_file,
750 : : GError **error)
751 : : {
752 : : const gchar **data_dirs, *data_dir;
753 : : gchar *path;
754 : : gint fd;
755 : :
756 : 3 : path = NULL;
757 : 3 : fd = -1;
758 : :
759 : 3 : if (dirs == NULL)
760 : 0 : return fd;
761 : :
762 : 3 : data_dirs = dirs;
763 : :
764 : 10 : while (data_dirs && (data_dir = *data_dirs) && fd == -1)
765 : : {
766 : : const gchar *candidate_file;
767 : : gchar *sub_dir;
768 : :
769 : 7 : candidate_file = file;
770 : 7 : sub_dir = g_strdup ("");
771 : 13 : while (candidate_file != NULL && fd == -1)
772 : : {
773 : : gchar *p;
774 : :
775 : 19 : path = g_build_filename (data_dir, sub_dir,
776 : 6 : candidate_file, NULL);
777 : :
778 : 13 : fd = g_open (path, O_RDONLY | O_CLOEXEC, 0);
779 : :
780 : 13 : if (fd == -1)
781 : : {
782 : 12 : g_free (path);
783 : 12 : path = NULL;
784 : 6 : }
785 : :
786 : 13 : candidate_file = strchr (candidate_file, '-');
787 : :
788 : 13 : if (candidate_file == NULL)
789 : 7 : break;
790 : :
791 : 6 : candidate_file++;
792 : :
793 : 6 : g_free (sub_dir);
794 : 6 : sub_dir = g_strndup (file, candidate_file - file - 1);
795 : :
796 : 48 : for (p = sub_dir; *p != '\0'; p++)
797 : : {
798 : 42 : if (*p == '-')
799 : 0 : *p = G_DIR_SEPARATOR;
800 : 21 : }
801 : : }
802 : 7 : g_free (sub_dir);
803 : 7 : data_dirs++;
804 : : }
805 : :
806 : 3 : if (fd == -1)
807 : : {
808 : 3 : g_set_error_literal (error, G_KEY_FILE_ERROR,
809 : : G_KEY_FILE_ERROR_NOT_FOUND,
810 : 1 : _("Valid key file could not be "
811 : : "found in search dirs"));
812 : 1 : }
813 : :
814 : 3 : if (output_file != NULL && fd != -1)
815 : 1 : *output_file = g_strdup (path);
816 : :
817 : 3 : g_free (path);
818 : :
819 : 3 : return fd;
820 : 1 : }
821 : :
822 : : static gboolean
823 : 1427 : g_key_file_load_from_fd (GKeyFile *key_file,
824 : : gint fd,
825 : : GKeyFileFlags flags,
826 : : GError **error)
827 : : {
828 : 1427 : GError *key_file_error = NULL;
829 : : gssize bytes_read;
830 : : struct stat stat_buf;
831 : : gchar read_buf[4096];
832 : : gchar list_separator;
833 : :
834 : 1427 : if (fstat (fd, &stat_buf) < 0)
835 : : {
836 : 0 : int errsv = errno;
837 : 0 : g_set_error_literal (error, G_FILE_ERROR,
838 : 0 : g_file_error_from_errno (errsv),
839 : 0 : g_strerror (errsv));
840 : 0 : return FALSE;
841 : : }
842 : :
843 : 1427 : if (!S_ISREG (stat_buf.st_mode))
844 : : {
845 : 0 : g_set_error_literal (error, G_KEY_FILE_ERROR,
846 : : G_KEY_FILE_ERROR_PARSE,
847 : 0 : _("Not a regular file"));
848 : 0 : return FALSE;
849 : : }
850 : :
851 : 1427 : list_separator = key_file->list_separator;
852 : 1427 : g_key_file_clear (key_file);
853 : 1427 : g_key_file_init (key_file);
854 : 1427 : key_file->list_separator = list_separator;
855 : 1427 : key_file->flags = flags;
856 : :
857 : 20 : do
858 : : {
859 : : int errsv;
860 : :
861 : 3212 : bytes_read = read (fd, read_buf, 4096);
862 : 3212 : errsv = errno;
863 : :
864 : 3212 : if (bytes_read == 0) /* End of File */
865 : 1423 : break;
866 : :
867 : 1789 : if (bytes_read < 0)
868 : : {
869 : 0 : if (errsv == EINTR || errsv == EAGAIN)
870 : 0 : continue;
871 : :
872 : 0 : g_set_error_literal (error, G_FILE_ERROR,
873 : 0 : g_file_error_from_errno (errsv),
874 : 0 : g_strerror (errsv));
875 : 0 : return FALSE;
876 : : }
877 : :
878 : 1810 : g_key_file_parse_data (key_file,
879 : 21 : read_buf, bytes_read,
880 : : &key_file_error);
881 : 21 : }
882 : 1789 : while (!key_file_error);
883 : :
884 : 1427 : if (key_file_error)
885 : : {
886 : 4 : g_propagate_error (error, key_file_error);
887 : 4 : return FALSE;
888 : : }
889 : :
890 : 1423 : g_key_file_flush_parse_buffer (key_file, &key_file_error);
891 : :
892 : 1423 : if (key_file_error)
893 : : {
894 : 0 : g_propagate_error (error, key_file_error);
895 : 0 : return FALSE;
896 : : }
897 : :
898 : 1423 : return TRUE;
899 : 20 : }
900 : :
901 : : /**
902 : : * g_key_file_load_from_file:
903 : : * @key_file: an empty key file
904 : : * @file: (type filename): the path of a filename to load, in the GLib filename encoding
905 : : * @flags: flags from [flags@GLib.KeyFileFlags]
906 : : * @error: return location for a [struct@GLib.Error]
907 : : *
908 : : * Loads a key file into an empty [struct@GLib.KeyFile] structure.
909 : : *
910 : : * If the OS returns an error when opening or reading the file, a
911 : : * [error@GLib.FileError] is returned. If there is a problem parsing the file,
912 : : * a [error@GLib.KeyFileError] is returned.
913 : : *
914 : : * This function will never return a [error@GLib.KeyFileError.NOT_FOUND]
915 : : * error. If the @file is not found, [error@GLib.FileError.NOENT] is returned.
916 : : *
917 : : * Returns: true if a key file could be loaded, false otherwise
918 : : *
919 : : * Since: 2.6
920 : : **/
921 : : gboolean
922 : 2676 : g_key_file_load_from_file (GKeyFile *key_file,
923 : : const gchar *file,
924 : : GKeyFileFlags flags,
925 : : GError **error)
926 : : {
927 : 2676 : GError *key_file_error = NULL;
928 : : gint fd;
929 : : int errsv;
930 : :
931 : 2676 : g_return_val_if_fail (key_file != NULL, FALSE);
932 : 2676 : g_return_val_if_fail (file != NULL, FALSE);
933 : :
934 : 2676 : fd = g_open (file, O_RDONLY | O_CLOEXEC, 0);
935 : 2676 : errsv = errno;
936 : :
937 : 2676 : if (fd == -1)
938 : : {
939 : 1283 : g_set_error_literal (error, G_FILE_ERROR,
940 : 1282 : g_file_error_from_errno (errsv),
941 : 1 : g_strerror (errsv));
942 : 1282 : return FALSE;
943 : : }
944 : :
945 : 1394 : g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
946 : 1394 : close (fd);
947 : :
948 : 1394 : if (key_file_error)
949 : : {
950 : 2 : g_propagate_error (error, key_file_error);
951 : 2 : return FALSE;
952 : : }
953 : :
954 : 1392 : return TRUE;
955 : 5 : }
956 : :
957 : : /**
958 : : * g_key_file_load_from_data:
959 : : * @key_file: an empty key file
960 : : * @data: key file loaded in memory
961 : : * @length: the length of @data in bytes (or `(gsize)-1` if data is nul-terminated)
962 : : * @flags: flags from [flags@GLib.KeyFileFlags]
963 : : * @error: return location for a [struct@GLib.Error]
964 : : *
965 : : * Loads a key file from memory into an empty [struct@GLib.KeyFile] structure.
966 : : *
967 : : * If the object cannot be created then a [error@GLib.KeyFileError is returned.
968 : : *
969 : : * Returns: true if a key file could be loaded, false otherwise
970 : : *
971 : : * Since: 2.6
972 : : **/
973 : : gboolean
974 : 140 : g_key_file_load_from_data (GKeyFile *key_file,
975 : : const gchar *data,
976 : : gsize length,
977 : : GKeyFileFlags flags,
978 : : GError **error)
979 : : {
980 : 140 : GError *key_file_error = NULL;
981 : : gchar list_separator;
982 : :
983 : 140 : g_return_val_if_fail (key_file != NULL, FALSE);
984 : 140 : g_return_val_if_fail (data != NULL || length == 0, FALSE);
985 : :
986 : 140 : if (length == (gsize)-1)
987 : 104 : length = strlen (data);
988 : :
989 : 140 : list_separator = key_file->list_separator;
990 : 140 : g_key_file_clear (key_file);
991 : 140 : g_key_file_init (key_file);
992 : 140 : key_file->list_separator = list_separator;
993 : 140 : key_file->flags = flags;
994 : :
995 : 140 : g_key_file_parse_data (key_file, data, length, &key_file_error);
996 : :
997 : 140 : if (key_file_error)
998 : : {
999 : 32 : g_propagate_error (error, key_file_error);
1000 : 32 : return FALSE;
1001 : : }
1002 : :
1003 : 108 : g_key_file_flush_parse_buffer (key_file, &key_file_error);
1004 : :
1005 : 108 : if (key_file_error)
1006 : : {
1007 : 0 : g_propagate_error (error, key_file_error);
1008 : 0 : return FALSE;
1009 : : }
1010 : :
1011 : 108 : return TRUE;
1012 : 61 : }
1013 : :
1014 : : /**
1015 : : * g_key_file_load_unix_configurations:
1016 : : * @key_file: an empty key file
1017 : : * @project: (nullable): name of the project used as subdirectory
1018 : : * @etc_subdir: (nullable) (type filename): directory path for administrative configuration files
1019 : : * @run_subdir: (nullable) (type filename): directory path for ephemeral overrides
1020 : : * @usr_subdir: (nullable) (type filename): directory path for vendor-defined settings
1021 : : * @config_name: (type filename): basename of the configuration file
1022 : : * @config_suffix: (nullable) (type filename): suffix of the configuration file
1023 : : * @flags: flags from [flags@GLib.KeyFileFlags]
1024 : : * @error: return location for a [struct@GLib.Error]
1025 : : *
1026 : : * Evaluates and merges configuration key/values from multiple Unix directories into a single key file.
1027 : : *
1028 : : * This function reads and merges all available configuration files based on the rules defined by
1029 : : * the [UAPI Configuration Files Specification](https://github.com/uapi-group/specifications/blob/main/specs/configuration_files_specification.md) (version 1).
1030 : : *
1031 : : * This API is primarily intended for system daemons or CLI tools that need to load systemd-style
1032 : : * configuration files spread across vendor and customization directories. User applications
1033 : : * should generally use [`GSettings`](../gio/class.Settings.html) instead to manage user preferences.
1034 : : *
1035 : : * ### Directory Layout Guidance
1036 : : * When choosing paths for @etc_subdir and @usr_subdir, you should prefer using your build
1037 : : * system's standard configuration variables (such as `$sysconfdir` and `$libdir`) rather
1038 : : * than hard-coding absolute paths. For context, on a standard Linux layout, @etc_subdir
1039 : : * typically points to administrative overrides (e.g., `/etc`), @run_subdir to /run while
1040 : : * @usr_subdir points to the vendor defaults (e.g., `/usr/lib` or `/usr/share`). Passing `NULL`
1041 : : * will fall back to platform-specific defaults where appropriate.
1042 : : *
1043 : : * ### Relationship to XDG Base Directory Specification
1044 : : * Note that this function operates independently of the
1045 : : * [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir/latest/)
1046 : : * and [func@GLib.get_system_config_dirs]. While XDG directories (like `$XDG_CONFIG_DIRS`)
1047 : : * are intended to manage desktop session applications and user-facing environments, this
1048 : : * API is strictly designed for low-level system-wide components following the UAPI
1049 : : * specification. Mixing the two concepts should be avoided.
1050 : : *
1051 : : * Note that this function is synchronous and blocking. Because it may load an arbitrary amount
1052 : : * of files, it is best suited for application startup or non-interactive environments. If called
1053 : : * from a user-interactive UI thread, you must handle asynchronicity yourself if needed.
1054 : : *
1055 : : * If no file for parsing has been found, [error@GLib.KeyFileError.NOT_FOUND] is returned.
1056 : : * If files have been found but the OS returns an error when opening or reading a
1057 : : * file, a [error@GLib.FileError] is returned. If there is a problem parsing
1058 : : * files, a [error@GLib.KeyFileError] is returned.
1059 : : *
1060 : : *
1061 : : * The following example parses files in following order:
1062 : : *
1063 : : * - `<SYSCONFDIR>/project/mydaemon.conf`
1064 : : * - `/run/project/mydaemon.conf` (if <SYSCONFDIR>/project/mydaemon.conf is not defined)
1065 : : * - `<LIBDIR>/project/mydaemon.conf`
1066 : : * (if `<SYSCONFDIR>/project/mydaemon.conf` and `/run/project/mydaemon.conf are not defined`)
1067 : : * - valid drop-ins in `<SYSCONFDIR>/project/mydaemon.conf.d/`, `/run/project/mydaemon.conf.d/`, `<LIBDIR>/project/mydaemon.conf.d/`
1068 : : *
1069 : : *```
1070 : : * g_autoptr(GKeyFile) kf = g_key_file_new ();
1071 : : * g_autoptr(GError) local_error = NULL;
1072 : : *
1073 : : * // Using build-configured paths or defaults instead of hardcoded strings
1074 : : * gboolean success = g_key_file_load_unix_configurations (kf,
1075 : : * "my-daemon",
1076 : : * SYSCONFDIR,
1077 : : * RUNDIR,
1078 : : * LIBDIR,
1079 : : * "mydaemon",
1080 : : * "conf",
1081 : : * G_KEY_FILE_NONE,
1082 : : * &local_error);
1083 : : * if (!success)
1084 : : * {
1085 : : * g_warning ("Failed to load configuration: %s", local_error->message);
1086 : : * return;
1087 : : * }
1088 : : *
1089 : : * g_autofree char *val = g_key_file_get_string (kf, "Management", "Setting", NULL);
1090 : : *```
1091 : : *
1092 : : * Returns: true on success, false otherwise
1093 : : * Since: 2.90
1094 : : */
1095 : : gboolean
1096 : 16 : g_key_file_load_unix_configurations (GKeyFile *key_file,
1097 : : const gchar *project,
1098 : : const gchar *etc_subdir,
1099 : : const gchar *run_subdir,
1100 : : const gchar *usr_subdir,
1101 : : const gchar *config_name,
1102 : : const gchar *config_suffix,
1103 : : GKeyFileFlags flags,
1104 : : GError **error)
1105 : : {
1106 : 16 : gchar *suffix = NULL;
1107 : 16 : gboolean ret = TRUE;
1108 : 16 : GArray *parsing_fd_list = NULL;
1109 : 16 : GPtrArray *etc_list = NULL;
1110 : 16 : GPtrArray *usr_list = NULL;
1111 : 16 : GPtrArray *run_list = NULL;
1112 : :
1113 : 16 : g_return_val_if_fail (key_file != NULL, FALSE);
1114 : 16 : g_return_val_if_fail (config_name != NULL, FALSE);
1115 : 16 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1116 : :
1117 : : /* Array of file descriptors to parse. */
1118 : 16 : parsing_fd_list = g_array_new (FALSE, FALSE, sizeof (int));
1119 : 16 : g_array_set_clear_func (parsing_fd_list, clear_fd);
1120 : :
1121 : 16 : etc_list = g_ptr_array_new_with_free_func (g_free);
1122 : 16 : usr_list = g_ptr_array_new_with_free_func (g_free);
1123 : 16 : run_list = g_ptr_array_new_with_free_func (g_free);
1124 : :
1125 : : /* Default settings */
1126 : 16 : if (!etc_subdir)
1127 : 2 : etc_subdir = "/etc";
1128 : 16 : if (!run_subdir)
1129 : 2 : run_subdir = "/run";
1130 : 16 : if (!usr_subdir)
1131 : 0 : usr_subdir = "/usr/share";
1132 : :
1133 : 16 : gchar *config_filename = NULL;
1134 : 16 : if (config_suffix)
1135 : 12 : config_filename = g_strconcat (config_name, ".", config_suffix, NULL);
1136 : : else
1137 : 4 : config_filename = g_strdup (config_name);
1138 : :
1139 : 16 : if (!project)
1140 : 14 : project = "";
1141 : :
1142 : 32 : const char *top_level_dirs[] = {
1143 : 8 : etc_subdir,
1144 : 8 : run_subdir,
1145 : 8 : usr_subdir
1146 : : };
1147 : : /* Evaluating first "main" file which has to be parsed */
1148 : 40 : for (size_t i = 0; i < G_N_ELEMENTS (top_level_dirs); i++)
1149 : : {
1150 : 34 : char *path = g_build_filename (top_level_dirs[i], project, config_filename, NULL);
1151 : 34 : int fd = g_open (path, O_RDONLY | O_CLOEXEC, 0);
1152 : 34 : g_free (path);
1153 : 34 : if (fd >= 0)
1154 : : {
1155 : 10 : int stolen_fd = g_steal_fd (&fd);
1156 : 10 : g_array_append_val (parsing_fd_list, stolen_fd);
1157 : 10 : break;
1158 : : }
1159 : 12 : }
1160 : 16 : g_free (config_filename);
1161 : :
1162 : : /* Evaluating all "Drop-ins" files which have to be parsed and insert them into
1163 : : lists. The content of each directory has an own list. */
1164 : 16 : gchar *config_d_filename = NULL;
1165 : :
1166 : 16 : if (config_suffix)
1167 : : {
1168 : 12 : config_d_filename = g_strconcat (config_name, ".", config_suffix, ".d", NULL);
1169 : 12 : suffix = g_strconcat (".", config_suffix, NULL);
1170 : 6 : }
1171 : : else
1172 : 4 : config_d_filename = g_strconcat (config_name, ".d", NULL);
1173 : :
1174 : 32 : GPtrArray *top_level_list[] = {
1175 : 8 : etc_list,
1176 : 8 : run_list,
1177 : 8 : usr_list
1178 : : };
1179 : :
1180 : 64 : for (size_t i = 0; i < G_N_ELEMENTS (top_level_dirs); i++)
1181 : : {
1182 : 72 : gchar *scan_dir = g_build_filename (top_level_dirs[i], project,
1183 : 24 : config_d_filename, NULL);
1184 : 48 : GDir *dir = g_dir_open (scan_dir, 0, NULL);
1185 : 48 : if (dir)
1186 : : {
1187 : : const gchar *file;
1188 : 46 : while ((file = g_dir_read_name (dir)) != NULL)
1189 : : {
1190 : 28 : if (!suffix || g_str_has_suffix (file, suffix))
1191 : 28 : g_ptr_array_add (top_level_list[i], g_strdup (file));
1192 : : }
1193 : 18 : g_clear_pointer (&dir, g_dir_close);
1194 : 9 : }
1195 : 48 : g_free (scan_dir);
1196 : 24 : }
1197 : 16 : g_free (suffix);
1198 : :
1199 : : /* Sorting all lists */
1200 : 16 : g_ptr_array_sort_values (usr_list, (GCompareFunc) g_strcmp0);
1201 : 16 : g_ptr_array_sort_values (run_list, (GCompareFunc) g_strcmp0);
1202 : 16 : g_ptr_array_sort_values (etc_list, (GCompareFunc) g_strcmp0);
1203 : :
1204 : : /* Evaluate the right order of available "Drop-ins" which have to be parsed.
1205 : : * The rules are described in:
1206 : : * https://github.com/uapi-group/specifications/blob/main/specs/configuration_files_specification.md#drop-ins */
1207 : :
1208 : 16 : guint size_etc = etc_list->len;
1209 : 16 : guint size_run = run_list->len;
1210 : 16 : guint size_usr = usr_list->len;
1211 : : /* Descriptive indices to track the current position in each list */
1212 : 16 : guint idx_etc = 0;
1213 : 16 : guint idx_run = 0;
1214 : 16 : guint idx_usr = 0;
1215 : :
1216 : 38 : while (idx_etc < size_etc || idx_run < size_run || idx_usr < size_usr)
1217 : : {
1218 : : /* Pointers to the current smallest string from each list,
1219 : : * or NULL if the list is exhausted */
1220 : 22 : const gchar *val_etc = (idx_etc < size_etc) ? (gchar *) g_ptr_array_index (etc_list, idx_etc) : NULL;
1221 : 22 : const gchar *val_run = (idx_run < size_run) ? (gchar *) g_ptr_array_index (run_list, idx_run) : NULL;
1222 : 22 : const gchar *val_usr = (idx_usr < size_usr) ? (gchar *) g_ptr_array_index (usr_list, idx_usr) : NULL;
1223 : :
1224 : : /* Step 1: Find the absolute alphabetically "smallest" string available */
1225 : 22 : const gchar *smallest = NULL;
1226 : :
1227 : 22 : if (val_etc)
1228 : 12 : smallest = val_etc;
1229 : 22 : if (val_run)
1230 : 11 : if (!smallest || strcmp (val_run, smallest) < 0)
1231 : 6 : smallest = val_run;
1232 : :
1233 : 22 : if (val_usr)
1234 : 23 : if (!smallest || strcmp (val_usr, smallest) < 0)
1235 : 6 : smallest = val_usr;
1236 : :
1237 : 22 : const gchar *choice = NULL;
1238 : 22 : const gchar *choice_dir = NULL;
1239 : :
1240 : : /* Step 2: We have the globally smallest alphabetical string.
1241 : : * Now apply Priority #2: If this string exists in multiple lists,
1242 : : * we officially take it from the highest priority list (etc > run > var). */
1243 : 22 : if (val_etc && strcmp (val_etc, smallest) == 0)
1244 : : {
1245 : 12 : choice = val_etc; /* etc wins the tie-breaker */
1246 : 12 : choice_dir = etc_subdir;
1247 : 6 : }
1248 : 10 : else if (val_run && strcmp (val_run, smallest) == 0)
1249 : : {
1250 : 4 : choice = val_run; /* run wins the tie-breaker */
1251 : 4 : choice_dir = run_subdir;
1252 : 2 : }
1253 : : else
1254 : : {
1255 : 6 : choice = val_usr; /* usr wins by default */
1256 : 6 : choice_dir = usr_subdir;
1257 : : }
1258 : :
1259 : : /* Add the chosen winner to the merged list */
1260 : 33 : char *path = g_build_filename (choice_dir, project,
1261 : 11 : config_d_filename, choice, NULL);
1262 : 22 : int fd = g_open (path, O_RDONLY | O_CLOEXEC, 0);
1263 : 22 : g_free (path);
1264 : 22 : if (fd >= 0)
1265 : : {
1266 : 22 : int stolen_fd = g_steal_fd (&fd);
1267 : 22 : g_array_append_val (parsing_fd_list, stolen_fd);
1268 : 11 : }
1269 : :
1270 : : /* Step 3: Advance indices for ANY list that matches the chosen string value
1271 : : This clears duplicates out of the way for the next iteration. */
1272 : 22 : if (val_etc && strcmp (choice, val_etc) == 0)
1273 : 12 : idx_etc++;
1274 : 22 : if (val_run && strcmp (choice, val_run) == 0)
1275 : 4 : idx_run++;
1276 : 22 : if (val_usr && strcmp (choice, val_usr) == 0)
1277 : 10 : idx_usr++;
1278 : : }
1279 : 16 : g_free (config_d_filename);
1280 : :
1281 : : /* Parsing all configuration files in the correct order and merging the entries.*/
1282 : 48 : for (guint index = 0; index < parsing_fd_list->len; index++)
1283 : : {
1284 : 32 : GKeyFile *parsed_key_file = g_key_file_new ();
1285 : 32 : GError *key_file_error = NULL;
1286 : :
1287 : 48 : if (!g_key_file_load_from_fd (parsed_key_file, g_array_index (parsing_fd_list, int, index),
1288 : 16 : flags, &key_file_error))
1289 : : {
1290 : 2 : g_propagate_error (error, g_steal_pointer (&key_file_error));
1291 : 2 : ret = FALSE;
1292 : 2 : g_key_file_unref (parsed_key_file);
1293 : 2 : continue;
1294 : : }
1295 : :
1296 : 92 : for (GList *g = parsed_key_file->groups; g != NULL; g = g->next)
1297 : : {
1298 : 62 : GKeyFileGroup *group = (GKeyFileGroup *) g->data;
1299 : :
1300 : 62 : if (group->name == NULL)
1301 : 30 : continue;
1302 : :
1303 : 112 : for (GList *p = group->key_value_pairs; p != NULL; p = p->next)
1304 : : {
1305 : 80 : GKeyFileKeyValuePair *pair = (GKeyFileKeyValuePair *) p->data;
1306 : :
1307 : 80 : if (pair->key == NULL)
1308 : 2 : continue;
1309 : :
1310 : 78 : g_key_file_set_value (key_file, group->name, pair->key, pair->value);
1311 : 39 : }
1312 : 16 : }
1313 : :
1314 : 30 : g_key_file_unref (parsed_key_file);
1315 : 15 : }
1316 : :
1317 : 16 : if (parsing_fd_list->len == 0)
1318 : : {
1319 : 3 : g_set_error_literal (error, G_KEY_FILE_ERROR,
1320 : : G_KEY_FILE_ERROR_NOT_FOUND,
1321 : 1 : _("Valid key file could not be "
1322 : : "found in search dirs"));
1323 : 2 : ret = FALSE;
1324 : 1 : }
1325 : :
1326 : 16 : g_ptr_array_unref (usr_list);
1327 : 16 : g_ptr_array_unref (etc_list);
1328 : 16 : g_ptr_array_unref (run_list);
1329 : 16 : g_array_unref (parsing_fd_list);
1330 : :
1331 : 16 : return ret;
1332 : 8 : }
1333 : :
1334 : : /**
1335 : : * g_key_file_load_from_bytes:
1336 : : * @key_file: an empty [struct@GLib.KeyFile] struct
1337 : : * @bytes: a [struct@GLib.Bytes]
1338 : : * @flags: flags from [flags@GLib.KeyFileFlags]
1339 : : * @error: return location for a [struct@GLib.Error]
1340 : : *
1341 : : * Loads a key file from the data in @bytes into an empty [struct@GLib.KeyFile]
1342 : : * structure.
1343 : : *
1344 : : * If the object cannot be created then a [error@GLib.KeyFileError] is returned.
1345 : : *
1346 : : * Returns: true if a key file could be loaded, false otherwise
1347 : : *
1348 : : * Since: 2.50
1349 : : **/
1350 : : gboolean
1351 : 2 : g_key_file_load_from_bytes (GKeyFile *key_file,
1352 : : GBytes *bytes,
1353 : : GKeyFileFlags flags,
1354 : : GError **error)
1355 : : {
1356 : : const guchar *data;
1357 : : gsize size;
1358 : :
1359 : 2 : g_return_val_if_fail (key_file != NULL, FALSE);
1360 : 2 : g_return_val_if_fail (bytes != NULL, FALSE);
1361 : :
1362 : 2 : data = g_bytes_get_data (bytes, &size);
1363 : 2 : return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1364 : 1 : }
1365 : :
1366 : : /**
1367 : : * g_key_file_load_from_dirs:
1368 : : * @key_file: an empty [struct@GLib.KeyFile] struct
1369 : : * @file: (type filename): a relative path to a filename to open and parse
1370 : : * @search_dirs: (array zero-terminated=1) (element-type filename): `NULL`-terminated
1371 : : * array of directories to search
1372 : : * @full_path: (out) (type filename) (optional): return location for a string
1373 : : * containing the full path of the file, or `NULL` to ignore
1374 : : * @flags: flags from [flags@GLib.KeyFileFlags]
1375 : : * @error: return location for a [struct@GLib.Error]
1376 : : *
1377 : : * Looks for a key file named @file in the paths specified in @search_dirs,
1378 : : * loads the file into @key_file and returns the file’s full path in @full_path.
1379 : : *
1380 : : * @search_dirs are checked in the order listed in the array, with the highest
1381 : : * priority directory listed first. Within each directory, @file is looked for.
1382 : : * If it’s not found, `-` characters in @file are progressively replaced with
1383 : : * directory separators to search subdirectories of the search directory. If the
1384 : : * file has not been found after all `-` characters have been replaced, the next
1385 : : * search directory in @search_dirs is checked.
1386 : : *
1387 : : * If the file could not be found in any of the @search_dirs,
1388 : : * [error@GLib.KeyFileError.NOT_FOUND] is returned. If
1389 : : * the file is found but the OS returns an error when opening or reading the
1390 : : * file, a [error@GLib.FileError] is returned. If there is a problem parsing the
1391 : : * file, a [error@GLib.KeyFileError] is returned.
1392 : : *
1393 : : * Returns: true if a key file could be loaded, false otherwise
1394 : : *
1395 : : * Since: 2.14
1396 : : **/
1397 : : gboolean
1398 : 3 : g_key_file_load_from_dirs (GKeyFile *key_file,
1399 : : const gchar *file,
1400 : : const gchar **search_dirs,
1401 : : gchar **full_path,
1402 : : GKeyFileFlags flags,
1403 : : GError **error)
1404 : : {
1405 : 3 : GError *key_file_error = NULL;
1406 : : const gchar **data_dirs;
1407 : : gchar *output_path;
1408 : : gint fd;
1409 : : gboolean found_file;
1410 : :
1411 : 3 : g_return_val_if_fail (key_file != NULL, FALSE);
1412 : 3 : g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1413 : 3 : g_return_val_if_fail (search_dirs != NULL, FALSE);
1414 : :
1415 : 3 : found_file = FALSE;
1416 : 3 : data_dirs = search_dirs;
1417 : 3 : output_path = NULL;
1418 : 4 : while (*data_dirs != NULL && !found_file)
1419 : : {
1420 : 3 : g_free (output_path);
1421 : 3 : output_path = NULL;
1422 : :
1423 : 3 : fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1424 : : &key_file_error);
1425 : :
1426 : 3 : if (fd == -1)
1427 : : {
1428 : 2 : if (key_file_error)
1429 : 2 : g_propagate_error (error, key_file_error);
1430 : 2 : break;
1431 : : }
1432 : :
1433 : 1 : found_file = g_key_file_load_from_fd (key_file, fd, flags,
1434 : : &key_file_error);
1435 : 1 : close (fd);
1436 : :
1437 : 1 : if (key_file_error)
1438 : : {
1439 : 0 : g_propagate_error (error, key_file_error);
1440 : 0 : break;
1441 : : }
1442 : : }
1443 : :
1444 : 3 : if (found_file && full_path)
1445 : 0 : *full_path = output_path;
1446 : : else
1447 : 3 : g_free (output_path);
1448 : :
1449 : 3 : return found_file;
1450 : 1 : }
1451 : :
1452 : : /**
1453 : : * g_key_file_load_from_data_dirs:
1454 : : * @key_file: an empty [struct@GLib.KeyFile] struct
1455 : : * @file: (type filename): a relative path to a filename to open and parse
1456 : : * @full_path: (out) (type filename) (optional): return location for a string
1457 : : * containing the full path of the file, or `NULL` to ignore
1458 : : * @flags: flags from [flags@GLib.KeyFileFlags]
1459 : : * @error: return location for a [struct@GLib.Error]
1460 : : *
1461 : : * Looks for a key file named @file in the paths returned from
1462 : : * [func@GLib.get_user_data_dir] and [func@GLib.get_system_data_dirs].
1463 : : *
1464 : : * The search algorithm from [method@GLib.KeyFile.load_from_dirs] is used. If
1465 : : * @file is found, it’s loaded into @key_file and its full path is returned in
1466 : : * @full_path.
1467 : : *
1468 : : * If the file could not be loaded then either a [error@GLib.FileError] or
1469 : : * [error@GLib.KeyFileError] is returned.
1470 : : *
1471 : : * Returns: true if a key file could be loaded, false otherwise
1472 : : * Since: 2.6
1473 : : **/
1474 : : gboolean
1475 : 3 : g_key_file_load_from_data_dirs (GKeyFile *key_file,
1476 : : const gchar *file,
1477 : : gchar **full_path,
1478 : : GKeyFileFlags flags,
1479 : : GError **error)
1480 : : {
1481 : : gchar **all_data_dirs;
1482 : : const gchar * user_data_dir;
1483 : : const gchar * const * system_data_dirs;
1484 : : gsize i, j;
1485 : : gboolean found_file;
1486 : :
1487 : 3 : g_return_val_if_fail (key_file != NULL, FALSE);
1488 : 3 : g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1489 : :
1490 : 3 : user_data_dir = g_get_user_data_dir ();
1491 : 3 : system_data_dirs = g_get_system_data_dirs ();
1492 : 3 : all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1493 : :
1494 : 3 : i = 0;
1495 : 3 : all_data_dirs[i++] = g_strdup (user_data_dir);
1496 : :
1497 : 3 : j = 0;
1498 : 9 : while (system_data_dirs[j] != NULL)
1499 : 10 : all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1500 : 3 : all_data_dirs[i] = NULL;
1501 : :
1502 : 4 : found_file = g_key_file_load_from_dirs (key_file,
1503 : 1 : file,
1504 : 1 : (const gchar **)all_data_dirs,
1505 : 1 : full_path,
1506 : 1 : flags,
1507 : 1 : error);
1508 : :
1509 : 3 : g_strfreev (all_data_dirs);
1510 : :
1511 : 3 : return found_file;
1512 : 1 : }
1513 : :
1514 : : /**
1515 : : * g_key_file_ref: (skip)
1516 : : * @key_file: a key file
1517 : : *
1518 : : * Increases the reference count of @key_file.
1519 : : *
1520 : : * Returns: (transfer full): the same @key_file.
1521 : : *
1522 : : * Since: 2.32
1523 : : **/
1524 : : GKeyFile *
1525 : 275 : g_key_file_ref (GKeyFile *key_file)
1526 : : {
1527 : 275 : g_return_val_if_fail (key_file != NULL, NULL);
1528 : :
1529 : 275 : g_atomic_int_inc (&key_file->ref_count);
1530 : :
1531 : 275 : return key_file;
1532 : 3 : }
1533 : :
1534 : : /**
1535 : : * g_key_file_free: (skip)
1536 : : * @key_file: (transfer full): a key file
1537 : : *
1538 : : * Clears all keys and groups from @key_file, and decreases the
1539 : : * reference count by 1.
1540 : : *
1541 : : * If the reference count reaches zero, frees the key file and all its allocated
1542 : : * memory.
1543 : : *
1544 : : * Since: 2.6
1545 : : **/
1546 : : void
1547 : 2608 : g_key_file_free (GKeyFile *key_file)
1548 : : {
1549 : 2608 : g_return_if_fail (key_file != NULL);
1550 : :
1551 : 2608 : g_key_file_clear (key_file);
1552 : :
1553 : 2608 : if (g_atomic_int_dec_and_test (&key_file->ref_count))
1554 : 2604 : g_free_sized (key_file, sizeof (GKeyFile));
1555 : : else
1556 : 4 : g_key_file_init (key_file);
1557 : 78 : }
1558 : :
1559 : : /**
1560 : : * g_key_file_unref:
1561 : : * @key_file: (transfer full): a key file
1562 : : *
1563 : : * Decreases the reference count of @key_file by 1.
1564 : : *
1565 : : * If the reference count reaches zero, frees the key file and all its allocated
1566 : : * memory.
1567 : : *
1568 : : * Since: 2.32
1569 : : **/
1570 : : void
1571 : 605 : g_key_file_unref (GKeyFile *key_file)
1572 : : {
1573 : 605 : g_return_if_fail (key_file != NULL);
1574 : :
1575 : 605 : if (g_atomic_int_dec_and_test (&key_file->ref_count))
1576 : : {
1577 : 334 : g_key_file_clear (key_file);
1578 : 334 : g_free_sized (key_file, sizeof (GKeyFile));
1579 : 23 : }
1580 : 24 : }
1581 : :
1582 : : /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1583 : : * true for locales that match those in g_get_language_names().
1584 : : */
1585 : : static gboolean
1586 : 30146 : g_key_file_locale_is_interesting (GKeyFile *key_file,
1587 : : const gchar *locale,
1588 : : gsize locale_len)
1589 : : {
1590 : : gsize i;
1591 : :
1592 : 30146 : if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1593 : 28 : return TRUE;
1594 : :
1595 : 30118 : if (!key_file->checked_locales)
1596 : : {
1597 : 728 : g_assert (key_file->locales == NULL);
1598 : 728 : key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
1599 : 728 : key_file->checked_locales = TRUE;
1600 : 4 : }
1601 : :
1602 : 90149 : for (i = 0; key_file->locales[i] != NULL; i++)
1603 : : {
1604 : 60164 : if (g_ascii_strncasecmp (key_file->locales[i], locale, locale_len) == 0 &&
1605 : 157 : key_file->locales[i][locale_len] == '\0')
1606 : 133 : return TRUE;
1607 : 40 : }
1608 : :
1609 : 29985 : return FALSE;
1610 : 38 : }
1611 : :
1612 : : static void
1613 : 60254 : g_key_file_parse_line (GKeyFile *key_file,
1614 : : const gchar *line,
1615 : : gsize length,
1616 : : GError **error)
1617 : : {
1618 : 60254 : GError *parse_error = NULL;
1619 : : const gchar *line_start;
1620 : :
1621 : 60261 : g_return_if_fail (key_file != NULL);
1622 : 60254 : g_return_if_fail (line != NULL);
1623 : :
1624 : 60254 : line_start = line;
1625 : 60266 : while (g_ascii_isspace (*line_start))
1626 : 12 : line_start++;
1627 : :
1628 : 60254 : if (g_key_file_line_is_comment (line_start))
1629 : 705 : g_key_file_parse_comment (key_file, line, length, &parse_error);
1630 : 59549 : else if (g_key_file_line_is_group (line_start))
1631 : 1989 : g_key_file_parse_group (key_file, line_start,
1632 : 1890 : length - (line_start - line),
1633 : : &parse_error);
1634 : 57659 : else if (g_key_file_line_is_key_value_pair (line_start))
1635 : 57962 : g_key_file_parse_key_value_pair (key_file, line_start,
1636 : 57645 : length - (line_start - line),
1637 : : &parse_error);
1638 : : else
1639 : : {
1640 : 14 : gchar *line_utf8 = g_utf8_make_valid (line, length);
1641 : 21 : g_set_error (error, G_KEY_FILE_ERROR,
1642 : : G_KEY_FILE_ERROR_PARSE,
1643 : 7 : _("Key file contains line “%s” which is not "
1644 : : "a key-value pair, group, or comment"),
1645 : 7 : line_utf8);
1646 : 14 : g_free (line_utf8);
1647 : :
1648 : 14 : return;
1649 : : }
1650 : :
1651 : 60240 : if (parse_error)
1652 : 22 : g_propagate_error (error, parse_error);
1653 : 456 : }
1654 : :
1655 : : static void
1656 : 1077 : g_key_file_parse_comment (GKeyFile *key_file,
1657 : : const gchar *line,
1658 : : gsize length,
1659 : : GError **error)
1660 : : {
1661 : : GKeyFileKeyValuePair *pair;
1662 : :
1663 : 1077 : if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1664 : 1017 : return;
1665 : :
1666 : 60 : g_warn_if_fail (key_file->current_group != NULL);
1667 : :
1668 : 60 : pair = g_new (GKeyFileKeyValuePair, 1);
1669 : 60 : pair->key = NULL;
1670 : 60 : pair->value = g_strndup (line, length);
1671 : :
1672 : 60 : key_file->current_group->key_value_pairs =
1673 : 60 : g_list_prepend (key_file->current_group->key_value_pairs, pair);
1674 : 47 : }
1675 : :
1676 : : static void
1677 : 1890 : g_key_file_parse_group (GKeyFile *key_file,
1678 : : const gchar *line,
1679 : : gsize length,
1680 : : GError **error)
1681 : : {
1682 : : gchar *group_name;
1683 : : const gchar *group_name_start, *group_name_end;
1684 : :
1685 : : /* advance past opening '['
1686 : : */
1687 : 1890 : group_name_start = line + 1;
1688 : 1890 : group_name_end = line + length - 1;
1689 : :
1690 : 1892 : while (*group_name_end != ']')
1691 : 2 : group_name_end--;
1692 : :
1693 : 1989 : group_name = g_strndup (group_name_start,
1694 : 1890 : group_name_end - group_name_start);
1695 : :
1696 : 1890 : if (!g_key_file_is_group_name (group_name))
1697 : : {
1698 : 9 : g_set_error (error, G_KEY_FILE_ERROR,
1699 : : G_KEY_FILE_ERROR_PARSE,
1700 : 3 : _("Invalid group name: %s"), group_name);
1701 : 6 : g_free (group_name);
1702 : 6 : return;
1703 : : }
1704 : :
1705 : 1884 : g_key_file_add_group (key_file, group_name, FALSE);
1706 : 1884 : g_free (group_name);
1707 : 99 : }
1708 : :
1709 : : static void
1710 : 57645 : g_key_file_parse_key_value_pair (GKeyFile *key_file,
1711 : : const gchar *line,
1712 : : gsize length,
1713 : : GError **error)
1714 : : {
1715 : : gchar *key;
1716 : : const gchar *key_end, *value_start;
1717 : : const gchar *locale;
1718 : : gsize locale_len;
1719 : : gsize key_len, value_len;
1720 : :
1721 : 57645 : if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1722 : : {
1723 : 3 : g_set_error_literal (error, G_KEY_FILE_ERROR,
1724 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1725 : 1 : _("Key file does not start with a group"));
1726 : 9 : return;
1727 : : }
1728 : :
1729 : 57643 : key_end = value_start = strchr (line, '=');
1730 : :
1731 : 57643 : g_warn_if_fail (key_end != NULL);
1732 : :
1733 : 57643 : key_end--;
1734 : 57643 : value_start++;
1735 : :
1736 : : /* Pull the key name from the line (chomping trailing whitespace)
1737 : : */
1738 : 57912 : while (g_ascii_isspace (*key_end))
1739 : 269 : key_end--;
1740 : :
1741 : 57643 : key_len = key_end - line + 2;
1742 : :
1743 : 57643 : g_warn_if_fail (key_len <= length);
1744 : :
1745 : 57643 : if (!g_key_file_is_key_name (line, key_len - 1))
1746 : : {
1747 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
1748 : : G_KEY_FILE_ERROR_PARSE,
1749 : 8 : _("Invalid key name: %.*s"), (int) key_len - 1, line);
1750 : 8 : return;
1751 : : }
1752 : :
1753 : 57635 : key = g_strndup (line, key_len - 1);
1754 : :
1755 : : /* Pull the value from the line (chugging leading whitespace)
1756 : : */
1757 : 57898 : while (g_ascii_isspace (*value_start))
1758 : 263 : value_start++;
1759 : :
1760 : 57635 : value_len = line + length - value_start;
1761 : :
1762 : 57635 : g_warn_if_fail (key_file->start_group != NULL);
1763 : :
1764 : : /* Checked on entry to this function */
1765 : 57635 : g_assert (key_file->current_group != NULL);
1766 : 57635 : g_assert (key_file->current_group->name != NULL);
1767 : :
1768 : 57635 : if (key_file->start_group == key_file->current_group
1769 : 56941 : && strcmp (key, "Encoding") == 0)
1770 : : {
1771 : 355 : if (value_len != strlen ("UTF-8") ||
1772 : 176 : g_ascii_strncasecmp (value_start, "UTF-8", value_len) != 0)
1773 : : {
1774 : 6 : gchar *value_utf8 = g_utf8_make_valid (value_start, value_len);
1775 : 9 : g_set_error (error, G_KEY_FILE_ERROR,
1776 : : G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1777 : 3 : _("Key file contains unsupported "
1778 : 3 : "encoding “%s”"), value_utf8);
1779 : 6 : g_free (value_utf8);
1780 : :
1781 : 6 : g_free (key);
1782 : 6 : return;
1783 : : }
1784 : 0 : }
1785 : :
1786 : : /* Is this key a translation? If so, is it one that we care about?
1787 : : */
1788 : 57629 : locale = key_get_locale (key, &locale_len);
1789 : :
1790 : 57629 : if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale, locale_len))
1791 : : {
1792 : : GKeyFileKeyValuePair *pair;
1793 : :
1794 : 27644 : pair = g_new (GKeyFileKeyValuePair, 1);
1795 : 27644 : pair->key = g_steal_pointer (&key);
1796 : 27644 : pair->value = g_strndup (value_start, value_len);
1797 : :
1798 : 27933 : g_key_file_add_key_value_pair (key_file, key_file->current_group, pair,
1799 : 27644 : key_file->current_group->key_value_pairs);
1800 : 289 : }
1801 : :
1802 : 57629 : g_free (key);
1803 : 317 : }
1804 : :
1805 : : static const gchar *
1806 : 57629 : key_get_locale (const gchar *key,
1807 : : gsize *len_out)
1808 : : {
1809 : : const gchar *locale;
1810 : : gsize locale_len;
1811 : :
1812 : 57629 : locale = g_strrstr (key, "[");
1813 : 57629 : if (locale != NULL)
1814 : 30146 : locale_len = strlen (locale);
1815 : : else
1816 : 27483 : locale_len = 0;
1817 : :
1818 : 57629 : if (locale_len > 2)
1819 : : {
1820 : 30146 : locale++; /* skip `[` */
1821 : 30146 : locale_len -= 2; /* drop `[` and `]` */
1822 : 38 : }
1823 : : else
1824 : : {
1825 : 27483 : locale = NULL;
1826 : 27483 : locale_len = 0;
1827 : : }
1828 : :
1829 : 57629 : *len_out = locale_len;
1830 : 57629 : return locale;
1831 : : }
1832 : :
1833 : : static void
1834 : 1929 : g_key_file_parse_data (GKeyFile *key_file,
1835 : : const gchar *data,
1836 : : gsize length,
1837 : : GError **error)
1838 : : {
1839 : : GError *parse_error;
1840 : : gsize i;
1841 : :
1842 : 1947 : g_return_if_fail (key_file != NULL);
1843 : 1929 : g_return_if_fail (data != NULL || length == 0);
1844 : :
1845 : 1929 : parse_error = NULL;
1846 : :
1847 : 1929 : if (!key_file->parse_buffer)
1848 : 1551 : key_file->parse_buffer = g_string_sized_new (128);
1849 : :
1850 : 1929 : i = 0;
1851 : 123023 : while (i < length)
1852 : : {
1853 : 121130 : if (data[i] == '\n')
1854 : : {
1855 : 60527 : if (key_file->parse_buffer->len > 0
1856 : 60169 : && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1857 : 444 : == '\r'))
1858 : 107 : g_string_erase (key_file->parse_buffer,
1859 : 102 : key_file->parse_buffer->len - 1,
1860 : : 1);
1861 : :
1862 : : /* When a newline is encountered flush the parse buffer so that the
1863 : : * line can be parsed. Note that completely blank lines won't show
1864 : : * up in the parse buffer, so they get parsed directly.
1865 : : */
1866 : 60527 : if (key_file->parse_buffer->len > 0)
1867 : 60155 : g_key_file_flush_parse_buffer (key_file, &parse_error);
1868 : : else
1869 : 372 : g_key_file_parse_comment (key_file, "", 1, &parse_error);
1870 : :
1871 : 60527 : if (parse_error)
1872 : : {
1873 : 36 : g_propagate_error (error, parse_error);
1874 : 36 : return;
1875 : : }
1876 : 60491 : i++;
1877 : 440 : }
1878 : : else
1879 : : {
1880 : : const gchar *start_of_line;
1881 : : const gchar *end_of_line;
1882 : : gsize line_length;
1883 : :
1884 : 60603 : start_of_line = data + i;
1885 : 60603 : end_of_line = memchr (start_of_line, '\n', length - i);
1886 : :
1887 : 60603 : if (end_of_line == NULL)
1888 : 477 : end_of_line = data + length;
1889 : :
1890 : 60603 : line_length = end_of_line - start_of_line;
1891 : :
1892 : 60603 : g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1893 : 60603 : i += line_length;
1894 : : }
1895 : : }
1896 : 82 : }
1897 : :
1898 : : static void
1899 : 61686 : g_key_file_flush_parse_buffer (GKeyFile *key_file,
1900 : : GError **error)
1901 : : {
1902 : 61686 : GError *file_error = NULL;
1903 : :
1904 : 61720 : g_return_if_fail (key_file != NULL);
1905 : :
1906 : 61686 : if (!key_file->parse_buffer)
1907 : 16 : return;
1908 : :
1909 : 61670 : file_error = NULL;
1910 : :
1911 : 61670 : if (key_file->parse_buffer->len > 0)
1912 : : {
1913 : 60710 : g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1914 : 60254 : key_file->parse_buffer->len,
1915 : : &file_error);
1916 : 60254 : g_string_erase (key_file->parse_buffer, 0, -1);
1917 : :
1918 : 60254 : if (file_error)
1919 : : {
1920 : 36 : g_propagate_error (error, file_error);
1921 : 36 : return;
1922 : : }
1923 : 438 : }
1924 : 507 : }
1925 : :
1926 : : /**
1927 : : * g_key_file_to_data:
1928 : : * @key_file: a key file
1929 : : * @length: (out) (optional): return location for the length of the
1930 : : * returned string, or `NULL` to ignore
1931 : : * @error: return location for a [struct@GLib.Error]
1932 : : *
1933 : : * Outputs @key_file as a string.
1934 : : *
1935 : : * Note that this function never reports an error.
1936 : : *
1937 : : * Returns: a newly allocated string holding the contents of the key file
1938 : : *
1939 : : * Since: 2.6
1940 : : **/
1941 : : gchar *
1942 : 181 : g_key_file_to_data (GKeyFile *key_file,
1943 : : gsize *length,
1944 : : GError **error)
1945 : : {
1946 : : GString *data_string;
1947 : : GList *group_node, *key_file_node;
1948 : :
1949 : 181 : g_return_val_if_fail (key_file != NULL, NULL);
1950 : :
1951 : 181 : data_string = g_string_new (NULL);
1952 : :
1953 : 201 : for (group_node = g_list_last (key_file->groups);
1954 : 729 : group_node != NULL;
1955 : 548 : group_node = group_node->prev)
1956 : : {
1957 : : GKeyFileGroup *group;
1958 : :
1959 : 548 : group = (GKeyFileGroup *) group_node->data;
1960 : :
1961 : 548 : if (group->name != NULL)
1962 : 367 : g_string_append_printf (data_string, "[%s]\n", group->name);
1963 : :
1964 : 586 : for (key_file_node = g_list_last (group->key_value_pairs);
1965 : 1139 : key_file_node != NULL;
1966 : 591 : key_file_node = key_file_node->prev)
1967 : : {
1968 : : GKeyFileKeyValuePair *pair;
1969 : :
1970 : 591 : pair = (GKeyFileKeyValuePair *) key_file_node->data;
1971 : :
1972 : 591 : if (pair->key != NULL)
1973 : 367 : g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1974 : : else
1975 : 224 : g_string_append_printf (data_string, "%s\n", pair->value);
1976 : 38 : }
1977 : 20 : }
1978 : :
1979 : 181 : if (length)
1980 : 179 : *length = data_string->len;
1981 : :
1982 : 181 : return g_string_free (data_string, FALSE);
1983 : 6 : }
1984 : :
1985 : : /**
1986 : : * g_key_file_get_keys:
1987 : : * @key_file: a key file
1988 : : * @group_name: a group name
1989 : : * @length: (out) (optional): return location for the number of keys returned,
1990 : : * or `NULL` to ignore
1991 : : * @error: return location for a [struct@GLib.Error]
1992 : : *
1993 : : * Returns all keys for the group name @group_name.
1994 : : *
1995 : : * The array of returned keys will be `NULL`-terminated, so @length may
1996 : : * optionally be `NULL`. If the @group_name cannot be found,
1997 : : * [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.
1998 : : *
1999 : : * Returns: (array zero-terminated=1) (transfer full): a newly-allocated
2000 : : * `NULL`-terminated array of strings. Use [func@GLib.strfreev] to free it.
2001 : : *
2002 : : * Since: 2.6
2003 : : **/
2004 : : gchar **
2005 : 520 : g_key_file_get_keys (GKeyFile *key_file,
2006 : : const gchar *group_name,
2007 : : gsize *length,
2008 : : GError **error)
2009 : : {
2010 : : GKeyFileGroup *group;
2011 : : GList *tmp;
2012 : : gchar **keys;
2013 : : gsize i, num_keys;
2014 : :
2015 : 520 : g_return_val_if_fail (key_file != NULL, NULL);
2016 : 520 : g_return_val_if_fail (group_name != NULL, NULL);
2017 : :
2018 : 520 : group = g_key_file_lookup_group (key_file, group_name);
2019 : :
2020 : 520 : if (!group)
2021 : : {
2022 : 272 : g_set_error (error, G_KEY_FILE_ERROR,
2023 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2024 : 1 : _("Key file does not have group “%s”"),
2025 : 1 : group_name);
2026 : 271 : return NULL;
2027 : : }
2028 : :
2029 : 249 : num_keys = 0;
2030 : 14347 : for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
2031 : : {
2032 : : GKeyFileKeyValuePair *pair;
2033 : :
2034 : 14098 : pair = (GKeyFileKeyValuePair *) tmp->data;
2035 : :
2036 : 14098 : if (pair->key)
2037 : 14005 : num_keys++;
2038 : 18 : }
2039 : :
2040 : 249 : keys = g_new (gchar *, num_keys + 1);
2041 : :
2042 : 249 : i = num_keys - 1;
2043 : 14347 : for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
2044 : : {
2045 : : GKeyFileKeyValuePair *pair;
2046 : :
2047 : 14098 : pair = (GKeyFileKeyValuePair *) tmp->data;
2048 : :
2049 : 14098 : if (pair->key)
2050 : : {
2051 : 14005 : keys[i] = g_strdup (pair->key);
2052 : 14005 : i--;
2053 : 10 : }
2054 : 18 : }
2055 : :
2056 : 249 : keys[num_keys] = NULL;
2057 : :
2058 : 249 : if (length)
2059 : 6 : *length = num_keys;
2060 : :
2061 : 249 : return keys;
2062 : 4 : }
2063 : :
2064 : : /**
2065 : : * g_key_file_get_start_group:
2066 : : * @key_file: a key file
2067 : : *
2068 : : * Returns the name of the start group of the file.
2069 : : *
2070 : : * Returns: (nullable): The start group of the key file.
2071 : : *
2072 : : * Since: 2.6
2073 : : **/
2074 : : gchar *
2075 : 281 : g_key_file_get_start_group (GKeyFile *key_file)
2076 : : {
2077 : 281 : g_return_val_if_fail (key_file != NULL, NULL);
2078 : :
2079 : 281 : if (key_file->start_group)
2080 : 561 : return g_strdup (key_file->start_group->name);
2081 : :
2082 : 0 : return NULL;
2083 : 1 : }
2084 : :
2085 : : /**
2086 : : * g_key_file_get_groups:
2087 : : * @key_file: a key file
2088 : : * @length: (out) (optional): return location for the number of returned groups,
2089 : : * or `NULL` to ignore
2090 : : *
2091 : : * Returns all groups in the key file loaded with @key_file.
2092 : : *
2093 : : * The array of returned groups will be `NULL`-terminated, so
2094 : : * @length may optionally be `NULL`.
2095 : : *
2096 : : * Returns: (array zero-terminated=1) (transfer full): a newly-allocated
2097 : : * `NULL`-terminated array of strings. Use [func@GLib.strfreev] to free it.
2098 : : * Since: 2.6
2099 : : **/
2100 : : gchar **
2101 : 15 : g_key_file_get_groups (GKeyFile *key_file,
2102 : : gsize *length)
2103 : : {
2104 : : GList *group_node;
2105 : : gchar **groups;
2106 : : gsize i, num_groups;
2107 : :
2108 : 15 : g_return_val_if_fail (key_file != NULL, NULL);
2109 : :
2110 : 15 : num_groups = g_list_length (key_file->groups);
2111 : :
2112 : 15 : g_return_val_if_fail (num_groups > 0, NULL);
2113 : :
2114 : 15 : group_node = g_list_last (key_file->groups);
2115 : :
2116 : 15 : g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
2117 : :
2118 : : /* Only need num_groups instead of num_groups + 1
2119 : : * because the first group of the file (last in the
2120 : : * list) is always the comment group at the top,
2121 : : * which we skip
2122 : : */
2123 : 15 : groups = g_new (gchar *, num_groups);
2124 : :
2125 : :
2126 : 15 : i = 0;
2127 : 25 : for (group_node = group_node->prev;
2128 : 40 : group_node != NULL;
2129 : 25 : group_node = group_node->prev)
2130 : : {
2131 : : GKeyFileGroup *group;
2132 : :
2133 : 25 : group = (GKeyFileGroup *) group_node->data;
2134 : :
2135 : 25 : g_warn_if_fail (group->name != NULL);
2136 : :
2137 : 40 : groups[i++] = g_strdup (group->name);
2138 : 10 : }
2139 : 15 : groups[i] = NULL;
2140 : :
2141 : 15 : if (length)
2142 : 10 : *length = i;
2143 : :
2144 : 15 : return groups;
2145 : 5 : }
2146 : :
2147 : : static void
2148 : 8972 : set_not_found_key_error (const char *group_name,
2149 : : const char *key,
2150 : : GError **error)
2151 : : {
2152 : 9003 : g_set_error (error, G_KEY_FILE_ERROR,
2153 : : G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2154 : 31 : _("Key file does not have key “%s” in group “%s”"),
2155 : 31 : key, group_name);
2156 : 8972 : }
2157 : :
2158 : : /**
2159 : : * g_key_file_get_value:
2160 : : * @key_file: a key file
2161 : : * @group_name: a group name
2162 : : * @key: a key
2163 : : * @error: return location for a [struct@GLib.Error]
2164 : : *
2165 : : * Returns the raw value associated with @key under @group_name.
2166 : : *
2167 : : * Use [method@GLib.KeyFile.get_string] to retrieve an unescaped UTF-8 string.
2168 : : *
2169 : : * If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND]
2170 : : * is returned. If the @group_name cannot be found,
2171 : : * [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.
2172 : : *
2173 : : * Returns: a newly allocated string or `NULL` if the specified
2174 : : * key cannot be found.
2175 : : *
2176 : : * Since: 2.6
2177 : : **/
2178 : : gchar *
2179 : 27866 : g_key_file_get_value (GKeyFile *key_file,
2180 : : const gchar *group_name,
2181 : : const gchar *key,
2182 : : GError **error)
2183 : : {
2184 : : GKeyFileGroup *group;
2185 : : GKeyFileKeyValuePair *pair;
2186 : 27866 : gchar *value = NULL;
2187 : :
2188 : 27866 : g_return_val_if_fail (key_file != NULL, NULL);
2189 : 27866 : g_return_val_if_fail (group_name != NULL, NULL);
2190 : 27866 : g_return_val_if_fail (key != NULL, NULL);
2191 : :
2192 : 27866 : group = g_key_file_lookup_group (key_file, group_name);
2193 : :
2194 : 27866 : if (!group)
2195 : : {
2196 : 188 : g_set_error (error, G_KEY_FILE_ERROR,
2197 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2198 : 9 : _("Key file does not have group “%s”"),
2199 : 9 : group_name);
2200 : 179 : return NULL;
2201 : : }
2202 : :
2203 : 27687 : pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2204 : :
2205 : 27687 : if (pair)
2206 : 37266 : value = g_strdup (pair->value);
2207 : : else
2208 : 8925 : set_not_found_key_error (group_name, key, error);
2209 : :
2210 : 27687 : return value;
2211 : 297 : }
2212 : :
2213 : : /**
2214 : : * g_key_file_set_value:
2215 : : * @key_file: a key file
2216 : : * @group_name: a group name
2217 : : * @key: a key
2218 : : * @value: a string
2219 : : *
2220 : : * Associates a new value with @key under @group_name.
2221 : : *
2222 : : * If @key cannot be found then it is created. If @group_name cannot
2223 : : * be found then it is created. To set an UTF-8 string which may contain
2224 : : * characters that need escaping (such as newlines or spaces), use
2225 : : * [method@GLib.KeyFile.set_string].
2226 : : *
2227 : : * Since: 2.6
2228 : : **/
2229 : : void
2230 : 486 : g_key_file_set_value (GKeyFile *key_file,
2231 : : const gchar *group_name,
2232 : : const gchar *key,
2233 : : const gchar *value)
2234 : : {
2235 : : GKeyFileGroup *group;
2236 : : GKeyFileKeyValuePair *pair;
2237 : :
2238 : 486 : g_return_if_fail (key_file != NULL);
2239 : 486 : g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
2240 : 486 : g_return_if_fail (key != NULL && g_key_file_is_key_name (key, strlen (key)));
2241 : 486 : g_return_if_fail (value != NULL);
2242 : :
2243 : 486 : group = g_key_file_lookup_group (key_file, group_name);
2244 : :
2245 : 486 : if (!group)
2246 : : {
2247 : 122 : g_key_file_add_group (key_file, group_name, TRUE);
2248 : 122 : group = (GKeyFileGroup *) key_file->groups->data;
2249 : :
2250 : 122 : g_key_file_add_key (key_file, group, key, value);
2251 : 20 : }
2252 : : else
2253 : : {
2254 : 364 : pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2255 : :
2256 : 364 : if (!pair)
2257 : 241 : g_key_file_add_key (key_file, group, key, value);
2258 : : else
2259 : : {
2260 : 123 : g_free (pair->value);
2261 : 123 : pair->value = g_strdup (value);
2262 : : }
2263 : : }
2264 : 82 : }
2265 : :
2266 : : /**
2267 : : * g_key_file_get_string:
2268 : : * @key_file: a key file
2269 : : * @group_name: a group name
2270 : : * @key: a key
2271 : : * @error: return location for a [struct@GLib.Error]
2272 : : *
2273 : : * Returns the string value associated with @key under @group_name.
2274 : : *
2275 : : * Unlike [method@GLib.KeyFile.get_value], this function handles escape
2276 : : * sequences like `\s`.
2277 : : *
2278 : : * If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2279 : : * returned. If the @group_name cannot be found,
2280 : : * [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.
2281 : : *
2282 : : * Returns: a newly allocated string or `NULL` if the specified
2283 : : * key cannot be found.
2284 : : *
2285 : : * Since: 2.6
2286 : : **/
2287 : : gchar *
2288 : 8968 : g_key_file_get_string (GKeyFile *key_file,
2289 : : const gchar *group_name,
2290 : : const gchar *key,
2291 : : GError **error)
2292 : : {
2293 : : gchar *value, *string_value;
2294 : : GError *key_file_error;
2295 : :
2296 : 8968 : g_return_val_if_fail (key_file != NULL, NULL);
2297 : 8968 : g_return_val_if_fail (group_name != NULL, NULL);
2298 : 8968 : g_return_val_if_fail (key != NULL, NULL);
2299 : :
2300 : 8968 : key_file_error = NULL;
2301 : :
2302 : 8968 : value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2303 : :
2304 : 8968 : if (key_file_error)
2305 : : {
2306 : 5077 : g_propagate_error (error, key_file_error);
2307 : 5077 : return NULL;
2308 : : }
2309 : :
2310 : 3891 : if (!g_utf8_validate (value, -1, NULL))
2311 : : {
2312 : 3 : gchar *value_utf8 = g_utf8_make_valid (value, -1);
2313 : 4 : g_set_error (error, G_KEY_FILE_ERROR,
2314 : : G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2315 : 1 : _("Key file contains key “%s” with value “%s” "
2316 : 1 : "which is not UTF-8"), key, value_utf8);
2317 : 3 : g_free (value_utf8);
2318 : 3 : g_free (value);
2319 : :
2320 : 3 : return NULL;
2321 : : }
2322 : :
2323 : 3888 : string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
2324 : : &key_file_error);
2325 : 3888 : g_free (value);
2326 : :
2327 : 3888 : if (key_file_error)
2328 : : {
2329 : 14 : if (g_error_matches (key_file_error,
2330 : 4 : G_KEY_FILE_ERROR,
2331 : : G_KEY_FILE_ERROR_INVALID_VALUE))
2332 : : {
2333 : 14 : g_set_error (error, G_KEY_FILE_ERROR,
2334 : : G_KEY_FILE_ERROR_INVALID_VALUE,
2335 : 4 : _("Key file contains key “%s” "
2336 : : "which has a value that cannot be interpreted."),
2337 : 4 : key);
2338 : 10 : g_error_free (key_file_error);
2339 : 4 : }
2340 : : else
2341 : 0 : g_propagate_error (error, key_file_error);
2342 : 4 : }
2343 : :
2344 : 3888 : return string_value;
2345 : 152 : }
2346 : :
2347 : : /**
2348 : : * g_key_file_set_string:
2349 : : * @key_file: a key file
2350 : : * @group_name: a group name
2351 : : * @key: a key
2352 : : * @string: a string
2353 : : *
2354 : : * Associates a new string value with @key under @group_name.
2355 : : *
2356 : : * If @key cannot be found then it is created.
2357 : : * If @group_name cannot be found then it is created.
2358 : : * Unlike [method@GLib.KeyFile.set_value], this function handles characters
2359 : : * that need escaping, such as newlines.
2360 : : *
2361 : : * Since: 2.6
2362 : : **/
2363 : : void
2364 : 234 : g_key_file_set_string (GKeyFile *key_file,
2365 : : const gchar *group_name,
2366 : : const gchar *key,
2367 : : const gchar *string)
2368 : : {
2369 : : gchar *value;
2370 : :
2371 : 234 : g_return_if_fail (key_file != NULL);
2372 : 234 : g_return_if_fail (string != NULL);
2373 : :
2374 : 234 : value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2375 : 234 : g_key_file_set_value (key_file, group_name, key, value);
2376 : 234 : g_free (value);
2377 : 22 : }
2378 : :
2379 : : /**
2380 : : * g_key_file_get_string_list:
2381 : : * @key_file: a key file
2382 : : * @group_name: a group name
2383 : : * @key: a key
2384 : : * @length: (out) (optional): return location for the number of returned
2385 : : * strings, or `NULL` to ignore
2386 : : * @error: return location for a [struct@GLib.Error]
2387 : : *
2388 : : * Returns the values associated with @key under @group_name.
2389 : : *
2390 : : * If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2391 : : * returned. If the @group_name cannot be found,
2392 : : * [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.
2393 : : *
2394 : : * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2395 : : * a `NULL`-terminated string array or `NULL` if the specified
2396 : : * key cannot be found. The array should be freed with [func@GLib.strfreev].
2397 : : *
2398 : : * Since: 2.6
2399 : : **/
2400 : : gchar **
2401 : 16167 : g_key_file_get_string_list (GKeyFile *key_file,
2402 : : const gchar *group_name,
2403 : : const gchar *key,
2404 : : gsize *length,
2405 : : GError **error)
2406 : : {
2407 : 16167 : GError *key_file_error = NULL;
2408 : : gchar *value, *string_value, **values;
2409 : : gint i, len;
2410 : 16167 : GSList *p, *pieces = NULL;
2411 : :
2412 : 16167 : g_return_val_if_fail (key_file != NULL, NULL);
2413 : 16167 : g_return_val_if_fail (group_name != NULL, NULL);
2414 : 16167 : g_return_val_if_fail (key != NULL, NULL);
2415 : :
2416 : 16167 : if (length)
2417 : 274 : *length = 0;
2418 : :
2419 : 16167 : value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2420 : :
2421 : 16167 : if (key_file_error)
2422 : : {
2423 : 1803 : g_propagate_error (error, key_file_error);
2424 : 1803 : return NULL;
2425 : : }
2426 : :
2427 : 14364 : if (!g_utf8_validate (value, -1, NULL))
2428 : : {
2429 : 2 : gchar *value_utf8 = g_utf8_make_valid (value, -1);
2430 : 3 : g_set_error (error, G_KEY_FILE_ERROR,
2431 : : G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2432 : 1 : _("Key file contains key “%s” with value “%s” "
2433 : 1 : "which is not UTF-8"), key, value_utf8);
2434 : 2 : g_free (value_utf8);
2435 : 2 : g_free (value);
2436 : :
2437 : 2 : return NULL;
2438 : : }
2439 : :
2440 : 14362 : string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2441 : 14362 : g_free (value);
2442 : 14362 : g_free (string_value);
2443 : :
2444 : 14362 : if (key_file_error)
2445 : : {
2446 : 3 : if (g_error_matches (key_file_error,
2447 : 1 : G_KEY_FILE_ERROR,
2448 : : G_KEY_FILE_ERROR_INVALID_VALUE))
2449 : : {
2450 : 3 : g_set_error (error, G_KEY_FILE_ERROR,
2451 : : G_KEY_FILE_ERROR_INVALID_VALUE,
2452 : 1 : _("Key file contains key “%s” "
2453 : : "which has a value that cannot be interpreted."),
2454 : 1 : key);
2455 : 2 : g_error_free (key_file_error);
2456 : 1 : }
2457 : : else
2458 : 0 : g_propagate_error (error, key_file_error);
2459 : :
2460 : 2 : g_slist_free_full (pieces, g_free);
2461 : 2 : return NULL;
2462 : : }
2463 : :
2464 : 14360 : len = g_slist_length (pieces);
2465 : 14360 : values = g_new (gchar *, len + 1);
2466 : 30541 : for (p = pieces, i = 0; p; p = p->next)
2467 : 16181 : values[i++] = p->data;
2468 : 14360 : values[len] = NULL;
2469 : :
2470 : 14360 : g_slist_free (pieces);
2471 : :
2472 : 14360 : if (length)
2473 : 116 : *length = len;
2474 : :
2475 : 14360 : return values;
2476 : 20 : }
2477 : :
2478 : : /**
2479 : : * g_key_file_set_string_list:
2480 : : * @key_file: a key file
2481 : : * @group_name: a group name
2482 : : * @key: a key
2483 : : * @list: (array zero-terminated=1 length=length) (element-type utf8): an array
2484 : : * of string values
2485 : : * @length: number of string values in @list
2486 : : *
2487 : : * Associates a list of string values for @key under @group_name.
2488 : : *
2489 : : * If @key cannot be found then it is created.
2490 : : * If @group_name cannot be found then it is created.
2491 : : *
2492 : : * Since: 2.6
2493 : : **/
2494 : : void
2495 : 94 : g_key_file_set_string_list (GKeyFile *key_file,
2496 : : const gchar *group_name,
2497 : : const gchar *key,
2498 : : const gchar * const list[],
2499 : : gsize length)
2500 : : {
2501 : : GString *value_list;
2502 : : gsize i;
2503 : :
2504 : 94 : g_return_if_fail (key_file != NULL);
2505 : 94 : g_return_if_fail (list != NULL || length == 0);
2506 : :
2507 : 94 : value_list = g_string_sized_new (length * 128);
2508 : 236 : for (i = 0; i < length && list[i] != NULL; i++)
2509 : : {
2510 : : gchar *value;
2511 : :
2512 : 142 : value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2513 : 7 : g_string_append (value_list, value);
2514 : 142 : g_string_append_c (value_list, key_file->list_separator);
2515 : :
2516 : 142 : g_free (value);
2517 : 7 : }
2518 : :
2519 : 94 : g_key_file_set_value (key_file, group_name, key, value_list->str);
2520 : 94 : g_string_free (value_list, TRUE);
2521 : 3 : }
2522 : :
2523 : : /**
2524 : : * g_key_file_set_locale_string:
2525 : : * @key_file: a key file
2526 : : * @group_name: a group name
2527 : : * @key: a key
2528 : : * @locale: a locale identifier
2529 : : * @string: a string
2530 : : *
2531 : : * Associates a string value for @key and @locale under @group_name.
2532 : : *
2533 : : * If the translation for @key cannot be found then it is created.
2534 : : *
2535 : : * If @locale is `C` then the untranslated value is set (since GLib 2.84).
2536 : : *
2537 : : * Since: 2.6
2538 : : **/
2539 : : void
2540 : 4 : g_key_file_set_locale_string (GKeyFile *key_file,
2541 : : const gchar *group_name,
2542 : : const gchar *key,
2543 : : const gchar *locale,
2544 : : const gchar *string)
2545 : : {
2546 : : gchar *full_key, *value;
2547 : :
2548 : 4 : g_return_if_fail (key_file != NULL);
2549 : 4 : g_return_if_fail (key != NULL);
2550 : 4 : g_return_if_fail (locale != NULL);
2551 : 4 : g_return_if_fail (string != NULL);
2552 : :
2553 : 4 : value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2554 : 5 : full_key = g_strcmp0 (locale, "C") != 0 ? g_strdup_printf ("%s[%s]", key, locale) : g_strdup (key);
2555 : 4 : g_key_file_set_value (key_file, group_name, full_key, value);
2556 : 4 : g_free (full_key);
2557 : 4 : g_free (value);
2558 : 2 : }
2559 : :
2560 : : /**
2561 : : * g_key_file_get_locale_string:
2562 : : * @key_file: a key file
2563 : : * @group_name: a group name
2564 : : * @key: a key
2565 : : * @locale: (nullable): a locale identifier or `NULL` to use the current locale
2566 : : * @error: return location for a [struct@GLib.Error]
2567 : : *
2568 : : * Returns the value associated with @key under @group_name
2569 : : * translated in the given @locale if available.
2570 : : *
2571 : : * If @locale is `C` then the untranslated value is returned (since GLib 2.84).
2572 : : *
2573 : : * If @locale is `NULL` then the current locale is assumed.
2574 : : *
2575 : : * If @locale is to be non-`NULL`, or if the current locale will change over
2576 : : * the lifetime of the [struct@GLib.KeyFile], it must be loaded with
2577 : : * [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all
2578 : : * locales.
2579 : : *
2580 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2581 : : * returned. If the value associated
2582 : : * with @key cannot be interpreted or no suitable translation can
2583 : : * be found then the untranslated value is returned.
2584 : : *
2585 : : * Returns: a newly allocated string or `NULL` if the specified
2586 : : * key cannot be found.
2587 : : *
2588 : : * Since: 2.6
2589 : : **/
2590 : : gchar *
2591 : 5678 : g_key_file_get_locale_string (GKeyFile *key_file,
2592 : : const gchar *group_name,
2593 : : const gchar *key,
2594 : : const gchar *locale,
2595 : : GError **error)
2596 : : {
2597 : : gchar *candidate_key, *translated_value;
2598 : : GError *key_file_error;
2599 : : gchar **languages;
2600 : 5678 : gboolean free_languages = FALSE;
2601 : : gint i;
2602 : :
2603 : 5678 : g_return_val_if_fail (key_file != NULL, NULL);
2604 : 5678 : g_return_val_if_fail (group_name != NULL, NULL);
2605 : 5678 : g_return_val_if_fail (key != NULL, NULL);
2606 : :
2607 : 5678 : candidate_key = NULL;
2608 : 5678 : translated_value = NULL;
2609 : 5678 : key_file_error = NULL;
2610 : :
2611 : 5678 : if (locale)
2612 : : {
2613 : 66 : languages = g_get_locale_variants (locale);
2614 : 66 : free_languages = TRUE;
2615 : 33 : }
2616 : : else
2617 : : {
2618 : 5612 : languages = (gchar **) g_get_language_names ();
2619 : 5612 : free_languages = FALSE;
2620 : : }
2621 : :
2622 : 6971 : for (i = 0; languages[i]; i++)
2623 : : {
2624 : 6947 : if (g_strcmp0 (languages[i], "C") == 0)
2625 : 5518 : break;
2626 : :
2627 : 1429 : candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2628 : :
2629 : 1475 : translated_value = g_key_file_get_string (key_file,
2630 : 46 : group_name,
2631 : 46 : candidate_key, NULL);
2632 : 1429 : g_free (candidate_key);
2633 : :
2634 : 1429 : if (translated_value)
2635 : 136 : break;
2636 : 25 : }
2637 : :
2638 : : /* Fallback to untranslated key
2639 : : */
2640 : 5678 : if (!translated_value)
2641 : : {
2642 : 5542 : translated_value = g_key_file_get_string (key_file, group_name, key,
2643 : : &key_file_error);
2644 : :
2645 : 5542 : if (!translated_value)
2646 : 2676 : g_propagate_error (error, key_file_error);
2647 : 16 : }
2648 : :
2649 : 5678 : if (free_languages)
2650 : 66 : g_strfreev (languages);
2651 : :
2652 : 5678 : return translated_value;
2653 : 37 : }
2654 : :
2655 : : /**
2656 : : * g_key_file_get_locale_for_key:
2657 : : * @key_file: a key file
2658 : : * @group_name: a group name
2659 : : * @key: a key
2660 : : * @locale: (nullable): a locale identifier or `NULL` to use the current locale
2661 : : *
2662 : : * Returns the actual locale which the result of
2663 : : * [method@GLib.KeyFile.get_locale_string] or
2664 : : * [method@GLib.KeyFile.get_locale_string_list] came from.
2665 : : *
2666 : : * If calling [method@GLib.KeyFile.get_locale_string] or
2667 : : * [method@GLib.KeyFile.get_locale_string_list] with exactly the same @key_file,
2668 : : * @group_name, @key and @locale, the result of those functions will
2669 : : * have originally been tagged with the locale that is the result of
2670 : : * this function.
2671 : : *
2672 : : * Returns: (nullable): the locale from the file, or `NULL` if the key was not
2673 : : * found or the entry in the file was was untranslated
2674 : : *
2675 : : * Since: 2.56
2676 : : */
2677 : : gchar *
2678 : 14 : g_key_file_get_locale_for_key (GKeyFile *key_file,
2679 : : const gchar *group_name,
2680 : : const gchar *key,
2681 : : const gchar *locale)
2682 : : {
2683 : 14 : gchar **languages_allocated = NULL;
2684 : : const gchar * const *languages;
2685 : 14 : gchar *result = NULL;
2686 : : gsize i;
2687 : :
2688 : 14 : g_return_val_if_fail (key_file != NULL, NULL);
2689 : 14 : g_return_val_if_fail (group_name != NULL, NULL);
2690 : 14 : g_return_val_if_fail (key != NULL, NULL);
2691 : :
2692 : 14 : if (locale != NULL)
2693 : : {
2694 : 8 : languages_allocated = g_get_locale_variants (locale);
2695 : 8 : languages = (const gchar * const *) languages_allocated;
2696 : 4 : }
2697 : : else
2698 : 6 : languages = g_get_language_names ();
2699 : :
2700 : 22 : for (i = 0; languages[i] != NULL; i++)
2701 : : {
2702 : : gchar *candidate_key, *translated_value;
2703 : :
2704 : 20 : if (g_strcmp0 (languages[i], "C") == 0)
2705 : 6 : break;
2706 : :
2707 : 14 : candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2708 : 14 : translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2709 : 14 : g_free (translated_value);
2710 : 14 : g_free (candidate_key);
2711 : :
2712 : 14 : if (translated_value != NULL)
2713 : 6 : break;
2714 : 4 : }
2715 : :
2716 : 14 : result = g_strdup (languages[i]);
2717 : :
2718 : 14 : g_strfreev (languages_allocated);
2719 : :
2720 : 14 : return result;
2721 : 7 : }
2722 : :
2723 : : /**
2724 : : * g_key_file_get_locale_string_list:
2725 : : * @key_file: a key file
2726 : : * @group_name: a group name
2727 : : * @key: a key
2728 : : * @locale: (nullable): a locale identifier or `NULL` to use the current locale
2729 : : * @length: (out) (optional): return location for the number of returned strings
2730 : : * or `NULL` to ignore
2731 : : * @error: return location for a [struct@GLib.Error]
2732 : : *
2733 : : * Returns the values associated with @key under @group_name
2734 : : * translated in the given @locale if available.
2735 : : *
2736 : : * If @locale is `C` then the untranslated value is returned (since GLib 2.84).
2737 : : *
2738 : : * If @locale is `NULL` then the current locale is assumed.
2739 : : *
2740 : : * If @locale is to be non-`NULL`, or if the current locale will change over
2741 : : * the lifetime of the [struct@GLib.KeyFile], it must be loaded with
2742 : : * [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all
2743 : : * locales.
2744 : : *
2745 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2746 : : * returned. If the values associated
2747 : : * with @key cannot be interpreted or no suitable translations
2748 : : * can be found then the untranslated values are returned. The
2749 : : * returned array is `NULL`-terminated, so @length may optionally
2750 : : * be `NULL`.
2751 : : *
2752 : : * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2753 : : * a newly allocated `NULL`-terminated string array or `NULL` if the key
2754 : : * isn’t found. The string array should be freed with [func@GLib.strfreev].
2755 : : *
2756 : : * Since: 2.6
2757 : : **/
2758 : : gchar **
2759 : 280 : g_key_file_get_locale_string_list (GKeyFile *key_file,
2760 : : const gchar *group_name,
2761 : : const gchar *key,
2762 : : const gchar *locale,
2763 : : gsize *length,
2764 : : GError **error)
2765 : : {
2766 : : GError *key_file_error;
2767 : : gchar **values, *value;
2768 : : char list_separator[2];
2769 : : gsize len;
2770 : :
2771 : 280 : g_return_val_if_fail (key_file != NULL, NULL);
2772 : 280 : g_return_val_if_fail (group_name != NULL, NULL);
2773 : 280 : g_return_val_if_fail (key != NULL, NULL);
2774 : :
2775 : 280 : key_file_error = NULL;
2776 : :
2777 : 286 : value = g_key_file_get_locale_string (key_file, group_name,
2778 : 6 : key, locale,
2779 : : &key_file_error);
2780 : :
2781 : 280 : if (key_file_error)
2782 : 216 : g_propagate_error (error, key_file_error);
2783 : :
2784 : 280 : if (!value)
2785 : : {
2786 : 216 : if (length)
2787 : 0 : *length = 0;
2788 : 216 : return NULL;
2789 : : }
2790 : :
2791 : 64 : len = strlen (value);
2792 : 64 : if (len > 0 && value[len - 1] == key_file->list_separator)
2793 : 62 : value[len - 1] = '\0';
2794 : :
2795 : 64 : list_separator[0] = key_file->list_separator;
2796 : 64 : list_separator[1] = '\0';
2797 : 64 : values = g_strsplit (value, list_separator, 0);
2798 : :
2799 : 64 : g_free (value);
2800 : :
2801 : 64 : if (length)
2802 : 6 : *length = g_strv_length (values);
2803 : :
2804 : 64 : return values;
2805 : 6 : }
2806 : :
2807 : : /**
2808 : : * g_key_file_set_locale_string_list:
2809 : : * @key_file: a key file
2810 : : * @group_name: a group name
2811 : : * @key: a key
2812 : : * @locale: a locale identifier
2813 : : * @list: (array zero-terminated=1 length=length): a `NULL`-terminated array of
2814 : : * locale string values
2815 : : * @length: the length of @list
2816 : : *
2817 : : * Associates a list of string values for @key and @locale under
2818 : : * @group_name.
2819 : : *
2820 : : * If @locale is `C` then the untranslated value is set (since GLib 2.84).
2821 : : *
2822 : : * If the translation for @key cannot be found then it is created.
2823 : : *
2824 : : * Since: 2.6
2825 : : **/
2826 : : void
2827 : 4 : g_key_file_set_locale_string_list (GKeyFile *key_file,
2828 : : const gchar *group_name,
2829 : : const gchar *key,
2830 : : const gchar *locale,
2831 : : const gchar * const list[],
2832 : : gsize length)
2833 : : {
2834 : : GString *value_list;
2835 : : gchar *full_key;
2836 : : gsize i;
2837 : :
2838 : 4 : g_return_if_fail (key_file != NULL);
2839 : 4 : g_return_if_fail (key != NULL);
2840 : 4 : g_return_if_fail (locale != NULL);
2841 : 4 : g_return_if_fail (length != 0);
2842 : :
2843 : 4 : value_list = g_string_sized_new (length * 128);
2844 : 12 : for (i = 0; i < length && list[i] != NULL; i++)
2845 : : {
2846 : : gchar *value;
2847 : :
2848 : 8 : value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2849 : 4 : g_string_append (value_list, value);
2850 : 8 : g_string_append_c (value_list, key_file->list_separator);
2851 : :
2852 : 8 : g_free (value);
2853 : 4 : }
2854 : :
2855 : 4 : full_key = g_strcmp0 (locale, "C") != 0 ? g_strdup_printf ("%s[%s]", key, locale) : g_strdup (key);
2856 : 4 : g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2857 : 4 : g_free (full_key);
2858 : 4 : g_string_free (value_list, TRUE);
2859 : 2 : }
2860 : :
2861 : : /**
2862 : : * g_key_file_get_boolean:
2863 : : * @key_file: a key file
2864 : : * @group_name: a group name
2865 : : * @key: a key
2866 : : * @error: return location for a [struct@GLib.Error]
2867 : : *
2868 : : * Returns the value associated with @key under @group_name as a
2869 : : * boolean.
2870 : : *
2871 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2872 : : * returned. Likewise, if the value associated with @key cannot be interpreted
2873 : : * as a boolean then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
2874 : : *
2875 : : * Returns: the value associated with the key as a boolean,
2876 : : * or false if the key was not found or could not be parsed.
2877 : : *
2878 : : * Since: 2.6
2879 : : **/
2880 : : gboolean
2881 : 2475 : g_key_file_get_boolean (GKeyFile *key_file,
2882 : : const gchar *group_name,
2883 : : const gchar *key,
2884 : : GError **error)
2885 : : {
2886 : 2475 : GError *key_file_error = NULL;
2887 : : gchar *value;
2888 : : gboolean bool_value;
2889 : :
2890 : 2475 : g_return_val_if_fail (key_file != NULL, FALSE);
2891 : 2475 : g_return_val_if_fail (group_name != NULL, FALSE);
2892 : 2475 : g_return_val_if_fail (key != NULL, FALSE);
2893 : :
2894 : 2475 : value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2895 : :
2896 : 2475 : if (!value)
2897 : : {
2898 : 2210 : g_propagate_error (error, key_file_error);
2899 : 2210 : return FALSE;
2900 : : }
2901 : :
2902 : 265 : bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2903 : : &key_file_error);
2904 : 265 : g_free (value);
2905 : :
2906 : 265 : if (key_file_error)
2907 : : {
2908 : 12 : if (g_error_matches (key_file_error,
2909 : 4 : G_KEY_FILE_ERROR,
2910 : : G_KEY_FILE_ERROR_INVALID_VALUE))
2911 : : {
2912 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
2913 : : G_KEY_FILE_ERROR_INVALID_VALUE,
2914 : 4 : _("Key file contains key “%s” "
2915 : : "which has a value that cannot be interpreted."),
2916 : 4 : key);
2917 : 8 : g_error_free (key_file_error);
2918 : 4 : }
2919 : : else
2920 : 0 : g_propagate_error (error, key_file_error);
2921 : 4 : }
2922 : :
2923 : 265 : return bool_value;
2924 : 11 : }
2925 : :
2926 : : /**
2927 : : * g_key_file_set_boolean:
2928 : : * @key_file: a key file
2929 : : * @group_name: a group name
2930 : : * @key: a key
2931 : : * @value: true or false
2932 : : *
2933 : : * Associates a new boolean value with @key under @group_name.
2934 : : *
2935 : : * If @key cannot be found then it is created.
2936 : : *
2937 : : * Since: 2.6
2938 : : **/
2939 : : void
2940 : 38 : g_key_file_set_boolean (GKeyFile *key_file,
2941 : : const gchar *group_name,
2942 : : const gchar *key,
2943 : : gboolean value)
2944 : : {
2945 : : const gchar *result;
2946 : :
2947 : 38 : g_return_if_fail (key_file != NULL);
2948 : :
2949 : 38 : result = g_key_file_parse_boolean_as_value (key_file, value);
2950 : 38 : g_key_file_set_value (key_file, group_name, key, result);
2951 : 1 : }
2952 : :
2953 : : /**
2954 : : * g_key_file_get_boolean_list:
2955 : : * @key_file: a key file
2956 : : * @group_name: a group name
2957 : : * @key: a key
2958 : : * @length: (out): the number of booleans returned
2959 : : * @error: return location for a [struct@GLib.Error]
2960 : : *
2961 : : * Returns the values associated with @key under @group_name as
2962 : : * booleans.
2963 : : *
2964 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
2965 : : * returned. Likewise, if the values associated with @key cannot be interpreted
2966 : : * as booleans then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
2967 : : *
2968 : : * Returns: (array length=length) (element-type gboolean) (transfer container):
2969 : : * the values associated with the key as a list of booleans, or `NULL` if the
2970 : : * key was not found or could not be parsed. The returned list of booleans
2971 : : * should be freed with [func@GLib.free] when no longer needed.
2972 : : *
2973 : : * Since: 2.6
2974 : : **/
2975 : : gboolean *
2976 : 2 : g_key_file_get_boolean_list (GKeyFile *key_file,
2977 : : const gchar *group_name,
2978 : : const gchar *key,
2979 : : gsize *length,
2980 : : GError **error)
2981 : : {
2982 : : GError *key_file_error;
2983 : : gchar **values;
2984 : : gboolean *bool_values;
2985 : : gsize i, num_bools;
2986 : :
2987 : 2 : g_return_val_if_fail (key_file != NULL, NULL);
2988 : 2 : g_return_val_if_fail (group_name != NULL, NULL);
2989 : 2 : g_return_val_if_fail (key != NULL, NULL);
2990 : :
2991 : 2 : if (length)
2992 : 2 : *length = 0;
2993 : :
2994 : 2 : key_file_error = NULL;
2995 : :
2996 : 2 : values = g_key_file_get_string_list (key_file, group_name, key,
2997 : : &num_bools, &key_file_error);
2998 : :
2999 : 2 : if (key_file_error)
3000 : 0 : g_propagate_error (error, key_file_error);
3001 : :
3002 : 2 : if (!values)
3003 : 0 : return NULL;
3004 : :
3005 : 2 : bool_values = g_new (gboolean, num_bools);
3006 : :
3007 : 6 : for (i = 0; i < num_bools; i++)
3008 : : {
3009 : 8 : bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
3010 : 4 : values[i],
3011 : : &key_file_error);
3012 : :
3013 : 4 : if (key_file_error)
3014 : : {
3015 : 0 : g_propagate_error (error, key_file_error);
3016 : 0 : g_strfreev (values);
3017 : 0 : g_free (bool_values);
3018 : :
3019 : 0 : return NULL;
3020 : : }
3021 : 2 : }
3022 : 2 : g_strfreev (values);
3023 : :
3024 : 2 : if (length)
3025 : 2 : *length = num_bools;
3026 : :
3027 : 2 : return bool_values;
3028 : 1 : }
3029 : :
3030 : : /**
3031 : : * g_key_file_set_boolean_list:
3032 : : * @key_file: a key file
3033 : : * @group_name: a group name
3034 : : * @key: a key
3035 : : * @list: (array length=length): an array of boolean values
3036 : : * @length: length of @list
3037 : : *
3038 : : * Associates a list of boolean values with @key under @group_name.
3039 : : *
3040 : : * If @key cannot be found then it is created.
3041 : : *
3042 : : * Since: 2.6
3043 : : **/
3044 : : void
3045 : 2 : g_key_file_set_boolean_list (GKeyFile *key_file,
3046 : : const gchar *group_name,
3047 : : const gchar *key,
3048 : : gboolean list[],
3049 : : gsize length)
3050 : : {
3051 : : GString *value_list;
3052 : : gsize i;
3053 : :
3054 : 2 : g_return_if_fail (key_file != NULL);
3055 : 2 : g_return_if_fail (list != NULL);
3056 : :
3057 : 2 : value_list = g_string_sized_new (length * 8);
3058 : 6 : for (i = 0; i < length; i++)
3059 : : {
3060 : : const gchar *value;
3061 : :
3062 : 4 : value = g_key_file_parse_boolean_as_value (key_file, list[i]);
3063 : :
3064 : 2 : g_string_append (value_list, value);
3065 : 4 : g_string_append_c (value_list, key_file->list_separator);
3066 : 2 : }
3067 : :
3068 : 2 : g_key_file_set_value (key_file, group_name, key, value_list->str);
3069 : 2 : g_string_free (value_list, TRUE);
3070 : 1 : }
3071 : :
3072 : : /**
3073 : : * g_key_file_get_integer:
3074 : : * @key_file: a key file
3075 : : * @group_name: a group name
3076 : : * @key: a key
3077 : : * @error: return location for a [struct@GLib.Error]
3078 : : *
3079 : : * Returns the value associated with @key under @group_name as an
3080 : : * integer.
3081 : : *
3082 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
3083 : : * returned. Likewise, if the value associated with @key cannot be interpreted
3084 : : * as an integer, or is out of range for a `gint`, then
3085 : : * [error@GLib.KeyFileError.INVALID_VALUE] is returned.
3086 : : *
3087 : : * Returns: the value associated with the key as an integer, or
3088 : : * `0` if the key was not found or could not be parsed.
3089 : : *
3090 : : * Since: 2.6
3091 : : **/
3092 : : gint
3093 : 204 : g_key_file_get_integer (GKeyFile *key_file,
3094 : : const gchar *group_name,
3095 : : const gchar *key,
3096 : : GError **error)
3097 : : {
3098 : : GError *key_file_error;
3099 : : gchar *value;
3100 : : gint int_value;
3101 : :
3102 : 204 : g_return_val_if_fail (key_file != NULL, -1);
3103 : 204 : g_return_val_if_fail (group_name != NULL, -1);
3104 : 204 : g_return_val_if_fail (key != NULL, -1);
3105 : :
3106 : 204 : key_file_error = NULL;
3107 : :
3108 : 204 : value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3109 : :
3110 : 204 : if (key_file_error)
3111 : : {
3112 : 0 : g_propagate_error (error, key_file_error);
3113 : 0 : return 0;
3114 : : }
3115 : :
3116 : 204 : int_value = g_key_file_parse_value_as_integer (key_file, value,
3117 : : &key_file_error);
3118 : 204 : g_free (value);
3119 : :
3120 : 204 : if (key_file_error)
3121 : : {
3122 : 12 : if (g_error_matches (key_file_error,
3123 : 4 : G_KEY_FILE_ERROR,
3124 : : G_KEY_FILE_ERROR_INVALID_VALUE))
3125 : : {
3126 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
3127 : : G_KEY_FILE_ERROR_INVALID_VALUE,
3128 : 4 : _("Key file contains key “%s” in group “%s” "
3129 : : "which has a value that cannot be interpreted."),
3130 : 4 : key, group_name);
3131 : 8 : g_error_free (key_file_error);
3132 : 4 : }
3133 : : else
3134 : 0 : g_propagate_error (error, key_file_error);
3135 : 4 : }
3136 : :
3137 : 204 : return int_value;
3138 : 102 : }
3139 : :
3140 : : /**
3141 : : * g_key_file_set_integer:
3142 : : * @key_file: a key file
3143 : : * @group_name: a group name
3144 : : * @key: a key
3145 : : * @value: an integer value
3146 : : *
3147 : : * Associates a new integer value with @key under @group_name.
3148 : : *
3149 : : * If @key cannot be found then it is created.
3150 : : *
3151 : : * Since: 2.6
3152 : : **/
3153 : : void
3154 : 4 : g_key_file_set_integer (GKeyFile *key_file,
3155 : : const gchar *group_name,
3156 : : const gchar *key,
3157 : : gint value)
3158 : : {
3159 : : gchar *result;
3160 : :
3161 : 4 : g_return_if_fail (key_file != NULL);
3162 : :
3163 : 4 : result = g_key_file_parse_integer_as_value (key_file, value);
3164 : 4 : g_key_file_set_value (key_file, group_name, key, result);
3165 : 4 : g_free (result);
3166 : 2 : }
3167 : :
3168 : : /**
3169 : : * g_key_file_get_int64:
3170 : : * @key_file: a key file
3171 : : * @group_name: a group name
3172 : : * @key: a key
3173 : : * @error: return location for a [struct@GLib.Error]
3174 : : *
3175 : : * Returns the value associated with @key under @group_name as a signed
3176 : : * 64-bit integer.
3177 : : *
3178 : : * This is similar to [method@GLib.KeyFile.get_integer] but can return
3179 : : * 64-bit results without truncation.
3180 : : *
3181 : : * Returns: the value associated with the key as a signed 64-bit integer, or
3182 : : * `0` if the key was not found or could not be parsed.
3183 : : *
3184 : : * Since: 2.26
3185 : : */
3186 : : gint64
3187 : 2 : g_key_file_get_int64 (GKeyFile *key_file,
3188 : : const gchar *group_name,
3189 : : const gchar *key,
3190 : : GError **error)
3191 : : {
3192 : : gchar *s, *end;
3193 : : gint64 v;
3194 : :
3195 : 2 : g_return_val_if_fail (key_file != NULL, -1);
3196 : 2 : g_return_val_if_fail (group_name != NULL, -1);
3197 : 2 : g_return_val_if_fail (key != NULL, -1);
3198 : :
3199 : 2 : s = g_key_file_get_value (key_file, group_name, key, error);
3200 : :
3201 : 2 : if (s == NULL)
3202 : 0 : return 0;
3203 : :
3204 : 2 : v = g_ascii_strtoll (s, &end, 10);
3205 : :
3206 : 2 : if (*s == '\0' || *end != '\0')
3207 : : {
3208 : 0 : g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
3209 : 0 : _("Key “%s” in group “%s” has value “%s” "
3210 : : "where %s was expected"),
3211 : 0 : key, group_name, s, "int64");
3212 : 0 : g_free (s);
3213 : 0 : return 0;
3214 : : }
3215 : :
3216 : 2 : g_free (s);
3217 : 2 : return v;
3218 : 1 : }
3219 : :
3220 : : /**
3221 : : * g_key_file_set_int64:
3222 : : * @key_file: a key file
3223 : : * @group_name: a group name
3224 : : * @key: a key
3225 : : * @value: an integer value
3226 : : *
3227 : : * Associates a new integer value with @key under @group_name.
3228 : : *
3229 : : * If @key cannot be found then it is created.
3230 : : *
3231 : : * Since: 2.26
3232 : : **/
3233 : : void
3234 : 2 : g_key_file_set_int64 (GKeyFile *key_file,
3235 : : const gchar *group_name,
3236 : : const gchar *key,
3237 : : gint64 value)
3238 : : {
3239 : : gchar *result;
3240 : :
3241 : 2 : g_return_if_fail (key_file != NULL);
3242 : :
3243 : 2 : result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
3244 : 2 : g_key_file_set_value (key_file, group_name, key, result);
3245 : 2 : g_free (result);
3246 : 1 : }
3247 : :
3248 : : /**
3249 : : * g_key_file_get_uint64:
3250 : : * @key_file: a key file
3251 : : * @group_name: a group name
3252 : : * @key: a key
3253 : : * @error: return location for a [struct@GLib.Error]
3254 : : *
3255 : : * Returns the value associated with @key under @group_name as an unsigned
3256 : : * 64-bit integer.
3257 : : *
3258 : : * This is similar to [method@GLib.KeyFile.get_integer] but can return
3259 : : * large positive results without truncation.
3260 : : *
3261 : : * Returns: the value associated with the key as an unsigned 64-bit integer,
3262 : : * or `0` if the key was not found or could not be parsed.
3263 : : *
3264 : : * Since: 2.26
3265 : : */
3266 : : guint64
3267 : 4 : g_key_file_get_uint64 (GKeyFile *key_file,
3268 : : const gchar *group_name,
3269 : : const gchar *key,
3270 : : GError **error)
3271 : : {
3272 : : gchar *s, *end;
3273 : : guint64 v;
3274 : :
3275 : 4 : g_return_val_if_fail (key_file != NULL, -1);
3276 : 4 : g_return_val_if_fail (group_name != NULL, -1);
3277 : 4 : g_return_val_if_fail (key != NULL, -1);
3278 : :
3279 : 4 : s = g_key_file_get_value (key_file, group_name, key, error);
3280 : :
3281 : 4 : if (s == NULL)
3282 : 0 : return 0;
3283 : :
3284 : 4 : v = g_ascii_strtoull (s, &end, 10);
3285 : :
3286 : 4 : if (*s == '\0' || *end != '\0')
3287 : : {
3288 : 0 : g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
3289 : 0 : _("Key “%s” in group “%s” has value “%s” "
3290 : : "where %s was expected"),
3291 : 0 : key, group_name, s, "uint64");
3292 : 0 : g_free (s);
3293 : 0 : return 0;
3294 : : }
3295 : :
3296 : 4 : g_free (s);
3297 : 4 : return v;
3298 : 2 : }
3299 : :
3300 : : /**
3301 : : * g_key_file_set_uint64:
3302 : : * @key_file: a key file
3303 : : * @group_name: a group name
3304 : : * @key: a key
3305 : : * @value: an integer value
3306 : : *
3307 : : * Associates a new integer value with @key under @group_name.
3308 : : *
3309 : : * If @key cannot be found then it is created.
3310 : : *
3311 : : * Since: 2.26
3312 : : **/
3313 : : void
3314 : 2 : g_key_file_set_uint64 (GKeyFile *key_file,
3315 : : const gchar *group_name,
3316 : : const gchar *key,
3317 : : guint64 value)
3318 : : {
3319 : : gchar *result;
3320 : :
3321 : 2 : g_return_if_fail (key_file != NULL);
3322 : :
3323 : 2 : result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
3324 : 2 : g_key_file_set_value (key_file, group_name, key, result);
3325 : 2 : g_free (result);
3326 : 1 : }
3327 : :
3328 : : /**
3329 : : * g_key_file_get_integer_list:
3330 : : * @key_file: a key file
3331 : : * @group_name: a group name
3332 : : * @key: a key
3333 : : * @length: (out): the number of integers returned
3334 : : * @error: return location for a [struct@GLib.Error]
3335 : : *
3336 : : * Returns the values associated with @key under @group_name as
3337 : : * integers.
3338 : : *
3339 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
3340 : : * returned. Likewise, if the values associated with @key cannot be interpreted
3341 : : * as integers, or are out of range for `gint`, then
3342 : : * [error@GLib.KeyFileError.INVALID_VALUE] is returned.
3343 : : *
3344 : : * Returns: (array length=length) (element-type gint) (transfer container):
3345 : : * the values associated with the key as a list of integers, or `NULL` if
3346 : : * the key was not found or could not be parsed. The returned list of
3347 : : * integers should be freed with [func@GLib.free] when no longer needed.
3348 : : *
3349 : : * Since: 2.6
3350 : : **/
3351 : : gint *
3352 : 6 : g_key_file_get_integer_list (GKeyFile *key_file,
3353 : : const gchar *group_name,
3354 : : const gchar *key,
3355 : : gsize *length,
3356 : : GError **error)
3357 : : {
3358 : 6 : GError *key_file_error = NULL;
3359 : : gchar **values;
3360 : : gint *int_values;
3361 : : gsize i, num_ints;
3362 : :
3363 : 6 : g_return_val_if_fail (key_file != NULL, NULL);
3364 : 6 : g_return_val_if_fail (group_name != NULL, NULL);
3365 : 6 : g_return_val_if_fail (key != NULL, NULL);
3366 : :
3367 : 6 : if (length)
3368 : 6 : *length = 0;
3369 : :
3370 : 6 : values = g_key_file_get_string_list (key_file, group_name, key,
3371 : : &num_ints, &key_file_error);
3372 : :
3373 : 6 : if (key_file_error)
3374 : 0 : g_propagate_error (error, key_file_error);
3375 : :
3376 : 6 : if (!values)
3377 : 0 : return NULL;
3378 : :
3379 : 6 : int_values = g_new (gint, num_ints);
3380 : :
3381 : 24 : for (i = 0; i < num_ints; i++)
3382 : : {
3383 : 36 : int_values[i] = g_key_file_parse_value_as_integer (key_file,
3384 : 18 : values[i],
3385 : : &key_file_error);
3386 : :
3387 : 18 : if (key_file_error)
3388 : : {
3389 : 0 : g_propagate_error (error, key_file_error);
3390 : 0 : g_strfreev (values);
3391 : 0 : g_free (int_values);
3392 : :
3393 : 0 : return NULL;
3394 : : }
3395 : 9 : }
3396 : 6 : g_strfreev (values);
3397 : :
3398 : 6 : if (length)
3399 : 6 : *length = num_ints;
3400 : :
3401 : 6 : return int_values;
3402 : 3 : }
3403 : :
3404 : : /**
3405 : : * g_key_file_set_integer_list:
3406 : : * @key_file: a key file
3407 : : * @group_name: a group name
3408 : : * @key: a key
3409 : : * @list: (array length=length): an array of integer values
3410 : : * @length: number of integer values in @list
3411 : : *
3412 : : * Associates a list of integer values with @key under @group_name.
3413 : : *
3414 : : * If @key cannot be found then it is created.
3415 : : *
3416 : : * Since: 2.6
3417 : : **/
3418 : : void
3419 : 4 : g_key_file_set_integer_list (GKeyFile *key_file,
3420 : : const gchar *group_name,
3421 : : const gchar *key,
3422 : : gint list[],
3423 : : gsize length)
3424 : : {
3425 : : GString *values;
3426 : : gsize i;
3427 : :
3428 : 4 : g_return_if_fail (key_file != NULL);
3429 : 4 : g_return_if_fail (list != NULL);
3430 : :
3431 : 4 : values = g_string_sized_new (length * 16);
3432 : 16 : for (i = 0; i < length; i++)
3433 : : {
3434 : : gchar *value;
3435 : :
3436 : 12 : value = g_key_file_parse_integer_as_value (key_file, list[i]);
3437 : :
3438 : 6 : g_string_append (values, value);
3439 : 12 : g_string_append_c (values, key_file->list_separator);
3440 : :
3441 : 12 : g_free (value);
3442 : 6 : }
3443 : :
3444 : 4 : g_key_file_set_value (key_file, group_name, key, values->str);
3445 : 4 : g_string_free (values, TRUE);
3446 : 2 : }
3447 : :
3448 : : /**
3449 : : * g_key_file_get_double:
3450 : : * @key_file: a key file
3451 : : * @group_name: a group name
3452 : : * @key: a key
3453 : : * @error: return location for a [struct@GLib.Error]
3454 : : *
3455 : : * Returns the value associated with @key under @group_name as a double.
3456 : : *
3457 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
3458 : : * returned. Likewise, if the value associated with @key cannot be interpreted
3459 : : * as a double then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
3460 : : *
3461 : : * Returns: the value associated with the key as a double, or
3462 : : * `0.0` if the key was not found or could not be parsed.
3463 : : *
3464 : : * Since: 2.12
3465 : : **/
3466 : : gdouble
3467 : 14 : g_key_file_get_double (GKeyFile *key_file,
3468 : : const gchar *group_name,
3469 : : const gchar *key,
3470 : : GError **error)
3471 : : {
3472 : : GError *key_file_error;
3473 : : gchar *value;
3474 : : gdouble double_value;
3475 : :
3476 : 14 : g_return_val_if_fail (key_file != NULL, -1);
3477 : 14 : g_return_val_if_fail (group_name != NULL, -1);
3478 : 14 : g_return_val_if_fail (key != NULL, -1);
3479 : :
3480 : 14 : key_file_error = NULL;
3481 : :
3482 : 14 : value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3483 : :
3484 : 14 : if (key_file_error)
3485 : : {
3486 : 0 : g_propagate_error (error, key_file_error);
3487 : 0 : return 0;
3488 : : }
3489 : :
3490 : 14 : double_value = g_key_file_parse_value_as_double (key_file, value,
3491 : : &key_file_error);
3492 : 14 : g_free (value);
3493 : :
3494 : 14 : if (key_file_error)
3495 : : {
3496 : 12 : if (g_error_matches (key_file_error,
3497 : 4 : G_KEY_FILE_ERROR,
3498 : : G_KEY_FILE_ERROR_INVALID_VALUE))
3499 : : {
3500 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
3501 : : G_KEY_FILE_ERROR_INVALID_VALUE,
3502 : 4 : _("Key file contains key “%s” in group “%s” "
3503 : : "which has a value that cannot be interpreted."),
3504 : 4 : key, group_name);
3505 : 8 : g_error_free (key_file_error);
3506 : 4 : }
3507 : : else
3508 : 0 : g_propagate_error (error, key_file_error);
3509 : 4 : }
3510 : :
3511 : 14 : return double_value;
3512 : 7 : }
3513 : :
3514 : : /**
3515 : : * g_key_file_set_double:
3516 : : * @key_file: a key file
3517 : : * @group_name: a group name
3518 : : * @key: a key
3519 : : * @value: a double value
3520 : : *
3521 : : * Associates a new double value with @key under @group_name.
3522 : : *
3523 : : * If @key cannot be found then it is created.
3524 : : *
3525 : : * Since: 2.12
3526 : : **/
3527 : : void
3528 : 2 : g_key_file_set_double (GKeyFile *key_file,
3529 : : const gchar *group_name,
3530 : : const gchar *key,
3531 : : gdouble value)
3532 : : {
3533 : : gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3534 : :
3535 : 2 : g_return_if_fail (key_file != NULL);
3536 : :
3537 : 2 : g_ascii_dtostr (result, sizeof (result), value);
3538 : 2 : g_key_file_set_value (key_file, group_name, key, result);
3539 : 1 : }
3540 : :
3541 : : /**
3542 : : * g_key_file_get_double_list:
3543 : : * @key_file: a key file
3544 : : * @group_name: a group name
3545 : : * @key: a key
3546 : : * @length: (out): the number of doubles returned
3547 : : * @error: return location for a [struct@GLib.Error]
3548 : : *
3549 : : * Returns the values associated with @key under @group_name as
3550 : : * doubles.
3551 : : *
3552 : : * If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
3553 : : * returned. Likewise, if the values associated with @key cannot be interpreted
3554 : : * as doubles then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
3555 : : *
3556 : : * Returns: (array length=length) (element-type gdouble) (transfer container):
3557 : : * the values associated with the key as a list of doubles, or `NULL` if the
3558 : : * key was not found or could not be parsed. The returned list of doubles
3559 : : * should be freed with [func@GLib.free] when no longer needed.
3560 : : *
3561 : : * Since: 2.12
3562 : : **/
3563 : : gdouble *
3564 : 6 : g_key_file_get_double_list (GKeyFile *key_file,
3565 : : const gchar *group_name,
3566 : : const gchar *key,
3567 : : gsize *length,
3568 : : GError **error)
3569 : : {
3570 : 6 : GError *key_file_error = NULL;
3571 : : gchar **values;
3572 : : gdouble *double_values;
3573 : : gsize i, num_doubles;
3574 : :
3575 : 6 : g_return_val_if_fail (key_file != NULL, NULL);
3576 : 6 : g_return_val_if_fail (group_name != NULL, NULL);
3577 : 6 : g_return_val_if_fail (key != NULL, NULL);
3578 : :
3579 : 6 : if (length)
3580 : 6 : *length = 0;
3581 : :
3582 : 6 : values = g_key_file_get_string_list (key_file, group_name, key,
3583 : : &num_doubles, &key_file_error);
3584 : :
3585 : 6 : if (key_file_error)
3586 : 0 : g_propagate_error (error, key_file_error);
3587 : :
3588 : 6 : if (!values)
3589 : 0 : return NULL;
3590 : :
3591 : 6 : double_values = g_new (gdouble, num_doubles);
3592 : :
3593 : 20 : for (i = 0; i < num_doubles; i++)
3594 : : {
3595 : 28 : double_values[i] = g_key_file_parse_value_as_double (key_file,
3596 : 14 : values[i],
3597 : : &key_file_error);
3598 : :
3599 : 14 : if (key_file_error)
3600 : : {
3601 : 0 : g_propagate_error (error, key_file_error);
3602 : 0 : g_strfreev (values);
3603 : 0 : g_free (double_values);
3604 : :
3605 : 0 : return NULL;
3606 : : }
3607 : 7 : }
3608 : 6 : g_strfreev (values);
3609 : :
3610 : 6 : if (length)
3611 : 6 : *length = num_doubles;
3612 : :
3613 : 6 : return double_values;
3614 : 3 : }
3615 : :
3616 : : /**
3617 : : * g_key_file_set_double_list:
3618 : : * @key_file: a key file
3619 : : * @group_name: a group name
3620 : : * @key: a key
3621 : : * @list: (array length=length): an array of double values
3622 : : * @length: number of double values in @list
3623 : : *
3624 : : * Associates a list of double values with @key under @group_name.
3625 : : *
3626 : : * If @key cannot be found then it is created.
3627 : : *
3628 : : * Since: 2.12
3629 : : **/
3630 : : void
3631 : 4 : g_key_file_set_double_list (GKeyFile *key_file,
3632 : : const gchar *group_name,
3633 : : const gchar *key,
3634 : : gdouble list[],
3635 : : gsize length)
3636 : : {
3637 : : GString *values;
3638 : : gsize i;
3639 : :
3640 : 4 : g_return_if_fail (key_file != NULL);
3641 : 4 : g_return_if_fail (list != NULL);
3642 : :
3643 : 4 : values = g_string_sized_new (length * 16);
3644 : 12 : for (i = 0; i < length; i++)
3645 : : {
3646 : : gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3647 : :
3648 : 8 : g_ascii_dtostr( result, sizeof (result), list[i] );
3649 : :
3650 : 4 : g_string_append (values, result);
3651 : 8 : g_string_append_c (values, key_file->list_separator);
3652 : 4 : }
3653 : :
3654 : 4 : g_key_file_set_value (key_file, group_name, key, values->str);
3655 : 4 : g_string_free (values, TRUE);
3656 : 2 : }
3657 : :
3658 : : static gboolean
3659 : 4 : g_key_file_set_key_comment (GKeyFile *key_file,
3660 : : const gchar *group_name,
3661 : : const gchar *key,
3662 : : const gchar *comment,
3663 : : GError **error)
3664 : : {
3665 : : GKeyFileGroup *group;
3666 : : GKeyFileKeyValuePair *pair;
3667 : : GList *key_node, *comment_node, *tmp;
3668 : :
3669 : 4 : group = g_key_file_lookup_group (key_file, group_name);
3670 : 4 : if (!group)
3671 : : {
3672 : 0 : g_set_error (error, G_KEY_FILE_ERROR,
3673 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3674 : 0 : _("Key file does not have group “%s”"),
3675 : 0 : group_name ? group_name : "(null)");
3676 : :
3677 : 0 : return FALSE;
3678 : : }
3679 : :
3680 : : /* First find the key the comments are supposed to be
3681 : : * associated with
3682 : : */
3683 : 4 : key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3684 : :
3685 : 4 : if (key_node == NULL)
3686 : : {
3687 : 0 : set_not_found_key_error (group->name, key, error);
3688 : 0 : return FALSE;
3689 : : }
3690 : :
3691 : : /* Then find all the comments already associated with the
3692 : : * key and free them
3693 : : */
3694 : 4 : tmp = key_node->next;
3695 : 8 : while (tmp != NULL)
3696 : : {
3697 : 8 : pair = (GKeyFileKeyValuePair *) tmp->data;
3698 : :
3699 : 8 : if (pair->key != NULL)
3700 : 4 : break;
3701 : :
3702 : 4 : comment_node = tmp;
3703 : 4 : tmp = tmp->next;
3704 : 6 : g_key_file_remove_key_value_pair_node (key_file, group,
3705 : 2 : comment_node);
3706 : : }
3707 : :
3708 : 4 : if (comment == NULL)
3709 : 2 : return TRUE;
3710 : :
3711 : : /* Now we can add our new comment
3712 : : */
3713 : 2 : pair = g_new (GKeyFileKeyValuePair, 1);
3714 : 2 : pair->key = NULL;
3715 : 2 : pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3716 : :
3717 : 2 : key_node = g_list_insert (key_node, pair, 1);
3718 : 1 : (void) key_node;
3719 : :
3720 : 2 : return TRUE;
3721 : 2 : }
3722 : :
3723 : : static gboolean
3724 : 6 : g_key_file_set_top_comment (GKeyFile *key_file,
3725 : : const gchar *comment,
3726 : : GError **error)
3727 : : {
3728 : : GList *group_node;
3729 : : GKeyFileGroup *group;
3730 : : GKeyFileKeyValuePair *pair;
3731 : :
3732 : : /* The last group in the list should be the top (comments only)
3733 : : * group in the file
3734 : : */
3735 : 6 : g_warn_if_fail (key_file->groups != NULL);
3736 : 6 : group_node = g_list_last (key_file->groups);
3737 : 6 : group = (GKeyFileGroup *) group_node->data;
3738 : 6 : g_warn_if_fail (group->name == NULL);
3739 : :
3740 : : /* Note all keys must be comments at the top of
3741 : : * the file, so we can just free it all.
3742 : : */
3743 : 6 : g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3744 : 6 : group->key_value_pairs = NULL;
3745 : :
3746 : 6 : if (comment == NULL)
3747 : 2 : return TRUE;
3748 : :
3749 : 4 : pair = g_new (GKeyFileKeyValuePair, 1);
3750 : 4 : pair->key = NULL;
3751 : 4 : pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3752 : :
3753 : 4 : group->key_value_pairs =
3754 : 4 : g_list_prepend (group->key_value_pairs, pair);
3755 : :
3756 : 4 : return TRUE;
3757 : 3 : }
3758 : :
3759 : : static gboolean
3760 : 4 : g_key_file_set_group_comment (GKeyFile *key_file,
3761 : : const gchar *group_name,
3762 : : const gchar *comment,
3763 : : GError **error)
3764 : : {
3765 : : GKeyFileGroup *group;
3766 : : GList *group_node;
3767 : : GKeyFileKeyValuePair *pair;
3768 : :
3769 : 4 : g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), FALSE);
3770 : :
3771 : 4 : group = g_key_file_lookup_group (key_file, group_name);
3772 : 4 : if (!group)
3773 : : {
3774 : 0 : g_set_error (error, G_KEY_FILE_ERROR,
3775 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3776 : 0 : _("Key file does not have group “%s”"),
3777 : 0 : group_name);
3778 : :
3779 : 0 : return FALSE;
3780 : : }
3781 : :
3782 : 4 : if (group == key_file->start_group)
3783 : 2 : return g_key_file_set_top_comment (key_file, comment, error);
3784 : :
3785 : : /* First remove any existing comment
3786 : : */
3787 : 2 : group_node = g_key_file_lookup_group_node (key_file, group_name);
3788 : 2 : group = group_node->next->data;
3789 : 6 : for (GList *lp = group->key_value_pairs; lp != NULL; )
3790 : : {
3791 : 6 : GList *lnext = lp->next;
3792 : 6 : pair = lp->data;
3793 : 6 : if (pair->key != NULL)
3794 : 2 : break;
3795 : :
3796 : 4 : g_key_file_remove_key_value_pair_node (key_file, group, lp);
3797 : 4 : lp = lnext;
3798 : : }
3799 : :
3800 : 2 : if (comment == NULL)
3801 : 2 : return TRUE;
3802 : :
3803 : : /* Now we can add our new comment
3804 : : */
3805 : 0 : pair = g_new (GKeyFileKeyValuePair, 1);
3806 : 0 : pair->key = NULL;
3807 : 0 : pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3808 : 0 : group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3809 : :
3810 : 0 : return TRUE;
3811 : 2 : }
3812 : :
3813 : : /**
3814 : : * g_key_file_set_comment:
3815 : : * @key_file: a key file
3816 : : * @group_name: (nullable): a group name, or `NULL` to write a top-level comment
3817 : : * @key: (nullable): a key, or `NULL` to write a group comment
3818 : : * @comment: a comment
3819 : : * @error: return location for a [struct@GLib.Error]
3820 : : *
3821 : : * Places a comment above @key from @group_name.
3822 : : *
3823 : : * If @key is `NULL` then @comment will be written above @group_name.
3824 : : * If both @key and @group_name are `NULL`, then @comment will be
3825 : : * written above the first group in the file.
3826 : : *
3827 : : * Passing a non-existent @group_name or @key to this function returns
3828 : : * false and populates @error. (In contrast, passing a non-existent
3829 : : * `group_name` or `key` to [method@GLib.KeyFile.set_string]
3830 : : * creates the associated group name and key.)
3831 : : *
3832 : : * Note that this function prepends a `#` comment marker to
3833 : : * each line of @comment.
3834 : : *
3835 : : * Returns: true if the comment was written, false otherwise
3836 : : *
3837 : : * Since: 2.6
3838 : : **/
3839 : : gboolean
3840 : 6 : g_key_file_set_comment (GKeyFile *key_file,
3841 : : const gchar *group_name,
3842 : : const gchar *key,
3843 : : const gchar *comment,
3844 : : GError **error)
3845 : : {
3846 : 6 : g_return_val_if_fail (key_file != NULL, FALSE);
3847 : :
3848 : 6 : if (group_name != NULL && key != NULL)
3849 : : {
3850 : 2 : if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3851 : 0 : return FALSE;
3852 : 1 : }
3853 : 4 : else if (group_name != NULL)
3854 : : {
3855 : 2 : if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3856 : 0 : return FALSE;
3857 : 1 : }
3858 : : else
3859 : : {
3860 : 2 : if (!g_key_file_set_top_comment (key_file, comment, error))
3861 : 0 : return FALSE;
3862 : : }
3863 : :
3864 : 6 : return TRUE;
3865 : 3 : }
3866 : :
3867 : : static gchar *
3868 : 14 : g_key_file_get_key_comment (GKeyFile *key_file,
3869 : : const gchar *group_name,
3870 : : const gchar *key,
3871 : : GError **error)
3872 : : {
3873 : : GKeyFileGroup *group;
3874 : : GKeyFileKeyValuePair *pair;
3875 : : GList *key_node, *tmp;
3876 : : GString *string;
3877 : : gchar *comment;
3878 : :
3879 : 14 : g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), NULL);
3880 : :
3881 : 14 : group = g_key_file_lookup_group (key_file, group_name);
3882 : 14 : if (!group)
3883 : : {
3884 : 9 : g_set_error (error, G_KEY_FILE_ERROR,
3885 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3886 : 3 : _("Key file does not have group “%s”"),
3887 : 3 : group_name ? group_name : "(null)");
3888 : :
3889 : 6 : return NULL;
3890 : : }
3891 : :
3892 : : /* First find the key the comments are supposed to be
3893 : : * associated with
3894 : : */
3895 : 8 : key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3896 : :
3897 : 8 : if (key_node == NULL)
3898 : : {
3899 : 0 : set_not_found_key_error (group->name, key, error);
3900 : 0 : return NULL;
3901 : : }
3902 : :
3903 : 8 : string = NULL;
3904 : :
3905 : : /* Then find all the comments already associated with the
3906 : : * key and concatenate them.
3907 : : */
3908 : 8 : tmp = key_node->next;
3909 : 8 : if (!key_node->next)
3910 : 0 : return NULL;
3911 : :
3912 : 8 : pair = (GKeyFileKeyValuePair *) tmp->data;
3913 : 8 : if (pair->key != NULL)
3914 : 2 : return NULL;
3915 : :
3916 : 8 : while (tmp->next)
3917 : : {
3918 : 8 : pair = (GKeyFileKeyValuePair *) tmp->next->data;
3919 : :
3920 : 8 : if (pair->key != NULL)
3921 : 6 : break;
3922 : :
3923 : 2 : tmp = tmp->next;
3924 : : }
3925 : :
3926 : 14 : while (tmp != key_node)
3927 : : {
3928 : 8 : pair = (GKeyFileKeyValuePair *) tmp->data;
3929 : :
3930 : 8 : if (string == NULL)
3931 : 6 : string = g_string_sized_new (512);
3932 : :
3933 : 12 : comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3934 : 8 : (tmp->prev == key_node));
3935 : 8 : g_string_append (string, comment);
3936 : 8 : g_free (comment);
3937 : :
3938 : 8 : tmp = tmp->prev;
3939 : : }
3940 : :
3941 : 6 : if (string != NULL)
3942 : 6 : comment = g_string_free_and_steal (g_steal_pointer (&string));
3943 : : else
3944 : 0 : comment = NULL;
3945 : :
3946 : 6 : return comment;
3947 : 7 : }
3948 : :
3949 : : static gchar *
3950 : 28 : get_group_comment (GKeyFile *key_file,
3951 : : GKeyFileGroup *group,
3952 : : GError **error)
3953 : : {
3954 : : GString *string;
3955 : : GList *tmp;
3956 : : gchar *comment;
3957 : :
3958 : 28 : string = NULL;
3959 : :
3960 : 28 : tmp = group->key_value_pairs;
3961 : 54 : while (tmp)
3962 : : {
3963 : : GKeyFileKeyValuePair *pair;
3964 : :
3965 : 48 : pair = (GKeyFileKeyValuePair *) tmp->data;
3966 : :
3967 : 48 : if (pair->key != NULL)
3968 : : {
3969 : 12 : tmp = tmp->prev;
3970 : 12 : break;
3971 : : }
3972 : :
3973 : 36 : if (tmp->next == NULL)
3974 : 10 : break;
3975 : :
3976 : 26 : tmp = tmp->next;
3977 : : }
3978 : :
3979 : 64 : while (tmp != NULL)
3980 : : {
3981 : : GKeyFileKeyValuePair *pair;
3982 : :
3983 : 36 : pair = (GKeyFileKeyValuePair *) tmp->data;
3984 : :
3985 : 36 : if (string == NULL)
3986 : 18 : string = g_string_sized_new (512);
3987 : :
3988 : 54 : comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3989 : 36 : (tmp->prev == NULL));
3990 : 18 : g_string_append (string, comment);
3991 : 36 : g_free (comment);
3992 : :
3993 : 36 : tmp = tmp->prev;
3994 : : }
3995 : :
3996 : 28 : if (string != NULL)
3997 : 18 : return g_string_free (string, FALSE);
3998 : :
3999 : 10 : return NULL;
4000 : 14 : }
4001 : :
4002 : : static gchar *
4003 : 24 : g_key_file_get_group_comment (GKeyFile *key_file,
4004 : : const gchar *group_name,
4005 : : GError **error)
4006 : : {
4007 : : GList *group_node;
4008 : : GKeyFileGroup *group;
4009 : :
4010 : 24 : group = g_key_file_lookup_group (key_file, group_name);
4011 : 24 : if (!group)
4012 : : {
4013 : 3 : g_set_error (error, G_KEY_FILE_ERROR,
4014 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4015 : 1 : _("Key file does not have group “%s”"),
4016 : 1 : group_name ? group_name : "(null)");
4017 : :
4018 : 2 : return NULL;
4019 : : }
4020 : :
4021 : 22 : group_node = g_key_file_lookup_group_node (key_file, group_name);
4022 : 22 : group_node = group_node->next;
4023 : 22 : group = (GKeyFileGroup *)group_node->data;
4024 : 22 : return get_group_comment (key_file, group, error);
4025 : 12 : }
4026 : :
4027 : : static gchar *
4028 : 6 : g_key_file_get_top_comment (GKeyFile *key_file,
4029 : : GError **error)
4030 : : {
4031 : : GList *group_node;
4032 : : GKeyFileGroup *group;
4033 : :
4034 : : /* The last group in the list should be the top (comments only)
4035 : : * group in the file
4036 : : */
4037 : 6 : g_warn_if_fail (key_file->groups != NULL);
4038 : 6 : group_node = g_list_last (key_file->groups);
4039 : 6 : group = (GKeyFileGroup *) group_node->data;
4040 : 6 : g_warn_if_fail (group->name == NULL);
4041 : :
4042 : 6 : return get_group_comment (key_file, group, error);
4043 : : }
4044 : :
4045 : : /**
4046 : : * g_key_file_get_comment:
4047 : : * @key_file: a key file
4048 : : * @group_name: (nullable): a group name, or `NULL` to get a top-level comment
4049 : : * @key: (nullable): a key, or `NULL` to get a group comment
4050 : : * @error: return location for a [struct@GLib.Error]
4051 : : *
4052 : : * Retrieves a comment above @key from @group_name.
4053 : : *
4054 : : * If @key is `NULL` then @comment will be read from above
4055 : : * @group_name. If both @key and @group_name are `NULL`, then
4056 : : * @comment will be read from above the first group in the file.
4057 : : *
4058 : : * Note that the returned string does not include the `#` comment markers,
4059 : : * but does include any whitespace after them (on each line). It includes
4060 : : * the line breaks between lines, but does not include the final line break.
4061 : : *
4062 : : * Returns: a comment that should be freed with [func@GLib.free]
4063 : : *
4064 : : * Since: 2.6
4065 : : **/
4066 : : gchar *
4067 : 44 : g_key_file_get_comment (GKeyFile *key_file,
4068 : : const gchar *group_name,
4069 : : const gchar *key,
4070 : : GError **error)
4071 : : {
4072 : 44 : g_return_val_if_fail (key_file != NULL, NULL);
4073 : :
4074 : 44 : if (group_name != NULL && key != NULL)
4075 : 14 : return g_key_file_get_key_comment (key_file, group_name, key, error);
4076 : 30 : else if (group_name != NULL)
4077 : 24 : return g_key_file_get_group_comment (key_file, group_name, error);
4078 : : else
4079 : 6 : return g_key_file_get_top_comment (key_file, error);
4080 : 22 : }
4081 : :
4082 : : /**
4083 : : * g_key_file_remove_comment:
4084 : : * @key_file: a key file
4085 : : * @group_name: (nullable): a group name, or `NULL` to get a top-level comment
4086 : : * @key: (nullable): a key, or `NULL` to get a group comment
4087 : : * @error: return location for a [struct@GLib.Error]
4088 : : *
4089 : : * Removes a comment above @key from @group_name.
4090 : : *
4091 : : * If @key is `NULL` then @comment will be removed above @group_name.
4092 : : * If both @key and @group_name are `NULL`, then @comment will
4093 : : * be removed above the first group in the file.
4094 : : *
4095 : : * Returns: true if the comment was removed, false otherwise
4096 : : *
4097 : : * Since: 2.6
4098 : : **/
4099 : :
4100 : : gboolean
4101 : 6 : g_key_file_remove_comment (GKeyFile *key_file,
4102 : : const gchar *group_name,
4103 : : const gchar *key,
4104 : : GError **error)
4105 : : {
4106 : 6 : g_return_val_if_fail (key_file != NULL, FALSE);
4107 : :
4108 : 6 : if (group_name != NULL && key != NULL)
4109 : 2 : return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
4110 : 4 : else if (group_name != NULL)
4111 : 2 : return g_key_file_set_group_comment (key_file, group_name, NULL, error);
4112 : : else
4113 : 2 : return g_key_file_set_top_comment (key_file, NULL, error);
4114 : 3 : }
4115 : :
4116 : : /**
4117 : : * g_key_file_has_group:
4118 : : * @key_file: a key file
4119 : : * @group_name: a group name
4120 : : *
4121 : : * Looks whether the key file has the group @group_name.
4122 : : *
4123 : : * Returns: true if @group_name is a part of @key_file, false otherwise.
4124 : : * Since: 2.6
4125 : : **/
4126 : : gboolean
4127 : 139 : g_key_file_has_group (GKeyFile *key_file,
4128 : : const gchar *group_name)
4129 : : {
4130 : 139 : g_return_val_if_fail (key_file != NULL, FALSE);
4131 : 139 : g_return_val_if_fail (group_name != NULL, FALSE);
4132 : :
4133 : 139 : return g_key_file_lookup_group (key_file, group_name) != NULL;
4134 : 7 : }
4135 : :
4136 : : /* This code remains from a historical attempt to add a new public API
4137 : : * which respects the GError rules.
4138 : : */
4139 : : static gboolean
4140 : 23 : g_key_file_has_key_full (GKeyFile *key_file,
4141 : : const gchar *group_name,
4142 : : const gchar *key,
4143 : : gboolean *has_key,
4144 : : GError **error)
4145 : : {
4146 : : GKeyFileKeyValuePair *pair;
4147 : : GKeyFileGroup *group;
4148 : :
4149 : 23 : g_return_val_if_fail (key_file != NULL, FALSE);
4150 : 23 : g_return_val_if_fail (group_name != NULL, FALSE);
4151 : 23 : g_return_val_if_fail (key != NULL, FALSE);
4152 : :
4153 : 23 : group = g_key_file_lookup_group (key_file, group_name);
4154 : :
4155 : 23 : if (!group)
4156 : : {
4157 : 6 : g_set_error (error, G_KEY_FILE_ERROR,
4158 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4159 : 2 : _("Key file does not have group “%s”"),
4160 : 2 : group_name);
4161 : :
4162 : 4 : return FALSE;
4163 : : }
4164 : :
4165 : 19 : pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4166 : :
4167 : 19 : if (has_key)
4168 : 19 : *has_key = pair != NULL;
4169 : 19 : return TRUE;
4170 : 8 : }
4171 : :
4172 : : /**
4173 : : * g_key_file_has_key: (skip)
4174 : : * @key_file: a key file
4175 : : * @group_name: a group name
4176 : : * @key: a key name
4177 : : * @error: return location for a [struct@GLib.Error]
4178 : : *
4179 : : * Looks whether the key file has the key @key in the group
4180 : : * @group_name.
4181 : : *
4182 : : * Note that this function does not follow the rules for [struct@GLib.Error]
4183 : : * strictly;
4184 : : * the return value both carries meaning and signals an error. To use
4185 : : * this function, you must pass a [struct@GLib.Error] pointer in @error, and
4186 : : * check whether it is not `NULL` to see if an error occurred.
4187 : : *
4188 : : * Language bindings should use [method@GLib.KeyFile.get_value] to test whether
4189 : : * a key exists.
4190 : : *
4191 : : * Returns: true if @key is a part of @group_name, false otherwise
4192 : : *
4193 : : * Since: 2.6
4194 : : **/
4195 : : gboolean
4196 : 23 : g_key_file_has_key (GKeyFile *key_file,
4197 : : const gchar *group_name,
4198 : : const gchar *key,
4199 : : GError **error)
4200 : : {
4201 : 23 : GError *temp_error = NULL;
4202 : : gboolean has_key;
4203 : :
4204 : 23 : if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
4205 : : {
4206 : 19 : return has_key;
4207 : : }
4208 : : else
4209 : : {
4210 : 4 : g_propagate_error (error, temp_error);
4211 : 4 : return FALSE;
4212 : : }
4213 : 8 : }
4214 : :
4215 : : static void
4216 : 2006 : g_key_file_add_group (GKeyFile *key_file,
4217 : : const gchar *group_name,
4218 : : gboolean created)
4219 : : {
4220 : : GKeyFileGroup *group;
4221 : :
4222 : 2006 : g_return_if_fail (key_file != NULL);
4223 : 2006 : g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
4224 : :
4225 : 2006 : group = g_key_file_lookup_group (key_file, group_name);
4226 : 2006 : if (group != NULL)
4227 : : {
4228 : 4 : key_file->current_group = group;
4229 : 4 : return;
4230 : : }
4231 : :
4232 : 2002 : group = g_new0 (GKeyFileGroup, 1);
4233 : 2002 : group->name = g_strdup (group_name);
4234 : 2002 : group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
4235 : 2002 : key_file->groups = g_list_prepend (key_file->groups, group);
4236 : 2002 : key_file->current_group = group;
4237 : :
4238 : 2002 : if (key_file->start_group == NULL)
4239 : : {
4240 : 1603 : key_file->start_group = group;
4241 : 84 : }
4242 : 399 : else if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS) || created)
4243 : : {
4244 : : /* separate groups by a blank line if we don't keep comments or group is created */
4245 : 375 : GKeyFileGroup *next_group = key_file->groups->next->data;
4246 : : GKeyFileKeyValuePair *pair;
4247 : 375 : if (next_group->key_value_pairs != NULL)
4248 : 289 : pair = next_group->key_value_pairs->data;
4249 : :
4250 : 390 : if (next_group->key_value_pairs == NULL ||
4251 : 289 : (pair->key != NULL && !g_strstr_len (pair->value, -1, "\n")))
4252 : : {
4253 : 373 : GKeyFileKeyValuePair *pair = g_new (GKeyFileKeyValuePair, 1);
4254 : 373 : pair->key = NULL;
4255 : 373 : pair->value = g_strdup ("");
4256 : 373 : next_group->key_value_pairs = g_list_prepend (next_group->key_value_pairs, pair);
4257 : 17 : }
4258 : 18 : }
4259 : :
4260 : 2002 : if (!key_file->group_hash)
4261 : 1603 : key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
4262 : :
4263 : 2002 : g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
4264 : 116 : }
4265 : :
4266 : : static void
4267 : 28446 : g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
4268 : : {
4269 : 28446 : if (pair != NULL)
4270 : : {
4271 : 28446 : g_free (pair->key);
4272 : 28446 : g_free (pair->value);
4273 : 28446 : g_free_sized (pair, sizeof (GKeyFileKeyValuePair));
4274 : 403 : }
4275 : 28446 : }
4276 : :
4277 : : /* Be careful not to call this function on a node with data in the
4278 : : * lookup map without removing it from the lookup map, first.
4279 : : *
4280 : : * Some current cases where this warning is not a concern are
4281 : : * when:
4282 : : * - the node being removed is a comment node
4283 : : * - the entire lookup map is getting destroyed soon after
4284 : : * anyway.
4285 : : */
4286 : : static void
4287 : 28361 : g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
4288 : : GKeyFileGroup *group,
4289 : : GList *pair_node)
4290 : : {
4291 : :
4292 : : GKeyFileKeyValuePair *pair;
4293 : :
4294 : 28361 : pair = (GKeyFileKeyValuePair *) pair_node->data;
4295 : :
4296 : 28361 : group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
4297 : :
4298 : 28361 : g_warn_if_fail (pair->value != NULL);
4299 : :
4300 : 28361 : g_key_file_key_value_pair_free (pair);
4301 : :
4302 : 28361 : g_list_free_1 (pair_node);
4303 : 28361 : }
4304 : :
4305 : : static void
4306 : 6511 : g_key_file_remove_group_node (GKeyFile *key_file,
4307 : : GList *group_node)
4308 : : {
4309 : : GKeyFileGroup *group;
4310 : : GList *tmp;
4311 : :
4312 : 6511 : group = (GKeyFileGroup *) group_node->data;
4313 : :
4314 : 6511 : if (group->name)
4315 : : {
4316 : 2002 : g_assert (key_file->group_hash);
4317 : 2002 : g_hash_table_remove (key_file->group_hash, group->name);
4318 : 114 : }
4319 : :
4320 : : /* If the current group gets deleted make the current group the last
4321 : : * added group.
4322 : : */
4323 : 6511 : if (key_file->current_group == group)
4324 : : {
4325 : : /* groups should always contain at least the top comment group,
4326 : : * unless g_key_file_clear has been called
4327 : : */
4328 : 4509 : if (key_file->groups)
4329 : 4509 : key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
4330 : : else
4331 : 0 : key_file->current_group = NULL;
4332 : 182 : }
4333 : :
4334 : : /* If the start group gets deleted make the start group the first
4335 : : * added group.
4336 : : */
4337 : 6511 : if (key_file->start_group == group)
4338 : : {
4339 : 1607 : tmp = g_list_last (key_file->groups);
4340 : 4821 : while (tmp != NULL)
4341 : : {
4342 : 3218 : if (tmp != group_node &&
4343 : 1611 : ((GKeyFileGroup *) tmp->data)->name != NULL)
4344 : 4 : break;
4345 : :
4346 : 3214 : tmp = tmp->prev;
4347 : : }
4348 : :
4349 : 1607 : if (tmp)
4350 : 4 : key_file->start_group = (GKeyFileGroup *) tmp->data;
4351 : : else
4352 : 1603 : key_file->start_group = NULL;
4353 : 86 : }
4354 : :
4355 : 6511 : key_file->groups = g_list_remove_link (key_file->groups, group_node);
4356 : :
4357 : 6511 : tmp = group->key_value_pairs;
4358 : 34864 : while (tmp != NULL)
4359 : : {
4360 : : GList *pair_node;
4361 : :
4362 : 28353 : pair_node = tmp;
4363 : 28353 : tmp = tmp->next;
4364 : 28353 : g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
4365 : : }
4366 : :
4367 : 6511 : g_warn_if_fail (group->key_value_pairs == NULL);
4368 : :
4369 : 6511 : if (group->lookup_map)
4370 : : {
4371 : 2002 : g_hash_table_destroy (group->lookup_map);
4372 : 2002 : group->lookup_map = NULL;
4373 : 114 : }
4374 : :
4375 : 6511 : g_free ((gchar *) group->name);
4376 : 6511 : g_free_sized (group, sizeof (GKeyFileGroup));
4377 : 6511 : g_list_free_1 (group_node);
4378 : 6511 : }
4379 : :
4380 : : /**
4381 : : * g_key_file_remove_group:
4382 : : * @key_file: a key file
4383 : : * @group_name: a group name
4384 : : * @error: return location for a [struct@GLib.Error]
4385 : : *
4386 : : * Removes the specified group, @group_name,
4387 : : * from the key file.
4388 : : *
4389 : : * Returns: true if the group was removed, false otherwise
4390 : : *
4391 : : * Since: 2.6
4392 : : **/
4393 : : gboolean
4394 : 8 : g_key_file_remove_group (GKeyFile *key_file,
4395 : : const gchar *group_name,
4396 : : GError **error)
4397 : : {
4398 : : GList *group_node;
4399 : :
4400 : 8 : g_return_val_if_fail (key_file != NULL, FALSE);
4401 : 8 : g_return_val_if_fail (group_name != NULL, FALSE);
4402 : :
4403 : 8 : group_node = g_key_file_lookup_group_node (key_file, group_name);
4404 : :
4405 : 8 : if (!group_node)
4406 : : {
4407 : 3 : g_set_error (error, G_KEY_FILE_ERROR,
4408 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4409 : 1 : _("Key file does not have group “%s”"),
4410 : 1 : group_name);
4411 : 2 : return FALSE;
4412 : : }
4413 : :
4414 : 6 : g_key_file_remove_group_node (key_file, group_node);
4415 : :
4416 : 6 : return TRUE;
4417 : 4 : }
4418 : :
4419 : : static void
4420 : 28007 : g_key_file_add_key_value_pair (GKeyFile *key_file,
4421 : : GKeyFileGroup *group,
4422 : : GKeyFileKeyValuePair *pair,
4423 : : GList *sibling)
4424 : : {
4425 : 28007 : g_hash_table_replace (group->lookup_map, pair->key, pair);
4426 : 28007 : group->key_value_pairs = g_list_insert_before (group->key_value_pairs, sibling, pair);
4427 : 28007 : }
4428 : :
4429 : : static void
4430 : 363 : g_key_file_add_key (GKeyFile *key_file,
4431 : : GKeyFileGroup *group,
4432 : : const gchar *key,
4433 : : const gchar *value)
4434 : : {
4435 : : GKeyFileKeyValuePair *pair;
4436 : : GList *lp;
4437 : :
4438 : 363 : pair = g_new (GKeyFileKeyValuePair, 1);
4439 : 363 : pair->key = g_strdup (key);
4440 : 363 : pair->value = g_strdup (value);
4441 : :
4442 : : /* skip group comment */
4443 : 363 : lp = group->key_value_pairs;
4444 : 396 : while (lp != NULL && ((GKeyFileKeyValuePair *) lp->data)->key == NULL)
4445 : 33 : lp = lp->next;
4446 : :
4447 : 363 : g_key_file_add_key_value_pair (key_file, group, pair, lp);
4448 : 363 : }
4449 : :
4450 : : /**
4451 : : * g_key_file_remove_key:
4452 : : * @key_file: a key file
4453 : : * @group_name: a group name
4454 : : * @key: a key name to remove
4455 : : * @error: return location for a [struct@GLib.Error]
4456 : : *
4457 : : * Removes @key in @group_name from the key file.
4458 : : *
4459 : : * Returns: true if the key was removed, false otherwise
4460 : : *
4461 : : * Since: 2.6
4462 : : **/
4463 : : gboolean
4464 : 222 : g_key_file_remove_key (GKeyFile *key_file,
4465 : : const gchar *group_name,
4466 : : const gchar *key,
4467 : : GError **error)
4468 : : {
4469 : : GKeyFileGroup *group;
4470 : : GKeyFileKeyValuePair *pair;
4471 : :
4472 : 222 : g_return_val_if_fail (key_file != NULL, FALSE);
4473 : 222 : g_return_val_if_fail (group_name != NULL, FALSE);
4474 : 222 : g_return_val_if_fail (key != NULL, FALSE);
4475 : :
4476 : 222 : pair = NULL;
4477 : :
4478 : 222 : group = g_key_file_lookup_group (key_file, group_name);
4479 : 222 : if (!group)
4480 : : {
4481 : 97 : g_set_error (error, G_KEY_FILE_ERROR,
4482 : : G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4483 : 1 : _("Key file does not have group “%s”"),
4484 : 1 : group_name);
4485 : 96 : return FALSE;
4486 : : }
4487 : :
4488 : 126 : pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4489 : :
4490 : 126 : if (!pair)
4491 : : {
4492 : 47 : set_not_found_key_error (group->name, key, error);
4493 : 47 : return FALSE;
4494 : : }
4495 : :
4496 : 79 : group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4497 : 79 : g_hash_table_remove (group->lookup_map, pair->key);
4498 : 79 : g_key_file_key_value_pair_free (pair);
4499 : :
4500 : 79 : return TRUE;
4501 : 4 : }
4502 : :
4503 : : static GList *
4504 : 32 : g_key_file_lookup_group_node (GKeyFile *key_file,
4505 : : const gchar *group_name)
4506 : : {
4507 : : GKeyFileGroup *group;
4508 : :
4509 : 32 : group = g_key_file_lookup_group (key_file, group_name);
4510 : 32 : if (group == NULL)
4511 : 2 : return NULL;
4512 : :
4513 : 30 : return g_list_find (key_file->groups, group);
4514 : 16 : }
4515 : :
4516 : : static GKeyFileGroup *
4517 : 31340 : g_key_file_lookup_group (GKeyFile *key_file,
4518 : : const gchar *group_name)
4519 : : {
4520 : 31340 : if (!key_file->group_hash)
4521 : 1861 : return NULL;
4522 : :
4523 : 29479 : return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4524 : 557 : }
4525 : :
4526 : : static GList *
4527 : 12 : g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4528 : : GKeyFileGroup *group,
4529 : : const gchar *key)
4530 : : {
4531 : : GList *key_node;
4532 : :
4533 : 33 : for (key_node = group->key_value_pairs;
4534 : 54 : key_node != NULL;
4535 : 42 : key_node = key_node->next)
4536 : : {
4537 : : GKeyFileKeyValuePair *pair;
4538 : :
4539 : 54 : pair = (GKeyFileKeyValuePair *) key_node->data;
4540 : :
4541 : 54 : if (pair->key && strcmp (pair->key, key) == 0)
4542 : 12 : break;
4543 : 21 : }
4544 : :
4545 : 12 : return key_node;
4546 : : }
4547 : :
4548 : : static GKeyFileKeyValuePair *
4549 : 28196 : g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4550 : : GKeyFileGroup *group,
4551 : : const gchar *key)
4552 : : {
4553 : 28196 : return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4554 : : }
4555 : :
4556 : : /* Lines starting with # or consisting entirely of whitespace are merely
4557 : : * recorded, not parsed. This function assumes all leading whitespace
4558 : : * has been stripped.
4559 : : */
4560 : : static gboolean
4561 : 60254 : g_key_file_line_is_comment (const gchar *line)
4562 : : {
4563 : 60254 : return (*line == '#' || *line == '\0' || *line == '\n');
4564 : : }
4565 : :
4566 : : static gboolean
4567 : 4400 : g_key_file_is_group_name (const gchar *name)
4568 : : {
4569 : : const gchar *p, *q;
4570 : :
4571 : 4400 : g_assert (name != NULL);
4572 : :
4573 : 4400 : p = q = name;
4574 : 64005 : while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4575 : 59605 : q = g_utf8_find_next_char (q, NULL);
4576 : :
4577 : 4400 : if (*q != '\0' || q == p)
4578 : 6 : return FALSE;
4579 : :
4580 : 4394 : return TRUE;
4581 : 306 : }
4582 : :
4583 : : static gboolean
4584 : 58129 : g_key_file_is_key_name (const gchar *name,
4585 : : gsize len)
4586 : : {
4587 : : const gchar *p, *q, *end;
4588 : :
4589 : 58129 : g_assert (name != NULL);
4590 : :
4591 : 58129 : p = q = name;
4592 : 58129 : end = name + len;
4593 : :
4594 : : /* We accept a little more than the desktop entry spec says,
4595 : : * since gnome-vfs uses mime-types as keys in its cache.
4596 : : */
4597 : 779495 : while (q < end && *q && *q != '=' && *q != '[' && *q != ']')
4598 : : {
4599 : 663635 : q = g_utf8_find_next_char (q, end);
4600 : 663635 : if (q == NULL)
4601 : 27969 : q = end;
4602 : : }
4603 : :
4604 : : /* No empty keys, please */
4605 : 58129 : if (q == p)
4606 : 2 : return FALSE;
4607 : :
4608 : : /* We accept spaces in the middle of keys to not break
4609 : : * existing apps, but we don't tolerate initial or final
4610 : : * spaces, which would lead to silent corruption when
4611 : : * rereading the file.
4612 : : */
4613 : 58127 : if (*p == ' ' || q[-1] == ' ')
4614 : 2 : return FALSE;
4615 : :
4616 : 58125 : if (*q == '[')
4617 : : {
4618 : 30156 : q++;
4619 : 30562 : while (q < end &&
4620 : 133232 : *q != '\0' &&
4621 : 133231 : (g_unichar_isalnum (g_utf8_get_char_validated (q, end - q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4622 : : {
4623 : 103077 : q = g_utf8_find_next_char (q, end);
4624 : 103077 : if (q == NULL)
4625 : : {
4626 : 0 : q = end;
4627 : 0 : break;
4628 : : }
4629 : : }
4630 : :
4631 : 30156 : if (*q != ']')
4632 : 4 : return FALSE;
4633 : :
4634 : 30152 : q++;
4635 : 41 : }
4636 : :
4637 : 58121 : if (q < end)
4638 : 0 : return FALSE;
4639 : :
4640 : 58121 : return TRUE;
4641 : 398 : }
4642 : :
4643 : : /* A group in a key file is made up of a starting '[' followed by one
4644 : : * or more letters making up the group name followed by ']'.
4645 : : */
4646 : : static gboolean
4647 : 59549 : g_key_file_line_is_group (const gchar *line)
4648 : : {
4649 : : const gchar *p;
4650 : :
4651 : 59549 : p = line;
4652 : 59549 : if (*p != '[')
4653 : 57655 : return FALSE;
4654 : :
4655 : 1894 : p++;
4656 : :
4657 : 27934 : while (*p && *p != ']')
4658 : 26040 : p = g_utf8_find_next_char (p, NULL);
4659 : :
4660 : 1894 : if (*p != ']')
4661 : 0 : return FALSE;
4662 : :
4663 : : /* silently accept whitespace after the ] */
4664 : 1894 : p = g_utf8_find_next_char (p, NULL);
4665 : 1898 : while (*p == ' ' || *p == '\t')
4666 : 4 : p = g_utf8_find_next_char (p, NULL);
4667 : :
4668 : 1894 : if (*p)
4669 : 4 : return FALSE;
4670 : :
4671 : 1890 : return TRUE;
4672 : 423 : }
4673 : :
4674 : : static gboolean
4675 : 57659 : g_key_file_line_is_key_value_pair (const gchar *line)
4676 : : {
4677 : : const gchar *p;
4678 : :
4679 : 57659 : p = g_utf8_strchr (line, -1, '=');
4680 : :
4681 : 57659 : if (!p)
4682 : 12 : return FALSE;
4683 : :
4684 : : /* Key must be non-empty
4685 : : */
4686 : 57647 : if (*p == line[0])
4687 : 2 : return FALSE;
4688 : :
4689 : 57645 : return TRUE;
4690 : 324 : }
4691 : :
4692 : : static gchar *
4693 : 18250 : g_key_file_parse_value_as_string (GKeyFile *key_file,
4694 : : const gchar *value,
4695 : : GSList **pieces,
4696 : : GError **error)
4697 : : {
4698 : : gchar *string_value, *q0, *q;
4699 : 18250 : GSList *tmp_pieces = NULL;
4700 : : const gchar *p;
4701 : :
4702 : 18250 : g_assert (pieces == NULL || *pieces == NULL);
4703 : :
4704 : 18250 : string_value = g_new (gchar, strlen (value) + 1);
4705 : :
4706 : 18250 : p = value;
4707 : 18250 : q0 = q = string_value;
4708 : 382226 : while (*p)
4709 : : {
4710 : 363988 : if (*p == '\\')
4711 : : {
4712 : 36 : p++;
4713 : :
4714 : 36 : switch (*p)
4715 : : {
4716 : 2 : case 's':
4717 : 4 : *q = ' ';
4718 : 4 : break;
4719 : :
4720 : 2 : case 'n':
4721 : 4 : *q = '\n';
4722 : 4 : break;
4723 : :
4724 : 2 : case 't':
4725 : 4 : *q = '\t';
4726 : 4 : break;
4727 : :
4728 : 2 : case 'r':
4729 : 4 : *q = '\r';
4730 : 4 : break;
4731 : :
4732 : 2 : case '\\':
4733 : 4 : *q = '\\';
4734 : 4 : break;
4735 : :
4736 : 3 : case '\0':
4737 : 7 : g_set_error_literal (error, G_KEY_FILE_ERROR,
4738 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4739 : 2 : _("Key file contains escape character "
4740 : : "at end of line"));
4741 : 5 : goto error;
4742 : :
4743 : 6 : default:
4744 : 11 : if (pieces && *p == key_file->list_separator)
4745 : 4 : *q = key_file->list_separator;
4746 : : else
4747 : : {
4748 : 7 : *q++ = '\\';
4749 : 7 : *q = *p;
4750 : :
4751 : 7 : if (*error == NULL)
4752 : : {
4753 : : gchar sequence[3];
4754 : :
4755 : 7 : sequence[0] = '\\';
4756 : 7 : sequence[1] = *p;
4757 : 7 : sequence[2] = '\0';
4758 : :
4759 : 10 : g_set_error (error, G_KEY_FILE_ERROR,
4760 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4761 : 3 : _("Key file contains invalid escape "
4762 : 3 : "sequence “%s”"), sequence);
4763 : 7 : goto error;
4764 : : }
4765 : : }
4766 : 4 : break;
4767 : : }
4768 : 12 : }
4769 : : else
4770 : : {
4771 : 363952 : *q = *p;
4772 : 363952 : if (pieces && (*p == key_file->list_separator))
4773 : : {
4774 : 15563 : tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
4775 : 15563 : q0 = q + 1;
4776 : 27 : }
4777 : : }
4778 : :
4779 : 363976 : if (*p == '\0')
4780 : 0 : break;
4781 : :
4782 : 363976 : q++;
4783 : 363976 : p++;
4784 : : }
4785 : :
4786 : 18238 : *q = '\0';
4787 : 18238 : if (pieces)
4788 : : {
4789 : 14360 : if (q0 < q)
4790 : 618 : tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
4791 : 14360 : *pieces = g_slist_reverse (tmp_pieces);
4792 : 18 : }
4793 : :
4794 : 18238 : return string_value;
4795 : :
4796 : 7 : error:
4797 : 12 : g_free (string_value);
4798 : 12 : g_slist_free_full (tmp_pieces, g_free);
4799 : :
4800 : 12 : return NULL;
4801 : 131 : }
4802 : :
4803 : : static gchar *
4804 : 388 : g_key_file_parse_string_as_value (GKeyFile *key_file,
4805 : : const gchar *string,
4806 : : gboolean escape_separator)
4807 : : {
4808 : : gchar *value, *q;
4809 : : const gchar *p;
4810 : : gsize length;
4811 : : gboolean parsing_leading_space;
4812 : :
4813 : 388 : length = strlen (string) + 1;
4814 : :
4815 : : /* Worst case would be that every character needs to be escaped.
4816 : : * In other words every character turns to two characters
4817 : : */
4818 : 388 : value = g_new (gchar, 2 * length);
4819 : :
4820 : 388 : p = string;
4821 : 388 : q = value;
4822 : 388 : parsing_leading_space = TRUE;
4823 : 7158 : while (p < (string + length - 1))
4824 : : {
4825 : 6770 : gchar escaped_character[3] = { '\\', 0, 0 };
4826 : :
4827 : 6770 : switch (*p)
4828 : : {
4829 : 166 : case ' ':
4830 : 170 : if (parsing_leading_space)
4831 : : {
4832 : 2 : escaped_character[1] = 's';
4833 : 2 : strcpy (q, escaped_character);
4834 : 2 : q += 2;
4835 : 1 : }
4836 : : else
4837 : : {
4838 : 168 : *q = *p;
4839 : 168 : q++;
4840 : : }
4841 : 170 : break;
4842 : 1 : case '\t':
4843 : 2 : if (parsing_leading_space)
4844 : : {
4845 : 2 : escaped_character[1] = 't';
4846 : 2 : strcpy (q, escaped_character);
4847 : 2 : q += 2;
4848 : 1 : }
4849 : : else
4850 : : {
4851 : 0 : *q = *p;
4852 : 0 : q++;
4853 : : }
4854 : 2 : break;
4855 : 1 : case '\n':
4856 : 2 : escaped_character[1] = 'n';
4857 : 2 : strcpy (q, escaped_character);
4858 : 2 : q += 2;
4859 : 2 : break;
4860 : 1 : case '\r':
4861 : 2 : escaped_character[1] = 'r';
4862 : 2 : strcpy (q, escaped_character);
4863 : 2 : q += 2;
4864 : 2 : break;
4865 : 1 : case '\\':
4866 : 2 : escaped_character[1] = '\\';
4867 : 2 : strcpy (q, escaped_character);
4868 : 2 : q += 2;
4869 : 2 : parsing_leading_space = FALSE;
4870 : 2 : break;
4871 : 6356 : default:
4872 : 6592 : if (escape_separator && *p == key_file->list_separator)
4873 : : {
4874 : 2 : escaped_character[1] = key_file->list_separator;
4875 : 2 : strcpy (q, escaped_character);
4876 : 2 : q += 2;
4877 : 2 : parsing_leading_space = TRUE;
4878 : 1 : }
4879 : : else
4880 : : {
4881 : 6590 : *q = *p;
4882 : 6590 : q++;
4883 : 6590 : parsing_leading_space = FALSE;
4884 : : }
4885 : 6592 : break;
4886 : : }
4887 : 6770 : p++;
4888 : : }
4889 : 388 : *q = '\0';
4890 : :
4891 : 388 : return value;
4892 : : }
4893 : :
4894 : : static gint
4895 : 222 : g_key_file_parse_value_as_integer (GKeyFile *key_file,
4896 : : const gchar *value,
4897 : : GError **error)
4898 : : {
4899 : : gchar *eof_int;
4900 : : glong long_value;
4901 : : gint int_value;
4902 : : int errsv;
4903 : :
4904 : 222 : errno = 0;
4905 : 222 : long_value = strtol (value, &eof_int, 10);
4906 : 222 : errsv = errno;
4907 : :
4908 : 222 : if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4909 : : {
4910 : 8 : gchar *value_utf8 = g_utf8_make_valid (value, -1);
4911 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
4912 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4913 : 4 : _("Value “%s” cannot be interpreted "
4914 : 4 : "as a number."), value_utf8);
4915 : 8 : g_free (value_utf8);
4916 : :
4917 : 8 : return 0;
4918 : : }
4919 : :
4920 : 214 : int_value = long_value;
4921 : 214 : if (int_value != long_value || errsv == ERANGE)
4922 : : {
4923 : 0 : gchar *value_utf8 = g_utf8_make_valid (value, -1);
4924 : 0 : g_set_error (error,
4925 : 0 : G_KEY_FILE_ERROR,
4926 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4927 : 0 : _("Integer value “%s” out of range"),
4928 : 0 : value_utf8);
4929 : 0 : g_free (value_utf8);
4930 : :
4931 : 0 : return 0;
4932 : : }
4933 : :
4934 : 214 : return int_value;
4935 : 111 : }
4936 : :
4937 : : static gchar *
4938 : 16 : g_key_file_parse_integer_as_value (GKeyFile *key_file,
4939 : : gint value)
4940 : :
4941 : : {
4942 : 16 : return g_strdup_printf ("%d", value);
4943 : : }
4944 : :
4945 : : static gdouble
4946 : 28 : g_key_file_parse_value_as_double (GKeyFile *key_file,
4947 : : const gchar *value,
4948 : : GError **error)
4949 : : {
4950 : : gchar *end_of_valid_d;
4951 : 28 : gdouble double_value = 0;
4952 : :
4953 : 28 : double_value = g_ascii_strtod (value, &end_of_valid_d);
4954 : :
4955 : 28 : if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4956 : : {
4957 : 8 : gchar *value_utf8 = g_utf8_make_valid (value, -1);
4958 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
4959 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4960 : 4 : _("Value “%s” cannot be interpreted "
4961 : : "as a float number."),
4962 : 4 : value_utf8);
4963 : 8 : g_free (value_utf8);
4964 : :
4965 : 8 : double_value = 0;
4966 : 4 : }
4967 : :
4968 : 28 : return double_value;
4969 : : }
4970 : :
4971 : : static gint
4972 : 473 : strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4973 : : {
4974 : 473 : size_t len2 = strlen (s2);
4975 : 473 : return strncmp (s1, s2, MAX (len1, len2));
4976 : : }
4977 : :
4978 : : static gboolean
4979 : 269 : g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4980 : : const gchar *value,
4981 : : GError **error)
4982 : : {
4983 : : gchar *value_utf8;
4984 : 269 : gint i, length = 0;
4985 : :
4986 : : /* Count the number of non-whitespace characters */
4987 : 1403 : for (i = 0; value[i]; i++)
4988 : 1174 : if (!g_ascii_isspace (value[i]))
4989 : 1132 : length = i + 1;
4990 : :
4991 : 269 : if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4992 : 173 : return TRUE;
4993 : 96 : else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4994 : 88 : return FALSE;
4995 : :
4996 : 8 : value_utf8 = g_utf8_make_valid (value, -1);
4997 : 12 : g_set_error (error, G_KEY_FILE_ERROR,
4998 : : G_KEY_FILE_ERROR_INVALID_VALUE,
4999 : 4 : _("Value “%s” cannot be interpreted "
5000 : 4 : "as a boolean."), value_utf8);
5001 : 8 : g_free (value_utf8);
5002 : :
5003 : 8 : return FALSE;
5004 : 13 : }
5005 : :
5006 : : static const gchar *
5007 : 42 : g_key_file_parse_boolean_as_value (GKeyFile *key_file,
5008 : : gboolean value)
5009 : : {
5010 : 42 : if (value)
5011 : 38 : return "true";
5012 : : else
5013 : 4 : return "false";
5014 : 3 : }
5015 : :
5016 : : static gchar *
5017 : 44 : g_key_file_parse_value_as_comment (GKeyFile *key_file,
5018 : : const gchar *value,
5019 : : gboolean is_final_line)
5020 : : {
5021 : : GString *string;
5022 : : gchar **lines;
5023 : : gsize i;
5024 : :
5025 : 44 : string = g_string_sized_new (512);
5026 : :
5027 : 44 : lines = g_strsplit (value, "\n", 0);
5028 : :
5029 : 78 : for (i = 0; lines[i] != NULL; i++)
5030 : : {
5031 : 34 : const gchar *line = lines[i];
5032 : :
5033 : 34 : if (i != 0)
5034 : 0 : g_string_append_c (string, '\n');
5035 : :
5036 : 34 : if (line[0] == '#')
5037 : 34 : line++;
5038 : 17 : g_string_append (string, line);
5039 : 17 : }
5040 : 44 : g_strfreev (lines);
5041 : :
5042 : : /* This function gets called once per line of a comment, but we don’t want
5043 : : * to add a trailing newline. */
5044 : 44 : if (!is_final_line)
5045 : 10 : g_string_append_c (string, '\n');
5046 : :
5047 : 44 : return g_string_free (string, FALSE);
5048 : : }
5049 : :
5050 : : static gchar *
5051 : 6 : g_key_file_parse_comment_as_value (GKeyFile *key_file,
5052 : : const gchar *comment)
5053 : : {
5054 : : GString *string;
5055 : : gchar **lines;
5056 : : gsize i;
5057 : :
5058 : 6 : string = g_string_sized_new (512);
5059 : :
5060 : 6 : lines = g_strsplit (comment, "\n", 0);
5061 : :
5062 : 12 : for (i = 0; lines[i] != NULL; i++)
5063 : 9 : g_string_append_printf (string, "#%s%s", lines[i],
5064 : 6 : lines[i + 1] == NULL? "" : "\n");
5065 : 6 : g_strfreev (lines);
5066 : :
5067 : 6 : return g_string_free (string, FALSE);
5068 : : }
5069 : :
5070 : : /**
5071 : : * g_key_file_save_to_file:
5072 : : * @key_file: a key file
5073 : : * @filename: the name of the file to write to
5074 : : * @error: return location for a [struct@GLib.Error]
5075 : : *
5076 : : * Writes the contents of @key_file to @filename using
5077 : : * [func@GLib.file_set_contents].
5078 : : *
5079 : : * If you need stricter guarantees about durability of
5080 : : * the written file than are provided by [func@GLib.file_set_contents], use
5081 : : * [func@GLib.file_set_contents_full] with the return value of
5082 : : * [method@GLib.KeyFile.to_data].
5083 : : *
5084 : : * This function can fail for any of the reasons that
5085 : : * [func@GLib.file_set_contents] may fail.
5086 : : *
5087 : : * Returns: true if successful, false otherwise
5088 : : *
5089 : : * Since: 2.40
5090 : : */
5091 : : gboolean
5092 : 8 : g_key_file_save_to_file (GKeyFile *key_file,
5093 : : const gchar *filename,
5094 : : GError **error)
5095 : : {
5096 : : gchar *contents;
5097 : : gboolean success;
5098 : : gsize length;
5099 : :
5100 : 8 : g_return_val_if_fail (key_file != NULL, FALSE);
5101 : 8 : g_return_val_if_fail (filename != NULL, FALSE);
5102 : 8 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
5103 : :
5104 : 8 : contents = g_key_file_to_data (key_file, &length, NULL);
5105 : 8 : g_assert (contents != NULL);
5106 : :
5107 : 8 : success = g_file_set_contents (filename, contents, length, error);
5108 : 8 : g_free (contents);
5109 : :
5110 : 8 : return success;
5111 : 2 : }
|