X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=blobdiff_plain;f=linux%2Fdbstore.c;h=1a85dbac1a7a50eb2564982cead098587f1fcc08;hp=395654da09f681d35921206e0103d2decbf91093;hb=8ccd96947c30f401410182f29e7a9cd19224380c;hpb=896a89d59b527f5099b38ea58aebeb6ad674ae13 diff --git a/linux/dbstore.c b/linux/dbstore.c index 395654d..1a85dba 100644 --- a/linux/dbstore.c +++ b/linux/dbstore.c @@ -1,11 +1,64 @@ #include #include #include +#include +#include + +#include #include #include "dbstore.h" +static char *host = NULL; +static char *user = NULL; +static char *pass = NULL; +static char *dbnm = "watermeter"; + +int dbconfig(char *conffile) +{ + FILE *fp = fopen(conffile, "r"); + int rc = 0; + char buf[128]; + + if (!fp) + return 1; + while (fgets(buf, sizeof(buf), fp)) { + char *k, *v, *e; + + e = buf + strlen(buf) - 1; + if (*e == '\n') + *e = '\0'; + else { + /* line too long */ + rc = 1; + break; + } + for (k = buf; k < e && isspace(k); k++) /*nothing*/ ; + if (*k == '#') break; + for (v = k; v < e && !isspace(v) + && *v != ':' && *v != '='; v++) /*nothing*/ ; + if (v < e && (*v == ':' || *v == '=')) v++; + for (; v < e && (isspace(v) || *v == ':' || *v == '=') + ; v++) /*nothing*/ ; + if (v >= e) { + /* no value */ + rc = 1; + break; + } + if (!strcmp(k, "host")) host = strdup(v); + else if (!strcmp(k, "user")) user = strdup(v); + else if (!strcmp(k, "password")) pass = strdup(v); + else if (!strcmp(k, "database")) dbnm = strdup(v); + else { + /* unknown key */ + rc = 1; + break; + } + } + return rc; +} + int dbstore(uint8_t which, uint32_t val) { time_t t; @@ -22,10 +75,8 @@ int dbstore(uint8_t which, uint32_t val) (void)gmtime_r(&t, &tm); (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm); mysql_init(&mysql); - if(!mysql_real_connect(&mysql, NULL, "pulsecounter", - "xxxxxxxxxxxxx", "watermeter", 0, NULL, 0)) { - fprintf(stderr, "mysql connect error: %s\n", - mysql_error(&mysql)); + if(!mysql_real_connect(&mysql, host, user, pass, dbnm, 0, NULL, 0)) { + g_warning("mysql connect error: %s\n", mysql_error(&mysql)); return 1; } snprintf(statement, sizeof(statement), @@ -33,8 +84,8 @@ int dbstore(uint8_t which, uint32_t val) table, tstr, val); rc = mysql_query(&mysql, statement); if (rc) - fprintf(stderr, "mysql insert \"%s\" error: %s\n", - statement, mysql_error(&mysql)); + g_warning("mysql insert \"%s\" error: %s\n", + statement, mysql_error(&mysql)); mysql_close(&mysql); return rc; }