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
|
#include <string.h>
#include <stdlib.h>
#include <l4arg.h>
#include <l4darr.h>
/* 基本上優先順序是 escape -> quoting -> delimiter */
#define abort_l4arg_toargv \
do{ \
l4da_free(parr); \
l4da_free(tmpstr); \
return NULL; \
}while(0)
char** l4arg_toargv(const char* str,
const char* delim, const char* quoting, const char* esc){
int i;
char escaped = 0, quoted = 0, delimed = 0;
L4DA* parr;
L4DA* tmpstr;
char* addstr;
char** rval;
parr = l4da_create(sizeof(char*), 0);
if(parr == NULL){
return NULL;
}
tmpstr = l4da_create(sizeof(char), 0);
if(tmpstr == NULL){
l4da_free(parr);
return NULL;
}
for(i=0; str[i]!='\0'; i++){
if(escaped){
if(l4da_pushback(tmpstr, &str[i]) < 0){
abort_l4arg_toargv;
}
escaped = 0;
delimed = 0;
continue;
}
if(quoted){
if(strchr(quoting, str[i]) != NULL){
quoted = 0;
continue;
}
if(l4da_pushback(tmpstr, &str[i]) < 0){
abort_l4arg_toargv;
}
delimed = 0;
}
if(strchr(esc, str[i]) != NULL){
escaped = 1;
continue;
}
if(strchr(quoting, str[i]) != NULL){
quoted = 1;
continue;
}
if(strchr(delim, str[i]) != NULL){
if(l4da_pushback(tmpstr, '\0') < 0){
abort_l4arg_toargv;
}
addstr = (char*)l4da_drop_struct(tmpstr);
if(l4da_pushback(parr, &addstr) < 0){
l4da_free(parr);
return NULL;
}
tmpstr = l4da_create(sizeof(char), 0);
if(tmpstr == NULL){
l4da_free(parr);
return NULL;
}
delimed = 1;
}
if(l4da_pushback(tmpstr, &str[i]) < 0){
abort_l4arg_toargv;
}
delimed = 0;
}
if(!delimed){
if(l4da_pushback(tmpstr, '\0') < 0){
abort_l4arg_toargv;
}
addstr = (char*)l4da_drop_struct(tmpstr);
if(l4da_pushback(parr, &addstr) < 0){
l4da_free(parr);
return NULL;
}
}
rval = (char**)l4da_drop_struct(parr);
return rval;
}
void l4arg_toargv_free(char** pargv){
int i;
for(i=0; pargv[i]!=NULL; i++){
free(pargv[i]);
}
free(pargv);
}
|