-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringlist.c
122 lines (103 loc) · 2.71 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
#include "common.h"
#include "stringlist.h"
#include "strnatcmp.h"
// stringlists...
// ...DO NOT store duplicates of strings
// ...free the stored strings
struct stringlist *stringlist_create()
{
struct stringlist *list = malloc(sizeof(struct stringlist));
memset(list, 0, sizeof(struct stringlist));
list->count = 0;
list->size = 2;
list->data = calloc(list->size, sizeof(char *));
return list;
}
void stringlist_free(struct stringlist *list)
{
for(unsigned int i = 0; i < list->count; i++)
{
if(list->data[i])
free(list->data[i]);
}
free(list->data);
free(list);
}
struct stringlist *stringlist_copy(const struct stringlist *slist)
{
struct stringlist *new = malloc(sizeof(struct stringlist));
new->count = slist->count;
new->size = slist->size;
new->data = calloc(new->size, sizeof(char *));
for(unsigned int i = 0; i < slist->count; i++) // copy entries
new->data[i] = strdup(slist->data[i]);
return new;
}
void stringlist_add(struct stringlist *list, char *string)
{
if(list->count == list->size) // list is full, we need to allocate more memory
{
list->size <<= 1; // double size
list->data = realloc(list->data, list->size * sizeof(char *));
}
list->data[list->count++] = string;
}
void stringlist_del(struct stringlist *list, int pos)
{
assert(pos < (int)list->count);
if(list->data[pos])
free(list->data[pos]);
list->data[pos] = list->data[--list->count]; // copy last element into empty position
}
char *stringlist_shift(struct stringlist *list)
{
char *string;
if(list->count == 0)
return NULL;
string = strdup(list->data[0]);
free(list->data[0]);
list->count--;
memmove(list->data, list->data + 1, list->count * sizeof(*list->data));
return string;
}
int stringlist_find(struct stringlist *list, const char *string)
{
for(unsigned int i = 0; i < list->count; i++)
{
if(!strcasecmp(list->data[i], string))
return i;
}
return -1;
}
int stringlist_cmp(const void *a, const void *b)
{
return strnatcasecmp(*(const char **)a, *(const char **)b);
}
void stringlist_sort(struct stringlist *list)
{
qsort(list->data, list->count, sizeof(list->data[0]), stringlist_cmp);
}
struct stringlist *stringlist_build(const char *str, ...)
{
va_list args;
struct stringlist *list = stringlist_create();
stringlist_add(list, strdup(str));
va_start(args, str);
while((str = va_arg(args, const char *)))
stringlist_add(list, strdup(str));
va_end(args);
return list;
}
struct stringlist *stringlist_build_n(unsigned int count, ...)
{
va_list args;
struct stringlist *list = stringlist_create();
va_start(args, count);
for(unsigned int i = 0; i < count; i++)
{
const char *str = va_arg(args, const char *);
stringlist_add(list, str ? strdup(str) : NULL);
}
va_end(args);
return list;
}