]> mj.ucw.cz Git - moe.git/commitdiff
Improved parsing of switches.
authorMartin Mares <mj@ucw.cz>
Sat, 17 Nov 2007 22:02:44 +0000 (23:02 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 17 Nov 2007 22:02:44 +0000 (23:02 +0100)
judge/judge-tok.c

index bb4e95f0ed08bb4990bfab5b131998f20d2498c5..017f6081339c065efd039c56583b77ee20a5ad99 100644 (file)
@@ -6,33 +6,57 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <getopt.h>
 
 #include "judge.h"
 
+static int ignore_nl, ignore_trailing_nl;
+
 static int trailing_nl(struct tokenizer *t)
 {
   // Ignore empty lines at the end of file
-  if (t->token[0])
+  if (t->token[0] || !ignore_trailing_nl)
     return 0;
   t->flags &= ~TF_REPORT_LINES;
   return !get_token(t);
 }
 
+static void usage(void)
+{
+  fprintf(stderr, "Usage: judge-tok [<options>] <file1> <file2>\n\
+\n\
+Options:\n\
+-n\t\tIgnore newlines\n\
+-t\t\tIgnore newlines at the end of file\n\
+");
+  exit(2);
+}
+
 int main(int argc, char **argv)
 {
   struct tokenizer t1, t2;
-  int report_lines = 1;
-
-  if (argc != 3 && argc != 4)
-    die("Usage: judge-tok [-n] <file1> <file2>");
+  int opt;
 
-  // Check for -n parameter
-  report_lines = !(argc == 4 && !strcmp(argv[1], "-n"));
+  while ((opt = getopt(argc, argv, "nt")) >= 0)
+    switch (opt)
+      {
+      case 'n':
+       ignore_nl++;
+       break;
+      case 't':
+       ignore_trailing_nl++;
+       break;
+      default:
+       usage();
+      }
+  if (optind + 2 != argc)
+    usage();
 
-  tok_init(&t1, sopen_read(argv[argc-2]));
-  tok_init(&t2, sopen_read(argv[argc-1]));
-  if (report_lines)
+  tok_init(&t1, sopen_read(argv[optind]));
+  tok_init(&t2, sopen_read(argv[optind+1]));
+  if (!ignore_nl)
     t1.flags = t2.flags = TF_REPORT_LINES;
 
   for (;;)