-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.c
327 lines (270 loc) · 6.54 KB
/
util.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Utility functions
*
* Copyright (C) Navaneeth.K.N
*
* This is part of libvarnam. See LICENSE.txt for the license
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "util.h"
#include "vtypes.h"
/**
* substr(str,start,length,output) writes length characters of str beginning with start to substring.
* start is is 1-indexed and string should be valid UTF8.
**/
void
substr(char *substring, const char *string, int start, int len)
{
unsigned int bytes, i;
const unsigned char *str2, *input;
unsigned char *sub;
if(start <= 0) return;
if(len <= 0) return;
input = (const unsigned char*) string;
sub = (unsigned char*) substring;
--start;
while( *input && start ) {
SKIP_MULTI_BYTE_SEQUENCE(input);
--start;
}
for(str2 = input; *str2 && len; len--) {
SKIP_MULTI_BYTE_SEQUENCE(str2);
}
bytes = (unsigned int) (str2 - input);
for(i = 0; i < bytes; i++) {
*sub++ = *input++;
}
*sub = '\0';
}
/**
* calculates length of the UTF8 encoded string.
* length will be the total number of characters and not the bytes
**/
int
utf8_length(const char *string)
{
const unsigned char *ustring;
int len;
len = 0;
ustring = (const unsigned char*) string;
while( *ustring ) {
++len;
SKIP_MULTI_BYTE_SEQUENCE(ustring);
}
return len;
}
int
utf8_ends_with(const char *buffer, const char *tocheck)
{
size_t to_check_len, buffer_len, newlen;
const char *buf;
if(!tocheck) return 0;
to_check_len = strlen(tocheck);
buffer_len = strlen(buffer);
if(buffer_len < to_check_len) return 0;
newlen = (buffer_len - to_check_len);
buf = buffer + newlen;
return (strcmp(buf, tocheck) == 0);
}
/* return true if string1 starts with string2 */
int
startswith(const char *string1, const char *string2)
{
for(; ; string1++, string2++) {
if(!*string2) {
break;
}
else if(*string1 != *string2) {
return 0;
}
}
return 1;
}
void*
xmalloc(size_t size)
{
void *ret = malloc(size);
return ret;
}
void
xfree (void *ptr)
{
if(ptr)
free(ptr);
}
void
set_last_error(varnam *handle, const char *format, ...)
{
struct strbuf *last_error;
va_list args;
last_error = handle->internal->last_error;
strbuf_clear (last_error);
if (format != NULL)
{
va_start (args, format);
strbuf_addvf (last_error, format, args);
va_end (args);
}
}
void varnam_debug(varnam* handle, const char *format, ...)
{
va_list args;
struct strbuf *log_message = handle->internal->log_message;
if (handle->internal->log_level == VARNAM_LOG_DEBUG && handle->internal->log_callback != NULL)
{
strbuf_clear (log_message);
va_start (args, format);
strbuf_addvf(log_message, format, args);
va_end(args);
handle->internal->log_callback(log_message->buffer);
}
}
void varnam_log(varnam* handle, const char *format, ...)
{
va_list args;
struct strbuf *log_message = handle->internal->log_message;
if (handle->internal->log_callback != NULL)
{
strbuf_clear (log_message);
va_start (args, format);
strbuf_addvf(log_message, format, args);
va_end(args);
handle->internal->log_callback(log_message->buffer);
}
}
bool is_utf8(const char *string)
{
const unsigned char * bytes;
if(!string)
return 0;
bytes = (const unsigned char*) string;
while(*bytes)
{
if( (/* ASCII */
bytes[0] == 0x09 ||
bytes[0] == 0x0A ||
bytes[0] == 0x0D ||
(0x20 <= bytes[0] && bytes[0] <= 0x7E)
)
) {
bytes += 1;
continue;
}
if( (/* non-overlong 2-byte */
(0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF)
)
) {
bytes += 2;
continue;
}
if( (/* excluding overlongs */
bytes[0] == 0xE0 &&
(0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) ||
(/* straight 3-byte */
((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
bytes[0] == 0xEE ||
bytes[0] == 0xEF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) ||
(/* excluding surrogates */
bytes[0] == 0xED &&
(0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
)
) {
bytes += 3;
continue;
}
if( (/* planes 1-3 */
bytes[0] == 0xF0 &&
(0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) ||
(/* planes 4-15 */
(0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) ||
(/* plane 16 */
bytes[0] == 0xF4 &&
(0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
)
) {
bytes += 4;
continue;
}
return 0;
}
return 1;
}
const char zwnj[] = {'\xe2', '\x80', '\x8c', '\0'};
const char *ZWNJ()
{
return zwnj;
}
const char zwj[] = {'\xe2', '\x80', '\x8d', '\0'};
const char *ZWJ()
{
return zwj;
}
char *trimwhitespace(char *str)
{
char *end;
/* Trim leading space */
while(isspace(*str)) str++;
if(*str == 0) /* All spaces? */
return str;
/* Trim trailing space */
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
/* Write new null terminator */
*(end+1) = 0;
return str;
}
static char special_chars[] = {'\n', '\t', '\r', ',', '.', '/', '<', '>', '?', ';', '\'', ':',
'"', '[', ']', '{', '}', '~', '`', '!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '-', '_', '+', '=', '\\', '|', ' '};
bool is_special_character(char c)
{
int i;
for (i = 0; i < ARRAY_SIZE(special_chars); i++)
{
if (c == special_chars[i])
return true;
}
return false;
}
int get_stat(const char *pathname)
{
struct stat info;
if( stat( pathname, &info ) != 0 )
return V_PATHNAME_INVALID;
#ifdef S_IFDIR
else if( info.st_mode & S_IFDIR )
#else
else if( S_ISDIR (info.st_mode) )
#endif
return V_PATHNAME_DIRECTORY;
return V_PATHNAME_FILE;
}
bool is_directory(const char *pathname)
{
return get_stat (pathname) == V_PATHNAME_DIRECTORY;
}
bool is_path_exists(const char *pathname)
{
return get_stat (pathname) != V_PATHNAME_INVALID;
}