-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxs_io.h
90 lines (59 loc) · 1.44 KB
/
xs_io.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* copyright (c) 2022 grunfink - MIT license */
#ifndef _XS_IO_H
#define _XS_IO_H
d_char *xs_readline(FILE *f);
d_char *xs_read(FILE *f, int *size);
d_char *xs_readall(FILE *f);
#ifdef XS_IMPLEMENTATION
d_char *xs_readline(FILE *f)
/* reads a line from a file */
{
d_char *s = NULL;
errno = 0;
/* don't even try on eof */
if (!feof(f)) {
int c;
s = xs_str_new(NULL);
while ((c = fgetc(f)) != EOF) {
unsigned char rc = c;
s = xs_append_m(s, (char *)&rc, 1);
if (c == '\n')
break;
}
}
return s;
}
d_char *xs_read(FILE *f, int *sz)
/* reads up to size bytes from f */
{
d_char *s = NULL;
int size = *sz;
int rdsz = 0;
errno = 0;
while (size > 0 && !feof(f)) {
char tmp[4096];
int n, r;
if ((n = sizeof(tmp)) > size)
n = size;
r = fread(tmp, 1, n, f);
/* open room */
s = xs_realloc(s, rdsz + r);
/* copy read data */
memcpy(s + rdsz, tmp, r);
rdsz += r;
size -= r;
}
/* null terminate, just in case it's treated as a string */
s = xs_realloc(s, _xs_blk_size(rdsz + 1));
s[rdsz] = '\0';
*sz = rdsz;
return s;
}
d_char *xs_readall(FILE *f)
/* reads the rest of the file into a string */
{
int size = XS_ALL;
return xs_read(f, &size);
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_IO_H */