This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.c
95 lines (85 loc) · 1.75 KB
/
util.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
#include <stdarg.h>
#include <sys/param.h>
#include <sys/unistd.h>
#include "sys.h"
#include "util.h"
int g_debug_mask = 0;
static void string_lower(char *p) {
for (; *p; ++p) {
if (*p >= 'A' && *p <= 'Z') {
*p += 'a' - 'A';
}
}
}
static void string_upper(char *p) {
for (; *p; ++p) {
if (*p >= 'a' && *p <= 'z') {
*p += 'A' - 'a';
}
}
}
#ifndef NDEBUG
void print_debug(int debug_channel, const char *msg, ...) {
if (g_debug_mask & debug_channel) {
char buf[256];
va_list va;
va_start(va, msg);
vsprintf(buf, msg, va);
va_end(va);
fprintf(stdout, "%s\n", buf);
g_sys.print_log(stdout, buf);
}
}
#endif
void print_warning(const char *msg, ...) {
char buf[256];
va_list va;
va_start(va, msg);
vsprintf(buf, msg, va);
va_end(va);
fprintf(stderr, "WARNING: %s\n", buf);
g_sys.print_log(stderr, buf);
}
void print_error(const char *msg, ...) {
char buf[256];
va_list va;
va_start(va, msg);
vsprintf(buf, msg, va);
va_end(va);
fprintf(stderr, "ERROR: %s!\n", buf);
g_sys.print_log(stderr, buf);
exit(-1);
}
void print_info(const char *msg, ...) {
char buf[256];
va_list va;
va_start(va, msg);
vsprintf(buf, msg, va);
va_end(va);
fprintf(stdout, "%s\n", buf);
g_sys.print_log(stdout, buf);
}
FILE *fopen_nocase(const char *path, const char *filename) {
char buf[MAXPATHLEN];
snprintf(buf, sizeof(buf), "%s/%s", path, filename);
FILE *fp = fopen(buf, "rb");
if (!fp) {
static void (*const str[3])(char *) = {
string_upper,
string_lower,
0
};
char *p = buf + strlen(path) + 1;
for (int i = 0; str[i] && !fp; ++i) {
(str[i])(p);
if (strcmp(filename, p) != 0) {
fp = fopen(buf, "rb");
}
}
}
return fp;
}
uint16_t fread_le16(FILE *fp) {
const uint16_t val = fgetc(fp);
return val | (fgetc(fp) << 8);
}