]> git.itanic.dy.fi Git - glucose/commitdiff
Add support for printing out data to a file
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 5 May 2011 16:00:11 +0000 (19:00 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 5 May 2011 16:00:11 +0000 (19:00 +0300)
If no file is given, results are printed out to stdout, as before.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c
options.c
options.h

diff --git a/main.c b/main.c
index 79dc67dc8c6d58fdddec31fbf7c3d234ad41d905..888c33965a2b28e9de9fac1f469dfe2231d0532d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,7 +2,11 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <linux/types.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <string.h>
+#include <errno.h>
 
 #include "hiddev.h"
 #include "utils.h"
 
 int main(int argc, char *argv[])
 {
-       int fd, usage_code, ret;
+       FILE *outf;
        struct user_options opts;
        struct msg msg;
+       int fd, usage_code, ret, error;
 
        read_args(argc, argv, &opts);
+
        trace_level = opts.trace_level;
 
+       if (opts.output_path) {
+               outf = fopen(opts.output_path, "w");
+               if (outf == NULL) {
+                       error = errno;
+                       trace(0, "Failed to open output file %s: %s\n",
+                               opts.output_path, strerror(error));
+                       return 1;
+               }
+       } else {
+               outf = stdout;
+       }
+
        if (opts.usbdev == NULL)
                fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
                                CONTOUR_USB_PRODUCT_ID, &usage_code);
@@ -32,10 +50,12 @@ int main(int argc, char *argv[])
        trace(0, "Done! Reading data\n");
        while (1) {
                ret = contour_read_entry(fd, usage_code, &msg);
-               print_ascii(msg.data, ret);
+               sanitize_ascii(msg.data, ret);
 
                if (ret < 45)
                        break;
+
+               fprintf(outf, "%s\n", msg.data);
        }
 
        return 0;
index 90fb44867d5f6696f0d085eb09a519fba3ac1368..ffca63e63ac87f61d5cc0575b07aa70dc905f0a7 100644 (file)
--- a/options.c
+++ b/options.c
@@ -9,9 +9,10 @@ int read_args(int argc, char *argv[], struct user_options *opts)
         int option_index = 0, c;
         static struct option long_options[] = {
                 { .val = 'd', .name = "device", .has_arg = 1, },
-                { .val = 'v', .name = "verbose", .has_arg = 2},
+                { .val = 'v', .name = "verbose", .has_arg = 2 },
+               { .val = 'o', .name = "output", .has_arg = 1 },
         };
-        char short_options[] = "d:v";
+        char short_options[] = "d:v:o:";
 
        memset(opts, 0, sizeof(*opts));
 
@@ -31,6 +32,9 @@ int read_args(int argc, char *argv[], struct user_options *opts)
                                opts->trace_level = atoi(optarg);
                        else
                                opts->trace_level++;
+               case 'o':
+                       opts->output_path = optarg;
+                       break;
                 case '?':
                        return -1;
                 }
index 8e629fb7f67e0021c29fae9d7beb3111c5fcb67e..ee78d58db64de226198c67a8cc6de8baa662e119 100644 (file)
--- a/options.h
+++ b/options.h
@@ -3,6 +3,7 @@
 
 struct user_options {
        char *usbdev;
+       char *output_path;
        int trace_level;
 };