blob: 093297862ab2a4894112d4bbd67b51efca24f213 (
plain)
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
|
#include "input.h"
// Converts decimal values of a uint32_t argument to hex.
uint32_t hex_to_decimal(const uint32_t decimal) {
uint32_t ret = 0;
for (size_t i = 0; i < sizeof(uint32_t); ++i) { // Iterate through 4 bytes.
uint32_t tmp = (decimal & (0xFFu << 8 * i)) >> 8 * i;
if (isalpha(tmp)) {
tmp -= 'a';
tmp += 10;
} else if (isdigit(tmp)) {
tmp -= '0';
}
// There should be no other cases if the input is hex.
ret |= (tmp << 4 * i);
}
return ret;
}
// Returns the next instruction in hex. Returns 0 if EOF reached.
uint32_t get_next_instruction(FILE *const iptr) {
uint32_t ret = 0;
int i = 0;
for (int c = fgetc(iptr); c != '\n'; c = fgetc(iptr), ++i) {
if (c == EOF) {
return 0u;
}
uint32_t tmp = hex_to_decimal((uint32_t)c);
ret |= (tmp << (28 - i * 4));
}
// We have to take into account the potential for the hex value to not be
// "full", ie we may read abc instead of 00000abc.
ret >>= (32 - i * 4);
return ret;
}
// Sets the iptr to the previous instruction, which sits on the newline so that
// later calls to get_next_instruction work as expected.
// Also, if unable to find a previous instruction, sets the iptr to EOF.
void goto_previous_instruction(FILE *const iptr) {
int c = 0;
fseek(iptr, -2L, SEEK_CUR);
while (ftell(iptr)) {
c = fgetc(iptr);
if (c == '\n') {
return;
}
fseek(iptr, -2L, SEEK_CUR);
}
// Start of file reached, out of bounds, so set EOF.
fseek(iptr, 0L, SEEK_END);
}
|