From 0f1adf5570b17da28577152e26e743e4e031d640 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Nov 2007 23:07:53 +0100 Subject: [PATCH] Added a filter for weeding out comments. --- judge/Makefile | 5 +++-- judge/filter-cmt.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 judge/filter-cmt.c diff --git a/judge/Makefile b/judge/Makefile index b140d68..e231fc2 100644 --- a/judge/Makefile +++ b/judge/Makefile @@ -2,16 +2,17 @@ CC=gcc-4.1.1 CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wredundant-decls -Winline $(DEBUG) -std=gnu99 CFLAGS+=-Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers -all: test-io test-tok judge-tok +all: test-io test-tok judge-tok filter-cmt JLIB=utils.o io.o token.o test-io: test-io.o $(JLIB) test-tok: test-tok.o $(JLIB) judge-tok: judge-tok.o $(JLIB) +filter-cmt: filter-cmt.o $(JLIB) clean:: rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core` - rm -f test-io test-tok judge-tok + rm -f test-io test-tok judge-tok filter-cmt .PHONY: all clean distclean diff --git a/judge/filter-cmt.c b/judge/filter-cmt.c new file mode 100644 index 0000000..12978ad --- /dev/null +++ b/judge/filter-cmt.c @@ -0,0 +1,44 @@ +/* + * A filter removing C++-style comments + * + * (c) 2007 Martin Mares + */ + +#include "judge.h" + +int main(void) +{ + struct stream *i = sopen_fd("stdin", 0); + struct stream *o = sopen_fd("stdout", 1); + + int c, d, nl = 1; + c = sgetc(i); + for (;;) + { + if (c == '/') + { + if ((d = sgetc(i)) == '/') + { + // skipping a comment... + while ((c = sgetc(i)) >= 0 && c != '\n') + ; + if (nl) + continue; + } + else + { + sputc(o, c); + c = d; + } + } + if (c < 0) + break; + sputc(o, c); + nl = (c == '\n'); + c = sgetc(i); + } + + sclose(o); + sclose(i); + return 0; +} -- 2.39.2