From 02c4fd2b4121e55e0cff1f56d9bb12576fb78211 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Thu, 5 May 2011 19:00:11 +0300 Subject: [PATCH] Add support for printing out data to a file If no file is given, results are printed out to stdout, as before. Signed-off-by: Timo Kokkonen --- main.c | 24 ++++++++++++++++++++++-- options.c | 8 ++++++-- options.h | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 79dc67d..888c339 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,11 @@ #include #include #include +#include +#include +#include #include +#include #include "hiddev.h" #include "utils.h" @@ -11,13 +15,27 @@ 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; diff --git a/options.c b/options.c index 90fb448..ffca63e 100644 --- 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; } diff --git a/options.h b/options.h index 8e629fb..ee78d58 100644 --- a/options.h +++ b/options.h @@ -3,6 +3,7 @@ struct user_options { char *usbdev; + char *output_path; int trace_level; }; -- 2.44.0