]> git.itanic.dy.fi Git - glucose/blob - main.c
main.c: Remove forward declaration for token()
[glucose] / main.c
1 /*
2  * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <linux/types.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "hiddev.h"
31 #include "utils.h"
32 #include "options.h"
33 #include "contour-protocol.h"
34
35 static char *token(char **str, char sep)
36 {
37         char *start = *str;
38         char *cur;
39
40         for (cur = start; *cur && (*cur != sep); ++cur);
41
42         *cur = 0;
43         *str = cur+1;
44
45         return start;
46 }
47
48 int main(int argc, char *argv[])
49 {
50         FILE *outf;
51         struct user_options opts = { .usbdev = NULL, .output_path = NULL, .output_format = CLEAN, .trace_level = 0 };
52         struct msg msg;
53         int fd, usage_code, ret, error;
54         int entries = 0;
55
56         if ( read_args(argc, argv, &opts) )
57                 return -1;
58
59         trace_level = opts.trace_level;
60
61         if (opts.output_path) {
62                 outf = fopen(opts.output_path, "w");
63                 if (outf == NULL) {
64                         error = errno;
65                         trace(0, "Failed to open output file %s: %s\n",
66                                 opts.output_path, strerror(error));
67                         return 1;
68                 }
69         } else {
70                 outf = stdout;
71         }
72
73         if (opts.usbdev == NULL)
74                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
75                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
76         else
77                 fd = hiddev_open(opts.usbdev, &usage_code);
78         if (fd < 0)
79                 return 1;
80         trace(0, "Initializing ...\n");
81         contour_initialize(fd, usage_code);
82
83         trace(0, "Reading data ...\n");
84         if ( opts.output_format == CSV )
85                 fprintf(outf, "#,Time,Type,Value,Unit,\"Before meal\",\"After meal\",Stress,Sick,\"Dont feel right\",Activity,\"Control test\"\n");
86
87         while (1) {
88                 ret = contour_read_entry(fd, usage_code, &msg);
89                 if (ret < 45)
90                         break;
91
92                 if ( opts.output_format == CSV ) {
93                         char *tok = (char *) &msg.data;
94                                       token(&tok, '|');  // unknown
95                         char *seq =   token(&tok, '|');
96                         char *type =  token(&tok, '|');
97                         char *val =   token(&tok, '|');
98                         char *unit =  token(&tok, '|');
99                                       token(&tok, '|');  // unknown
100                         char *notes = token(&tok, '|');
101                                       token(&tok, '|');  // unknown
102                         char *time =  token(&tok, '\r');
103                         unit[strlen(unit)-2] = 0;
104                         fprintf(outf, "%s,\"%.4s-%.2s-%.2s %.2s:%.2s\",\"%s\",\"%s\",\"%s\",%s,%s,%s,%s,%s,%s,%s\n",
105                                 seq, &time[0], &time[4], &time[6], &time[8], &time[10], &type[3], val, unit,
106                                 strchr(notes, 'B') ? "X" : "", strchr(notes, 'A') ? "X" : "",
107                                 strchr(notes, 'S') ? "X" : "", strchr(notes, 'I') ? "X" : "",
108                                 strchr(notes, 'D') ? "X" : "", strchr(notes, 'X') ? "X" : "",
109                                 strchr(notes, 'C') ? "X" : ""
110                         );
111                 } else
112                 if ( opts.output_format == CLEAN ) {
113                         sanitize_ascii(msg.data, ret);
114                         fprintf(outf, "%s\n", msg.data);
115                 } else
116                 if ( opts.output_format == RAW ) {
117                         fprintf(outf, "%s", msg.data);
118                 }
119
120                 entries++;
121                 fflush(outf);
122
123                 if ((outf != stdout) || !isatty(fileno(stdout))) {
124                         trace(0, "\r%d entries", entries);
125                         fflush(stdout);
126                 }
127         }
128         trace(0, "\nDone.\n");
129
130         return 0;
131 }