-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.c
65 lines (55 loc) · 1.07 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
#include "util.h"
#include <ctype.h>
#include <string.h>
char* trim(char *str)
{
char *left = str;
char *right = str + (strlen(str) - 1);
while (isspace(*left)) {
left++;
}
while (isspace(*right)) {
right--;
}
if (left == str) {
*++right = '\0';
}
else {
char *p = str;
while (left <= right) {
*p++ = *left++;
}
*p = '\0';
}
return str;
}
char* split(char *str, char delimiter)
{
char *p = strchr(str, delimiter);
if (p != NULL) {
*p = '\0';
return p + 1;
}
return NULL;
}
char *escapech(char *buf, char c, char *str)
{
int l = strlen(str) + 1;
// just needs to loop through and add backslash to the front
int i = 0, pi = 0;
while (i < l) {
char ch = str[i++];
if (ch == c)
buf[pi++] = '\\';
buf[pi++] = ch;
}
return buf;
}
char *escapesh(char *buf, char *str)
{
return escapech(buf, '\'', str);
}
char *escapejson(char *buf, char *str)
{
return escapech(buf, '"', str);
}