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
|
/* 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);
}
tlp_str_list *tlp_str_list_new()
{
tlp_str_list *list = tlp_new(tlp_str_list);
if (list == NULL)
return NULL;
list->head = list->tail = NULL;
return list;
}
tlp_str_list *tlp_str_list_from_array(const char *array[])
{
tlp_str_list *list = tlp_str_list_new();
int i, size;
if (list == NULL)
return NULL;
size = sizeof(array) / sizeof(array[0]);
for (i = 0; i < size; i++)
if (tlp_str_list_append_bytes(list, array[i]) != TLP_OK)
{
tlp_str_list_free(list);
return NULL;
}
return list;
}
tlp_result tlp_str_list_append(tlp_str_list *list, tlp_str str)
{
tlp_str_list_node *node = tlp_new(tlp_str_list_node);
if (node == NULL)
return TLP_FAILED;
node->str = str;
node->next = NULL;
if (list->head == NULL)
list->head = node;
if (list->tail == NULL)
list->tail = node;
else
{
list->tail->next = node;
list->tail = node;
}
return TLP_OK;
}
tlp_result tlp_str_list_append_bytes(tlp_str_list *list, const char *bytes)
{
tlp_str str = tlp_str_from_bytes(bytes);
if (str == NULL)
return TLP_FAILED;
return tlp_str_list_append(list, str);
}
void tlp_str_list_free(tlp_str_list *list)
{
tlp_str_list_node *node = list->head, *temp;
while (node != NULL)
{
temp = node->next;
tlp_str_free(node->str);
free(node);
node = temp;
}
free(list);
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);
if (prefix_str == NULL)
return 0;
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);
}
|