2 * A simple library for Moe judges
4 * (c) 2007 Martin Mares <mj@ucw.cz>
12 #define NONRET __attribute__((noreturn))
17 /* utils.c: Utility functions */
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);
23 /* io.c: Simple buffered I/O streams */
28 unsigned char *pos, *stop, *end;
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);
38 int sgetc_slow(struct stream *s);
39 int speekc_slow(struct stream *s);
41 static inline int sgetc(struct stream *s)
43 return (s->pos < s->stop) ? *s->pos++ : sgetc_slow(s);
46 static inline int speekc(struct stream *s)
48 return (s->pos < s->stop) ? *s->pos : speekc_slow(s);
51 static inline void sputc(struct stream *s, int c)
53 if (s->pos >= s->stop)
58 static inline void sungetc(struct stream *s)
63 /* token.c: Tokenization of input */
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
75 enum tokenizer_flags {
76 TF_REPORT_LINES = 1, // Report an empty token at the end of each line
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);
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);
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);