From a7cd93f39e181bc41193689fdbf6de6dd5a963d5 Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Thu, 13 Jul 2017 14:12:30 +0900 Subject: [PATCH] update it --- lib/base/context.c | 29 ++++++++++++++++++----------- lib/core/include/core_jsmn.h | 4 ++-- lib/core/src/jsmn.c | 4 ++-- src/init.c | 7 +++++-- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/base/context.c b/lib/base/context.c index ed7dd2c5c..aaaa61919 100644 --- a/lib/base/context.c +++ b/lib/base/context.c @@ -71,33 +71,40 @@ status_t context_read_file(char *file_path) config->token, sizeof(config->token)/sizeof(config->token[0])); if (config->num_of_token < 0) { - d_fatal("Failed to parse configuration '%s'(jsmnerr = %d)", + d_fatal("Failed to parse configuration '%s' (jsmnerr = %d)", config->json, config->num_of_token); return CORE_ERROR; } + if (config->num_of_token < 1 || config->token[0].type != JSMN_OBJECT) + { + d_fatal("Failed to parse configuration file '%s' (OBJECT expected)", + path); + return CORE_ERROR; + } + return CORE_OK; } status_t context_parse_config() { - config_t *config = &self.config; int i; + config_t *config = &self.config; - for (i = 0; i < config->num_of_token; i++) + char *json = config->json; + int num_of_token = config->num_of_token; + jsmntok_t *token = config->token; + + for (i = 0; i < num_of_token; i++) { - if (json_token_streq(config->json, &config->token[i], - "DB_URI") == 0) + if (jsmntok_equal(json, &token[i], "DB_URI") == 0) { - self.db_uri = - json_token_tostr(config->json, &config->token[i+1]); + self.db_uri = jsmntok_to_string(json, &token[i+1]); i++; } - if (json_token_streq(config->json, &config->token[i], - "LOG_PATH") == 0) + if (jsmntok_equal(json, &token[i], "LOG_PATH") == 0) { - self.log_path = - json_token_tostr(config->json, &config->token[i+1]); + self.log_path = jsmntok_to_string(json, &token[i+1]); i++; } } diff --git a/lib/core/include/core_jsmn.h b/lib/core/include/core_jsmn.h index 79f975a36..6a46bbf46 100644 --- a/lib/core/include/core_jsmn.h +++ b/lib/core/include/core_jsmn.h @@ -69,8 +69,8 @@ void jsmn_init(jsmn_parser *parser); int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens); -int json_token_streq(char *json, jsmntok_t *token, const char *s); -char *json_token_tostr(char *json, jsmntok_t *token); +int jsmntok_equal(char *json, jsmntok_t *token, const char *s); +char *jsmntok_to_string(char *json, jsmntok_t *token); #ifdef __cplusplus } diff --git a/lib/core/src/jsmn.c b/lib/core/src/jsmn.c index 5919eb0c1..801bd7aef 100644 --- a/lib/core/src/jsmn.c +++ b/lib/core/src/jsmn.c @@ -313,7 +313,7 @@ void jsmn_init(jsmn_parser *parser) { parser->toksuper = -1; } -int json_token_streq(char *json, jsmntok_t *token, const char *s) +int jsmntok_equal(char *json, jsmntok_t *token, const char *s) { if (strlen(s) == (size_t)(token->end - token->start) && strncmp(json + token->start, s, token->end - token->start) == 0) @@ -323,7 +323,7 @@ int json_token_streq(char *json, jsmntok_t *token, const char *s) return -1; } -char *json_token_tostr(char *json, jsmntok_t *token) +char *jsmntok_to_string(char *json, jsmntok_t *token) { json[token->end] = '\0'; return json + token->start; diff --git a/src/init.c b/src/init.c index da26c81ce..90e48365a 100644 --- a/src/init.c +++ b/src/init.c @@ -29,9 +29,12 @@ status_t app_will_initialize(char *config_path, char *log_path) rv = context_parse_config(); if (rv != CORE_OK) return rv; - if (log_path) + if (log_path) + context_self()->log_path = log_path; + + if (context_self()->log_path) { - logger_pid = logger_start(log_path); + logger_pid = logger_start(context_self()->log_path); d_assert(logger_pid > 0, return -1, "logger_start() failed"); }