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

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


void magi_str_lowercase(char *str)
{
    for (; str && *str; ++str) {
        *str = tolower(*str);
    }
}

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

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

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