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
|
/* SPDX-FileCopyrightText: Copyright (C) 2024 ZeldaRET */
/* SPDX-License-Identifier: CC0-1.0 */
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "util.h"
// TODO ideally we should be collecting all errors and displaying them all before exiting
NORETURN void
error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\x1b[91m"
"Error: "
"\x1b[97m");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\x1b[0m"
"\n");
va_end(ap);
exit(EXIT_FAILURE);
}
void
warning(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\x1b[95m"
"Warning: "
"\x1b[97m");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\x1b[0m"
"\n");
va_end(ap);
}
void *
util_read_whole_file(const char *filename, size_t *size_out)
{
FILE *file = fopen(filename, "rb");
void *buffer = NULL;
size_t size;
if (file == NULL)
error("failed to open file '%s' for reading: %s", filename, strerror(errno));
// get size
fseek(file, 0, SEEK_END);
size = ftell(file);
// if the file is empty, return NULL buffer and 0 size
if (size != 0) {
// allocate buffer
buffer = malloc(size + 1);
if (buffer == NULL)
error("could not allocate buffer for file '%s'", filename);
// read file
fseek(file, 0, SEEK_SET);
if (fread(buffer, size, 1, file) != 1)
error("error reading from file '%s': %s", filename, strerror(errno));
// null-terminate the buffer (in case of text files)
((char *)buffer)[size] = '\0';
}
fclose(file);
if (size_out != NULL)
*size_out = size;
return buffer;
}
void
util_write_whole_file(const char *filename, const void *data, size_t size)
{
FILE *file = fopen(filename, "wb");
if (file == NULL)
error("failed to open file '%s' for writing: %s", filename, strerror(errno));
if (fwrite(data, size, 1, file) != 1)
error("error writing to file '%s': %s", filename, strerror(errno));
fclose(file);
}
bool
str_is_c_identifier(const char *str)
{
// A C language identifier must:
// - ONLY contain [_, abc..xyz, ABC..XYZ, 0..9] (we do not support unicode or extensions like $)
// - NOT be a keyword
// - NOT start with a digit [0..9]
static const char *const c_kwds[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"inline", "int", "long", "register", "restrict", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void",
"volatile", "while",
"_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary", "_Noreturn",
"_Static_assert", "_Thread_local",
};
if (str == NULL) {
return false;
}
if (isdigit(str[0])) {
// Starts with a digit, fail
return false;
}
size_t len = strlen(str);
for (size_t i = 0; i < len; i++) {
char c = str[i];
bool alpha = isalpha(c);
bool digit = isdigit(c);
bool uscore = c == '_';
if (!(alpha || digit || uscore)) {
// Contains bad character, fail
return false;
}
}
for (size_t i = 0; i < ARRAY_COUNT(c_kwds); i++) {
if (strequ(str, c_kwds[i])) {
// Matched a C keyword, fail
return false;
}
}
return true;
}
|