forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringlist.c
executable file
·193 lines (147 loc) · 3.38 KB
/
stringlist.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
#include <string.h>
#include "stringlist.h"
#include "utils.h"
static int Divide(char *Str, char Delimiter)
{
int Count = 0;
char *Itr = Str;
for(Itr = strchr(Itr, Delimiter); Itr != NULL; Itr = strchr(Itr, Delimiter))
{
*Itr = '\0';
++Itr;
++Count;
}
return Count + 1;
}
int StringList_Init(__in StringList *s, __in const char *ori, __in char Delimiter)
{
if( s == NULL )
return -1;
if( ori == NULL )
{
ExtendableBuffer_Init((ExtendableBuffer *)s, 0, -1);
return 0;
} else {
if( ExtendableBuffer_Init((ExtendableBuffer *)s, strlen(ori) + 1, -1) != 0 )
{
return -1;
}
ExtendableBuffer_Add((ExtendableBuffer *)s, ori, strlen(ori) + 1);
return Divide(ExtendableBuffer_GetData((ExtendableBuffer *)s), Delimiter);
}
}
const char *StringList_GetNext(__in const StringList *s, __in const char *Current)
{
const char *n;
const char *End;
const char *Data;
if( s == NULL )
return NULL;
Data = ExtendableBuffer_GetData((ExtendableBuffer *)s);
if( Current == NULL )
return Data;
End = Data + ExtendableBuffer_GetUsedBytes((ExtendableBuffer *)s);
if( End == NULL || End == Data )
{
return NULL;
}
if( Current < Data || Current >= End )
return NULL;
n = Current + strlen(Current) + 1;
return n >= End ? NULL : n;
}
const char *StringList_Get(__in StringList *s, __in int Subscript)
{
int i = 0;
const char *itr;
if( s == NULL || Subscript < 0 )
return NULL;
for(itr = ExtendableBuffer_GetData((ExtendableBuffer *)s); i < Subscript; ++i)
{
itr = StringList_GetNext(s, itr);
if( itr == NULL )
return NULL;
}
return itr;
}
int StringList_Count(StringList *s)
{
int n = 0;
const char *itr = NULL;
if( s == NULL )
return 0;
for(itr = StringList_GetNext(s, itr); itr != NULL; itr = StringList_GetNext(s, itr))
{
++n;
}
return n;
}
int32_t StringList_Add(StringList *s, const char *str, char Delimiter)
{
int Offset = ExtendableBuffer_Add((ExtendableBuffer *)s, str, strlen(str) + 1);
if( Offset < 0 )
{
return -1;
}
Divide(s -> Data + Offset, Delimiter);
return Offset;
}
const char *StringList_Find(StringList *s, const char *str)
{
const char *itr = NULL;
if( s == NULL )
return 0;
for(itr = StringList_GetNext(s, itr); itr != NULL; itr = StringList_GetNext(s, itr))
{
if( strcmp(itr, str) == 0 )
{
return itr;
}
}
return NULL;
}
int32_t StringList_AppendLast(StringList *s, const char *str, char Delimiter)
{
char *Tail;
int Length = strlen(str);
if( s == NULL )
return 0;
if( ExtendableBuffer_GuarantyLeft(s, Length) == FALSE )
{
return -1;
}
Tail = s -> Data + s -> Used - 1;
s -> Used += Length;
strcat(Tail, str);
Divide(Tail, Delimiter);
return 0;
}
void StringList_Catenate(StringList *des, StringList *src)
{
if( des == NULL || src == NULL )
{
return;
}
ExtendableBuffer_Add(des, src -> Data, src -> Used);
}
const char **StringList_ToCharPtrArray(StringList *s)
{
const char **URLs;
int NumberOfURLs = 0;
int Count = StringList_Count(s);
const char *Str_Itr;
URLs = malloc(sizeof(char *) * (Count + 1));
if( URLs == NULL )
{
return NULL;
}
Str_Itr = StringList_GetNext(s, NULL);
while( Str_Itr != NULL )
{
URLs[NumberOfURLs] = Str_Itr;
++NumberOfURLs;
Str_Itr = StringList_GetNext(s, Str_Itr);
}
URLs[NumberOfURLs] = NULL;
return URLs;
}