-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
52 lines (42 loc) · 1 KB
/
file.c
File metadata and controls
52 lines (42 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include "mouselisp.h"
ml_file ml_file_new_str(const char *str) {
return (ml_file){
.tag = ML_FILE_STRING, .str = str, .pos = 0, .buf = {0}, .buf_pos = 0};
}
ml_file ml_file_new_file(FILE *file) {
return (ml_file){
.tag = ML_FILE_FILE, .file = file, .pos = 0, .buf = {0}, .buf_pos = 0};
}
static inline char readc(FILE *file) {
int c = fgetc(file);
return ferror(file) || feof(file) ? '\0' : c;
}
ml_read_char ml_file_read(ml_file *f) {
char c;
if (f->buf_pos > 0) {
bug(f->buf_pos > countof(f->buf));
c = f->buf[--f->buf_pos];
} else {
switch (f->tag) {
case ML_FILE_STRING:
c = f->str[f->pos];
break;
case ML_FILE_FILE:
c = readc(f->file);
break;
}
}
ml_read_char ret = {.c = c, .eof = 0, .pos = f->pos};
if (c == '\0')
ret.eof = 1;
else
f->pos++;
return ret;
}
void ml_file_unread(ml_file *f, char c) {
bug(f->buf_pos >= countof(f->buf));
bug(f->pos == 0);
f->buf[f->buf_pos++] = c;
f->pos--;
}