#include #include #include #include #include #include #include #include #include "scanner_toml.h" static toml_table_t *toml_open(struct logger *logger, const char *toml_path) { FILE *fp=fopen(toml_path, "r"); if (NULL==fp) { STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_bool_get can't open config file: %s", toml_path); return NULL; } char errbuf[256]={0}; toml_table_t *root=toml_parse_file(fp, errbuf, sizeof(errbuf)); fclose(fp); return root; } static void toml_close(struct toml_table_t *root) { toml_free(root); } void toml_bool_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, bool *value) { toml_table_t *root=toml_open(logger, toml_path); if(NULL==root) { return ; } toml_table_t *table=toml_table_in(root, table_key); if(NULL==table) { STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_bool_get can't find key: [%s] in config file: %s", table_key, toml_path); toml_close(root); return ; } toml_datum_t val=toml_bool_in(table, key); if(val.ok>0) { *value=val.u.b; } else { *value=false; } toml_close(root); } void toml_int_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, int *value) { toml_table_t *root=toml_open(logger, toml_path); if(NULL==root) { return ; } toml_table_t *table=toml_table_in(root, table_key); if(NULL==table) { STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_int_get can't find key: [%s] in config file: %s", table_key, toml_path); toml_close(root); return ; } toml_datum_t val=toml_int_in(table, key); if(val.ok>0) { *value=val.u.i; } else { *value=0; } toml_close(root); } void toml_string_get(struct logger *logger, const char *toml_path, const char *table_key, const char *key, char *value, size_t value_len) { toml_table_t *root=toml_open(logger, toml_path); if(NULL==root) { return ; } toml_table_t *table=toml_table_in(root, table_key); if(NULL==table) { STELLAR_LOG_FATAL(logger, SCANNER_MODULE_NAME, "toml_string_get can't find key: [%s] in config file: %s", table_key, toml_path); toml_close(root); return ; } toml_datum_t val=toml_string_in(table, key); if(val.ok>0) { strncpy(value, val.u.s, MIN(value_len-1, strlen(val.u.s))); free(val.u.s); } else { strncpy(value, "", value_len); } toml_close(root); }