C: Showing to the user the number of the annoying line, if the parsing fails.
authorviric <viriketo@gmail.com>
Mon, 15 Mar 2010 23:31:57 +0100
changeset 13 387ab315b0f3
parent 12 5925d3fbb330
child 14 c0ee2a224f73
C: Showing to the user the number of the annoying line, if the parsing fails.
tt.c
--- a/tt.c	Mon Mar 15 23:27:21 2010 +0100
+++ b/tt.c	Mon Mar 15 23:31:57 2010 +0100
@@ -27,6 +27,15 @@
 #include <string.h>
 #include <errno.h>
 
+/* For debug messages */
+static line_counter;
+
+void parsing_error()
+{
+    fprintf(stderr, "Parsing error at line %i\n", line_counter);
+    exit(1);
+}
+
 static FILE *
 open_file(const char *attr)
 {
@@ -118,7 +127,7 @@
 
     ptr_end = strchr(ptr_begin, '-');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &t.tm_year);
     t.tm_year -= 1900;
@@ -126,7 +135,7 @@
     ptr_begin = ptr_end + 1;
     ptr_end = strchr(ptr_begin, '-');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &t.tm_mon);
     /* starting at 0 */
@@ -135,21 +144,21 @@
     ptr_begin = ptr_end + 1;
     ptr_end = strchr(ptr_begin, ' ');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &t.tm_mday);
 
     ptr_begin = ptr_end + 1;
     ptr_end = strchr(ptr_begin, ':');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &t.tm_hour);
 
     ptr_begin = ptr_end + 1;
     ptr_end = strchr(ptr_begin, ':');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &t.tm_min);
 
@@ -208,6 +217,8 @@
     char *prev_task = 0;
     char *prev_time_str = 0;
 
+    line_counter = 1;
+
     /* Read lines */
     do {
         /* Read a line */
@@ -230,7 +241,7 @@
 
             end_date = strchr(buf, '\t');
             if (!end_date)
-                abort();
+                parsing_error();
             *end_date = 0;
             start_task = end_date + 1;
 
@@ -262,6 +273,8 @@
             free(prev_task);
             free(prev_time_str);
         }
+
+        line_counter++;
     }
     while(!feof(f));
 
@@ -294,14 +307,14 @@
 
     ptr_end = strchr(ptr_begin, ':');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &hours);
 
     ptr_begin = ptr_end + 1;
     ptr_end = strchr(ptr_begin, ':');
     if (!ptr_end)
-        abort();
+        parsing_error();
     *ptr_end = '\0';
     sscanf(ptr_begin, "%d", &minutes);
 
@@ -319,6 +332,8 @@
     int accum_time = 0;
     char *str_hms;
 
+    line_counter = 1;
+
     /* Read lines */
     do {
         /* Read a line */
@@ -341,12 +356,13 @@
 
             end = str_until_space(buf);
             if (!end)
-                abort();
+                parsing_error();
             *end = 0;
 
             time = hms2time(buf);
             accum_time += time;
         }
+        ++line_counter;
     }
     while(!feof(stdin));