-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
320 lines (272 loc) · 7.1 KB
/
utils.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string>
#include <initializer_list>
typedef uint8_t u8;
typedef uint16_t u16;
typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
void printBinary(u32 n) {
for (i32 i = 31; i >= 0; i--) {
u32 bit = (n & 1<<i) >> i;
printf("%u", bit);
}
}
struct Input {
char *data;
char *cursor;
char *end;
u64 size;
};
Input loadInput(const char *fileName) {
FILE* fileHandle = fopen(fileName, "r");
if (fileHandle == NULL) {
printf("Unable to load file \"%s\"!\n", fileName);
}
fseek(fileHandle, 0, SEEK_END);
Input input;
input.size = ftell(fileHandle);
input.data = input.cursor = (char*)malloc((input.size+1)*sizeof(char));
input.end = input.cursor+input.size;
rewind(fileHandle);
fread(input.data, sizeof(char), input.size, fileHandle);
fclose(fileHandle);
input.data[input.size] = 0; // null terminate for convenience
// fwrite(input.data, sizeof(char), input.size, stdout);
return input;
}
void printInput(Input *input) {
fwrite(input->data, sizeof(char), input->size, stdout);
}
char getAlpha(Input *input) {
if (isalpha(*input->cursor)) {
char out = *(input->cursor++);
return out;
} else {
u64 position = input->cursor-input->data;
printf("Requested alpha. Byte at %llu is %02x, (%c).\n", position, *input->cursor, *input->cursor);
assert(false);
return 0;
}
}
char peekAlpha(Input *input) {
if (isalpha(*input->cursor)) {
char out = *(input->cursor);
return out;
} else {
u64 position = input->cursor-input->data;
printf("Requested alpha. Byte at %llu is %02x, (%c).\n", position, *input->cursor, *input->cursor);
assert(false);
return 0;
}
}
char peekByte(Input *input) {
char out = *(input->cursor);
return out;
}
char getByte(Input *input) {
char out = *(input->cursor++);
return out;
}
void expectByte(Input *input, char expected) {
if (*input->cursor != expected) {
u64 position = input->cursor-input->data;
printf("Requested %02x (%c). Byte at %llu is %02x, (%c).\n", expected, expected, position, *input->cursor, *input->cursor);
assert(false);
return;
}
input->cursor++;
}
u64 countByte(Input input, u8 byte) {
u64 count = 0;
while (input.cursor != input.end) {
count += (*input.cursor == byte);
input.cursor++;
}
return count;
}
i32 getInt(Input *input) {
if (!(('0' <= *input->cursor && *input->cursor <= '9') || *input->cursor == '+' || *input->cursor == '-')) {
u64 position = input->cursor-input->data;
printf("Requested i32. Byte at %llu is %02x, (%c).\n", position, *input->cursor, *input->cursor);
assert(false);
return 0;
}
i32 sign = 1;
if (*input->cursor == '+') {
input->cursor++;
sign = 1;
} else if (*input->cursor == '-') {
input->cursor++;
sign = -1;
}
i32 number = 0;
while ('0' <= *input->cursor && *input->cursor <= '9') {
number = number*10;
number += (*input->cursor-'0');
input->cursor++;
}
return sign*number;
}
std::string_view getStringUntilByte(Input *input, std::initializer_list<char> skip) {
u32 length = 0;
while (std::find(skip.begin(), skip.end(), *(input->cursor+length)) == skip.end()) {
length++;
}
std::string_view str(input->cursor, length);
input->cursor += length;
return str;
}
std::string_view getStringUntilString(Input *input, const char* string) {
char *found = strstr(input->cursor, string);
if (found != NULL) {
u64 len = (found - input->cursor);
std::string_view result(input->cursor, len);
input->cursor = found;
return result;
} else {
printf("Error in getStringUntilString, couldn't find string \"%s\".", string);
assert(false);
}
}
void skipChars(Input *input, std::initializer_list<char> l) {
while (std::find(l.begin(), l.end(), *input->cursor) != l.end()) {
input->cursor++;
}
}
void expectStr(Input *input, const char *expectedString) {
if (strncmp(input->cursor, expectedString, strlen(expectedString)) != 0) {
u64 position = (input->cursor)-(input->data);
printf("Expected string \"%s\", but string at %llu is \"%.*s\".\n", expectedString, position, (i32)strlen(expectedString), input->cursor);
assert(false);
} else {
input->cursor += strlen(expectedString);
}
}
bool isStr(Input *input, const char *queryString) {
return (strncmp(input->cursor, queryString, strlen(queryString)) == 0);
}
std::string_view getAlphaNumString(Input *input) {
u64 len = 0;
while (isalnum(*(input->cursor+len))) {
len++;
}
std::string_view str(input->cursor, len);
input->cursor += len;
return str;
}
std::string_view getDigitString(Input *input) {
u64 len = 0;
while (isdigit(*(input->cursor+len))) {
len++;
}
std::string_view str(input->cursor, len);
input->cursor += len;
return str;
}
std::string getLine(Input *input) {
u64 len = 0;
while (*(input->cursor+len) != '\n') {
++len;
}
std::string str(input->cursor, len);
input->cursor += (len + 1);
return str;
}
struct vec2 {
i32 x;
i32 y;
vec2 operator+(vec2 b) {
return vec2{x+b.x, y+b.y};
}
vec2 operator*(i32 b) {
return vec2{b*x, b*y};
}
vec2& operator+=(vec2 b) {
x += b.x;
y += b.y;
return *this;
}
};
void rotateClockwise(vec2 *vec, i64 angle) {
while (angle > 0) {
*vec = vec2{vec->y, -vec->x};
angle -= 90;
}
}
void rotateAntiClockwise(vec2 *vec, i64 angle) {
while (angle > 0) {
*vec = vec2{-vec->y, vec->x};
angle -= 90;
}
}
struct Grid {
u32 w, h, stride, offsetX, offsetY;
char *data;
// Grid()
// : w(0), h(0), stride(0), offsetX(0), offsetY(0), data(0) {
// }
Grid()
: Grid(0,0,0,0) {}
Grid(u32 _w, u32 _h, u32 _offsetX, u32 _offsetY)
: w(_w), h(_h), stride(_w), offsetX(_offsetX), offsetY(_offsetY) {
data = (char*)calloc(w*h, sizeof(char));
}
// Grid(char *_data)
// : w(0), h(0),
// data(_data)
// ~Grid() { if (data) {free(data);} }
void insert(i32 x, i32 y, char val) {
data[(offsetX + x) + w*(offsetY + y)] = val;
}
void insert(vec2 v, char val) {
insert(v.x, v.y, val);
}
char get(i32 x, i32 y) {
return data[(offsetX + x) + stride*(offsetY + y)];
}
char get(vec2 v) { return get(v.x, v.y); }
char& operator()(i32 x, i32 y) {
return data[(offsetX + x) + stride*(offsetY + y)];
}
char& operator()(vec2 v) {
return operator()(v.x, v.y);
}
void operator=(std::initializer_list<char> l) {
int i = 0;
for (auto el: l) {
data[i++] = el;
}
}
};
// Grid loadGrid(const char *fileName) {
// FILE* fileHandle = fopen(fileName, "r");
// fseek(fileHandle, 0, SEEK_END);
// size = ftell(fileHandle);
// data = cursor = (char*)malloc((input.size+1)*sizeof(char));
// end = cursor+size;
// // we assume that the input is actually a grid
// u32 w = 0;
// while (*cursor != '\n') {
// w++;
// cursor++;
// }
// rewind(fileHandle);
// fread(input.data, sizeof(char), input.size, fileHandle);
// fclose(fileHandle);
// input.data[input.size] = 0; // null terminate for convenience
// // fwrite(input.data, sizeof(char), input.size, stdout);
// return input;
// }
void printGrid(Grid *grid) {
for (int y = 0; y < grid->h; y++) {
for (int x = 0; x < grid->w; x++) {
printf("%d ", grid->data[x + grid->w*y]);
}
printf("\n");
}
}