2 * << Haru Free PDF Library >> -- hpdf_list.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
18 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_consts.h"
21 #include "hpdf_list.h"
24 Resize (HPDF_List list,
31 * mmgr : handle to a HPDF_MMgr object.
32 * items_per_block : number of increases of pointers.
34 * return: If HPDF_List_New success, it returns a handle to new HPDF_List
35 * object, otherwise it returns NULL.
40 HPDF_List_New (HPDF_MMgr mmgr,
41 HPDF_UINT items_per_block)
45 HPDF_PTRACE((" HPDF_List_New\n"));
50 list = (HPDF_List)HPDF_GetMem (mmgr, sizeof(HPDF_List_Rec));
53 list->error = mmgr->error;
55 list->items_per_block =
56 (items_per_block <= 0 ? HPDF_DEF_ITEMS_PER_BLOCK : items_per_block);
67 * list : Pointer to a HPDF_List object.
68 * item : Pointer to a object to be added.
70 * return: If HPDF_List_Add success, it returns HPDF_OK.
71 * HPDF_FAILD_TO_ALLOC_MEM is returned when the expansion of the
72 * object list is failed.
77 HPDF_List_Add (HPDF_List list,
80 HPDF_PTRACE((" HPDF_List_Add\n"));
82 if (list->count >= list->block_siz) {
83 HPDF_STATUS ret = Resize (list,
84 list->block_siz + list->items_per_block);
91 list->obj[list->count++] = item;
99 * list : Pointer to a HPDF_List object.
100 * target : Pointer to the target object.
101 * item : Pointer to a object to be inserted.
103 * insert the item before the target.
105 * return: If HPDF_List_Add success, it returns HPDF_OK.
106 * HPDF_FAILD_TO_ALLOC_MEM is returned when the expansion of the
107 * object list is failed.
108 * HPDF_ITEM_NOT_FOUND is returned where the target object is not
114 HPDF_List_Insert (HPDF_List list,
118 HPDF_INT target_idx = HPDF_List_Find (list, target);
119 void *last_item = list->obj[list->count - 1];
122 HPDF_PTRACE((" HPDF_List_Insert\n"));
125 return HPDF_ITEM_NOT_FOUND;
127 /* move the item of the list to behind one by one. */
128 for (i = list->count - 2; i >= target_idx; i--)
129 list->obj[i + 1] = list->obj[i];
131 list->obj[target_idx] = item;
133 return HPDF_List_Add (list, last_item);
139 * Remove the object specified by item parameter from the list object. The
140 * memory area that the object uses is not released.
142 * list : Pointer to a HPDF_List object.
143 * item : Pointer to a object to be remove.
145 * return: If HPDF_List_Remove success, it returns HPDF_OK.
146 * HPDF_ITEM_NOT_FOUND is returned when the object specified by item
147 * parameter is not found.
152 HPDF_List_Remove (HPDF_List list,
156 void **obj = list->obj;
158 HPDF_PTRACE((" HPDF_List_Remove\n"));
160 for (i = 0; i < list->count; i++) {
162 HPDF_List_RemoveByIndex(list, i);
168 return HPDF_ITEM_NOT_FOUND;
172 * HPDF_List_RemoveByIndex
174 * Remove the object by index number.
176 * list : Pointer to a HPDF_List object.
177 * index : Index of a object to be remove.
179 * return: If HPDF_List_RemoveByIndex success, it returns HPDF_OK.
180 * HPDF_ITEM_NOT_FOUND is returned when the value which is specified
181 * by index parameter is invalid.
186 HPDF_List_RemoveByIndex (HPDF_List list,
191 HPDF_PTRACE((" HPDF_List_RemoveByIndex\n"));
193 if (list->count <= index)
196 tmp = list->obj[index];
198 while (index < list->count - 1) {
199 list->obj[index] = list->obj[index + 1];
211 * list : Pointer to a HPDF_List object.
212 * index : Index of a object.
214 * return: If HPDF_List_at success, it returns a pointer to the object.
215 * otherwise it returns NULL.
220 HPDF_List_ItemAt (HPDF_List list,
223 HPDF_PTRACE((" HPDF_List_ItemAt\n"));
225 return (list->count <= index) ? NULL : list->obj[index];
231 * list : Pointer to a HPDF_List object.
236 HPDF_List_Free (HPDF_List list)
238 HPDF_PTRACE((" HPDF_List_Free\n"));
243 HPDF_List_Clear (list);
244 HPDF_FreeMem (list->mmgr, list);
250 * list : Pointer to a HPDF_List object.
255 HPDF_List_Clear (HPDF_List list)
257 HPDF_PTRACE((" HPDF_List_Clear\n"));
260 HPDF_FreeMem (list->mmgr, list->obj);
270 * list : Pointer to a HPDF_List object.
271 * count : The size of array of pointers.
273 * return: If Resize success, it returns HPDF_OK.
274 * otherwise it returns error-code which is set by HPDF_MMgr object.
279 Resize (HPDF_List list,
284 HPDF_PTRACE((" HPDF_List_Resize\n"));
286 if (list->count >= count) {
287 if (list->count == count)
290 return HPDF_INVALID_PARAMETER;
293 new_obj = (void **)HPDF_GetMem (list->mmgr, count * sizeof(void *));
296 return HPDF_Error_GetCode (list->error);
299 HPDF_MemCpy ((HPDF_BYTE *)new_obj, (HPDF_BYTE *)list->obj,
300 list->block_siz * sizeof(void *));
302 list->block_siz = count;
304 HPDF_FreeMem (list->mmgr, list->obj);
313 * list : Pointer to a HPDF_List object.
314 * count : the size of array of pointers.
316 * return: If HPDF_List_Find success, it returns index of the object.
317 * otherwise it returns negative value.
322 HPDF_List_Find (HPDF_List list,
327 HPDF_PTRACE((" HPDF_List_Find\n"));
329 for (i = 0; i < list->count; i++) {
330 if (list->obj[i] == item)