diff options
Diffstat (limited to 'src/json/json.c')
-rw-r--r-- | src/json/json.c | 73 |
1 files changed, 69 insertions, 4 deletions
diff --git a/src/json/json.c b/src/json/json.c index a2d1a9608..c182a02f4 100644 --- a/src/json/json.c +++ b/src/json/json.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | 3 | Copyright (C) 2014-2017 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify it under the | 5 | GNUnet is free software; you can redistribute it and/or modify it under the |
6 | terms of the GNU General Public License as published by the Free Software | 6 | terms of the GNU General Public License as published by the Free Software |
@@ -82,13 +82,78 @@ GNUNET_JSON_parse (const json_t *root, | |||
82 | void | 82 | void |
83 | GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec) | 83 | GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec) |
84 | { | 84 | { |
85 | unsigned int i; | 85 | for (unsigned int i=0;NULL != spec[i].parser;i++) |
86 | |||
87 | for (i=0;NULL != spec[i].parser;i++) | ||
88 | if (NULL != spec[i].cleaner) | 86 | if (NULL != spec[i].cleaner) |
89 | spec[i].cleaner (spec[i].cls, | 87 | spec[i].cleaner (spec[i].cls, |
90 | &spec[i]); | 88 | &spec[i]); |
91 | } | 89 | } |
92 | 90 | ||
93 | 91 | ||
92 | /** | ||
93 | * Set an option with a JSON value from the command line. | ||
94 | * A pointer to this function should be passed as part of the | ||
95 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
96 | * of this type. | ||
97 | * | ||
98 | * @param ctx command line processing context | ||
99 | * @param scls additional closure (will point to the 'json_t *') | ||
100 | * @param option name of the option | ||
101 | * @param value actual value of the option as a string. | ||
102 | * @return #GNUNET_OK if parsing the value worked | ||
103 | */ | ||
104 | static int | ||
105 | set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
106 | void *scls, | ||
107 | const char *option, | ||
108 | const char *value) | ||
109 | { | ||
110 | json_t **json = scls; | ||
111 | json_error_t error; | ||
112 | |||
113 | *json = json_loads (value, | ||
114 | JSON_REJECT_DUPLICATES, | ||
115 | &error); | ||
116 | if (NULL == *json) | ||
117 | { | ||
118 | FPRINTF (stderr, | ||
119 | _("Failed to parse JSON in option `%s': %s (%s)\n"), | ||
120 | option, | ||
121 | error.text, | ||
122 | error.source); | ||
123 | return GNUNET_SYSERR; | ||
124 | } | ||
125 | return GNUNET_OK; | ||
126 | } | ||
127 | |||
128 | |||
129 | /** | ||
130 | * Allow user to specify a JSON input value. | ||
131 | * | ||
132 | * @param shortName short name of the option | ||
133 | * @param name long name of the option | ||
134 | * @param argumentHelp help text for the option argument | ||
135 | * @param description long help text for the option | ||
136 | * @param[out] val set to the JSON specified at the command line | ||
137 | */ | ||
138 | struct GNUNET_GETOPT_CommandLineOption | ||
139 | GNUNET_JSON_getopt (char shortName, | ||
140 | const char *name, | ||
141 | const char *argumentHelp, | ||
142 | const char *description, | ||
143 | json_t **json) | ||
144 | { | ||
145 | struct GNUNET_GETOPT_CommandLineOption clo = { | ||
146 | .shortName = shortName, | ||
147 | .name = name, | ||
148 | .argumentHelp = argumentHelp, | ||
149 | .description = description, | ||
150 | .require_argument = 1, | ||
151 | .processor = &set_json, | ||
152 | .scls = (void *) json | ||
153 | }; | ||
154 | |||
155 | return clo; | ||
156 | } | ||
157 | |||
158 | |||
94 | /* end of json.c */ | 159 | /* end of json.c */ |