2 * A judge comparing shuffled sequences of tokens
4 * (c) 2007 Martin Krulis <bobrik@matfyz.cz>
5 * (c) 2007 Martin Mares <mj@ucw.cz>
18 static int ignore_nl, ignore_empty, ignore_case;
19 static int shuffle_lines, shuffle_words;
21 /*** Token buffer ***/
31 struct tokpage *first_page, *last_page;
32 unsigned int num_tokens;
34 struct tokpage *this_page;
38 #define TOKBUF_PAGE 256
40 static void init_tokbuf(struct tokbuf *tb)
42 memset(tb, 0, sizeof(*tb));
45 static void add_token(struct tokbuf *tb, char *token, int l)
48 struct tokpage *pg = tb->last_page;
49 if (!pg || pg->end - pg->pos < l)
51 int size = TOKBUF_PAGE - sizeof(struct tokbuf);
54 pg = xmalloc(sizeof(struct tokbuf) + size);
56 tb->last_page->next = pg;
62 pg->end = pg->buf + size;
64 memcpy(pg->pos, token, l);
69 static char *get_next(struct tokbuf *tb)
71 struct tokpage *pg = tb->this_page;
72 tb->read_pos += strlen(tb->read_pos) + 1;
73 if (tb->read_pos >= pg->end)
75 tb->this_page = pg = pg->next;
78 tb->read_pos = pg->buf;
83 static char *get_first(struct tokbuf *tb)
85 struct tokpage *pg = tb->first_page;
89 tb->read_pos = pg->buf;
93 /*** Reading of input ***/
102 unsigned int num_toks;
108 struct tok *tok_array;
109 struct line *line_array;
110 unsigned int num_lines;
113 static void read_input(struct tokenizer *t, struct tokbuf *tb)
119 while (tok = get_token(t))
129 add_token(tb, tok, t->toksize);
132 add_token(tb, "", 0);
135 static void slurp(struct tokenizer *t, struct shouffle *s)
141 static void usage(void)
143 fprintf(stderr, "Usage: judge-shuff [<options>] <output> <correct>\n\
146 -n\t\tIgnore newlines and match the whole input as a single line\n\
147 -e\t\tIgnore empty lines\n\
148 -l\t\tShuffle lines (i.e., ignore their order)\n\
149 -w\t\tShuffle words in each line\n\
155 int main(int argc, char **argv)
157 struct tokenizer t1, t2;
160 while ((opt = getopt(argc, argv, "nelwi")) >= 0)
181 if (optind + 2 != argc)
184 tok_init(&t1, sopen_read(argv[optind]));
185 tok_init(&t2, sopen_read(argv[optind+1]));
187 t1.flags = t2.flags = TF_REPORT_LINES;
190 read_input(&t1, &b1);