]> mj.ucw.cz Git - eval.git/blob - judge/judge.h
Eval: Added STACK_LIMIT.
[eval.git] / judge / judge.h
1 /*
2  *      A simple library for MO-Eval judges
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <sys/types.h>
8
9 /* GCC extensions */
10
11 #ifdef __GNUC__
12 #define NONRET __attribute__((noreturn))
13 #else
14 #define NONRET
15 #endif
16
17 /* utils.c: Utility functions */
18
19 void die(char *msg, ...) NONRET;                /* Dies with exit code 2 (judge error) */
20 void *xmalloc(size_t size);
21 void *xrealloc(void *p, size_t size);
22
23 /* io.c: Simple buffered I/O streams */
24
25 struct stream {
26   char *name;
27   int fd;
28   unsigned char *pos, *stop, *end;
29   unsigned char buf[];
30 };
31
32 struct stream *sopen_read(char *name);
33 struct stream *sopen_write(char *name);
34 struct stream *sopen_fd(char *name, int fd);
35 void sflush(struct stream *s);
36 void sclose(struct stream *s);
37
38 int sgetc_slow(struct stream *s);
39 int speekc_slow(struct stream *s);
40
41 static inline int sgetc(struct stream *s)
42 {
43   return (s->pos < s->stop) ? *s->pos++ : sgetc_slow(s);
44 }
45
46 static inline int speekc(struct stream *s)
47 {
48   return (s->pos < s->stop) ? *s->pos : speekc_slow(s);
49 }
50
51 static inline void sputc(struct stream *s, int c)
52 {
53   if (s->pos >= s->stop)
54     sflush(s);
55   *s->pos++ = c;
56 }
57
58 static inline void sungetc(struct stream *s)
59 {
60   s->pos--;
61 }
62
63 /* token.c: Tokenization of input */
64
65 struct tokenizer {
66   struct stream *stream;
67   unsigned int bufsize;         // Allocated buffer size
68   unsigned int maxsize;         // Maximal allowed token size
69   unsigned int flags;           // TF_xxx
70   unsigned char *token;         // Current token (in the buffer)
71   unsigned int toksize;         // ... and its size
72   int line;                     // ... and line number at its end
73 };
74
75 enum tokenizer_flags {
76   TF_REPORT_LINES = 1,          // Report an empty token at the end of each line
77 };
78
79 void tok_init(struct tokenizer *t, struct stream *s);
80 void tok_cleanup(struct tokenizer *t);
81 void tok_err(struct tokenizer *t, char *msg, ...) NONRET;
82 char *get_token(struct tokenizer *t);
83
84 // Parsing functions
85 int to_int(struct tokenizer *t, int *x);
86 int to_uint(struct tokenizer *t, unsigned int *x);
87 int to_long(struct tokenizer *t, long int *x);
88 int to_ulong(struct tokenizer *t, unsigned long int *x);
89 int to_double(struct tokenizer *t, double *x);
90 int to_long_double(struct tokenizer *t, long double *x);
91
92 // get_token() + parse or die
93 int get_int(struct tokenizer *t);
94 unsigned int get_uint(struct tokenizer *t);
95 long int get_long(struct tokenizer *t);
96 unsigned long int get_ulong(struct tokenizer *t);
97 double get_double(struct tokenizer *t);
98 long double get_long_double(struct tokenizer *t);
99 void get_nl(struct tokenizer *t);