/
string_list.c
141 lines (110 loc) · 2.63 KB
/
string_list.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
/*
Copyright (c) 2019 Colum Paget <colums.projects@googlemail.com>
* SPDX-License-Identifier: GPL-3.0
*/
#include "common.h"
#include "string_list.h"
TStringList *StringListCreate(int argc, char **argv)
{
TStringList *sl;
sl=(TStringList *) calloc(1, sizeof(TStringList));
sl->list=calloc(argc, sizeof(char *));
for (sl->size=0; sl->size < argc; sl->size++)
{
sl->list[sl->size]=strdup(argv[sl->size]);
}
return(sl);
}
int StringListAdd(TStringList *sl, const char *str)
{
if (str)
{
sl->list=realloc(sl->list, (sl->size+1) * sizeof(char *));
sl->list[sl->size]=strdup(str);
sl->size++;
}
return(sl->size -1);
}
int StringListDel(TStringList *sl, int pos)
{
if (sl->size > 0)
{
memmove(sl->list+pos, sl->list+(pos+1), (sl->size - pos) * sizeof(char *));
sl->size--;
}
return(sl->size);
}
void StringListSwap(TStringList *sl, int pos1, int pos2)
{
char *tmp;
tmp=sl->list[pos1];
sl->list[pos1]=sl->list[pos2];
sl->list[pos2]=tmp;
}
int StringListSplit(TStringList *sl, const char *str, const char *separators)
{
char *Token=NULL;
const char *ptr;
ptr=rstrtok(str, separators, &Token);
while (ptr)
{
StringListAdd(sl, Token);
ptr=rstrtok(ptr, separators, &Token);
}
destroy(Token);
return(sl->size);
}
const char *StringListItem(TStringList *sl, unsigned int pos)
{
if (pos >= sl->size) return(NULL);
return(sl->list[pos]);
}
const char *StringListGet(TStringList *sl, unsigned int pos)
{
if (pos >= sl->size) return(NULL);
sl->next=pos+1;
return(sl->list[pos]);
}
void StringListSet(TStringList *sl, unsigned int pos, const char *Str)
{
if (pos >= sl->size) return;
sl->list[pos]=rstrcpy(sl->list[pos], Str);
}
const char *StringListCurr(TStringList *sl)
{
if (! sl) return(NULL);
if (sl->size==0) return(NULL);
if (sl->next==0) return(NULL);
if (sl->next > sl->size) return(NULL);
return(sl->list[sl->next-1]);
}
const char *StringListPrev(TStringList *sl)
{
if (! sl) return(NULL);
if (sl->next ==0) return(NULL);
if (sl->next > 1) sl->next--;
return(sl->list[sl->next-1]);
}
const char *StringListNext(TStringList *sl)
{
if (! sl) return(NULL);
if (sl->next >= sl->size) return(NULL);
sl->next++;
return(sl->list[sl->next-1]);
}
void StringListClear(TStringList *sl)
{
int i;
for (i=0; i < sl->size; i++)
{
if (sl->list[i]) free(sl->list[i]);
}
free(sl->list);
sl->list=NULL;
sl->size=0;
}
void StringListDestroy(TStringList *sl)
{
StringListClear(sl);
free(sl);
}