-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
247 lines (221 loc) · 4.74 KB
/
util.c
File metadata and controls
247 lines (221 loc) · 4.74 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "util.h"
#define ID_STRING_MAX_LEN 16
#define ID_TO_STRING(string_name, id_name) \
char string_name[ID_STRING_MAX_LEN]; \
g_snprintf(string_name, ID_STRING_MAX_LEN, "%d", id_name);
#define CMP(A, op, B, comparator) comparator(A, B) op 0
GList *pathToList (const char *path)
{
if (g_str_has_prefix(path, "/"))
{
path++;
}
char **dirs = g_strsplit(path, "/", -1);
GList *res = NULL;
int i = 0;
while (dirs[i] != NULL)
{
res = g_list_prepend(res, g_strdup(dirs[i]));
i++;
}
g_strfreev(dirs);
return g_list_reverse(res);
}
// va_list must of course be NULL-terminated
GList *g_list_new (gpointer first, ...)
{
va_list args;
GList *res = NULL;
res = g_list_prepend(res, first);
va_start(args, first);
gpointer item = va_arg(args, gpointer);
while (item != NULL)
{
res = g_list_prepend(res, item);
item = va_arg(args, gpointer);
}
va_end(args);
return g_list_reverse(res);
}
GList *g_list_new_charlist (gchar first, ...)
{
va_list args;
GList *res = NULL;
res = g_list_prepend(res, GINT_TO_POINTER(first));
va_start(args, first);
int item = va_arg(args, int);
while (item != 0)
{
res = g_list_prepend(res, GINT_TO_POINTER(item));
item = va_arg(args, int);
}
va_end(args);
return g_list_reverse(res);
}
/* lol must be a list of lists with no non-list members */
GList *g_list_flatten (GList *lol)
{
GList *res = NULL;
LL(lol, ita)
{
LL((GList*)ita->data, itb)
{
res = g_list_prepend(res, itb->data);
}
} LL_END;
return g_list_reverse(res);
}
gboolean str_isalnum (const char *str)
{
int n = strlen(str);
int i;
for (i = 0; i < n; i++)
{
if (!(g_ascii_isalnum(str[i]) ||
str[i] == '-' ||
str[i] == '_'))
{
return FALSE;
}
}
return TRUE;
}
void print_list (GList *l, ToString s)
{
printf("(");
while (l != NULL)
{
printf("%s", s(l->data));
if (g_list_next(l) != NULL)
{
printf(" ");
}
l = g_list_next(l);
}
printf(")");
printf("\n");
}
void print_string_list (GList *l)
{
putc('(', stdout);
while (l != NULL)
{
fprintf(stdout, "%s", (char*) l->data);
if (g_list_next(l) != NULL)
{
putc(' ', stdout);
}
l = g_list_next(l);
}
putc(')', stdout);
}
void fprint_pair (gpointer key, gpointer val, gpointer f)
{
fprintf(f, "%p=>", key);
fprintf(f, "%p ", val);
}
void print_pair (gpointer key, gpointer val, gpointer not_used)
{
printf("%p=>", key);
printf("%p ", val);
}
void fprint_hash (FILE *f, GHashTable *hsh)
{
fprintf(f, "{");
g_hash_table_foreach(hsh, fprint_pair, f);
fprintf(f, "}");
fprintf(f, "\n");
}
void print_hash (GHashTable *hsh)
{
printf("{");
if (hsh != NULL)
g_hash_table_foreach(hsh, print_pair, NULL);
printf("}");
printf("\n");
}
void print_tree(GTree *tree)
{
g_tree_foreach(tree,(GTraverseFunc) print_pair, NULL);
printf("\n");
}
int max (int a, int b)
{
return (a > b)?a:b;
}
/* for use with qsort */
int cmp (gconstpointer a, gconstpointer b)
{
return *((gulong*) a) - *((gulong*) b);
}
int long_cmp (gpointer a, gpointer b)
{
return TO_S(a) - TO_S(b);
}
// gets the index of a string in a null-terminated string vector
int strv_index (const char *vector[], const char *str)
{
int i = 0;
while (vector[i] != NULL)
{
if (str_equal(vector[i], str))
return i;
i++;
}
return -1;
}
char *to_charp (size_t size, size_t n)
{
char *res = g_malloc0(size);
int i;
size_t mask = SCHAR_MAX;
for (i = 0; i < size; i++)
{
ldiv_t this = ldiv(n, mask);
res[i] = this.rem;
printf("%zd\n", this.rem);
n = this.quot;
}
return res;
}
size_t charp_to_size (size_t length, char *s)
{
size_t res = 0;
size_t mask = SCHAR_MAX;
size_t shift = 1;
int i;
for (i = 0; i < length; i++)
{
res += s[i] * shift;
shift *= mask;
}
return res;
}
buffer_t new_buffer(size_t size)
{
buffer_t b;
b.size = size;
b.content = calloc(1,size);
return b;
}
buffer_t buffer_wrap(size_t size, char *content)
{
buffer_t b;
b.size = size;
b.content = content;
return b;
}
unsigned short rand_lim (unsigned short limit)
{
/* return a random number between 0 and limit inclusive.
*/
int divisor = RAND_MAX/(limit+1);
unsigned short retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval + 1;
}