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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "any_log.h"
char *color_to_string(color_t *color)
{
unsigned int r = color->r * 255,
g = color->g * 255,
b = color->b * 255,
a = color->a * 255;
char buffer[32];
int n = snprintf(buffer, 32, "#%02x%02x%02x%02x", r, g, b, a);
return strslice(buffer, 0, n);
}
void color_print(FILE *stream, color_t *color)
{
unsigned int r = color->r * 255,
g = color->g * 255,
b = color->b * 255,
a = color->a * 255;
fprintf(stream, "#%02x%02x%02x%02x", r, g, b, a);
}
const struct timespec timespec_from_ms(long ms)
{
struct timespec ts = {
.tv_sec = ms / 1000,
.tv_nsec = (ms % 1000) * 1000000ul,
};
return ts;
}
const long timespec_to_ms(struct timespec ts)
{
return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000ul);
}
struct timespec timespec_diff(struct timespec a, struct timespec b)
{
bool over = (a.tv_nsec - b.tv_nsec) < 0;
struct timespec ts = {
.tv_sec = a.tv_sec - b.tv_sec - over,
.tv_nsec = a.tv_nsec - b.tv_nsec + over * 1000000000ul,
};
return ts;
}
struct timespec timespec_add(struct timespec a, struct timespec b)
{
bool over = (a.tv_nsec + b.tv_nsec) > 1000000000ul;
struct timespec ts = {
.tv_sec = a.tv_sec + b.tv_sec + over,
.tv_nsec = a.tv_nsec + b.tv_nsec - over * 1000000000ul,
};
return ts;
}
struct timespec timespec_div(struct timespec ts, int n)
{
ts.tv_nsec /= n;
ts.tv_nsec += ((ts.tv_sec % n) * 1000000000ul) / n;
ts.tv_sec /= n;
return ts;
}
bool timespec_greater(struct timespec a, struct timespec b)
{
return a.tv_sec > b.tv_sec
|| (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec);
}
bool timespec_zero(struct timespec ts)
{
return ts.tv_sec == 0 && ts.tv_nsec == 0;
}
void timespec_print(FILE *stream, struct timespec *ts)
{
fprintf(stream, "%ld.%.9ld", ts->tv_sec, ts->tv_nsec);
}
bool check_rect(int px, int py, int x, int y, int w, int h)
{
return px >= x && px <= x + w && py >= y && py <= y + h;
}
bool check_circle(int px, int py, int x, int y, int r)
{
int dx = x - px;
int dy = y - py;
return (dx * dx + dy * dy) <= r * r;
}
bool check_capsule(int px, int py, int x, int y, int w, int h)
{
assert(w >= h);
int radius = h / 2;
// Trivial case
if (w == h)
return check_circle(px, py, x + radius, y + radius, radius);
// General case
return check_circle(px, py, x + radius, y + radius, radius)
|| check_circle(px, py, x + w - radius, y + radius, radius)
|| check_rect(px, py, x + radius, y, w - 2 * radius, h);
}
char *strslice(const char *string, size_t start, size_t end)
{
if (string == NULL)
return NULL;
char *result = malloc(end - start + 1);
memcpy(result, string + start, end - start);
result[end - start] = '\0';
return result;
}
char *strcopy(const char *string)
{
if (string == NULL)
return NULL;
return strslice(string, 0, strlen(string));
}
bool strfind(const char *string, const char *cases[])
{
for (int i = 0; cases[i] != NULL; i++) {
if (strstr(string, cases[i]) != NULL)
return true;
}
return false;
}
size_t strprefix(const char *string, const char *prefix)
{
size_t i = 0;
for ( ; prefix[i] != '\0'; i++) {
if (string[i] != prefix[i])
return 0;
}
return i;
}
// FIXME: Slow and inefficient
char *strformat(const char *string, char delim, const char **keys, const char **values)
{
size_t length = strlen(string);
size_t size = length;
size_t n = 0;
char *buffer = malloc(size);
for (size_t i = 0; i < length; i++) {
if (string[i] == delim && i + 2 < length && string[i + 1] == '{') {
bool found = false;
size_t j = i + 2;
for ( ; j < length; j++) {
if (string[j] == '}') {
found = true;
break;
}
}
if (found) {
for (int k = 0; keys[k] != NULL; k++) {
if (!strncmp(string + i + 2, keys[k], j - i - 2)) {
size_t vl = strlen(values[k]);
while (n + vl >= size) {
size *= 1.5;
buffer = realloc(buffer, size);
if (buffer == NULL)
return NULL;
}
memcpy(buffer + n, values[k], vl);
n += vl;
i = j;
goto next;
}
}
}
}
if (n + 1 >= size) {
size *= 1.5;
buffer = realloc(buffer, size);
if (buffer == NULL)
return NULL;
}
buffer[n++] = string[i];
next:
}
buffer[n] = '\0';
return buffer;
}
void strfree(char **list)
{
if (list == NULL)
return;
for (char **head = list; *head != NULL; head++)
free(*head);
free(list);
}
|