@@ -120,6 +120,20 @@ void get_field_string_with_size(parsertype p, void *e, const char *path, char *d
}
}
+bool is_field_numeric(parsertype p, void *e, const char *path)
+{
+ switch (p) {
+ case LIBCFG_PARSER:
+ return is_field_numeric_cfg((config_setting_t *)e, path);
+ case JSON_PARSER:
+ return is_field_numeric_json((json_object *)e, path);
+ default:
+ (void)e;
+ (void)path;
+ }
+ return false;
+}
+
void get_field(parsertype p, void *e, const char *path, void *dest)
{
switch (p) {
@@ -71,6 +71,26 @@ void iterate_field_libconfig(config_setting_t *e, iterate_callback cb, void *dat
}
}
+bool is_field_numeric_cfg(config_setting_t *e, const char *path)
+{
+ config_setting_t *elem;
+ int type;
+
+ if (path)
+ elem = config_setting_lookup(e, path);
+ else
+ elem = e;
+
+ if (!elem)
+ return false;
+
+ type = config_setting_type(elem);
+
+ return type == CONFIG_TYPE_INT ||
+ type == CONFIG_TYPE_INT64 ||
+ type == CONFIG_TYPE_FLOAT;
+}
+
void get_field_cfg(config_setting_t *e, const char *path, void *dest)
{
config_setting_t *elem;
@@ -124,6 +124,23 @@ void get_value_json(json_object *e, void *dest)
}
}
+bool is_field_numeric_json(json_object *e, const char *path)
+{
+ enum json_type type;
+ json_object *fld = NULL;
+
+ if (path) {
+ if (!json_object_object_get_ex(e, path, &fld))
+ return false;
+ } else {
+ fld = e;
+ }
+
+ type = json_object_get_type(fld);
+ return type == json_type_int ||
+ type == json_type_double;
+}
+
void get_field_json(json_object *e, const char *path, void *dest)
{
json_object *fld = NULL;
@@ -33,6 +33,7 @@ typedef void (*iterate_callback)(const char *name, const char *value,
#define config_setting_lookup config_lookup_from
#endif
+bool is_field_numeric_cfg(config_setting_t *e, const char *path);
void get_value_libconfig(const config_setting_t *e, void *dest);
void get_field_cfg(config_setting_t *e, const char *path, void *dest);
void *get_child_libconfig(void *e, const char *name);
@@ -58,6 +59,7 @@ void *get_node_libconfig(config_t *cfg, const char **nodes);
#ifdef CONFIG_JSON
#include <json-c/json.h>
+bool is_field_numeric_json(json_object *e, const char *path);
const char *get_field_string_json(json_object *e, const char *path);
void get_value_json(json_object *e, void *dest);
void get_field_json(json_object *e, const char *path, void *dest);
@@ -85,6 +87,7 @@ void *get_node_json(json_object *root, const char **nodes);
#define get_node_json(root, nodes) (NULL)
#endif
+bool is_field_numeric(parsertype p, void *e, const char *path);
const char *get_field_string(parsertype p, void *e, const char *path);
void get_field_string_with_size(parsertype p, void *e, const char *path,
char *d, size_t n);
This allows to check if an elem is numeric to call the right funtion to retrieve the value. Signed-off-by: Stefano Babic <stefano.babic@swupdate.org> --- core/parsing_library.c | 14 ++++++++++++++ corelib/parsing_library_libconfig.c | 20 ++++++++++++++++++++ corelib/parsing_library_libjson.c | 17 +++++++++++++++++ include/parselib.h | 3 +++ 4 files changed, 54 insertions(+)