HPDF_STATUS HPDF_NameTree_Add (HPDF_NameTree tree, HPDF_String name, void *obj) { HPDF_Array items; HPDF_INT32 i, icount; if (!tree || !name) return HPDF_INVALID_PARAMETER; items = HPDF_Dict_GetItem (tree, "Names", HPDF_OCLASS_ARRAY); if (!items) return HPDF_INVALID_OBJECT; /* "The keys shall be sorted in lexical order" -- 7.9.6, Name Trees. * Since we store keys sorted, it's best to do a linear insertion sort * Find the first element larger than 'key', and insert 'key' and then * 'obj' into the items. */ icount = HPDF_Array_Items(items); /* If we're larger than the last element, append */ if (icount) { HPDF_String last = HPDF_Array_GetItem(items, icount - 2, HPDF_OCLASS_STRING); if (HPDF_String_Cmp(name, last) > 0) { HPDF_Array_Add(items, name); HPDF_Array_Add(items, obj); return HPDF_OK; } } /* Walk backwards through the list until we're smaller than an element= * That's the element to insert in front of. */ for (i = icount - 4; i >= 0; i -= 2) { HPDF_String elem = HPDF_Array_GetItem(items, i, HPDF_OCLASS_STRING); if (i == 0 || HPDF_String_Cmp(name, elem) < 0) { HPDF_Array_Insert(items, elem, name); HPDF_Array_Insert(items, elem, obj); return HPDF_OK; } } /* Items list is empty */ HPDF_Array_Add(items, name); HPDF_Array_Add(items, obj); return HPDF_OK; }
HPDF_STATUS HPDF_Page_InsertBefore (HPDF_Page page, HPDF_Page target) { HPDF_Page parent; HPDF_Array kids; HPDF_STATUS ret; HPDF_PageAttr attr; HPDF_PTRACE((" HPDF_Page_InsertBefore\n")); if (!target) return HPDF_INVALID_PARAMETER; attr = (HPDF_PageAttr )target->attr; parent = attr->parent; if (!parent) return HPDF_PAGE_CANNOT_SET_PARENT; if (HPDF_Dict_GetItem (page, "Parent", HPDF_OCLASS_DICT)) return HPDF_SetError (parent->error, HPDF_PAGE_CANNOT_SET_PARENT, 0); if ((ret = HPDF_Dict_Add (page, "Parent", parent)) != HPDF_OK) return ret; kids = (HPDF_Array )HPDF_Dict_GetItem (parent, "Kids", HPDF_OCLASS_ARRAY); if (!kids) return HPDF_SetError (parent->error, HPDF_PAGES_MISSING_KIDS_ENTRY, 0); attr = (HPDF_PageAttr)page->attr; attr->parent = parent; return HPDF_Array_Insert (kids, target, page); }