/[baselayout]/trunk/src/librc-strlist.c
Gentoo

Contents of /trunk/src/librc-strlist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2889 - (show annotations) (download) (as text)
Tue Sep 18 15:43:19 2007 UTC (6 years, 9 months ago) by uberlord
File MIME type: text/x-csrc
File size: 3546 byte(s)
Don't be an ass - don't free the 2nd list. Instead just empty it.
1 /*
2 librc-strlist.h
3 String list functions for using char ** arrays
4
5 Copyright 2007 Gentoo Foundation
6 Based on a previous implementation by Martin Schlemmer
7 Released under the GPLv2
8 */
9
10 #include "librc.h"
11
12 static char *_rc_strlist_add (char ***list, const char *item, bool uniq)
13 {
14 char **newlist;
15 char **lst = *list;
16 int i = 0;
17
18 if (! item)
19 return (NULL);
20
21 while (lst && lst[i]) {
22 if (uniq && strcmp (lst[i], item) == 0) {
23 errno = EEXIST;
24 return (NULL);
25 }
26 i++;
27 }
28
29 newlist = rc_xrealloc (lst, sizeof (char *) * (i + 2));
30 newlist[i] = rc_xstrdup (item);
31 newlist[i + 1] = NULL;
32
33 *list = newlist;
34 return (newlist[i]);
35 }
36
37 char *rc_strlist_add (char ***list, const char *item)
38 {
39 return (_rc_strlist_add (list, item, false));
40 }
41 librc_hidden_def(rc_strlist_add)
42
43 char *rc_strlist_addu (char ***list, const char *item)
44 {
45 return (_rc_strlist_add (list, item, true));
46 }
47 librc_hidden_def(rc_strlist_addu)
48
49 static char *_rc_strlist_addsort (char ***list, const char *item,
50 int (*sortfunc) (const char *s1,
51 const char *s2),
52 bool uniq)
53 {
54 char **newlist;
55 char **lst = *list;
56 int i = 0;
57 char *tmp1;
58 char *tmp2;
59 char *retval;
60
61 if (! item)
62 return (NULL);
63
64 while (lst && lst[i]) {
65 if (uniq && strcmp (lst[i], item) == 0) {
66 errno = EEXIST;
67 return (NULL);
68 }
69 i++;
70 }
71
72 newlist = rc_xrealloc (lst, sizeof (char *) * (i + 2));
73
74 if (! i)
75 newlist[i] = NULL;
76 newlist[i + 1] = NULL;
77
78 i = 0;
79 while (newlist[i] && sortfunc (newlist[i], item) < 0)
80 i++;
81
82 tmp1 = newlist[i];
83 retval = newlist[i] = rc_xstrdup (item);
84 do {
85 i++;
86 tmp2 = newlist[i];
87 newlist[i] = tmp1;
88 tmp1 = tmp2;
89 } while (tmp1);
90
91 *list = newlist;
92 return (retval);
93 }
94
95 char *rc_strlist_addsort (char ***list, const char *item)
96 {
97 return (_rc_strlist_addsort (list, item, strcoll, false));
98 }
99 librc_hidden_def(rc_strlist_addsort)
100
101 char *rc_strlist_addsortc (char ***list, const char *item)
102 {
103 return (_rc_strlist_addsort (list, item, strcmp, false));
104 }
105 librc_hidden_def(rc_strlist_addsortc)
106
107 char *rc_strlist_addsortu (char ***list, const char *item)
108 {
109 return (_rc_strlist_addsort (list, item, strcmp, true));
110 }
111 librc_hidden_def(rc_strlist_addsortu)
112
113 int rc_strlist_delete (char ***list, const char *item)
114 {
115 char **lst = *list;
116 int i = 0;
117 int retval = -1;
118
119 if (!lst || ! item)
120 return (-1);
121
122 while (lst[i])
123 if (! strcmp (lst[i], item)) {
124 free (lst[i]);
125 retval = 0;
126 do {
127 lst[i] = lst[i + 1];
128 i++;
129 } while (lst[i]);
130 }
131
132 if (retval)
133 errno = ENOENT;
134 return (retval);
135 }
136 librc_hidden_def(rc_strlist_delete)
137
138 int rc_strlist_join (char ***list1, char **list2)
139 {
140 char **lst1 = *list1;
141 char **newlist;
142 int i = 0;
143 int j = 0;
144
145 if (! list2)
146 return (0);
147
148 while (lst1 && lst1[i])
149 i++;
150
151 while (list2[j])
152 j++;
153
154 newlist = rc_xrealloc (lst1, sizeof (char *) * (i + j + 1));
155
156 j = 0;
157 while (list2[j]) {
158 newlist[i] = list2[j];
159 /* Take the item off the 2nd list as it's only a shallow copy */
160 list2[j] = NULL;
161 i++;
162 j++;
163 }
164 newlist[i] = NULL;
165
166 *list1 = newlist;
167 return (0);
168 }
169 librc_hidden_def(rc_strlist_join)
170
171 void rc_strlist_reverse (char **list)
172 {
173 char *item;
174 int i = 0;
175 int j = 0;
176
177 if (! list)
178 return;
179
180 while (list[j])
181 j++;
182 j--;
183
184 while (i < j && list[i] && list[j]) {
185 item = list[i];
186 list[i] = list[j];
187 list[j] = item;
188 i++;
189 j--;
190 }
191 }
192 librc_hidden_def(rc_strlist_reverse)
193
194 void rc_strlist_free (char **list)
195 {
196 int i = 0;
197
198 if (! list)
199 return;
200
201 while (list[i])
202 free (list[i++]);
203
204 free (list);
205 }
206 librc_hidden_def(rc_strlist_free)

  ViewVC Help
Powered by ViewVC 1.1.20