Repository navigation
Expand file tree
/
Copy pathStrUtil.c
More file actions
88 lines (78 loc) · 1.8 KB
/
Copy pathStrUtil.c
File metadata and controls
88 lines (78 loc) · 1.8 KB
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
#include "StrUtil.h"
char **initBuffer(void)
{
char **ret;
ret = (char **)calloc(1, MAX_ARG_COUNT * sizeof(char *));
*(ret + 0) = (char *)calloc(1, MAX_ARG_COUNT * (MAX_ARG_LENGTH + 1) * sizeof(char));
for (int i = 0; i < MAX_ARG_COUNT; i++)
{
*(ret + i) = (*ret + (MAX_ARG_LENGTH + 1) * i);
}
return ret;
}
void parse(const char *str, char **args, int *argc)
{
int pos = 0;
int i = 0;
int ch = 0;
int len = 0;
int intoken = TRUE;
int inliteral = FALSE;
*argc = 0;
memset(*(args + 0), 0, MAX_ARG_COUNT * (MAX_ARG_LENGTH + 1) * sizeof(char));
len = strlen(str);
for (i = 0; i < len; i++)
{
ch = str[i];
if (ch == 32)
{
if (inliteral)
{
*(args[*argc] + pos) = ch;
pos += 1;
}
else
{
if (intoken)
{
intoken = FALSE;
inliteral = FALSE;
pos = 0;
}
}
}
else if (ch == 34)
{
if (inliteral)
{
intoken = FALSE;
inliteral = FALSE;
pos = 0;
}
else
{
intoken = TRUE;
inliteral = TRUE;
*argc += 1;
pos = 0;
}
}
else if (ch >= 33 && ch <= 126)
{
if (intoken)
{
*(args[*argc] + pos) = ch;
pos += 1;
}
else
{
*argc += 1;
pos = 0;
*(args[*argc] + pos) = ch;
pos += 1;
}
intoken = TRUE;
}
}
*argc += 1;
}