Debian Codegraph @

src/utils/extra_arg.c

raw
1 #include <glib.h>
2 #include <glib-object.h>
3
4 #include "extra_arg.h"
5
6 /**
7 * bd_extra_arg_copy:
8 * @arg: (nullable): %BDExtraArg to copy
9 *
10 * Creates a new copy of @arg.
11 */
12 BDExtraArg* bd_extra_arg_copy (BDExtraArg *arg) {
13 if (arg == NULL)
14 return NULL;
15
16 BDExtraArg *ret = g_new0 (BDExtraArg, 1);
17 ret->opt = g_strdup (arg->opt);
18 ret->val = g_strdup (arg->val);
19
20 return ret;
21 }
22
23 /**
24 * bd_extra_arg_free:
25 * @arg: (nullable): %BDExtraArg to free
26 *
27 * Frees @arg.
28 */
29 void bd_extra_arg_free (BDExtraArg *arg) {
30 if (arg == NULL)
31 return;
32
33 g_free (arg->opt);
34 g_free (arg->val);
35 g_free (arg);
36 }
37
38 /**
39 * bd_extra_arg_list_free:
40 * @args: (nullable) (array zero-terminated=1): A list of %BDExtraArg to free
41 *
42 * Frees @args and all its elements.
43 */
44 void bd_extra_arg_list_free (BDExtraArg **args) {
45 BDExtraArg **a;
46
47 if (args == NULL)
48 return;
49
50 for (a = args; *a; a++)
51 bd_extra_arg_free (*a);
52 g_free (args);
53 }
54
55 GType bd_extra_arg_get_type (void) {
56 static GType type = 0;
57
58 if (G_UNLIKELY (!type))
59 type = g_boxed_type_register_static ("BDExtraArg",
60 (GBoxedCopyFunc) bd_extra_arg_copy,
61 (GBoxedFreeFunc) bd_extra_arg_free);
62
63 return type;
64 }
65
66 /**
67 * bd_extra_arg_new: (constructor)
68 * @opt: extra option
69 * @val: value for the extra option @opt
70 *
71 * Example of calling bd_fs_xfs_mkfs() with an extra argument.
72 * This will result in calling `mkfs.xfs` with `-L label`.
73 *
74 * |[<!-- language="C" -->
75 * BDExtraArg label_arg = {"-L", "label"};
76 * const BDExtraArg *extra_args[2] = {&label_arg, NULL};
77 *
78 * ret = bd_fs_xfs_mkfs ("/dev/sda", extra_args, error);
79 *
80 * ]|
81 *
82 * Returns: (transfer full): a new extra argument
83 */
84 BDExtraArg* bd_extra_arg_new (const gchar *opt, const gchar *val) {
85 BDExtraArg *ret = g_new0 (BDExtraArg, 1);
86 ret->opt = g_strdup (opt ? opt : "");
87 ret->val = g_strdup (val ? val : "");
88
89 return ret;
90 }