summaryrefslogtreecommitdiffstats
path: root/src/string.c
blob: 85a57678a353f9a2185c8bafad3a18b38f4bd233 (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
#include <stdio.h>
#include <string.h>

rcc_string rccStringInit(rcc_language_id language_id, const char *buf, int len, int *rlen) {
    rcc_string_header header = {RCC_STRING_MAGIC, language_id};

    len = STRNLEN(buf, len);
        
    res = (char*)malloc(len+sizeof(rcc_string_header)+1);
    if (!res) return NULL;

    strncpy(res + sizeof(rcc_string_header), buf, len);
    res[sizeof(rcc_string_header) + 1 + len] = 0;

    memcpy(res, &header, sizeof(rcc_string_header));

    if (rlen) *rlen = len + sizeof(rcc_string_header);
    return (rcc_string)res;
}

void rccStringFree(rcc_string str) {
    if (str) free(str);
}

rcc_language_id rccStringCheck(const rcc_string str) {
    int len;
    rcc_string_header *header;

    len = strlen(str);
    
    if ((!str)||(len<=sizeof(unsigned int))||(*((unsigned int*)(str))!=RCC_STRING_MAGIC)) return 0;

    header = (rcc_string_header*)(str);
    return header->language_id;
}

const char *rccStringGet(const rcc_string str) {
    if (rccStringCheck(str)) return str + sizeof(rcc_string_header);
    return (const char *)str;
}

char *rccStringExtract(const rcc_string buf, int len, int *rlen) {
    char *res;
    
    len = STRNLEN(buf, len) - sizeof(rcc_string_header);
    if (len<0) return NULL;
    
    res = (char*)malloc(len+1);
    if (!res) return NULL;

    strncpy(res, buf + sizeof(rcc_string_header), len);
    res[len] = 0;
    
    if (rlen) *rlen = len;
    
    return res;    
}

char *rccStringCmp(const rcc_string str1, const rcc_string str2) {
    return strcmp(rccStringGet(str1), rccStringGet(str2));
}

char *rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n) {
    return strncmp(rccStringGet(str1), rccStringGet(str2), n);
}

char *rccStringCaseCmp(const rcc_string str1, const rcc_string str2) {
    return strcasecmp(rccStringGet(str1), rccStringGet(str2));
}

char *rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n) {
    return strncasecmp(rccStringGet(str1), rccStringGet(str2), n);
}