-
Notifications
You must be signed in to change notification settings - Fork 302
/
stringlist.c
123 lines (96 loc) · 2.3 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
#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 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;
}
_32BIT_INT StringList_Add(StringList *s, const char *str)
{
return ExtendableBuffer_Add((ExtendableBuffer *)s, str, strlen(str) + 1);
}
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;
}