2017-07-13 22:13:40 +02:00
|
|
|
/** Helpers for configuration parsers.
|
2017-04-15 21:22:19 +02:00
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2017-04-15 21:22:19 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/config.hpp>
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2019-04-05 11:01:49 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
int json_to_config_type(int type)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
|
|
|
switch (type) {
|
2018-08-17 12:51:54 +02:00
|
|
|
case JSON_OBJECT:
|
|
|
|
return CONFIG_TYPE_GROUP;
|
|
|
|
|
|
|
|
case JSON_ARRAY:
|
|
|
|
return CONFIG_TYPE_LIST;
|
|
|
|
|
|
|
|
case JSON_STRING:
|
|
|
|
return CONFIG_TYPE_STRING;
|
|
|
|
|
|
|
|
case JSON_INTEGER:
|
|
|
|
return CONFIG_TYPE_INT64;
|
|
|
|
|
|
|
|
case JSON_REAL:
|
|
|
|
return CONFIG_TYPE_FLOAT;
|
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
case JSON_TRUE:
|
|
|
|
case JSON_FALSE:
|
2018-08-17 12:51:54 +02:00
|
|
|
case JSON_NULL:
|
|
|
|
return CONFIG_TYPE_BOOL;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
json_t * villas::node::config_to_json(config_setting_t *cfg)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
|
|
|
switch (config_setting_type(cfg)) {
|
2018-08-17 12:51:54 +02:00
|
|
|
case CONFIG_TYPE_INT:
|
|
|
|
return json_integer(config_setting_get_int(cfg));
|
|
|
|
|
|
|
|
case CONFIG_TYPE_INT64:
|
|
|
|
return json_integer(config_setting_get_int64(cfg));
|
|
|
|
|
|
|
|
case CONFIG_TYPE_FLOAT:
|
|
|
|
return json_real(config_setting_get_float(cfg));
|
|
|
|
|
|
|
|
case CONFIG_TYPE_STRING:
|
|
|
|
return json_string(config_setting_get_string(cfg));
|
|
|
|
|
|
|
|
case CONFIG_TYPE_BOOL:
|
|
|
|
return json_boolean(config_setting_get_bool(cfg));
|
2017-04-15 20:38:58 +02:00
|
|
|
|
|
|
|
case CONFIG_TYPE_ARRAY:
|
|
|
|
case CONFIG_TYPE_LIST: {
|
|
|
|
json_t *json = json_array();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
for (int i = 0; i < config_setting_length(cfg); i++) {
|
|
|
|
auto *elm = config_setting_get_elem(cfg, i);
|
|
|
|
json_array_append_new(json, config_to_json(elm));
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
return json;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
case CONFIG_TYPE_GROUP: {
|
|
|
|
json_t *json = json_object();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
for (int i = 0; i < config_setting_length(cfg); i++) {
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *elm = config_setting_get_elem(cfg, i);
|
2017-04-15 20:38:58 +02:00
|
|
|
json_object_set_new(json,
|
2021-08-10 10:12:48 -04:00
|
|
|
config_setting_name(elm),
|
|
|
|
config_to_json(elm)
|
2017-04-15 20:38:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return json_object();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::json_to_config(json_t *json, config_setting_t *parent)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
|
|
|
config_setting_t *cfg;
|
|
|
|
int ret, type;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
if (config_setting_is_root(parent)) {
|
|
|
|
if (!json_is_object(json))
|
|
|
|
return -1; /* The root must be an object! */
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
switch (json_typeof(json)) {
|
|
|
|
case JSON_OBJECT: {
|
|
|
|
const char *key;
|
|
|
|
json_t *json_value;
|
|
|
|
|
|
|
|
json_object_foreach(json, key, json_value) {
|
|
|
|
type = json_to_config_type(json_typeof(json_value));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
cfg = config_setting_add(parent, key, type);
|
|
|
|
ret = json_to_config(json_value, cfg);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
case JSON_ARRAY: {
|
|
|
|
size_t i;
|
|
|
|
json_t *json_value;
|
|
|
|
|
|
|
|
json_array_foreach(json, i, json_value) {
|
|
|
|
type = json_to_config_type(json_typeof(json_value));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-07 13:23:43 +02:00
|
|
|
cfg = config_setting_add(parent, nullptr, type);
|
2017-04-15 20:38:58 +02:00
|
|
|
ret = json_to_config(json_value, cfg);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
case JSON_STRING:
|
|
|
|
config_setting_set_string(parent, json_string_value(json));
|
|
|
|
break;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
case JSON_INTEGER:
|
|
|
|
config_setting_set_int64(parent, json_integer_value(json));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_REAL:
|
|
|
|
config_setting_set_float(parent, json_real_value(json));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_TRUE:
|
|
|
|
case JSON_FALSE:
|
|
|
|
case JSON_NULL:
|
|
|
|
config_setting_set_bool(parent, json_is_true(json));
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-04-05 11:01:49 +02:00
|
|
|
#endif /* WITH_CONFIG */
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void villas::node::json_object_extend_key_value_token(json_t *obj, const char *key, const char *value)
|
2019-02-18 01:10:45 +01:00
|
|
|
{
|
|
|
|
char *str = strdup(value);
|
2019-03-26 07:01:10 +01:00
|
|
|
const char *delim = ",";
|
2019-02-18 01:10:45 +01:00
|
|
|
|
|
|
|
char *lasts;
|
|
|
|
char *token = strtok_r(str, delim, &lasts);
|
|
|
|
|
|
|
|
while (token) {
|
|
|
|
json_object_extend_key_value(obj, key, token);
|
|
|
|
|
2019-04-07 13:23:43 +02:00
|
|
|
token = strtok_r(nullptr, delim, &lasts);
|
2019-02-18 01:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void villas::node::json_object_extend_key_value(json_t *obj, const char *key, const char *value)
|
2017-08-03 00:19:27 +02:00
|
|
|
{
|
2019-02-18 01:10:45 +01:00
|
|
|
char *end, *cpy, *key1, *key2, *lasts;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
double real;
|
|
|
|
long integer;
|
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
json_t *arr, *add, *existing, *subobj;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
/* Is the key pointing to an object? */
|
|
|
|
subobj = obj;
|
|
|
|
cpy = strdup(key);
|
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
key1 = strtok_r(cpy, ".", &lasts);
|
2019-04-07 13:23:43 +02:00
|
|
|
key2 = strtok_r(nullptr, ".", &lasts);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
while (key1 && key2) {
|
|
|
|
existing = json_object_get(subobj, key1);
|
|
|
|
if (existing)
|
|
|
|
subobj = existing;
|
|
|
|
else {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_object();
|
|
|
|
json_object_set(subobj, key1, add);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
subobj = add;
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
key1 = key2;
|
2019-04-07 15:13:40 +02:00
|
|
|
key2 = strtok_r(nullptr, ".", &lasts);
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to parse as integer */
|
|
|
|
integer = strtol(value, &end, 0);
|
|
|
|
if (*end == 0) {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_integer(integer);
|
2017-08-03 00:19:27 +02:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to parse as floating point */
|
|
|
|
real = strtod(value, &end);
|
|
|
|
if (*end == 0) {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_real(real);
|
2017-08-03 00:19:27 +02:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to parse special types */
|
|
|
|
if (!strcmp(value, "true")) {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_true();
|
2017-08-03 00:19:27 +02:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(value, "false")) {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_false();
|
2017-08-03 00:19:27 +02:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(value, "null")) {
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_null();
|
2017-08-03 00:19:27 +02:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallback to string */
|
2019-03-26 07:01:10 +01:00
|
|
|
add = json_string(value);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
success:
|
|
|
|
/* Does the key already exist?
|
|
|
|
* If yes, transform to array. */
|
|
|
|
existing = json_object_get(subobj, key1);
|
|
|
|
if (existing) {
|
|
|
|
if (json_is_array(existing))
|
|
|
|
arr = existing;
|
|
|
|
else {
|
|
|
|
arr = json_array();
|
|
|
|
json_object_set(subobj, key1, arr);
|
|
|
|
json_array_append(arr, existing);
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
json_array_append(arr, add);
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
else
|
2019-03-26 07:01:10 +01:00
|
|
|
json_object_set(subobj, key1, add);
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
json_t * villas::node::json_load_cli(int argc, const char *argv[])
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
2018-08-23 13:31:18 +02:00
|
|
|
const char *opt;
|
2019-04-07 13:23:43 +02:00
|
|
|
const char *key = nullptr;
|
|
|
|
const char *value = nullptr;
|
2018-08-23 13:31:18 +02:00
|
|
|
const char *sep;
|
2019-02-18 01:10:45 +01:00
|
|
|
char *cpy, *lasts;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
json_t *json = json_object();
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
opt = argv[i];
|
|
|
|
|
|
|
|
/* Long Option */
|
|
|
|
if (opt[0] == '-' && opt[1] == '-') {
|
|
|
|
/* Option without value? Abort. */
|
2019-04-07 13:23:43 +02:00
|
|
|
if (key != nullptr)
|
|
|
|
return nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
key = opt + 2;
|
|
|
|
|
|
|
|
/* Does this option has the form "--option=value"? */
|
|
|
|
sep = strchr(key, '=');
|
|
|
|
if (sep) {
|
|
|
|
cpy = strdup(key);
|
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
key = strtok_r(cpy, "=", &lasts);
|
2019-04-07 13:23:43 +02:00
|
|
|
value = strtok_r(nullptr, "", &lasts);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
json_object_extend_key_value_token(json, key, value);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
free(cpy);
|
2019-04-07 13:23:43 +02:00
|
|
|
key = nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Value */
|
|
|
|
else {
|
|
|
|
/* Value without key. Abort. */
|
2019-04-07 13:23:43 +02:00
|
|
|
if (key == nullptr)
|
|
|
|
return nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
value = opt;
|
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
json_object_extend_key_value_token(json, key, value);
|
2019-04-07 13:23:43 +02:00
|
|
|
key = nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::json_object_extend(json_t *obj, json_t *merge)
|
2017-08-03 00:19:27 +02:00
|
|
|
{
|
|
|
|
const char *key;
|
2017-04-15 20:38:58 +02:00
|
|
|
int ret;
|
2017-08-03 00:19:27 +02:00
|
|
|
json_t *merge_value, *obj_value;
|
|
|
|
|
|
|
|
if (!json_is_object(obj) || !json_is_object(merge))
|
|
|
|
return -1;
|
|
|
|
|
2017-10-20 11:37:25 +02:00
|
|
|
json_object_foreach(merge, key, merge_value) {
|
2017-08-03 00:19:27 +02:00
|
|
|
obj_value = json_object_get(obj, key);
|
|
|
|
if (obj_value && json_is_object(obj_value))
|
|
|
|
ret = json_object_extend(obj_value, merge_value);
|
|
|
|
else
|
|
|
|
ret = json_object_set(obj, key, merge_value);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::json_object_extend_str(json_t *obj, const char *str)
|
2017-08-03 00:19:27 +02:00
|
|
|
{
|
2019-02-18 01:10:45 +01:00
|
|
|
char *key, *value, *cpy, *lasts;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
cpy = strdup(str);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
key = strtok_r(cpy, "=", &lasts);
|
2019-04-07 15:13:40 +02:00
|
|
|
value = strtok_r(nullptr, "", &lasts);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!key || !value)
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-02-18 01:10:45 +01:00
|
|
|
json_object_extend_key_value_token(obj, key, value);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
free(cpy);
|
|
|
|
|
|
|
|
return 0;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|