]> git.itanic.dy.fi Git - glucose/blob - main.c
Added -format option (defaults to "clean"), added "raw" and "CSV" formats.
[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 char *token(char **str, char sep);
36
37 int main(int argc, char *argv[])
38 {
39         FILE *outf;
40         struct user_options opts = { .output_format = CLEAN };
41         struct msg msg;
42         int fd, usage_code, ret, error;
43         int entries = 0;
44
45         read_args(argc, argv, &opts);
46
47         trace_level = opts.trace_level;
48
49         if (opts.output_path) {
50                 outf = fopen(opts.output_path, "w");
51                 if (outf == NULL) {
52                         error = errno;
53                         trace(0, "Failed to open output file %s: %s\n",
54                                 opts.output_path, strerror(error));
55                         return 1;
56                 }
57         } else {
58                 outf = stdout;
59         }
60
61         if (opts.usbdev == NULL)
62                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
63                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
64         else
65                 fd = hiddev_open(opts.usbdev, &usage_code);
66         if (fd < 0)
67                 return 1;
68         trace(0, "Initializing\n");
69         contour_initialize(fd, usage_code);
70
71         trace(0, "Done! Reading data\n");
72         if ( opts.output_format == CSV )
73                 fprintf(outf, "#,Time,Type,Value,Unit,Before meal,After meal,Stress,Sick,Dont feel right,Activity,Control test\n");
74
75         while (1) {
76                 ret = contour_read_entry(fd, usage_code, &msg);
77                 if (ret < 45)
78                         break;
79
80                 if ( opts.output_format == CSV ) {
81                         char *tok = (char *) &msg.data;
82                                       token(&tok, '|');  // unknown
83                         char *seq =   token(&tok, '|');
84                         char *type =  token(&tok, '|');
85                         char *val =   token(&tok, '|');
86                         char *unit =  token(&tok, '|');
87                                       token(&tok, '|');  // unknown
88                         char *notes = token(&tok, '|');
89                                       token(&tok, '|');  // unknown
90                         char *time =  token(&tok, '\r');
91                         unit[strlen(unit)-2] = 0;
92                         fprintf(outf, "%s,%.4s-%.2s-%.2s %.2s:%.2s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
93                                 seq, &time[0], &time[4], &time[6], &time[8], &time[10], &type[3], val, unit,
94                                 strchr(notes, 'B') ? "X" : "", strchr(notes, 'A') ? "X" : "",
95                                 strchr(notes, 'S') ? "X" : "", strchr(notes, 'I') ? "X" : "",
96                                 strchr(notes, 'D') ? "X" : "", strchr(notes, 'X') ? "X" : "",
97                                 strchr(notes, 'C') ? "X" : ""
98                         );
99                 } else
100                 if ( opts.output_format == CLEAN ) {
101                         sanitize_ascii(msg.data, ret);
102                         fprintf(outf, "%s\n", msg.data);
103                 } else
104                 if ( opts.output_format == RAW ) {
105                         fprintf(outf, "%s", msg.data);
106                 }
107
108                 entries++;
109                 if (outf != stdout) {
110                         trace(0, "\r%d", entries);
111                         fflush(stdout);
112                 }
113         }
114         trace(0, "\n");
115
116         return 0;
117 }
118
119 char *token(char **str, char sep)
120 {
121   char *start = *str;
122   char *cur;
123   for ( cur = start; *cur && (*cur != sep); ++cur ) ;
124   *cur = 0;
125   *str = cur+1;
126   return start;
127 }