summaryrefslogtreecommitdiff
path: root/Master/tlpkg/lib/C/tlp-utils.c
blob: eda6510358416a479176df256a12d016548d341b (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
/* tlp-utils.h - module for general tlp utilities
 *
 * Copyright 2007 Jjgod Jiang
 * Derived from the Perl implementation by Norbert Preining
 *
 * This file is licensed under the GNU General Public License version 2
 * or any later version. */

#include <tlp-utils.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

tlp_str tlp_str_from_bytes(const char *bytes)
{
    return (tlp_str) strdup(bytes);
}

tlp_str tlp_str_copy(tlp_str str)
{
    if (str == NULL)
        return NULL;

    return strdup(str);
}

void tlp_str_free(tlp_str str)
{
    free(str);
}

void tlp_str_list_free(tlp_str_list *list)
{
    tlp_str_list *node = list, *temp;

    while (node != NULL)
    {
        temp = node->next;
        
        tlp_str_free(node->str);
        free(node);

        node = temp;
    }

    list = NULL;
}

tlp_str tlp_fgets(tlp_str s, int n, FILE *fp)
{
    return (tlp_str) fgets((char *) s, n, fp);
}

void tlp_str_output(FILE *fp, tlp_str str)
{
    fprintf(stderr, "%s", (char *) str);
}

size_t tlp_str_len(tlp_str str)
{
    return strlen((const char *) str);
}

int tlp_str_start_with(tlp_str str, tlp_str prefix)
{
    int len = tlp_str_len(prefix);

    if (tlp_str_len(str) < len)
        return 0;

    return strncmp((const char *) str, 
                   (const char *) prefix, len);
}

int tlp_str_start_with_bytes(tlp_str str, const char *prefix)
{
    tlp_str prefix_str;
    int ret;

    prefix_str = tlp_str_from_bytes(prefix);
    ret = tlp_str_start_with(str, prefix_str);

    tlp_str_free(prefix_str);
    return ret;
}

int tlp_char_is_space(tlp_char ch)
{
    return isspace((int) ch);
}

int tlp_char_is_eol(tlp_char ch)
{
    return (ch == '\r' || ch == '\n');
}

int tlp_char_in_word(tlp_char ch)
{
    return (isalnum(ch) || ch == '_');
}

void tlp_error_msg(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
}

void tlp_error_str(tlp_str str)
{
    tlp_str_output(stderr, str);
}