From: Olaf Wintermann Date: Fri, 15 Apr 2022 14:51:30 +0000 (+0200) Subject: add new json based config file X-Git-Url: https://develop.uap-core.de/gitweb/uwplayer.git/commitdiff_plain/a5bc5f23f204464e636227b79759d46a97df3a0e add new json based config file --- diff --git a/application/json.c b/application/json.c index 9bdea02..bd27f2f 100644 --- a/application/json.c +++ b/application/json.c @@ -51,6 +51,13 @@ JSONParser* json_parser_new(void) { return parser; } +void json_parser_free(JSONParser *p) { + if(p->states) free(p->states); + if(p->readvalue_stack) free(p->readvalue_stack); + + free(p); +} + void json_parser_fill(JSONParser *p, const char *buf, size_t size) { p->buffer = buf; p->size = size; @@ -745,10 +752,11 @@ static int add_to_parent(JSONParser *p, JSONValue *parent, JSONValue *v) { static int readvaluestack_add(JSONParser *p, JSONValue *v) { if(p->readvalue_nelm == p->readvalue_alloc) { p->readvalue_alloc *= 2; - p->readvalue_stack = realloc(p->readvalue_stack, sizeof(JSONValue*) * p->readvalue_alloc); - if(!p->readvalue_stack) { + JSONValue **new_stack = realloc(p->readvalue_stack, sizeof(JSONValue*) * p->readvalue_alloc); + if(!new_stack) { return -1; } + p->readvalue_stack = new_stack; } p->readvalue_stack[p->readvalue_nelm++] = v; return 0; diff --git a/application/json.h b/application/json.h index 0731cca..4898cba 100644 --- a/application/json.h +++ b/application/json.h @@ -182,6 +182,7 @@ struct JSONValue { JSONParser* json_parser_new(void); +void json_parser_free(JSONParser *p); void json_parser_fill(JSONParser *p, const char *buf, size_t size); diff --git a/application/main.c b/application/main.c index 028a163..42078dc 100644 --- a/application/main.c +++ b/application/main.c @@ -122,7 +122,7 @@ int main(int argc, char** argv) { NULL); // load settings - if(load_settings()) { + if(load_config()) { return 1; } diff --git a/application/settings.c b/application/settings.c index 59c366c..aa1b976 100644 --- a/application/settings.c +++ b/application/settings.c @@ -33,6 +33,7 @@ #include "main.h" #include "utils.h" +#include "json.h" #include #include @@ -41,13 +42,30 @@ #define CONFIG_BASE_DIR ".config" #define UWP_CONFIG_DIR "uwplayer" -#define UWP_CONFIG_FILE "uwplayer.properties" +#define UWP_CONFIG_FILE "uwplayer.conf" + +#define JS_READ_BUFSIZE 4096 static void* player_bin_search_thread(void *data); +static void conf_load_global_settings(void); static char *uwp_config_dir; + +/* + * root json config object + */ +static JSONObject *uwp_config; + +/* + * global settings from json config converted to key/value pairs + */ static UcxMap *uwp_settings; +/* + * default settings + */ +static UcxMap *uwp_default; + static int check_config_dir(void) { char *home = getenv("HOME"); if(!home) { @@ -77,12 +95,13 @@ static int check_config_dir(void) { return ret; } -int load_settings(void) { +int load_config(void) { if(check_config_dir()) { return 1; } uwp_settings = ucx_map_new(16); + uwp_default = ucx_map_new(32); char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE); FILE *cfgfile = fopen(cfgfile_path, "r"); @@ -90,10 +109,39 @@ int load_settings(void) { int ret = 0; if(cfgfile) { - if(ucx_properties_load(uwp_settings, cfgfile)) { - fprintf(stderr, "Error: Cannot read uwplayer settings\n"); + JSONParser *parser = json_parser_new(); + + JSONValue *value = NULL; + char buf[JS_READ_BUFSIZE]; + size_t r; + + while((ret = json_read_value(parser, &value)) >= 0) { + if(ret == 0) { + r = fread(buf, 1, JS_READ_BUFSIZE, cfgfile); + if(r == 0) { + break; + } + json_parser_fill(parser, buf, r); + } else { + break; + } + } + + json_parser_free(parser); + + if(value) { + if(value->type == JSON_OBJECT) { + ret = 0; + uwp_config = &value->value.object; + conf_load_global_settings(); + } else { + ret = 1; + } + } else { ret = 1; } + + fclose(cfgfile); if(ret) { @@ -117,6 +165,27 @@ int load_settings(void) { return 0; } +static void conf_load_global_settings(void) { + JSONValue *settings = json_obj_get(uwp_config, "settings"); + if(!settings) { + return; + } + + if(settings->type != JSON_OBJECT) { + fprintf(stderr, "Warning: 'settings' not an object\n"); + return; + } + + JSONObject *s = &settings->value.object; + + for(size_t i=0;isize;i++) { + JSONObjValue *gs = &s->values[i]; + if(gs->value->type == JSON_STRING) { + ucx_map_cstr_put(uwp_settings, gs->name, gs->value->value.string.string); + } + } +} + static char* get_which_output(FILE *f, UcxBuffer *buf) { buf->pos = 0; buf->size = 0; diff --git a/application/settings.h b/application/settings.h index aabd835..e97b080 100644 --- a/application/settings.h +++ b/application/settings.h @@ -28,14 +28,14 @@ extern "C" { #endif #define UWP_PLAYER_BIN "player" -#define UWP_PLAYER_TYPE "player_type" +#define UWP_PLAYER_TYPE "playertype" typedef struct PlayerInfo { char *bin; char *type; } PlayerInfo; -int load_settings(void); +int load_config(void); char* SettingsGetPlayerBin(void);