Debian Codegraph @

src/main/print-list.c

raw symbols SCIP
1 /*
2 *
3 * Gutenprint list functions. A doubly-linked list implementation,
4 * with callbacks for freeing, sorting, and retrieving nodes by name
5 * or long name.
6 *
7 * Copyright 2002 Roger Leigh ([email protected])
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23 /*
24 * This file must include only standard C header files. The core code must
25 * compile on generic platforms that don't support glib, gimp, etc.
26 */
27
28
29 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:411:9`!
30 #include <config.h>cxx . . $ `<file>/config.h`/
31 #endif
32 #include <gutenprint/gutenprint.h>cxx . . $ `<file>/include/gutenprint/gutenprint.h`/
33 #include "gutenprint-internal.h"cxx . . $ `<file>/src/main/gutenprint-internal.h`/
34 #include <gutenprint/gutenprint-intl-internal.h>cxx . . $ `<file>/include/gutenprint/gutenprint-intl-internal.h`/
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 /** The internal representation of an stp_list_item_t list node. */
40 struct stp_list_itemcxx . . $ stp_list_item#
41 {
42 void *datacxx . . $ stp_list_item#data.; /*!< Data */
43 struct stp_list_itemcxx . . $ stp_list_item# *prevcxx . . $ stp_list_item#prev.; /*!< Previous node */
44 struct stp_list_itemcxx . . $ stp_list_item# *nextcxx . . $ stp_list_item#next.; /*!< Next node */
45 };
46
47 /** The internal representation of an stp_list_t list. */
48 struct stp_listcxx . . $ stp_list#
49 {
50 struct stp_list_itemcxx . . $ stp_list_item# *startcxx . . $ stp_list#start.; /*!< Start node */
51 struct stp_list_itemcxx . . $ stp_list_item# *endcxx . . $ stp_list#end.; /*!< End node */
52 struct stp_list_itemcxx . . $ stp_list_item# *index_cache_nodecxx . . $ stp_list#index_cache_node.; /*!< Cached node (for index) */
53 char *name_cachecxx . . $ stp_list#name_cache.; /*!< Cached name */
54 struct stp_list_itemcxx . . $ stp_list_item# *name_cache_nodecxx . . $ stp_list#name_cache_node.; /*!< Cached node (for name) */
55 char *long_name_cachecxx . . $ stp_list#long_name_cache.; /*!< Cached long name */
56 struct stp_list_itemcxx . . $ stp_list_item# *long_name_cache_nodecxx . . $ stp_list#long_name_cache_node.; /*!< Cached node (for long name) */
57 stp_node_freefunccxx . . $ stp_node_freefunc# freefunccxx . . $ stp_list#freefunc.; /*!< Callback to free node data */
58 stp_node_copyfunccxx . . $ stp_node_copyfunc# copyfunccxx . . $ stp_list#copyfunc.; /*!< Callback to copy node */
59 stp_node_namefunccxx . . $ stp_node_namefunc# namefunccxx . . $ stp_list#namefunc.; /*!< Callback to get node name */
60 stp_node_namefunccxx . . $ stp_node_namefunc# long_namefunccxx . . $ stp_list#long_namefunc.; /*!< Callback to get node long name */
61 stp_node_sortfunccxx . . $ stp_node_sortfunc# sortfunccxx . . $ stp_list#sortfunc.; /*!< Callback to compare (sort) nodes */
62 int index_cachecxx . . $ stp_list#index_cache.; /*!< Cached node index */
63 int lengthcxx . . $ stp_list#length.; /*!< Number of nodes */
64 };
65
66 /**
67 * Cache a list node by its short name.
68 * @param list the list to use.
69 * @param name the short name.
70 * @param cache the node to cache.
71 */
72 static void
73 set_name_cachecxx . . $ set_name_cache(abc8020fbbca0f9a).(stp_list_tcxx . . $ stp_list_t# *list,
74 const char *name,
75 stp_list_item_tcxx . . $ stp_list_item_t# *cache)
76 {
77 if (list->name_cachecxx . . $ stp_list#name_cache.)
78 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(list->name_cachecxx . . $ stp_list#name_cache.);
79 list->name_cachecxx . . $ stp_list#name_cache. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
80 if (name)
81 list->name_cachecxx . . $ stp_list#name_cache. = stp_strdupcxx . . $ stp_strdup(a8b8e32347182212).(name);
82 list->name_cache_nodecxx . . $ stp_list#name_cache_node. = cache;
83 }
84
85 /**
86 * Cache a list node by its long name.
87 * @param list the list to use.
88 * @param long_name the long name.
89 * @param cache the node to cache.
90 */
91 static void
92 set_long_name_cachecxx . . $ set_long_name_cache(abc8020fbbca0f9a).(stp_list_tcxx . . $ stp_list_t# *list,
93 const char *long_name,
94 stp_list_item_tcxx . . $ stp_list_item_t# *cache)
95 {
96 if (list->long_name_cachecxx . . $ stp_list#long_name_cache.)
97 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(list->long_name_cachecxx . . $ stp_list#long_name_cache.);
98 list->long_name_cachecxx . . $ stp_list#long_name_cache. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
99 if (long_name)
100 list->long_name_cachecxx . . $ stp_list#long_name_cache. = stp_strdupcxx . . $ stp_strdup(a8b8e32347182212).(long_name);
101 list->long_name_cache_nodecxx . . $ stp_list#long_name_cache_node. = cache;
102 }
103
104 /**
105 * Clear cached nodes.
106 * @param list the list to use.
107 */
108 static inline void
109 clear_cachecxx . . $ clear_cache(36a0435a5e15cb95).(stp_list_tcxx . . $ stp_list_t# *list)
110 {
111 list->index_cachecxx . . $ stp_list#index_cache. = 0;
112 list->index_cache_nodecxx . . $ stp_list#index_cache_node. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
113 set_name_cachecxx . . $ set_name_cache(abc8020fbbca0f9a).(list, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!);
114 set_long_name_cachecxx . . $ set_long_name_cache(abc8020fbbca0f9a).(list, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!);
115 }
116
117 void
118 stp_list_node_free_datacxx . . $ stp_list_node_free_data(5d22bdacc48458e8). (void *item)
119 {
120 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(item);
121 stp_deprintfcxx . . $ stp_deprintf(3d303ebd7ab3b1e3).(STP_DBG_LISTcxx . . $ `include/gutenprint/util.h:177:9`!, "stp_list_node_free_data destructor\n");
122 }
123
124 /** Check the validity of a list. */
125 #define check_listcxx . . $ `src/main/print-list.c:125:9`!(List) STPI_ASSERTcxx . . $ `src/main/gutenprint-internal.h:58:9`!(List != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
126
127 /* List head functions.
128 * These functions operate on the list as a whole, and not the
129 * individual nodes in a list. */
130
131 /* create a new list */
132 stp_list_tcxx . . $ stp_list_t# *
133 stp_list_createcxx . . $ stp_list_create(728167c67729d082).(void)
134 {
135 stp_list_tcxx . . $ stp_list_t# *list =
136 stp_malloccxx . . $ stp_malloc(bfcfa4d6b7f7ef64).(sizeof(stp_list_tcxx . . $ stp_list_t#));
137
138 /* initialise an empty list */
139 list->index_cachecxx . . $ stp_list#index_cache. = 0;
140 list->lengthcxx . . $ stp_list#length. = 0;
141 list->startcxx . . $ stp_list#start. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
142 list->endcxx . . $ stp_list#end. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
143 list->index_cache_nodecxx . . $ stp_list#index_cache_node. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
144 list->freefunccxx . . $ stp_list#freefunc. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
145 list->namefunccxx . . $ stp_list#namefunc. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
146 list->long_namefunccxx . . $ stp_list#long_namefunc. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
147 list->sortfunccxx . . $ stp_list#sortfunc. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
148 list->copyfunccxx . . $ stp_list#copyfunc. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
149 list->name_cachecxx . . $ stp_list#name_cache. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
150 list->name_cache_nodecxx . . $ stp_list#name_cache_node. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
151 list->long_name_cachecxx . . $ stp_list#long_name_cache. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
152 list->long_name_cache_nodecxx . . $ stp_list#long_name_cache_node. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
153
154 stp_deprintfcxx . . $ stp_deprintf(3d303ebd7ab3b1e3).(STP_DBG_LISTcxx . . $ `include/gutenprint/util.h:177:9`!, "stp_list_head constructor\n");
155 return list;
156 }
157
158 stp_list_tcxx . . $ stp_list_t# *
159 stp_list_copycxx . . $ stp_list_copy(a0eb3ec16ac58482).(const stp_list_tcxx . . $ stp_list_t# *list)
160 {
161 stp_list_tcxx . . $ stp_list_t# *ret;
162 stp_node_copyfunccxx . . $ stp_node_copyfunc# copyfunc = stp_list_get_copyfunccxx . . $ stp_list_get_copyfunc(2c97a0b6118d8860).(list);
163 stp_list_item_tcxx . . $ stp_list_item_t# *item = list->startcxx . . $ stp_list#start.;
164
165 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
166
167 ret = stp_list_createcxx . . $ stp_list_create(728167c67729d082).();
168 stp_list_set_copyfunccxx . . $ stp_list_set_copyfunc(84c06c9bbe527258).(ret, stp_list_get_copyfunccxx . . $ stp_list_get_copyfunc(2c97a0b6118d8860).(list));
169 /* If we use default (shallow) copy, we can't free the elements of it */
170 if (stp_list_get_copyfunccxx . . $ stp_list_get_copyfunc(2c97a0b6118d8860).(list))
171 stp_list_set_freefunccxx . . $ stp_list_set_freefunc(309a84c3f950ef37).(ret, stp_list_get_freefunccxx . . $ stp_list_get_freefunc(e9bd5815d65d3fe9).(list));
172 stp_list_set_namefunccxx . . $ stp_list_set_namefunc(83503c4472347457).(ret, stp_list_get_namefunccxx . . $ stp_list_get_namefunc(b8e8c741a8902241).(list));
173 stp_list_set_long_namefunccxx . . $ stp_list_set_long_namefunc(83503c4472347457).(ret, stp_list_get_long_namefunccxx . . $ stp_list_get_long_namefunc(b8e8c741a8902241).(list));
174 stp_list_set_sortfunccxx . . $ stp_list_set_sortfunc(9dc8d599ee4ca1c).(ret, stp_list_get_sortfunccxx . . $ stp_list_get_sortfunc(d369e83346210011).(list));
175 while (item)
176 {
177 void *data = item->datacxx . . $ stp_list_item#data.;
178 if (copyfunc)
179 stp_list_item_createcxx . . $ stp_list_item_create(54f08bf465e39eec). (ret, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, (*copyfunc)(data));
180 else
181 stp_list_item_createcxx . . $ stp_list_item_create(54f08bf465e39eec).(ret, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, data);
182 item = stp_list_item_nextcxx . . $ stp_list_item_next(83833ec172b48f23).(item);
183 }
184 return ret;
185 }
186
187 /* free a list, freeing all child nodes first */
188 int
189 stp_list_destroycxx . . $ stp_list_destroy(29ae8ff464c7f7e5).(stp_list_tcxx . . $ stp_list_t# *list)
190 {
191 stp_list_item_tcxx . . $ stp_list_item_t# *cur;
192 stp_list_item_tcxx . . $ stp_list_item_t# *next;
193
194 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
195 clear_cachecxx . . $ clear_cache(36a0435a5e15cb95).(list);
196 cur = list->startcxx . . $ stp_list#start.;
197 while(cur)
198 {
199 next = cur->nextcxx . . $ stp_list_item#next.;
200 stp_list_item_destroycxx . . $ stp_list_item_destroy(e8436428ff1ae51).(list, cur);
201 cur = next;
202 }
203 stp_deprintfcxx . . $ stp_deprintf(3d303ebd7ab3b1e3).(STP_DBG_LISTcxx . . $ `include/gutenprint/util.h:177:9`!, "stp_list_head destructor\n");
204 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(list);
205
206 return 0;
207 }
208
209 int
210 stp_list_get_lengthcxx . . $ stp_list_get_length(c31cc8fb9e1f67aa).(const stp_list_tcxx . . $ stp_list_t# *list)
211 {
212 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
213 return list->lengthcxx . . $ stp_list#length.;
214 }
215
216 /* find a node */
217
218 /* get the first node in the list */
219
220 stp_list_item_tcxx . . $ stp_list_item_t# *
221 stp_list_get_startcxx . . $ stp_list_get_start(44f6202a74729935).(const stp_list_tcxx . . $ stp_list_t# *list)
222 {
223 return list->startcxx . . $ stp_list#start.;
224 }
225
226 /* get the last node in the list */
227
228 stp_list_item_tcxx . . $ stp_list_item_t# *
229 stp_list_get_endcxx . . $ stp_list_get_end(44f6202a74729935).(const stp_list_tcxx . . $ stp_list_t# *list)
230 {
231 return list->endcxx . . $ stp_list#end.;
232 }
233
234 static inline stp_list_tcxx . . $ stp_list_t# *
235 deconst_listcxx . . $ deconst_list(a0eb3ec16ac58482).(const stp_list_tcxx . . $ stp_list_t# *list)
236 {
237 return (stp_list_tcxx . . $ stp_list_t# *) stpi_cast_safecxx . . $ stpi_cast_safe(3343d82129b43479).(list);
238 }
239
240 /* get the node by its place in the list */
241 stp_list_item_tcxx . . $ stp_list_item_t# *
242 stp_list_get_item_by_indexcxx . . $ stp_list_get_item_by_index(868af1dce2882582).(const stp_list_tcxx . . $ stp_list_t# *list, int idx)
243 {
244 stp_list_item_tcxx . . $ stp_list_item_t# *node = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
245 stp_list_tcxx . . $ stp_list_t# *ulist = deconst_listcxx . . $ deconst_list(a0eb3ec16ac58482).(list);
246 int i; /* current index */
247 int d = 0; /* direction of list traversal, 0=forward */
248 int c = 0; /* use cache? */
249 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
250
251 if (idx >= list->lengthcxx . . $ stp_list#length.)
252 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
253
254 /*
255 * Optimize the most likely cases of looking for the same, next,
256 * or previou item
257 * */
258 if (ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.)
259 {
260 if (idx == ulist->index_cachecxx . . $ stp_list#index_cache.)
261 return ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.;
262 else if (idx == ulist->index_cachecxx . . $ stp_list#index_cache. + 1)
263 {
264 ulist->index_cachecxx . . $ stp_list#index_cache. = idx;
265 ulist->index_cache_nodecxx . . $ stp_list#index_cache_node. = ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.->nextcxx . . $ stp_list_item#next.;
266 return ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.;
267 }
268 else if (idx == ulist->index_cachecxx . . $ stp_list#index_cache. - 1)
269 {
270 ulist->index_cachecxx . . $ stp_list#index_cache. = idx;
271 ulist->index_cache_nodecxx . . $ stp_list#index_cache_node. = ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.->prevcxx . . $ stp_list_item#prev.;
272 return ulist->index_cache_nodecxx . . $ stp_list#index_cache_node.;
273 }
274 }
275 /*
276 * See if using the cache is worthwhile. If the desired index is closer
277 * to the cached index than it is to the start or end, it will be faster
278 * to start from the cached element.
279 *
280 * Otherwise, decide which direction is best to start from.
281 */
282 if (list->index_cachecxx . . $ stp_list#index_cache.)
283 {
284 if (idx < (list->lengthcxx . . $ stp_list#length./2))
285 {
286 if (idx > abscxx . . $ abs(7864480464b09eea).(idx - list->index_cachecxx . . $ stp_list#index_cache.))
287 c = 1;
288 else
289 d = 0;
290 }
291 else
292 {
293 if (list->lengthcxx . . $ stp_list#length. - 1 - idx >
294 abscxx . . $ abs(7864480464b09eea). (list->lengthcxx . . $ stp_list#length. - 1 - idx - list->index_cachecxx . . $ stp_list#index_cache.))
295 c = 1;
296 else
297 d = 1;
298 }
299 }
300
301 /* use the cached index and node */
302 if (c)
303 {
304 if (idx > list->index_cachecxx . . $ stp_list#index_cache.) /* forward */
305 d = 0;
306 else /* backward */
307 d = 1;
308 i = list->index_cachecxx . . $ stp_list#index_cache.;
309 node = list->index_cache_nodecxx . . $ stp_list#index_cache_node.;
310 }
311 else /* start from one end of the list */
312 {
313 if (d)
314 {
315 i = list->lengthcxx . . $ stp_list#length. - 1;
316 node = list->endcxx . . $ stp_list#end.;
317 }
318 else
319 {
320 i = 0;
321 node = list->startcxx . . $ stp_list#start.;
322 }
323 }
324
325 while (node && i != idx)
326 {
327 if (d)
328 {
329 i--;
330 node = node->prevcxx . . $ stp_list_item#prev.;
331 }
332 else
333 {
334 i++;
335 node = node->nextcxx . . $ stp_list_item#next.;
336 }
337 }
338
339 /* update cache */
340 ulist->index_cachecxx . . $ stp_list#index_cache. = i;
341 ulist->index_cache_nodecxx . . $ stp_list#index_cache_node. = node;
342
343 return node;
344 }
345
346 /**
347 * Find an item in a list by its name.
348 * This internal helper is not optimised to use any caching.
349 * @param list the list to use.
350 * @param name the name to find.
351 * @returns a pointer to the list item, or NULL if the name is
352 * invalid or the list is empty.
353 */
354 static stp_list_item_tcxx . . $ stp_list_item_t# *
355 stp_list_get_item_by_name_internalcxx . . $ stp_list_get_item_by_name_internal(27765cfe86595311).(const stp_list_tcxx . . $ stp_list_t# *list, const char *name)
356 {
357 stp_list_item_tcxx . . $ stp_list_item_t# *node = list->startcxx . . $ stp_list#start.;
358 while (node && strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, list->namefunccxx . . $ stp_list#namefunc.(node->datacxx . . $ stp_list_item#data.)))
359 {
360 node = node->nextcxx . . $ stp_list_item#next.;
361 }
362 return node;
363 }
364
365 /* get the first node with name; requires a callback function to
366 read data */
367 stp_list_item_tcxx . . $ stp_list_item_t# *
368 stp_list_get_item_by_namecxx . . $ stp_list_get_item_by_name(27765cfe86595311).(const stp_list_tcxx . . $ stp_list_t# *list, const char *name)
369 {
370 stp_list_item_tcxx . . $ stp_list_item_t# *node = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
371 stp_list_tcxx . . $ stp_list_t# *ulist = deconst_listcxx . . $ deconst_list(a0eb3ec16ac58482).(list);
372 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
373
374 if (!list->namefunccxx . . $ stp_list#namefunc. || !name)
375 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
376
377 if (list->name_cachecxx . . $ stp_list#name_cache. && list->name_cache_nodecxx . . $ stp_list#name_cache_node.)
378 {
379 const char *new_name;
380 node = list->name_cache_nodecxx . . $ stp_list#name_cache_node.;
381 /* Is this the item we've cached? */
382 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, list->name_cachecxx . . $ stp_list#name_cache.) == 0 &&
383 strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, list->namefunccxx . . $ stp_list#namefunc.(node->datacxx . . $ stp_list_item#data.)) == 0)
384 return node;
385
386 /* If not, check the next item in case we're searching the list */
387 node = node->nextcxx . . $ stp_list_item#next.;
388 if (node)
389 {
390 new_name = list->namefunccxx . . $ stp_list#namefunc.(node->datacxx . . $ stp_list_item#data.);
391 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, new_name) == 0)
392 {
393 set_name_cachecxx . . $ set_name_cache(abc8020fbbca0f9a).(ulist, new_name, node);
394 return node;
395 }
396 }
397 /* If not, check the index cache */
398 node = list->index_cache_nodecxx . . $ stp_list#index_cache_node.;
399 if (node)
400 {
401 new_name = list->namefunccxx . . $ stp_list#namefunc.(node->datacxx . . $ stp_list_item#data.);
402 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, new_name) == 0)
403 {
404 set_name_cachecxx . . $ set_name_cache(abc8020fbbca0f9a).(ulist, new_name, node);
405 return node;
406 }
407 }
408 }
409
410 node = stp_list_get_item_by_name_internalcxx . . $ stp_list_get_item_by_name_internal(27765cfe86595311).(list, name);
411
412 if (node)
413 set_name_cachecxx . . $ set_name_cache(abc8020fbbca0f9a).(ulist, name, node);
414
415 return node;
416 }
417
418
419 /**
420 * Find an item in a list by its long name.
421 * This internal helper is not optimised to use any caching.
422 * @param list the list to use.
423 * @param long_name the long name to find.
424 * @returns a pointer to the list item, or NULL if the long name is
425 * invalid or the list is empty.
426 */
427 static stp_list_item_tcxx . . $ stp_list_item_t# *
428 stp_list_get_item_by_long_name_internalcxx . . $ stp_list_get_item_by_long_name_internal(27765cfe86595311).(const stp_list_tcxx . . $ stp_list_t# *list,
429 const char *long_name)
430 {
431 stp_list_item_tcxx . . $ stp_list_item_t# *node = list->startcxx . . $ stp_list#start.;
432 while (node && strcmpcxx . . $ strcmp(5ae29d726ae57f88).(long_name, list->long_namefunccxx . . $ stp_list#long_namefunc.(node->datacxx . . $ stp_list_item#data.)))
433 {
434 node = node->nextcxx . . $ stp_list_item#next.;
435 }
436 return node;
437 }
438
439 /* get the first node with long_name; requires a callack function to
440 read data */
441 stp_list_item_tcxx . . $ stp_list_item_t# *
442 stp_list_get_item_by_long_namecxx . . $ stp_list_get_item_by_long_name(27765cfe86595311).(const stp_list_tcxx . . $ stp_list_t# *list, const char *long_name)
443 {
444 stp_list_item_tcxx . . $ stp_list_item_t# *node = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
445 stp_list_tcxx . . $ stp_list_t# *ulist = deconst_listcxx . . $ deconst_list(a0eb3ec16ac58482).(list);
446 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
447
448 if (!list->long_namefunccxx . . $ stp_list#long_namefunc. || !long_name)
449 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
450
451 if (list->long_name_cachecxx . . $ stp_list#long_name_cache. && list->long_name_cache_nodecxx . . $ stp_list#long_name_cache_node.)
452 {
453 const char *new_long_name;
454 node = list->long_name_cache_nodecxx . . $ stp_list#long_name_cache_node.;
455 /* Is this the item we've cached? */
456 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(long_name, list->long_name_cachecxx . . $ stp_list#long_name_cache.) == 0 &&
457 strcmpcxx . . $ strcmp(5ae29d726ae57f88).(long_name, list->long_namefunccxx . . $ stp_list#long_namefunc.(node->datacxx . . $ stp_list_item#data.)) == 0)
458 return node;
459
460 /* If not, check the next item in case we're searching the list */
461 node = node->nextcxx . . $ stp_list_item#next.;
462 if (node)
463 {
464 new_long_name = list->long_namefunccxx . . $ stp_list#long_namefunc.(node->datacxx . . $ stp_list_item#data.);
465 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(long_name, new_long_name) == 0)
466 {
467 set_long_name_cachecxx . . $ set_long_name_cache(abc8020fbbca0f9a).(ulist, new_long_name, node);
468 return node;
469 }
470 }
471 /* If not, check the index cache */
472 node = list->index_cache_nodecxx . . $ stp_list#index_cache_node.;
473 if (node)
474 {
475 new_long_name = list->long_namefunccxx . . $ stp_list#long_namefunc.(node->datacxx . . $ stp_list_item#data.);
476 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(long_name, new_long_name) == 0)
477 {
478 set_long_name_cachecxx . . $ set_long_name_cache(abc8020fbbca0f9a).(ulist, new_long_name, node);
479 return node;
480 }
481 }
482 }
483
484 node = stp_list_get_item_by_long_name_internalcxx . . $ stp_list_get_item_by_long_name_internal(27765cfe86595311).(list, long_name);
485
486 if (node)
487 set_long_name_cachecxx . . $ set_long_name_cache(abc8020fbbca0f9a).(ulist, long_name, node);
488
489 return node;
490 }
491
492
493 /* callback for freeing data */
494 void
495 stp_list_set_freefunccxx . . $ stp_list_set_freefunc(309a84c3f950ef37).(stp_list_tcxx . . $ stp_list_t# *list, stp_node_freefunccxx . . $ stp_node_freefunc# freefunc)
496 {
497 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
498 list->freefunccxx . . $ stp_list#freefunc. = freefunc;
499 }
500
501 stp_node_freefunccxx . . $ stp_node_freefunc#
502 stp_list_get_freefunccxx . . $ stp_list_get_freefunc(e9bd5815d65d3fe9).(const stp_list_tcxx . . $ stp_list_t# *list)
503 {
504 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
505 return list->freefunccxx . . $ stp_list#freefunc.;
506 }
507
508 /* callback for copying data */
509 void
510 stp_list_set_copyfunccxx . . $ stp_list_set_copyfunc(84c06c9bbe527258).(stp_list_tcxx . . $ stp_list_t# *list, stp_node_copyfunccxx . . $ stp_node_copyfunc# copyfunc)
511 {
512 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
513 list->copyfunccxx . . $ stp_list#copyfunc. = copyfunc;
514 }
515
516 stp_node_copyfunccxx . . $ stp_node_copyfunc#
517 stp_list_get_copyfunccxx . . $ stp_list_get_copyfunc(2c97a0b6118d8860).(const stp_list_tcxx . . $ stp_list_t# *list)
518 {
519 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
520 return list->copyfunccxx . . $ stp_list#copyfunc.;
521 }
522
523 /* callback for getting data name */
524 void
525 stp_list_set_namefunccxx . . $ stp_list_set_namefunc(83503c4472347457).(stp_list_tcxx . . $ stp_list_t# *list, stp_node_namefunccxx . . $ stp_node_namefunc# namefunc)
526 {
527 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
528 list->namefunccxx . . $ stp_list#namefunc. = namefunc;
529 }
530
531 stp_node_namefunccxx . . $ stp_node_namefunc#
532 stp_list_get_namefunccxx . . $ stp_list_get_namefunc(b8e8c741a8902241).(const stp_list_tcxx . . $ stp_list_t# *list)
533 {
534 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
535 return list->namefunccxx . . $ stp_list#namefunc.;
536 }
537
538 /* callback for getting data long_name */
539 void
540 stp_list_set_long_namefunccxx . . $ stp_list_set_long_namefunc(83503c4472347457).(stp_list_tcxx . . $ stp_list_t# *list, stp_node_namefunccxx . . $ stp_node_namefunc# long_namefunc)
541 {
542 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
543 list->long_namefunccxx . . $ stp_list#long_namefunc. = long_namefunc;
544 }
545
546 stp_node_namefunccxx . . $ stp_node_namefunc#
547 stp_list_get_long_namefunccxx . . $ stp_list_get_long_namefunc(b8e8c741a8902241).(const stp_list_tcxx . . $ stp_list_t# *list)
548 {
549 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
550 return list->long_namefunccxx . . $ stp_list#long_namefunc.;
551 }
552
553 /* callback for sorting nodes */
554 void
555 stp_list_set_sortfunccxx . . $ stp_list_set_sortfunc(9dc8d599ee4ca1c).(stp_list_tcxx . . $ stp_list_t# *list, stp_node_sortfunccxx . . $ stp_node_sortfunc# sortfunc)
556 {
557 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
558 list->sortfunccxx . . $ stp_list#sortfunc. = sortfunc;
559 }
560
561 stp_node_sortfunccxx . . $ stp_node_sortfunc#
562 stp_list_get_sortfunccxx . . $ stp_list_get_sortfunc(d369e83346210011).(const stp_list_tcxx . . $ stp_list_t# *list)
563 {
564 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
565 return list->sortfunccxx . . $ stp_list#sortfunc.;
566 }
567
568
569 /* list item functions */
570
571 /* these functions operate on individual nodes in a list */
572
573 /*
574 * create a new node in list, before next (may be null e.g. if sorting
575 * next is calculated automatically, else defaults to end). Must be
576 * initialised with data (null nodes are disallowed). The
577 * stp_list_item_t type can not exist unless it is associated with an
578 * stp_list_t list head.
579 */
580 int
581 stp_list_item_createcxx . . $ stp_list_item_create(54f08bf465e39eec).(stp_list_tcxx . . $ stp_list_t# *list,
582 stp_list_item_tcxx . . $ stp_list_item_t# *next,
583 const void *data)
584 {
585 stp_list_item_tcxx . . $ stp_list_item_t# *ln; /* list node to add */
586 stp_list_item_tcxx . . $ stp_list_item_t# *lnn; /* list node next */
587
588 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
589
590 clear_cachecxx . . $ clear_cache(36a0435a5e15cb95).(list);
591
592 ln = stp_malloccxx . . $ stp_malloc(bfcfa4d6b7f7ef64).(sizeof(stp_list_item_tcxx . . $ stp_list_item_t#));
593 ln->prevcxx . . $ stp_list_item#prev. = ln->nextcxx . . $ stp_list_item#next. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
594
595 if (data)
596 ln->datacxx . . $ stp_list_item#data. = stpi_cast_safecxx . . $ stpi_cast_safe(3343d82129b43479).(data);
597 else
598 {
599 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(ln);
600 return 1;
601 }
602
603 if (list->sortfunccxx . . $ stp_list#sortfunc.)
604 {
605 /* set np to the previous node (before the insertion */
606 lnn = list->endcxx . . $ stp_list#end.;
607 while (lnn)
608 {
609 if (list->sortfunccxx . . $ stp_list#sortfunc.(lnn->datacxx . . $ stp_list_item#data., ln->datacxx . . $ stp_list_item#data.) <= 0)
610 break;
611 lnn = lnn->prevcxx . . $ stp_list_item#prev.;
612 }
613 }
614 else
615 lnn = next;
616
617 /* got lnp; now insert the new ln */
618
619 /* set next */
620 ln->nextcxx . . $ stp_list_item#next. = lnn;
621
622 if (!ln->prevcxx . . $ stp_list_item#prev.) /* insert at start of list */
623 {
624 if (list->startcxx . . $ stp_list#start.) /* list not empty */
625 ln->prevcxx . . $ stp_list_item#prev. = list->endcxx . . $ stp_list#end.;
626 else
627 list->startcxx . . $ stp_list#start. = ln;
628 list->endcxx . . $ stp_list#end. = ln;
629 }
630
631 /* set prev (already set if at start of list) */
632
633 if (!ln->prevcxx . . $ stp_list_item#prev. && ln->nextcxx . . $ stp_list_item#next.) /* insert at end of list */
634 ln->prevcxx . . $ stp_list_item#prev. = ln->nextcxx . . $ stp_list_item#next.->prevcxx . . $ stp_list_item#prev.;
635
636 if (list->startcxx . . $ stp_list#start. == ln->nextcxx . . $ stp_list_item#next.) /* prev was old end */
637 {
638 list->startcxx . . $ stp_list#start. = ln;
639 }
640
641 /* set next->prev */
642 if (ln->nextcxx . . $ stp_list_item#next.)
643 ln->nextcxx . . $ stp_list_item#next.->prevcxx . . $ stp_list_item#prev. = ln;
644
645 /* set prev->next */
646 if (ln->prevcxx . . $ stp_list_item#prev.)
647 ln->prevcxx . . $ stp_list_item#prev.->nextcxx . . $ stp_list_item#next. = ln;
648
649 /* increment reference count */
650 list->lengthcxx . . $ stp_list#length.++;
651
652 stp_deprintfcxx . . $ stp_deprintf(3d303ebd7ab3b1e3).(STP_DBG_LISTcxx . . $ `include/gutenprint/util.h:177:9`!, "stp_list_node constructor\n");
653 return 0;
654 }
655
656 /* remove a node from list */
657 int
658 stp_list_item_destroycxx . . $ stp_list_item_destroy(e8436428ff1ae51).(stp_list_tcxx . . $ stp_list_t# *list, stp_list_item_tcxx . . $ stp_list_item_t# *item)
659 {
660 check_listcxx . . $ `src/main/print-list.c:125:9`!(list);
661
662 clear_cachecxx . . $ clear_cache(36a0435a5e15cb95).(list);
663 /* decrement reference count */
664 list->lengthcxx . . $ stp_list#length.--;
665
666 if (list->freefunccxx . . $ stp_list#freefunc.)
667 list->freefunccxx . . $ stp_list#freefunc.((void *) item->datacxx . . $ stp_list_item#data.);
668 if (item->prevcxx . . $ stp_list_item#prev.)
669 item->prevcxx . . $ stp_list_item#prev.->nextcxx . . $ stp_list_item#next. = item->nextcxx . . $ stp_list_item#next.;
670 else
671 list->startcxx . . $ stp_list#start. = item->nextcxx . . $ stp_list_item#next.;
672 if (item->nextcxx . . $ stp_list_item#next.)
673 item->nextcxx . . $ stp_list_item#next.->prevcxx . . $ stp_list_item#prev. = item->prevcxx . . $ stp_list_item#prev.;
674 else
675 list->endcxx . . $ stp_list#end. = item->prevcxx . . $ stp_list_item#prev.;
676 stp_freecxx . . $ stp_free(5d22bdacc48458e8).(item);
677
678 stp_deprintfcxx . . $ stp_deprintf(3d303ebd7ab3b1e3).(STP_DBG_LISTcxx . . $ `include/gutenprint/util.h:177:9`!, "stp_list_node destructor\n");
679 return 0;
680 }
681
682 /* get previous node, but don't update the cache */
683 stp_list_item_tcxx . . $ stp_list_item_t# *
684 stp_list_item_prevcxx . . $ stp_list_item_prev(83833ec172b48f23).(const stp_list_item_tcxx . . $ stp_list_item_t# *item)
685 {
686 return item->prevcxx . . $ stp_list_item#prev.;
687 }
688
689 /* get next node, but don't update the cache */
690 stp_list_item_tcxx . . $ stp_list_item_t# *
691 stp_list_item_nextcxx . . $ stp_list_item_next(83833ec172b48f23).(const stp_list_item_tcxx . . $ stp_list_item_t# *item)
692 {
693 return item->nextcxx . . $ stp_list_item#next.;
694 }
695
696 /* get data for node */
697 void *
698 stp_list_item_get_datacxx . . $ stp_list_item_get_data(9a5daa75441a53b5).(const stp_list_item_tcxx . . $ stp_list_item_t# *item)
699 {
700 return item->datacxx . . $ stp_list_item#data.;
701 }
702
703 /* set data for node */
704 int
705 stp_list_item_set_datacxx . . $ stp_list_item_set_data(3f61c1a85efb50ba).(stp_list_item_tcxx . . $ stp_list_item_t# *item, void *data)
706 {
707 if (data)
708 {
709 item->datacxx . . $ stp_list_item#data. = data;
710 return 0;
711 }
712 return 1; /* return error if data was NULL */
713 }