|
|
|
@ -3,65 +3,53 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
#include "conf.h"
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
skipspaces(char *p) {
|
|
|
|
|
|
|
|
|
|
while(isspace(*p)) p++;
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct conf *
|
|
|
|
|
conf_read(const char *filename) {
|
|
|
|
|
|
|
|
|
|
json_t *j, *jtmp;
|
|
|
|
|
json_error_t error;
|
|
|
|
|
struct conf *conf;
|
|
|
|
|
|
|
|
|
|
FILE *f = fopen(filename, "r");
|
|
|
|
|
|
|
|
|
|
if(!f) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
|
conf = calloc(1, sizeof(struct conf));
|
|
|
|
|
conf->redis_host = strdup("127.0.0.1");
|
|
|
|
|
conf->redis_port = 6379;
|
|
|
|
|
conf->http_host = strdup("0.0.0.0");
|
|
|
|
|
conf->http_port = 7379;
|
|
|
|
|
|
|
|
|
|
while(!feof(f)) {
|
|
|
|
|
char buffer[100], *ret;
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
if(!(ret = fgets(buffer, sizeof(buffer)-1, f))) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(*ret == '#') { /* comments */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
j = json_load_file(filename, 0, &error);
|
|
|
|
|
if(!j) {
|
|
|
|
|
fprintf(stderr, "Error: %s (line %d)\n", error.text, error.line);
|
|
|
|
|
return conf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(*ret != 0) {
|
|
|
|
|
ret[strlen(ret)-1] = 0; /* remove new line */
|
|
|
|
|
}
|
|
|
|
|
jtmp = json_object_get(j, "redis_host");
|
|
|
|
|
if(jtmp && json_typeof(jtmp) == JSON_STRING) {
|
|
|
|
|
free(conf->redis_host);
|
|
|
|
|
conf->redis_host = strdup(json_string_value(jtmp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(strncmp(ret, "redis_host", 10) == 0) {
|
|
|
|
|
conf->redis_host = strdup(skipspaces(ret + 11));
|
|
|
|
|
} else if(strncmp(ret, "redis_port", 10) == 0) {
|
|
|
|
|
conf->redis_port = (short)atoi(skipspaces(ret + 10));
|
|
|
|
|
} else if(strncmp(ret, "http_host", 10) == 0) {
|
|
|
|
|
conf->http_host = strdup(skipspaces(ret + 11));
|
|
|
|
|
} else if(strncmp(ret, "http_port", 9) == 0) {
|
|
|
|
|
conf->http_port = (short)atoi(skipspaces(ret + 10));
|
|
|
|
|
}
|
|
|
|
|
jtmp = json_object_get(j, "redis_port");
|
|
|
|
|
if(jtmp && json_typeof(jtmp) == JSON_INTEGER) {
|
|
|
|
|
conf->redis_port = json_integer_value(jtmp);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
/* default values */
|
|
|
|
|
if(!conf->redis_host) {
|
|
|
|
|
conf->redis_host = strdup("127.0.0.1");
|
|
|
|
|
jtmp = json_object_get(j, "http_host");
|
|
|
|
|
if(jtmp && json_typeof(jtmp) == JSON_STRING) {
|
|
|
|
|
free(conf->http_host);
|
|
|
|
|
conf->http_host = strdup(json_string_value(jtmp));
|
|
|
|
|
}
|
|
|
|
|
if(!conf->http_host) {
|
|
|
|
|
conf->http_host = strdup("0.0.0.0");
|
|
|
|
|
|
|
|
|
|
jtmp = json_object_get(j, "http_port");
|
|
|
|
|
if(jtmp && json_typeof(jtmp) == JSON_INTEGER) {
|
|
|
|
|
conf->http_port = json_integer_value(jtmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_decref(j);
|
|
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|