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