-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
119 lines (101 loc) · 2.14 KB
/
utils.h
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
#ifndef utils_h_included
#define utils_h_included
#include <syslog.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define TOKEN_MAX_SIZE 512
#define LOG(...) \
do { \
char _buf[4096]; \
int _ret = snprintf(_buf, sizeof(_buf), __VA_ARGS__); \
if (_ret >= (int)(sizeof(_buf))) { \
syslog(LOG_DEBUG | LOG_USER, \
"Next line truncated to %d symbols", \
_ret); \
} \
syslog(LOG_DEBUG | LOG_USER, \
"%s [%s, line %d, function %s()]", \
_buf, __FILE__, __LINE__, __func__); \
} while (0)
static inline char *
string_trim(char *str)
{
char *end;
/* Trim leading space */
while(isspace((unsigned char)*str)) {
str++;
}
if(*str == 0) {
/* All spaces? */
return str;
}
/* Trim trailing space */
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) {
end--;
}
/* Write new null terminator character */
end[1] = '\0';
return str;
}
static inline void
csv_next(char **line, char *val)
{
char *ptr = *line, *end;
size_t len;
/* skip spaces */
while (isspace(*ptr)) {
ptr++;
}
if (*ptr == '\0') {
/* empty */
val[0] = '\0';
return;
}
if (*ptr == '\"') {
/* string */
ptr++;
for (;;) {
if (*ptr == '\0') {
/* unexpected end, no closing quote */
*val = '\0';
*line = ptr;
return;
} else if (*ptr == '\"') {
if (*(ptr + 1) == '\"') {
*val = '\"';
val++;
ptr += 2;
} else {
ptr++;
break;
}
} else {
*val = *ptr;
val++;
ptr++;
}
}
end = strchr(ptr, ',');
if (!end) {
*line = strchr(ptr, '\0');
return;
}
*line = end + 1;
} else {
end = strchr(ptr, ',');
if (!end) {
/* no comma */
strcpy(val, ptr);
*line = strchr(ptr, '\0');
return;
}
len = end - ptr;
memcpy(val, ptr, len);
val[len] = '\0';
*line = end + 1;
}
}
typedef __int128_t xe_ip;
#endif