]> git.lizzy.rs Git - dragonstd.git/blob - number.c
Extract source from dragonblocks alpha
[dragonstd.git] / number.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <endian.h>
4 #include <poll.h>
5 #include "number.h"
6
7 bool read_full(int fd, char *buffer, size_t size)
8 {
9         size_t n_read_total = 0;
10         int n_read;
11         while (n_read_total < size) {
12                 if ((n_read = read(fd, buffer + n_read_total, size - n_read_total)) == -1) {
13                         perror("read");
14                         return false;
15                 }
16                 n_read_total += n_read;
17         }
18         return true;
19 }
20
21 #define htobe8(x) x
22 #define be8toh(x) x
23
24 #define READVEC(type, n) \
25         type buf[n]; \
26         for (int i = 0; i < n; i++) { \
27                 if (! read_ ## type(fd, &buf[i])) \
28                         return false; \
29         }
30
31 #define WRITEVEC(type, n) \
32         for (int i = 0; i < n; i++) { \
33                 if (! write_ ## type(fd, vec[i])) \
34                         return false; \
35         } \
36         return true;
37
38 #define DEFVEC(type) \
39         bool read_v2 ## type(int fd, v2 ## type *ptr) \
40         { \
41                 READVEC(type, 2) \
42                 ptr->x = buf[0]; \
43                 ptr->y = buf[1]; \
44                 return true; \
45         } \
46         bool write_v2 ## type(int fd, v2 ## type val) \
47         { \
48                 type vec[2] = {val.x, val.y}; \
49                 WRITEVEC(type, 2) \
50         } \
51         bool v2 ## type ## _equals(v2 ## type a, v2 ## type b) \
52         { \
53                 return a.x == b.x && a.y == b.y; \
54         } \
55         v2 ## type v2 ## type ## _add(v2 ## type a, v2 ## type b) \
56         { \
57                 return (v2 ## type) {a.x + b.x, a.y + b.y}; \
58         } \
59         bool read_v3 ## type(int fd, v3 ## type *ptr) \
60         { \
61                 READVEC(type, 3) \
62                 ptr->x = buf[0]; \
63                 ptr->y = buf[1]; \
64                 ptr->z = buf[2]; \
65                 return true; \
66         } \
67         bool write_v3 ## type(int fd, v3 ## type val) \
68         { \
69                 type vec[3] = {val.x, val.y, val.z}; \
70                 WRITEVEC(type, 3) \
71         } \
72         bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
73         { \
74                 return a.x == b.x && a.y == b.y && a.z == b.z; \
75         } \
76         v3 ## type v3 ## type ## _add(v3 ## type a, v3 ## type b) \
77         { \
78                 return (v3 ## type) {a.x + b.x, a.y + b.y, a.z + b.z}; \
79         }
80
81 #define DEFTYP(type, bits) \
82         bool read_ ## type(int fd, type *buf) \
83         { \
84                 u ## bits encoded; \
85                 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
86                         return false; \
87                 *buf = be ## bits ## toh(encoded); \
88                 return true; \
89         } \
90         bool write_ ## type(int fd, type val) \
91         { \
92                 u ## bits encoded = htobe ## bits(val); \
93                 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
94                         perror("write"); \
95                         return false; \
96                 } \
97                 return true; \
98         } \
99         DEFVEC(type)
100
101 #define DEFTYPES(bits) \
102         DEFTYP(s ## bits, bits) \
103         DEFTYP(u ## bits, bits)
104
105 DEFTYPES(8)
106 DEFTYPES(16)
107 DEFTYPES(32)
108 DEFTYPES(64)
109
110 #define DEFFLOAT(type) \
111         bool read_ ## type(int fd, type *buf) \
112         { \
113                 if (! read_full(fd, (char *) buf, sizeof(type))) \
114                         return false; \
115                 return true; \
116         } \
117         bool write_ ## type(int fd, type val) \
118         { \
119                 if (write(fd, &val, sizeof(val)) == -1) { \
120                         perror("write"); \
121                         return false; \
122                 } \
123                 return true; \
124         } \
125         DEFVEC(type)
126
127 DEFFLOAT(f32)
128 DEFFLOAT(f64)