]> www.average.org Git - pulsecounter.git/blob - linux/dbstore.c
keep database config in a separate file
[pulsecounter.git] / linux / dbstore.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <time.h>
4 #include <ctype.h>
5 #include <string.h>
6
7 #include <mysql/mysql.h>
8
9 #include "dbstore.h"
10
11 static char *host = NULL;
12 static char *user = NULL;
13 static char *pass = NULL;
14 static char *dbnm = "watermeter";
15
16 int dbconfig(char *conffile)
17 {
18         FILE *fp = fopen(conffile, "r");
19         int rc = 0;
20         char buf[128];
21
22         if (!fp)
23                 return 1;
24         while (fgets(buf, sizeof(buf), fp)) {
25                 char *k, *v, *e;
26
27                 e = buf + strlen(buf) - 1;
28                 if (*e == '\n')
29                         *e = '\0';
30                 else {
31                         /* line too long */
32                         rc = 1;
33                         break;
34                 }
35                 for (k = buf; k < e && isspace(k); k++) /*nothing*/ ;
36                 if (*k == '#') break;
37                 for (v = k; v < e && !isspace(v)
38                             && *v != ':' && *v != '='; v++) /*nothing*/ ;
39                 if (v < e && (*v == ':' || *v == '=')) v++;
40                 for (; v < e && (isspace(v) || *v == ':' || *v == '=')
41                                                         ; v++) /*nothing*/ ;
42                 if (v >= e) {
43                         /* no value */
44                         rc = 1;
45                         break;
46                 }
47                 if      (!strcmp(k, "host"))     host = strdup(v);
48                 else if (!strcmp(k, "user"))     user = strdup(v);
49                 else if (!strcmp(k, "password")) pass = strdup(v);
50                 else if (!strcmp(k, "database")) dbnm = strdup(v);
51                 else {
52                         /* unknown key */
53                         rc = 1;
54                         break;
55                 }
56         }
57         return rc;
58 }
59
60 int dbstore(uint8_t which, uint32_t val)
61 {
62         time_t t;
63         int i;
64         MYSQL mysql;
65         int rc = 0;
66         struct tm tm;
67         char buf[64];
68         char tstr[32];
69         char *table = (which == 1) ? "coldcnt" : "hotcnt";
70         char statement[64];
71
72         t = time(NULL);
73         (void)gmtime_r(&t, &tm);
74         (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
75         mysql_init(&mysql);
76         if(!mysql_real_connect(&mysql, host, user, pass, dbnm, 0, NULL, 0)) {
77                 fprintf(stderr, "mysql connect error: %s\n",
78                         mysql_error(&mysql));
79                 return 1;
80         }
81         snprintf(statement, sizeof(statement),
82                  "insert into %s values (\"%s\",%u);\n",
83                  table, tstr, val);
84         rc = mysql_query(&mysql, statement);
85         if (rc)
86                 fprintf(stderr, "mysql insert \"%s\" error: %s\n",
87                         statement, mysql_error(&mysql));
88         mysql_close(&mysql);
89         return rc;
90 }