Debian Codegraph @

texk/kpathsea/str-list.c

raw
1 /* str-list.c: define routines for string lists.
2
3 Copyright 1993, 2008, 2012, 2018 Karl Berry.
4 Copyright 2001, 2005 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 #include <kpathsea/config.h>
20
21 #include <kpathsea/str-list.h>
22
23
24 /* See the .h file for comments. */
25
26
27 void
28 str_list_add (str_list_type *l, string s)
29 {
30 STR_LIST_LENGTH (*l)++;
31 XRETALLOC (STR_LIST (*l), STR_LIST_LENGTH (*l), string);
32 STR_LIST_LAST_ELT (*l) = s;
33 }
34
35 void
36 cstr_list_add (cstr_list_type *l, const_string s)
37 {
38 STR_LIST_LENGTH (*l)++;
39 XRETALLOC (STR_LIST (*l), STR_LIST_LENGTH (*l), const_string);
40 STR_LIST_LAST_ELT (*l) = s;
41 }
42
43
44 /* May as well save some reallocations and do everything in a chunk
45 instead of calling str_list_add on each element. */
46
47 void
48 str_list_concat (str_list_type *target, str_list_type more)
49 {
50 unsigned e;
51 unsigned prev_len = STR_LIST_LENGTH (*target);
52
53 STR_LIST_LENGTH (*target) += STR_LIST_LENGTH (more);
54 XRETALLOC (STR_LIST (*target), STR_LIST_LENGTH (*target), string);
55
56 for (e = 0; e < STR_LIST_LENGTH (more); e++) {
57 STR_LIST_ELT (*target, prev_len + e) = STR_LIST_ELT (more, e);
58 }
59 }
60
61
62 /* Concatenate the elements of MORE to each element of TARGET. This
63 _must_ be done with the first index varying fastest.
64 We free the old elements of TARGET. */
65
66 void
67 str_list_concat_elements (str_list_type *target, str_list_type more)
68 {
69 if (STR_LIST_EMPTY (more)) {
70 return;
71
72 } else if (STR_LIST_EMPTY (*target)) {
73 unsigned i;
74 STR_LIST_LENGTH (*target) = STR_LIST_LENGTH (more);
75 STR_LIST (*target) = (string *) xmalloc (STR_LIST_LENGTH (more)
76 * sizeof (char *));
77 for (i = 0; i != STR_LIST_LENGTH (more); ++i) {
78 STR_LIST_ELT (*target, i) = xstrdup (STR_LIST_ELT (more, i));
79 }
80 return;
81
82 } else {
83 unsigned new_len;
84 char **new_list;
85 unsigned i, j;
86 new_list = (string *) xmalloc (STR_LIST_LENGTH (*target)
87 * STR_LIST_LENGTH (more)
88 * sizeof (char *));
89 new_len = 0;
90 for (j = 0; j != STR_LIST_LENGTH (more); ++j) {
91 for (i = 0; i != STR_LIST_LENGTH (*target); ++i) {
92 new_list[new_len] = concat (STR_LIST_ELT (*target, i),
93 STR_LIST_ELT (more, j));
94 ++new_len;
95 }
96 }
97 for (i = 0; i != STR_LIST_LENGTH(*target); ++i) {
98 free (STR_LIST_ELT (*target, i));
99 }
100 free (STR_LIST (*target));
101 STR_LIST_LENGTH (*target) = new_len;
102 STR_LIST (*target) = new_list;
103 }
104 }
105
106
107 /* Free the list (but not the elements within it). */
108
109 void
110 str_list_free (str_list_type *l)
111 {
112 if (STR_LIST (*l)) {
113 free (STR_LIST (*l));
114 STR_LIST (*l) = NULL;
115 }
116 }
117
118
119
120 /* Remove duplicate elements from L, freeing their space. Since our
121 lists are so short, we do a maximally inefficient bubble search. */
122
123 void
124 str_list_uniqify (str_list_type *l)
125 {
126 unsigned e;
127 str_list_type ret = str_list_init ();
128
129 for (e = 0; e < STR_LIST_LENGTH (*l); e++) {
130 string elt1 = STR_LIST_ELT (*l, e);
131 /* printf ("outer l[%d]: %s\n", e, elt1); */
132
133 /* Compare against known-unique list ret; this keeps the elements in
134 the return list in the same order as the input list. */
135 unsigned f;
136 for (f = 0; f < STR_LIST_LENGTH (ret); f++) {
137 string elt2 = STR_LIST_ELT (ret, f);
138 /* printf(" inner ret[%d]: %s\n", f, elt2); */
139
140 /* I don't think our lists should ever contain NULL's, but if
141 it does, let them stay and don't bother collapsing multiple
142 NULL's into one. */
143 if (FILESTRCASEEQ (elt1, elt2)) {
144 break;
145 }
146 }
147
148 if (f == STR_LIST_LENGTH (ret)) {
149 /* printf (" added new\n"); */
150 str_list_add (&ret, elt1); /* not found */
151 } else {
152 /* printf (" skipped duplicate\n"); */
153 free (elt1); /* duplicate, forget this one */
154 }
155 }
156
157 /* Replace the passed list with what we constructed. */
158 *l = ret;
159 }
160
161 #ifdef TEST
162 int
163 main ()
164 {
165 unsigned e;
166 str_list_type tst = str_list_init ();
167 str_list_add (&tst, xstrdup("a"));
168 str_list_add (&tst, xstrdup("a"));
169 str_list_add (&tst, xstrdup("b"));
170 str_list_add (&tst, xstrdup("a"));
171 str_list_uniqify (&tst);
172
173 for (e = 0; e < STR_LIST_LENGTH (tst); e++) {
174 printf ("ret[%d]: %s\n", e, STR_LIST_ELT(tst, e));
175 }
176
177 return 0;
178 }
179
180 #endif /* TEST */
181
182
183 /*
184 Local variables:
185 standalone-compile-command: "gcc -g -I. -I.. -I$WK -DTEST -DMAKE_KPSE_DLL str-list.c $wk/.libs/libkpathsea.a"
186 End:
187 */