/src/ostree/src/libotutil/ot-tool-util.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2015 Colin Walters <walters@verbum.org> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.0+ |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "config.h" |
21 | | |
22 | | #include "ot-tool-util.h" |
23 | | #include "otutil.h" |
24 | | |
25 | | gboolean |
26 | | ot_parse_boolean (const char *value, gboolean *out_parsed, GError **error) |
27 | 0 | { |
28 | 0 | #define ARG_EQ(x, y) (g_ascii_strcasecmp (x, y) == 0) |
29 | 0 | if (ARG_EQ (value, "1") || ARG_EQ (value, "true") || ARG_EQ (value, "yes")) |
30 | 0 | *out_parsed = TRUE; |
31 | 0 | else if (ARG_EQ (value, "0") || ARG_EQ (value, "false") || ARG_EQ (value, "no") |
32 | 0 | || ARG_EQ (value, "none")) |
33 | 0 | *out_parsed = FALSE; |
34 | 0 | else |
35 | 0 | { |
36 | 0 | return glnx_throw (error, "Invalid boolean argument '%s'", value); |
37 | 0 | } |
38 | | |
39 | 0 | return TRUE; |
40 | 0 | } |
41 | | |
42 | | gboolean |
43 | | ot_parse_keyvalue (const char *keyvalue, char **out_key, char **out_value, GError **error) |
44 | 0 | { |
45 | 0 | const char *eq = strchr (keyvalue, '='); |
46 | 0 | if (!eq) |
47 | 0 | { |
48 | 0 | return glnx_throw (error, "Missing '=' in KEY=VALUE for --set"); |
49 | 0 | } |
50 | 0 | *out_key = g_strndup (keyvalue, eq - keyvalue); |
51 | 0 | *out_value = g_strdup (eq + 1); |
52 | 0 | return TRUE; |
53 | 0 | } |
54 | | |
55 | | /** |
56 | | * Note: temporarily copied from GLib: |
57 | | * https://github.com/GNOME/glib/blob/a419146578a42c760cff684292465b38df855f75/glib/garray.c#L1664 |
58 | | * See documentation at: |
59 | | * https://developer.gnome.org/glib/stable/glib-Pointer-Arrays.html#g-ptr-array-find-with-equal-func |
60 | | * |
61 | | * ot_ptr_array_find_with_equal_func: (skip) |
62 | | * @haystack: pointer array to be searched |
63 | | * @needle: pointer to look for |
64 | | * @equal_func: (nullable): the function to call for each element, which should |
65 | | * return %TRUE when the desired element is found; or %NULL to use pointer |
66 | | * equality |
67 | | * @index_: (optional) (out caller-allocates): return location for the index of |
68 | | * the element, if found |
69 | | * |
70 | | * Checks whether @needle exists in @haystack, using the given @equal_func. |
71 | | * If the element is found, %TRUE is returned and the element’s index is |
72 | | * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_ |
73 | | * is undefined. If @needle exists multiple times in @haystack, the index of |
74 | | * the first instance is returned. |
75 | | * |
76 | | * @equal_func is called with the element from the array as its first parameter, |
77 | | * and @needle as its second parameter. If @equal_func is %NULL, pointer |
78 | | * equality is used. |
79 | | * |
80 | | * Returns: %TRUE if @needle is one of the elements of @haystack |
81 | | * Since: 2.54 |
82 | | */ |
83 | | gboolean |
84 | | ot_ptr_array_find_with_equal_func (GPtrArray *haystack, gconstpointer needle, GEqualFunc equal_func, |
85 | | guint *index_) |
86 | 0 | { |
87 | 0 | guint i; |
88 | |
|
89 | 0 | g_return_val_if_fail (haystack != NULL, FALSE); |
90 | | |
91 | 0 | if (equal_func == NULL) |
92 | 0 | equal_func = g_direct_equal; |
93 | |
|
94 | 0 | for (i = 0; i < haystack->len; i++) |
95 | 0 | { |
96 | 0 | if (equal_func (g_ptr_array_index (haystack, i), needle)) |
97 | 0 | { |
98 | 0 | if (index_ != NULL) |
99 | 0 | *index_ = i; |
100 | 0 | return TRUE; |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | 0 | return FALSE; |
105 | 0 | } |