3 #ifdef DRAGONTYPE_ENDIAN_HEADER
4 #include DRAGONTYPE_ENDIAN_HEADER
11 bool read_full(int fd, char *buffer, size_t size)
13 size_t n_read_total = 0;
15 while (n_read_total < size) {
16 if ((n_read = read(fd, buffer + n_read_total, size - n_read_total)) == -1) {
20 n_read_total += n_read;
28 #define READVEC(type, n) \
30 for (int i = 0; i < n; i++) { \
31 if (! read_ ## type(fd, &buf[i])) \
35 #define WRITEVEC(type, n) \
36 for (int i = 0; i < n; i++) { \
37 if (! write_ ## type(fd, vec[i])) \
42 #define DEFVEC(type) \
43 bool read_v2 ## type(int fd, v2 ## type *ptr) \
50 bool write_v2 ## type(int fd, v2 ## type val) \
52 type vec[2] = {val.x, val.y}; \
55 bool v2 ## type ## _equals(v2 ## type a, v2 ## type b) \
57 return a.x == b.x && a.y == b.y; \
59 v2 ## type v2 ## type ## _add(v2 ## type a, v2 ## type b) \
61 return (v2 ## type) {a.x + b.x, a.y + b.y}; \
63 bool read_v3 ## type(int fd, v3 ## type *ptr) \
71 bool write_v3 ## type(int fd, v3 ## type val) \
73 type vec[3] = {val.x, val.y, val.z}; \
76 bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
78 return a.x == b.x && a.y == b.y && a.z == b.z; \
80 v3 ## type v3 ## type ## _add(v3 ## type a, v3 ## type b) \
82 return (v3 ## type) {a.x + b.x, a.y + b.y, a.z + b.z}; \
84 bool read_v4 ## type(int fd, v4 ## type *ptr) \
93 bool write_v4 ## type(int fd, v4 ## type val) \
95 type vec[4] = {val.x, val.y, val.z, val.w}; \
98 bool v4 ## type ## _equals(v4 ## type a, v4 ## type b) \
100 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; \
102 v4 ## type v4 ## type ## _add(v4 ## type a, v4 ## type b) \
104 return (v4 ## type) {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; \
107 #define DEFTYP(type, bits) \
108 bool read_ ## type(int fd, type *buf) \
111 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
113 *buf = be ## bits ## toh(encoded); \
116 bool write_ ## type(int fd, type val) \
118 u ## bits encoded = htobe ## bits(val); \
119 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
127 #define DEFTYPES(bits) \
128 DEFTYP(s ## bits, bits) \
129 DEFTYP(u ## bits, bits)
136 #define DEFFLOAT(type) \
137 bool read_ ## type(int fd, type *buf) \
139 if (! read_full(fd, (char *) buf, sizeof(type))) \
143 bool write_ ## type(int fd, type val) \
145 if (write(fd, &val, sizeof(val)) == -1) { \