2 * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
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.
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.
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
23 #include <linux/types.h>
24 #include <sys/types.h>
33 #include "contour-next-protocol.h"
35 static char *token(char **str, char sep)
40 for (cur = start; *cur && (*cur != sep); ++cur);
48 static void format_csv(struct user_options *opts, unsigned char *msg)
50 char *tok = (char *)msg;
51 token(&tok, '|'); // unknown
52 char *seq = token(&tok, '|');
53 char *type = token(&tok, '|');
54 char *val = token(&tok, '|');
55 char *unit = token(&tok, '|');
56 token(&tok, '|');// unknown
57 char *notes = token(&tok, '|');
58 token(&tok, '|');// unknown
59 char *time = token(&tok, '\r');
61 unit[strlen(unit) - 2] = 0;
63 "%s,\"%.4s-%.2s-%.2s %.2s:%.2s\",\"%s\",\"%s\",\"%s\","
64 "%s,%s,%s,%s,%s,%s,%s\n",
65 seq, &time[0], &time[4], &time[6], &time[8], &time[10],
67 strchr(notes, 'B') ? "X" : "",
68 strchr(notes, 'A') ? "X" : "",
69 strchr(notes, 'S') ? "X" : "",
70 strchr(notes, 'I') ? "X" : "",
71 strchr(notes, 'D') ? "X" : "",
72 strchr(notes, 'X') ? "X" : "",
73 strchr(notes, 'C') ? "X" : ""
77 static int format_message(struct user_options *opts,
78 unsigned char *msg, int len)
80 switch (opts->output_format)
83 format_csv(opts, msg);
87 sanitize_ascii(msg, len);
88 fprintf(opts->outf, "%s\n", msg);
92 fprintf(opts->outf, "%s", msg);
96 trace(0, "BUG: Invalid message format %d\n",
106 static int dump_entries(struct user_options *opts)
108 unsigned char msg[64 + 1];
112 trace(0, "Reading data ...\n");
113 if (opts->output_format == CSV)
114 fprintf(opts->outf, "#,Time,Type,Value,Unit,\"Before meal\","
115 "\"After meal\",Stress,Sick,\"Dont feel right\","
116 "Activity,\"Control test\"\n");
119 ret = contour_next_read_entry(msg);
124 ret = format_message(opts, msg, ret);
130 if ((opts->outf != stdout) || !isatty(fileno(stdout))) {
131 trace(0, "\r%d entries", entries);
135 trace(0, "\nDone.\n");
140 int main(int argc, char *argv[])
142 struct user_options opts;
145 bzero(&opts, sizeof(opts));
146 opts.output_format = CLEAN;
148 if (read_args(argc, argv, &opts))
151 trace_level = opts.trace_level;
153 if (opts.output_path) {
154 opts.outf = fopen(opts.output_path, "w");
155 if (opts.outf == NULL) {
156 trace(0, "Failed to open output file \"%s\": %m\n",
164 trace(0, "Initializing ...\n");
165 contour_next_initialize();
167 ret = dump_entries(&opts);