]> mj.ucw.cz Git - eval.git/blob - judge/filter-cmt.c
Added a filter for weeding out comments.
[eval.git] / judge / filter-cmt.c
1 /*
2  *      A filter removing C++-style comments
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "judge.h"
8
9 int main(void)
10 {
11   struct stream *i = sopen_fd("stdin", 0);
12   struct stream *o = sopen_fd("stdout", 1);
13
14   int c, d, nl = 1;
15   c = sgetc(i);
16   for (;;)
17     {
18       if (c == '/')
19         {
20           if ((d = sgetc(i)) == '/')
21             {
22               // skipping a comment...
23               while ((c = sgetc(i)) >= 0 && c != '\n')
24                 ;
25               if (nl)
26                 continue;
27             }
28           else
29             {
30               sputc(o, c);
31               c = d;
32             }
33         }
34       if (c < 0)
35         break;
36       sputc(o, c);
37       nl = (c == '\n');
38       c = sgetc(i);
39     }
40
41   sclose(o);
42   sclose(i);
43   return 0;
44 }