]> mj.ucw.cz Git - moe.git/blob - judge/judge-tok.c
Added a simple judge on sequences of tokens.
[moe.git] / judge / judge-tok.c
1 /*
2  *      A judge comparing two sequences of tokens
3  *
4  *      (c) 2007 Martin Krulis <bobrik@matfyz.cz>
5  *      (c) 2007 Martin Mares <mj@ucw.cz>
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "judge.h"
12
13 static int trailing_nl(struct tokenizer *t)
14 {
15   // Ignore empty lines at the end of file
16   if (t->token[0])
17     return 0;
18   t->flags &= ~TF_REPORT_LINES;
19   return !get_token(t);
20 }
21
22 int main(int argc, char **argv)
23 {
24   struct tokenizer t1, t2;
25   int report_lines = 1;
26
27   if (argc != 3 && argc != 4)
28     die("Usage: judge-tok [-n] <file1> <file2>");
29
30   // Check for -n parameter
31   report_lines = !(argc == 4 && !strcmp(argv[1], "-n"));
32
33   tok_init(&t1, sopen_read(argv[argc-2]));
34   tok_init(&t2, sopen_read(argv[argc-1]));
35   if (report_lines)
36     t1.flags = t2.flags = TF_REPORT_LINES;
37
38   for (;;)
39     {
40       char *a = get_token(&t1), *b = get_token(&t2);
41       if (!a)
42         {
43           if (b && !trailing_nl(&t2))
44             tok_err(&t1, "Ends too early");
45           break;
46         }
47       else if (!b)
48         {
49           if (a && !trailing_nl(&t1))
50             tok_err(&t2, "Garbage at the end");
51           break;
52         }
53       else if (strcmp(a, b))
54         tok_err(&t1, "Found <%s>, expected <%s>", a, b);
55     }
56
57   return 0;
58 }