swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdbool.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #define MIN(a, b) (a) > (b) ? (b) : (a) |
| 7 | |
swissChili | d24cd20 | 2021-07-02 13:30:58 -0700 | [diff] [blame] | 8 | /// Virtual class representing an input stream. Subclasses must implement every |
| 9 | /// function. |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 10 | struct istream |
| 11 | { |
| 12 | void *data; |
| 13 | |
swissChili | d24cd20 | 2021-07-02 13:30:58 -0700 | [diff] [blame] | 14 | /// Returns -1 on error |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 15 | int (*peek)(struct istream *s); |
swissChili | d24cd20 | 2021-07-02 13:30:58 -0700 | [diff] [blame] | 16 | /// Returns -1 on error |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 17 | int (*get)(struct istream *s); |
| 18 | |
| 19 | int (*read)(struct istream *s, char *buffer, int size); |
| 20 | |
| 21 | void (*showpos)(struct istream *s, FILE *out); |
swissChili | d24cd20 | 2021-07-02 13:30:58 -0700 | [diff] [blame] | 22 | |
| 23 | void (*getpos)(struct istream *s, int *line, char **name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | struct istream *new_stristream(char *str, int length); |
| 27 | // same as above but null terminated |
| 28 | struct istream *new_stristream_nt(char *str); |
| 29 | void del_stristream(struct istream *stristream); |
| 30 | |
| 31 | struct istream *new_fistream(char *path, bool binary); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 32 | void del_fistream(struct istream *fistream); |