forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
149 lines (128 loc) · 3.66 KB
/
util.c
File metadata and controls
149 lines (128 loc) · 3.66 KB
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
static uint32_t
utf8_encode(uint8_t out[4], uint32_t cp)
{
uint32_t result;
if (cp <= 0x7F) {
out[0] = cp & 0x7F;
result = 1;
} else if (cp <= 0x7FF) {
result = 2;
out[0] = ((cp >> 6) & 0x1F) | 0xC0;
out[1] = ((cp >> 0) & 0x3F) | 0x80;
} else if (cp <= 0xFFFF) {
result = 3;
out[0] = ((cp >> 12) & 0x0F) | 0xE0;
out[1] = ((cp >> 6) & 0x3F) | 0x80;
out[2] = ((cp >> 0) & 0x3F) | 0x80;
} else if (cp <= 0x10FFFF) {
result = 4;
out[0] = ((cp >> 18) & 0x07) | 0xF0;
out[1] = ((cp >> 12) & 0x3F) | 0x80;
out[2] = ((cp >> 6) & 0x3F) | 0x80;
out[3] = ((cp >> 0) & 0x3F) | 0x80;
} else {
//out[0] = '?';
result = 0;
}
return result;
}
#define zero_struct(s) memset((s), 0, sizeof(*s))
enum { DA_INITIAL_CAP = 16 };
#define da_release(da) free((da)->data)
#define da_unordered_remove(da, index) do { \
if ((index) < (da)->count - 1) \
(da)->data[(index)] = (da)->data[(da)->count - 1]; \
(da)->count--; \
} while(0)
#define da_ordered_remove(da, index) do { \
if ((index) < (da)->count - 1) \
memmove((da)->data + (index), (da)->data + (index) + 1, \
sizeof(*(da)->data) * ((da)->count - (index) - 1)); \
(da)->count--; \
} while(0)
#define DA_COMPARE_FN(name) int name(const void *va, const void *vb)
#define da_sort(da, compare) qsort((da)->data, (da)->count, sizeof(*(da)->data), (compare))
#define da_reserve(vis, da, n) \
(da)->data = da_reserve_(vis, (da)->data, &(da)->capacity, (da)->count + n, sizeof(*(da)->data))
#define da_push(vis, da) \
((da)->count == (da)->capacity \
? da_reserve(vis, da, 1), \
(da)->data + (da)->count++ \
: (da)->data + (da)->count++)
static void *
da_reserve_(Vis *vis, void *data, VisDACount *capacity, VisDACount needed, size_t size)
{
VisDACount cap = *capacity;
if (!cap) cap = DA_INITIAL_CAP;
while (cap < needed) cap *= 2;
data = realloc(data, size * cap);
if (unlikely(data == 0))
longjmp(vis->oom_jmp_buf, 1);
memset((char *)data + (*capacity * size), 0, size * (cap - *capacity));
*capacity = cap;
return data;
}
static void *
memory_scan_reverse(const void *memory, uint8_t byte, ptrdiff_t n)
{
void *result = 0;
if (n > 0) {
const uint8_t *s = memory;
while (n) if (s[--n] == byte) { result = (void *)(s + n); break; }
}
return result;
}
static str8
str8_from_c_str(char *c_str)
{
str8 result = {
.length = strlen(c_str),
.data = (uint8_t *)c_str,
};
return result;
}
static void
str8_split_at(str8 s, str8 *left, str8 *right, ptrdiff_t n)
{
if (left) *left = (str8){0};
if (right) *right = (str8){0};
if (n >= 0 && n <= s.length) {
if (left) *left = (str8){
.length = n,
.data = s.data,
};
if (right) *right = (str8){
.length = MAX(0, s.length - n - 1),
.data = s.data + n + 1,
};
}
}
static void
path_split(str8 path, str8 *directory, str8 *basename)
{
str8_split_at(path, directory, basename,
(uint8_t *)memory_scan_reverse(path.data, '/', path.length) - path.data);
if (directory && directory->length == 0) *directory = str8(".");
if (basename && basename->length == 0) *basename = path;
}
static char *
absolute_path(const char *name)
{
if (!name)
return 0;
str8 base, dir, string = str8_from_c_str((char *)name);
path_split(string, &dir, &base);
char path_resolved[PATH_MAX];
char path_normalized[PATH_MAX];
memcpy(path_normalized, dir.data, dir.length);
path_normalized[dir.length] = 0;
char *result = 0;
if (realpath(path_normalized, path_resolved) == path_resolved) {
if (strcmp(path_resolved, "/") == 0)
path_resolved[0] = 0;
snprintf(path_normalized, sizeof(path_normalized), "%s/%.*s",
path_resolved, (int)base.length, base.data);
result = strdup(path_normalized);
}
return result;
}