aboutsummaryrefslogtreecommitdiff
path: root/src/inner_tools.c
blob: d958851eb367ed039a719f49227686487756dc39 (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
#include "inner_tools.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>


void magi_str_lowercase(char *str)
{
    if (!str) {
        return;
    }
    for (; *str; ++str) {
        *str = tolower(*str);
    }
}

char *magi_str_create_copy(const char *first, int len)
{
    char *copy = magi_str_create(len);
    if (copy) {
        memcpy(copy, first, len);
    }
    return copy;
}

char *magi_str_create(int len)
{
    char *str = malloc(len + 1);
    if (str) {
        str[len] = 0;
    }
    return str;
}

int magi_str_add(char **dest, int *len, int *size, char c)
{
    if (!*dest) {
        *dest = magi_str_create(1);
    } else if (*len + 1 == *size) {
        *size *= 2;
        *dest  = realloc(*dest, *size);
    }
    if (!*dest) {
        return 0;
    }
    (*dest)[*len] = c;
    ++*len;
    (*dest)[*len] = 0;
    return 1;
}