]> mj.ucw.cz Git - xsv.git/commitdiff
Added warnings and --quiet
authorMartin Mares <mj@ucw.cz>
Mon, 23 Jul 2012 18:34:39 +0000 (20:34 +0200)
committerMartin Mares <mj@ucw.cz>
Mon, 23 Jul 2012 18:34:39 +0000 (20:34 +0200)
xsv.c

diff --git a/xsv.c b/xsv.c
index 87c8dff8966fceaf2b1f1a2f5ee0c349f5205cca..c21a97f63aae9bfb41413a6c9bc09b334dd2a490 100644 (file)
--- a/xsv.c
+++ b/xsv.c
@@ -60,6 +60,7 @@ struct format {
        enum format_id id;
        int fs;
        int quote;
+       int quiet;
        int (*read_line)(void);
        void (*write_line)(void);
 };
@@ -77,6 +78,7 @@ DECLARE_BUF(line, unsigned char);
 static fields_t in_fields, out_fields;
 static struct field *in_field;
 static line_t in_line;
+static int line_number;
 
 static void new_field(void)
 {
@@ -91,19 +93,28 @@ static void ensure_field(void)
                new_field();
 }
 
+static void warn(struct format *fmt, char *msg)
+{
+       if (!fmt->quiet)
+               fprintf(stderr, "Warning at line %d: %s\n", line_number, msg);
+}
+
 static int csv_read(void)
 {
        int quoted = 0;
-       // FIXME: Complain if closing quote is missing?
        for (;;) {
                int c = getchar();
 restart:
-               if (c < 0)
-                       return !!fields_count(&in_fields);
                if (c == '\r')
                        continue;
-               if (c == '\n')
-                       return 1;
+               if (c < 0 || c == '\n') {
+                       if (quoted)
+                               warn(in_format, "Missing closing quote.");
+                       if (c < 0)
+                               return !!fields_count(&in_fields);
+                       else
+                               return 1;
+               }
                if (quoted) {
                        if (c == in_format->quote) {
                                c = getchar();
@@ -155,6 +166,8 @@ static void csv_write(void)
                        putchar(out_format->quote);
                for (int j=0; j < f->len; j++) {
                        int c = line[f->start_pos + j];
+                       if (c == out_format->fs && !need_quotes)
+                               warn(out_format, "Field separator found inside field and quoting is turned off.");
                        if (c == out_format->quote)
                                putchar(c);
                        putchar(c);
@@ -270,6 +283,7 @@ Formats:\n\
 \n\
 Format parameters:\n\
 -d, --fs=<char>                Delimiter of fields\n\
+-q, --quiet            Do not show warnings\n\
 \n\
 Other options:\n\
     --trim             Trim leading and trailing whitespaces in fields\n\
@@ -285,7 +299,7 @@ static void bad_args(char *msg)
        exit(1);
 }
 
-static const char short_options[] = "cd:tw";
+static const char short_options[] = "cd:qtw";
 
 enum long_options {
        OPT_HELP = 256,
@@ -295,6 +309,7 @@ enum long_options {
 static const struct option long_options[] = {
        { "csv",                0,      NULL,   'c' },
        { "fs",                 1,      NULL,   'd' },
+       { "quiet",              0,      NULL,   'q' },
        { "trim",               0,      NULL,   OPT_TRIM },
        { "tsv",                0,      NULL,   't' },
        { "ws",                 0,      NULL,   'w' },
@@ -363,6 +378,9 @@ int main(int argc, char **argv)
                                else
                                        bad_args("No field delimiter given.");
                                break;
+                       case 'q':
+                               current_format()->quiet = 1;
+                               break;
                        case 't':
                                set_format(FORM_TSV);
                                break;
@@ -394,6 +412,7 @@ int main(int argc, char **argv)
        line_init(&in_line);
 
        for (;;) {
+               line_number++;
                fields_reset(&in_fields);
                line_reset(&in_line);
                in_field = NULL;