summaryrefslogtreecommitdiff
path: root/io.c
blob: bb0ced788f1f78ae191983943e7ce28c612ca565 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
static void write_u8(FILE *fp, u8 x) {
	putc(x, fp);
}

static void write_u16(FILE *fp, u16 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_u32(FILE *fp, u32 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_u64(FILE *fp, u64 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_i8(FILE *fp, i8 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_i16(FILE *fp, i16 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_i32(FILE *fp, i32 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_i64(FILE *fp, i64 x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_float(FILE *fp, float x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_double(FILE *fp, double x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_char(FILE *fp, char x) {
	fwrite(&x, sizeof x, 1, fp);
}

static void write_bool(FILE *fp, bool x) {
	putc(x, fp);
}

static u8 read_u8(FILE *fp) {
	return (u8)getc(fp);
}

static u16 read_u16(FILE *fp) {
	u16 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static u32 read_u32(FILE *fp) {
	u32 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static u64 read_u64(FILE *fp) {
	u64 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static i8 read_i8(FILE *fp) {
	i8 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static i16 read_i16(FILE *fp) {
	i16 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static i32 read_i32(FILE *fp) {
	i32 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static i64 read_i64(FILE *fp) {
	i64 x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static float read_float(FILE *fp) {
	float x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static double read_double(FILE *fp) {
	double x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static char read_char(FILE *fp) {
	char x = 0;
	fread(&x, sizeof x, 1, fp);
	return x;
}

static bool read_bool(FILE *fp) {
	return (bool)getc(fp);
}

static void write_cstr(FILE *fp, char const *cstr) {
	fwrite(cstr, 1, strlen(cstr) + 1, fp);
}

static void read_cstr(FILE *fp, char *out, size_t out_sz) {
	char *p = out, *end = out + out_sz;
	while (1) {
		if (p >= end - 1) {
			*p = '\0';
			break;
		}
		int c = getc(fp);
		if (c == 0 || c == EOF) {
			*p = '\0';
			break;
		}
		*p++ = (char)c;
	}
}