aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/utils.c
blob: 9061e40948e32a6e28b2d0ee2a45b7199faba00f (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#define BC_STRING_CHUNK_SIZE 128

#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

#include "utils.h"


void*
bc_malloc(size_t size)
{
    // simple things simple!
    void *rv = malloc(size);
    if (rv == NULL) {
        fprintf(stderr, "fatal: Failed to allocate memory!\n");
        abort();
    }
    return rv;
}


void*
bc_realloc(void *ptr, size_t size)
{
    // simple things even simpler :P
    void *rv = realloc(ptr, size);
    if (rv == NULL && size != 0) {
        fprintf(stderr, "fatal: Failed to reallocate memory!\n");
        free(ptr);
        abort();
    }
    return rv;
}


bc_slist_t*
bc_slist_append(bc_slist_t *l, void *data)
{
    bc_slist_t *node = bc_malloc(sizeof(bc_slist_t));
    node->data = data;
    node->next = NULL;
    if (l == NULL) {
        l = node;
    }
    else {
        bc_slist_t *tmp;
        for (tmp = l; tmp->next != NULL; tmp = tmp->next);
        tmp->next = node;
    }
    return l;
}


bc_slist_t*
bc_slist_prepend(bc_slist_t *l, void *data)
{
    bc_slist_t *node = bc_malloc(sizeof(bc_slist_t));
    node->data = data;
    node->next = l;
    l = node;
    return l;
}


void
bc_slist_free_full(bc_slist_t *l, bc_free_func_t free_func)
{
    while (l != NULL) {
        bc_slist_t *tmp = l->next;
        if ((free_func != NULL) && (l->data != NULL))
            free_func(l->data);
        free(l);
        l = tmp;
    }
}


void
bc_slist_free(bc_slist_t *l)
{
    bc_slist_free_full(l, NULL);
}


size_t
bc_slist_length(bc_slist_t *l)
{
    if (l == NULL)
        return 0;
    size_t i;
    bc_slist_t *tmp;
    for (tmp = l, i = 0; tmp != NULL; tmp = tmp->next, i++);
    return i;
}


char*
bc_strdup(const char *s)
{
    if (s == NULL)
        return NULL;
    size_t l = strlen(s);
    char *tmp = malloc(l + 1);
    if (tmp == NULL)
        return NULL;
    memcpy(tmp, s, l + 1);
    return tmp;
}


char*
bc_strndup(const char *s, size_t n)
{
    if (s == NULL)
        return NULL;
    size_t l = strnlen(s, n);
    char *tmp = malloc(l + 1);
    if (tmp == NULL)
        return NULL;
    memcpy(tmp, s, l);
    tmp[l] = '\0';
    return tmp;
}


char*
bc_strdup_vprintf(const char *format, va_list ap)
{
    va_list ap2;
    va_copy(ap2, ap);
    int l = vsnprintf(NULL, 0, format, ap2);
    va_end(ap2);
    if (l < 0)
        return NULL;
    char *tmp = malloc(l + 1);
    if (!tmp)
        return NULL;
    int l2 = vsnprintf(tmp, l + 1, format, ap);
    if (l2 < 0) {
        free(tmp);
        return NULL;
    }
    return tmp;
}


char*
bc_strdup_printf(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    char *tmp = bc_strdup_vprintf(format, ap);
    va_end(ap);
    return tmp;
}


bool
bc_str_starts_with(const char *str, const char *prefix)
{
    int str_l = strlen(str);
    int str_lp = strlen(prefix);
    if (str_lp > str_l)
        return false;
    return strncmp(str, prefix, str_lp) == 0;
}


bool
bc_str_ends_with(const char *str, const char *suffix)
{
    int str_l = strlen(str);
    int str_ls = strlen(suffix);
    if (str_ls > str_l)
        return false;
    return strcmp(str + str_l - str_ls, suffix) == 0;
}


char*
bc_str_lstrip(char *str)
{
    if (str == NULL)
        return NULL;
    int i;
    size_t str_len = strlen(str);
    for (i = 0; i < str_len; i++) {
        if ((str[i] != ' ') && (str[i] != '\t') && (str[i] != '\n') &&
            (str[i] != '\r') && (str[i] != '\t') && (str[i] != '\f') &&
            (str[i] != '\v'))
        {
            str += i;
            break;
        }
        if (i == str_len - 1) {
            str += str_len;
            break;
        }
    }
    return str;
}


char*
bc_str_rstrip(char *str)
{
    if (str == NULL)
        return NULL;
    int i;
    size_t str_len = strlen(str);
    for (i = str_len - 1; i >= 0; i--) {
        if ((str[i] != ' ') && (str[i] != '\t') && (str[i] != '\n') &&
            (str[i] != '\r') && (str[i] != '\t') && (str[i] != '\f') &&
            (str[i] != '\v'))
        {
            str[i + 1] = '\0';
            break;
        }
        if (i == 0) {
            str[0] = '\0';
            break;
        }
    }
    return str;
}


char*
bc_str_strip(char *str)
{
    return bc_str_lstrip(bc_str_rstrip(str));
}


char**
bc_str_split(const char *str, char c, unsigned int max_pieces)
{
    if (str == NULL)
        return NULL;
    char **rv = bc_malloc(sizeof(char*));
    unsigned int i, start = 0, count = 0;
    for (i = 0; i < strlen(str) + 1; i++) {
        if (str[0] == '\0')
            break;
        if ((str[i] == c && (!max_pieces || count + 1 < max_pieces)) || str[i] == '\0') {
            rv = bc_realloc(rv, (count + 1) * sizeof(char*));
            rv[count] = bc_malloc(i - start + 1);
            memcpy(rv[count], str + start, i - start);
            rv[count++][i - start] = '\0';
            start = i + 1;
        }
    }
    rv = bc_realloc(rv, (count + 1) * sizeof(char*));
    rv[count] = NULL;
    return rv;
}


char*
bc_str_replace(const char *str, const char search, const char *replace)
{
    char **pieces = bc_str_split(str, search, 0);
    if (pieces == NULL)
        return NULL;
    char* rv = bc_strv_join(pieces, replace);
    bc_strv_free(pieces);
    if (rv == NULL)
        return bc_strdup(str);
    return rv;
}


char*
bc_str_find(const char *str, char c)
{
    // this is somewhat similar to strchr, but respects '\' escaping.
    if (str == NULL)
        return NULL;
    if (c == '\0')
        return (char*) str + strlen(str);
    for (size_t i = 0; str[i] != '\0'; i++) {
        if (str[i] == '\\') {
            i++;
            continue;
        }
        if (str[i] == c) {
            return (char*) str + i;
        }
    }
    return NULL;
}


void
bc_strv_free(char **strv)
{
    if (strv == NULL)
        return;
    for (size_t i = 0; strv[i] != NULL; i++)
        free(strv[i]);
    free(strv);
}


char*
bc_strv_join(char **strv, const char *separator)
{
    if (strv == NULL || separator == NULL)
        return NULL;
    bc_string_t *str = bc_string_new();
    for (size_t i = 0; strv[i] != NULL; i++) {
        str = bc_string_append(str, strv[i]);
        if (strv[i + 1] != NULL)
            str = bc_string_append(str, separator);
    }
    return bc_string_free(str, false);
}


size_t
bc_strv_length(char **strv)
{
    if (strv == NULL)
        return 0;
    size_t i;
    for (i = 0; strv[i] != NULL; i++);
    return i;
}


bc_string_t*
bc_string_new(void)
{
    bc_string_t* rv = bc_malloc(sizeof(bc_string_t));
    rv->str = NULL;
    rv->len = 0;
    rv->allocated_len = 0;

    // initialize with empty string
    rv = bc_string_append(rv, "");

    return rv;
}


char*
bc_string_free(bc_string_t *str, bool free_str)
{
    if (str == NULL)
        return NULL;
    char *rv = NULL;
    if (free_str)
        free(str->str);
    else
        rv = str->str;
    free(str);
    return rv;
}


bc_string_t*
bc_string_dup(bc_string_t *str)
{
    if (str == NULL)
        return NULL;
    bc_string_t* new = bc_string_new();
    return bc_string_append_len(new, str->str, str->len);
}


bc_string_t*
bc_string_append_len(bc_string_t *str, const char *suffix, size_t len)
{
    if (str == NULL)
        return NULL;
    if (suffix == NULL)
        return str;
    size_t old_len = str->len;
    str->len += len;
    if (str->len + 1 > str->allocated_len) {
        str->allocated_len = (((str->len + 1) / BC_STRING_CHUNK_SIZE) + 1) * BC_STRING_CHUNK_SIZE;
        str->str = bc_realloc(str->str, str->allocated_len);
    }
    memcpy(str->str + old_len, suffix, len);
    str->str[str->len] = '\0';
    return str;
}


bc_string_t*
bc_string_append(bc_string_t *str, const char *suffix)
{
    if (str == NULL)
        return NULL;
    const char *my_suffix = suffix == NULL ? "" : suffix;
    return bc_string_append_len(str, my_suffix, strlen(my_suffix));
}


bc_string_t*
bc_string_append_c(bc_string_t *str, char c)
{
    if (str == NULL)
        return NULL;
    size_t old_len = str->len;
    str->len += 1;
    if (str->len + 1 > str->allocated_len) {
        str->allocated_len = (((str->len + 1) / BC_STRING_CHUNK_SIZE) + 1) * BC_STRING_CHUNK_SIZE;
        str->str = bc_realloc(str->str, str->allocated_len);
    }
    str->str[old_len] = c;
    str->str[str->len] = '\0';
    return str;
}


bc_string_t*
bc_string_append_printf(bc_string_t *str, const char *format, ...)
{
    if (str == NULL)
        return NULL;
    va_list ap;
    va_start(ap, format);
    char *tmp = bc_strdup_vprintf(format, ap);
    va_end(ap);
    str = bc_string_append(str, tmp);
    free(tmp);
    return str;
}


bc_string_t*
bc_string_append_escaped(bc_string_t *str, const char *suffix)
{
    if (str == NULL)
        return NULL;
    if (suffix == NULL)
        return str;
    bool escaped = false;
    for (size_t i = 0; suffix[i] != '\0'; i++) {
        if (suffix[i] == '\\' && !escaped) {
            escaped = true;
            continue;
        }
        escaped = false;
        str = bc_string_append_c(str, suffix[i]);
    }
    return str;
}


bc_trie_t*
bc_trie_new(bc_free_func_t free_func)
{
    bc_trie_t *trie = bc_malloc(sizeof(bc_trie_t));
    trie->root = NULL;
    trie->free_func = free_func;
    return trie;
}


static void
bc_trie_free_node(bc_trie_t *trie, bc_trie_node_t *node)
{
    if (trie == NULL || node == NULL)
        return;
    if (node->data != NULL && trie->free_func != NULL)
        trie->free_func(node->data);
    bc_trie_free_node(trie, node->next);
    bc_trie_free_node(trie, node->child);
    free(node);
}


void
bc_trie_free(bc_trie_t *trie)
{
    if (trie == NULL)
        return;
    bc_trie_free_node(trie, trie->root);
    free(trie);
}


void
bc_trie_insert(bc_trie_t *trie, const char *key, void *data)
{
    if (trie == NULL || key == NULL || data == NULL)
        return;

    bc_trie_node_t *parent = NULL;
    bc_trie_node_t *previous;
    bc_trie_node_t *current;
    bc_trie_node_t *tmp;

    while (1) {

        if (trie->root == NULL || (parent != NULL && parent->child == NULL)) {
            current = bc_malloc(sizeof(bc_trie_node_t));
            current->key = *key;
            current->data = NULL;
            current->next = NULL;
            current->child = NULL;
            if (trie->root == NULL)
                trie->root = current;
            else
                parent->child = current;
            parent = current;
            goto clean;
        }

        tmp = parent == NULL ? trie->root : parent->child;
        previous = NULL;

        while (tmp != NULL && tmp->key != *key) {
            previous = tmp;
            tmp = tmp->next;
        }

        parent = tmp;

        if (previous == NULL || parent != NULL)
            goto clean;

        current = bc_malloc(sizeof(bc_trie_node_t));
        current->key = *key;
        current->data = NULL;
        current->next = NULL;
        current->child = NULL;
        previous->next = current;
        parent = current;

clean:
        if (*key == '\0') {
            if (parent->data != NULL && trie->free_func != NULL)
                trie->free_func(parent->data);
            parent->data = data;
            break;
        }
        key++;
    }
}


void*
bc_trie_lookup(bc_trie_t *trie, const char *key)
{
    if (trie == NULL || trie->root == NULL || key == NULL)
        return NULL;

    bc_trie_node_t *parent = trie->root;
    bc_trie_node_t *tmp;
    while (1) {
        for (tmp = parent; tmp != NULL; tmp = tmp->next) {

            if (tmp->key == *key) {
                if (tmp->key == '\0')
                    return tmp->data;
                parent = tmp->child;
                break;
            }
        }
        if (tmp == NULL)
            return NULL;

        if (*key == '\0')
            break;
        key++;
    }
    return NULL;
}


static void
bc_trie_size_node(bc_trie_node_t *node, size_t *count)
{
    if (node == NULL || count == NULL)
        return;

    if (node->key == '\0')
        (*count)++;

    bc_trie_size_node(node->next, count);
    bc_trie_size_node(node->child, count);
}


size_t
bc_trie_size(bc_trie_t *trie)
{
    if (trie == NULL)
        return 0;

    size_t count = 0;
    bc_trie_size_node(trie->root, &count);
    return count;
}


static void
bc_trie_foreach_node(bc_trie_node_t *node, bc_string_t *str,
    bc_trie_foreach_func_t func, void *user_data)
{
    if (node == NULL || str == NULL || func == NULL)
        return;

    if (node->key == '\0') {
        func(str->str, node->data, user_data);
        return;
    }

    if (node->child != NULL) {
        bc_string_t *child = bc_string_dup(str);
        child = bc_string_append_c(child, node->key);
        bc_trie_foreach_node(node->child, child, func, user_data);
        bc_string_free(child, true);
    }

    if (node->next != NULL)
        bc_trie_foreach_node(node->next, str, func, user_data);
}


void
bc_trie_foreach(bc_trie_t *trie, bc_trie_foreach_func_t func,
    void *user_data)
{
    if (trie == NULL || trie->root == NULL || func == NULL)
        return;

    bc_string_t *str = bc_string_new();
    bc_trie_foreach_node(trie->root, str, func, user_data);
    bc_string_free(str, true);
}


char*
bc_shell_quote(const char *command)
{
    bc_string_t *rv = bc_string_new();
    bc_string_append_c(rv, '\'');
    if (command != NULL) {
        for (size_t i = 0; i < strlen(command); i++) {
            switch (command[i]) {
                case '!':
                    bc_string_append(rv, "'\\!'");
                    break;
                case '\'':
                    bc_string_append(rv, "'\\''");
                    break;
                default:
                    bc_string_append_c(rv, command[i]);
            }
        }
    }
    bc_string_append_c(rv, '\'');
    return bc_string_free(rv, false);
}