]> git.itanic.dy.fi Git - glucose/commitdiff
Added -format option (defaults to "clean"), added "raw" and "CSV" formats.
authorSteve Sloan <steve@finagle.org>
Tue, 20 Mar 2012 16:58:32 +0000 (09:58 -0700)
committerSteve Sloan <steve@finagle.org>
Tue, 20 Mar 2012 16:58:32 +0000 (09:58 -0700)
contour-protocol.c
contour-protocol.h
main.c
options.c
options.h

index 141c5e2c3bb0984a292e16edb46086490a1f4af5..6e02fac1088d6341c805b94f6ddba173847922f7 100644 (file)
@@ -83,6 +83,7 @@ static int read_and_verify(struct msg *msg, int fd)
        }
 
        memcpy(msg->data, buf, sizeof(buf));
+       memset(&msg->data[sizeof(buf)], 0, sizeof(msg->data)-sizeof(buf));
        trace(2, "Got data %d: ", datalen(buf));
        if (trace_level >= 3)
                print_hex(buf, datalen(buf));
@@ -297,10 +298,9 @@ int contour_read_entry(int fd, int uc, struct msg *in)
        msg.direction = OUT;
        SET_FIRST_BYTE(0x01);
        SET_BYTE(1, 0x06);
-
        send_msg(&msg, fd, uc);
-       read_and_verify(in, fd);
 
+       read_and_verify(in, fd);
        return datalen(in->data);
 }
 
index 42c52ffce7325edf319820008c2b4b12f658dc7d..3bb2edbeea397bbe39d439888925f0704d1d987d 100644 (file)
@@ -25,7 +25,7 @@
 
 struct msg {
        int direction;
-       unsigned char data[64];
+       unsigned char data[64+1];
 };
 
 enum direction {
diff --git a/main.c b/main.c
index fed79a98b935a2690a792610c0f6359aa79090ff..0f2b641bf8af0419091a727efaba11a874e7b6ba 100644 (file)
--- a/main.c
+++ b/main.c
 #include "options.h"
 #include "contour-protocol.h"
 
+char *token(char **str, char sep);
+
 int main(int argc, char *argv[])
 {
        FILE *outf;
-       struct user_options opts;
+       struct user_options opts = { .output_format = CLEAN };
        struct msg msg;
        int fd, usage_code, ret, error;
        int entries = 0;
@@ -63,19 +65,45 @@ int main(int argc, char *argv[])
                fd = hiddev_open(opts.usbdev, &usage_code);
        if (fd < 0)
                return 1;
-
        trace(0, "Initializing\n");
        contour_initialize(fd, usage_code);
 
        trace(0, "Done! Reading data\n");
+       if ( opts.output_format == CSV )
+               fprintf(outf, "#,Time,Type,Value,Unit,Before meal,After meal,Stress,Sick,Dont feel right,Activity,Control test\n");
+
        while (1) {
                ret = contour_read_entry(fd, usage_code, &msg);
-               sanitize_ascii(msg.data, ret);
-
                if (ret < 45)
                        break;
 
-               fprintf(outf, "%s\n", msg.data);
+               if ( opts.output_format == CSV ) {
+                       char *tok = (char *) &msg.data;
+                                     token(&tok, '|');  // unknown
+                       char *seq =   token(&tok, '|');
+                       char *type =  token(&tok, '|');
+                       char *val =   token(&tok, '|');
+                       char *unit =  token(&tok, '|');
+                                     token(&tok, '|');  // unknown
+                       char *notes = token(&tok, '|');
+                                     token(&tok, '|');  // unknown
+                       char *time =  token(&tok, '\r');
+                       unit[strlen(unit)-2] = 0;
+                       fprintf(outf, "%s,%.4s-%.2s-%.2s %.2s:%.2s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
+                               seq, &time[0], &time[4], &time[6], &time[8], &time[10], &type[3], val, unit,
+                               strchr(notes, 'B') ? "X" : "", strchr(notes, 'A') ? "X" : "",
+                               strchr(notes, 'S') ? "X" : "", strchr(notes, 'I') ? "X" : "",
+                               strchr(notes, 'D') ? "X" : "", strchr(notes, 'X') ? "X" : "",
+                               strchr(notes, 'C') ? "X" : ""
+                       );
+               } else
+               if ( opts.output_format == CLEAN ) {
+                       sanitize_ascii(msg.data, ret);
+                       fprintf(outf, "%s\n", msg.data);
+               } else
+               if ( opts.output_format == RAW ) {
+                       fprintf(outf, "%s", msg.data);
+               }
 
                entries++;
                if (outf != stdout) {
@@ -87,3 +115,13 @@ int main(int argc, char *argv[])
 
        return 0;
 }
+
+char *token(char **str, char sep)
+{
+  char *start = *str;
+  char *cur;
+  for ( cur = start; *cur && (*cur != sep); ++cur ) ;
+  *cur = 0;
+  *str = cur+1;
+  return start;
+}
index 9d4ff032ed5c311beb29a3cd93eb8cbb8a7b9b60..7bd5b9bbfd9700a081a8753d46dc379c01903bf2 100644 (file)
--- a/options.c
+++ b/options.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 
 #include "options.h"
+#include "utils.h"
 
 int read_args(int argc, char *argv[], struct user_options *opts)
 {
@@ -30,8 +31,9 @@ int read_args(int argc, char *argv[], struct user_options *opts)
                 { .val = 'd', .name = "device", .has_arg = 1, },
                 { .val = 'v', .name = "verbose", .has_arg = 2 },
                { .val = 'o', .name = "output", .has_arg = 1 },
+                { .val = 'f', .name = "format", .has_arg = 1, },
         };
-        char short_options[] = "d:v:o:";
+        char short_options[] = "d:v:o:f:";
 
        memset(opts, 0, sizeof(*opts));
 
@@ -54,6 +56,20 @@ int read_args(int argc, char *argv[], struct user_options *opts)
                case 'o':
                        opts->output_path = optarg;
                        break;
+               case 'f':
+                       if ( strcmp(optarg, "raw") == 0 ) {
+                               opts->output_format = RAW;
+                       } else
+                       if ( strcmp(optarg, "clean") == 0 ) {
+                               opts->output_format = CLEAN;
+                       } else
+                       if ( strcmp(optarg, "csv") == 0 ) {
+                               opts->output_format = CSV;
+                       } else {
+                               trace(0, "Unknown format type \"%s\", must be one of \"raw\", \"clean\", or \"csv\"\n", optarg );
+                               return -1;
+                       }
+                       break;
                 case '?':
                        return -1;
                 }
index deb46cdb94e684d46e25e5d5d1beadd6f691c1e2..83fefb6ced1b3d4822a64dd7f376c948bebf6a6e 100644 (file)
--- a/options.h
+++ b/options.h
  */
 
 #ifndef _OPTIONS_H_
-#define _OPTINOS_H_
+#define _OPTIONS_H_
 
 struct user_options {
        char *usbdev;
        char *output_path;
+       enum {
+               RAW = 0,
+               CLEAN = 1,
+               CSV = 2,
+       } output_format;
        int trace_level;
 };