diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-06-18 18:15:25 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-06-18 18:46:48 +0200 |
commit | 04ca20e9f1999d2c780043152cf233bcb1836d18 (patch) | |
tree | cca304b6d34723ec3a2a13e6745a874443ce5704 /sources/json.c | |
parent | 0b9465f783124340d85ff414c2ffb1dc40745f10 (diff) |
#971 split into smaller file
Split ogAdmServer into several files:
* sources/rest.c that implements the server REST API.
* sources/client.c that implements the client REST API.
* sources/json.c that provides a few JSON helpers.
Diffstat (limited to 'sources/json.c')
-rw-r--r-- | sources/json.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/sources/json.c b/sources/json.c new file mode 100644 index 0000000..b76a3b7 --- /dev/null +++ b/sources/json.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2020 Soleta Networks <info@soleta.eu> + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the + * Free Software Foundation, version 3. + */ + +#include "json.h" +#include <stdint.h> + +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; +} + +int og_json_parse_uint(json_t *element, uint32_t *integer) +{ + if (json_typeof(element) != JSON_INTEGER) + return -1; + + *integer = json_integer_value(element); + return 0; +} + +int og_json_parse_bool(json_t *element, bool *value) +{ + if (json_typeof(element) == JSON_TRUE) + *value = true; + else if (json_typeof(element) == JSON_FALSE) + *value = false; + else + return -1; + + return 0; +} + +int og_json_parse_partition(json_t *element, struct og_partition *part, + uint64_t required_flags) +{ + uint64_t flags = 0UL; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "partition")) { + err = og_json_parse_string(value, &part->number); + flags |= OG_PARAM_PART_NUMBER; + } else if (!strcmp(key, "code")) { + err = og_json_parse_string(value, &part->code); + flags |= OG_PARAM_PART_CODE; + } else if (!strcmp(key, "filesystem")) { + err = og_json_parse_string(value, &part->filesystem); + flags |= OG_PARAM_PART_FILESYSTEM; + } else if (!strcmp(key, "size")) { + err = og_json_parse_string(value, &part->size); + flags |= OG_PARAM_PART_SIZE; + } else if (!strcmp(key, "format")) { + err = og_json_parse_string(value, &part->format); + flags |= OG_PARAM_PART_FORMAT; + } else if (!strcmp(key, "disk")) { + err = og_json_parse_string(value, &part->disk); + flags |= OG_PARAM_PART_DISK; + } else if (!strcmp(key, "os")) { + err = og_json_parse_string(value, &part->os); + flags |= OG_PARAM_PART_OS; + } else if (!strcmp(key, "used_size")) { + err = og_json_parse_string(value, &part->used_size); + flags |= OG_PARAM_PART_USED_SIZE; + } + + if (err < 0) + return err; + } + + if (flags != required_flags) + return -1; + + return err; +} |