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 v2 ## type v2 ## type ## _add(v2 ## type a, v2 ## type b) \
57 return (v2 ## type) {a.x + b.x, a.y + b.y}; \
59 bool read_v3 ## type(int fd, v3 ## type *ptr) \
67 bool write_v3 ## type(int fd, v3 ## type val) \
69 type vec[3] = {val.x, val.y, val.z}; \
72 bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
74 return a.x == b.x && a.y == b.y && a.z == b.z; \
76 v3 ## type v3 ## type ## _add(v3 ## type a, v3 ## type b) \
78 return (v3 ## type) {a.x + b.x, a.y + b.y, a.z + b.z}; \
81 #define DEFTYP(type, bits) \
82 bool read_ ## type(int fd, type *buf) \
85 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
87 *buf = be ## bits ## toh(encoded); \
90 bool write_ ## type(int fd, type val) \
92 u ## bits encoded = htobe ## bits(val); \
93 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
101 #define DEFTYPES(bits) \
102 DEFTYP(s ## bits, bits) \
103 DEFTYP(u ## bits, bits)
110 #define DEFFLOAT(type) \
111 bool read_ ## type(int fd, type *buf) \
113 if (! read_full(fd, (char *) buf, sizeof(type))) \
117 bool write_ ## type(int fd, type val) \
119 if (write(fd, &val, sizeof(val)) == -1) { \