7 bool read_full(int fd, char *buffer, size_t size)
9 size_t n_read_total = 0;
11 while (n_read_total < size) {
12 if ((n_read = read(fd, buffer + n_read_total, size - n_read_total)) == -1) {
16 n_read_total += n_read;
24 #define READVEC(type, n) \
26 for (int i = 0; i < n; i++) { \
27 if (! read_ ## type(fd, &buf[i])) \
31 #define WRITEVEC(type, n) \
32 for (int i = 0; i < n; i++) { \
33 if (! write_ ## type(fd, vec[i])) \
38 #define DEFVEC(type) \
39 bool read_v2 ## type(int fd, v2 ## type *ptr) \
46 bool write_v2 ## type(int fd, v2 ## type val) \
48 type vec[2] = {val.x, val.y}; \
51 bool v2 ## type ## _equals(v2 ## type a, v2 ## type b) \
53 return a.x == b.x && a.y == b.y; \
55 bool read_v3 ## type(int fd, v3 ## type *ptr) \
63 bool write_v3 ## type(int fd, v3 ## type val) \
65 type vec[3] = {val.x, val.y, val.z}; \
68 bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
70 return a.x == b.x && a.y == b.y && a.z == b.z; \
73 #define DEFTYP(type, bits) \
74 bool read_ ## type(int fd, type *buf) \
77 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
79 *buf = be ## bits ## toh(encoded); \
82 bool write_ ## type(int fd, type val) \
84 u ## bits encoded = htobe ## bits(val); \
85 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
93 #define DEFTYPES(bits) \
94 DEFTYP(s ## bits, bits) \
95 DEFTYP(u ## bits, bits)
102 #define DEFFLOAT(type) \
103 bool read_ ## type(int fd, type *buf) \
105 if (! read_full(fd, (char *) buf, sizeof(type))) \
109 bool write_ ## type(int fd, type val) \
111 if (write(fd, &val, sizeof(val)) == -1) { \