-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
tools.c
181 lines (163 loc) · 3.12 KB
/
tools.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
#ifndef WIN32
#include <unistd.h>
#ifndef __EMSCRIPTEN__
#include <zlib.h>
#endif /* __EMSCRIPTEN */
#endif
#include "tools.h"
uint8_t *load_file(const char *filename, size_t *size_out) {
FILE *f = fopen(filename, "rb");
if(!f) {
fprintf(
stderr,
"Could not open %s: %s (%d)\n",
filename,
strerror(errno),
errno
);
return NULL;
}
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
uint8_t *data = malloc(size);
if(!data) {
fprintf(
stderr,
"Could not allocate %zu bytes for %s: %s (%d)\n",
size, filename, strerror(errno), errno
);
fclose(f);
return NULL;
}
rewind(f);
fread(data, 1, size, f);
fclose(f);
if(size_out) *size_out = size;
return data;
}
#ifdef __MINGW32__
int alphasort(const struct dirent **a, const struct dirent **b) {
return strcmp((*a)->d_name, (*b)->d_name);
}
#endif
int gcd(int a, int b) {
int c = a % b;
while(c > 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
int find_pdx_file(const char *mdx_file_path, const char *pdx_filename, char *out, int out_len) {
char *s = strdup(mdx_file_path);
char *dn = dirname(s);
char buf[256];
struct stat st;
*out = 0;
for(int i = 0; i < 2; i++) {
if(i == 0) {
snprintf(buf, sizeof(buf), "%s", pdx_filename);
} else {
snprintf(buf, sizeof(buf), "%s.PDX", pdx_filename);
}
int r = stat(buf, &st);
if(r == 0) {
strncpy(out, buf, out_len);
goto good;
}
DIR *d;
struct dirent *dir;
d = opendir(dn);
int found = 0;
if(d) {
while((dir = readdir(d)) != NULL) {
if(!strcasecmp(dir->d_name, buf)) {
snprintf(out, out_len, "%s/%s", dn, dir->d_name);
found = 1;
}
}
closedir(d);
}
if(found)
goto good;
}
good:
free(s);
return 0;
}
void csv_quote(char *str, size_t len) {
if(len == 0) len = strlen(str);
if(str == 0) {
putchar('\\');
putchar('N');
return;
}
putchar('"');
for(int i = 0; i < len; i++) {
switch(str[i]) {
case 0:
putchar('\\');
putchar(0);
break;
case '\\':
putchar('\\');
putchar('\\');
break;
case '\b':
putchar('\\');
putchar('b');
break;
case '\n':
putchar('\\');
putchar('n');
break;
case '\r':
putchar('\\');
putchar('r');
break;
case '\t':
putchar('\\');
putchar('t');
break;
case 26:
putchar('\\');
putchar('Z');
break;
case '"':
putchar('"');
putchar('"');
break;
default: putchar(str[i]);
}
}
putchar('"');
}
void hex_dump(const uint8_t *data, size_t len) {
for(size_t i = 0; i < len; i++) printf("%02x", data[i]);
}
int replace_ext(char *out, size_t out_size, const char *in, const char *ext) {
int inlen = strlen(in);
int extlen = strlen(ext);
if(out_size < inlen + extlen + 2)
return -1;
strncpy(out, in, out_size);
char *b = basename(out);
char *pp = strrchr(b, '.');
if(!pp)
pp = b + strlen(b);
if(pp - out + extlen + 1 > out_size)
extlen = out_size - (pp - out + 1);
if(extlen > 0)
strncpy(pp + 1, ext, extlen+1);
return 0;
}