diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2019-10-01 11:22:51 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-10-01 12:56:10 +0200 |
commit | c87a1dbdfb3fd0a91acccd418951f879e9c33d13 (patch) | |
tree | 397287ba70abbe34bb183b8db31558c5c0b634bb | |
parent | 1a8ada191f779aec01e00ff429f437992bf8919c (diff) |
#915: add og_json_parse_string() and use it
This patch avoids repeating similar code by using just one json parser.
-rw-r--r-- | sources/ogAdmServer.cpp | 87 |
1 files changed, 17 insertions, 70 deletions
diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index 19c0eb1..8b95e3e 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3384,6 +3384,15 @@ static int og_json_parse_clients(json_t *element, struct og_msg_params *params) return 0; } +static int og_json_parse_string(json_t *element, const char **str) +{ + if (json_typeof(element) != JSON_STRING) + return -1; + + *str = json_string_value(element); + return 0; +} + static int og_cmd_legacy_send(struct og_msg_params *params, const char *cmd, const char *state) { @@ -3735,68 +3744,6 @@ static int og_cmd_run_get(json_t *element, struct og_msg_params *params, return 0; } -static int og_json_parse_disk(json_t *element, struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->disk = json_string_value(element); - - return 0; -} - -static int og_json_parse_partition(json_t *element, - struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->partition = json_string_value(element); - - return 0; -} - -static int og_json_parse_name(json_t *element, struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->name = json_string_value(element); - - return 0; -} - -static int og_json_parse_repository(json_t *element, - struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->repository = json_string_value(element); - - return 0; -} - -static int og_json_parse_id(json_t *element, struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->id = json_string_value(element); - - return 0; -} - -static int og_json_parse_code(json_t *element, struct og_msg_params *params) -{ - if (json_typeof(element) != JSON_STRING) - return -1; - - params->code = json_string_value(element); - - return 0; -} - static int og_cmd_session(json_t *element, struct og_msg_params *params) { char buf[4096], iph[4096]; @@ -3813,9 +3760,9 @@ static int og_cmd_session(json_t *element, struct og_msg_params *params) if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else if (!strcmp(key, "disk")) - err = og_json_parse_disk(value, params); + err = og_json_parse_string(value, ¶ms->disk); else if (!strcmp(key, "partition")) - err = og_json_parse_partition(value, params); + err = og_json_parse_string(value, ¶ms->partition); if (err < 0) return err; @@ -3977,19 +3924,19 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) json_object_foreach(element, key, value) { if (!strcmp(key, "disk")) - err = og_json_parse_disk(value, params); + err = og_json_parse_string(value, ¶ms->disk); else if (!strcmp(key, "partition")) - err = og_json_parse_partition(value, params); + err = og_json_parse_string(value, ¶ms->partition); else if (!strcmp(key, "name")) - err = og_json_parse_name(value, params); + err = og_json_parse_string(value, ¶ms->name); else if (!strcmp(key, "repository")) - err = og_json_parse_repository(value, params); + err = og_json_parse_string(value, ¶ms->repository); else if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else if (!strcmp(key, "id")) - err = og_json_parse_id(value, params); + err = og_json_parse_string(value, ¶ms->id); else if (!strcmp(key, "code")) - err = og_json_parse_code(value, params); + err = og_json_parse_string(value, ¶ms->code); if (err < 0) break; |