2 * Copyright 2001-2004 Brandon Long
5 * ClearSilver Templating System
7 * This code is made available under the terms of the ClearSilver License.
8 * http://www.clearsilver.net/license.hdf
12 #include "cs_config.h"
22 #define ULIST_DEFAULT_SIZE 10
24 static NEOERR
*check_resize (ULIST
*ul
, int size
)
34 new_size
= size
+ ul
->max
;
37 new_items
= (void **) realloc ((void *)(ul
->items
), new_size
* sizeof(void *));
38 if (new_items
== NULL
)
40 return nerr_raise(NERR_NOMEM
,
41 "Unable to resize ULIST to %d: Out of memory", new_size
);
43 ul
->items
= new_items
;
51 NEOERR
*uListInit(ULIST
**ul
, int size
, int flags
)
58 size
= ULIST_DEFAULT_SIZE
;
61 r_ul
= (ULIST
*) calloc (1, sizeof (ULIST
));
64 return nerr_raise(NERR_NOMEM
, "Unable to create ULIST: Out of memory");
66 r_ul
->items
= (void **) calloc (size
, sizeof(void *));
67 if (r_ul
->items
== NULL
)
70 return nerr_raise(NERR_NOMEM
, "Unable to create ULIST: Out of memory");
81 NEOERR
*uListAppend (ULIST
*ul
, void *data
)
85 r
= check_resize (ul
, ul
->num
+ 1);
89 ul
->items
[ul
->num
] = data
;
95 NEOERR
*uListGet (ULIST
*ul
, int x
, void **data
)
101 return nerr_raise(NERR_OUTOFRANGE
, "uListGet: past end (%d > %d)",
105 return nerr_raise(NERR_OUTOFRANGE
, "uListGet: past beginning (%d < 0)", x
);
107 *data
= ul
->items
[x
];
112 NEOERR
*uListSort (ULIST
*ul
, int (*compareFunc
)(const void *, const void*)) {
113 qsort(ul
->items
, ul
->num
, sizeof(void *), compareFunc
);
117 NEOERR
*uListDestroy (ULIST
**ul
, int flags
)
119 if (flags
& ULIST_FREE
)
121 return uListDestroyFunc(ul
, free
);
125 return uListDestroyFunc(ul
, NULL
);
129 NEOERR
*uListDestroyFunc (ULIST
**ul
, void (*destroyFunc
)(void *))
138 if (destroyFunc
!= NULL
)
141 for (x
= 0; x
< r_ul
->num
; x
++)
143 (*destroyFunc
)(r_ul
->items
[x
]);
153 int uListLength (ULIST
*ul
)
155 if (ul
== NULL
) return 0;