1 /***************************************************************************/ 2 /* */ 3 /* ftlist.c */ 4 /* */ 5 /* Generic list support for FreeType (body). */ 6 /* */ 7 /* Copyright 1996-2001, 2002 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* This file is part of the FreeType project, and may only be used, */ 11 /* modified, and distributed under the terms of the FreeType project */ 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 /* this file you indicate that you have read the license and */ 14 /* understand and accept it fully. */ 15 /* */ 16 /***************************************************************************/ 17 18 /*************************************************************************/ 19 /* */ 20 /* This file implements functions relative to list processing. Its */ 21 /* data structures are defined in `freetype/internal/ftlist.h'. */ 22 /* */ 23 /*************************************************************************/ 24 25 26 #include <ft2build.h> 27 #include FT_LIST_H 28 #include FT_INTERNAL_DEBUG_H 29 #include FT_INTERNAL_OBJECTS_H 30 31 32 /*************************************************************************/ 33 /* */ 34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 36 /* messages during execution. */ 37 /* */ 38 #undef FT_COMPONENT 39 #define FT_COMPONENT trace_list 40 41 42 /* documentation is in ftlist.h */ 43 44 FT_EXPORT_DEF( FT_ListNode ) FT_List_Find(FT_List list,void * data)45 FT_List_Find( FT_List list, 46 void* data ) 47 { 48 FT_ListNode cur; 49 50 51 cur = list->head; 52 while ( cur ) 53 { 54 if ( cur->data == data ) 55 return cur; 56 57 cur = cur->next; 58 } 59 60 return (FT_ListNode)0; 61 } 62 63 64 /* documentation is in ftlist.h */ 65 66 FT_EXPORT_DEF( void ) FT_List_Add(FT_List list,FT_ListNode node)67 FT_List_Add( FT_List list, 68 FT_ListNode node ) 69 { 70 FT_ListNode before = list->tail; 71 72 73 node->next = 0; 74 node->prev = before; 75 76 if ( before ) 77 before->next = node; 78 else 79 list->head = node; 80 81 list->tail = node; 82 } 83 84 85 /* documentation is in ftlist.h */ 86 87 FT_EXPORT_DEF( void ) FT_List_Insert(FT_List list,FT_ListNode node)88 FT_List_Insert( FT_List list, 89 FT_ListNode node ) 90 { 91 FT_ListNode after = list->head; 92 93 94 node->next = after; 95 node->prev = 0; 96 97 if ( !after ) 98 list->tail = node; 99 else 100 after->prev = node; 101 102 list->head = node; 103 } 104 105 106 /* documentation is in ftlist.h */ 107 108 FT_EXPORT_DEF( void ) FT_List_Remove(FT_List list,FT_ListNode node)109 FT_List_Remove( FT_List list, 110 FT_ListNode node ) 111 { 112 FT_ListNode before, after; 113 114 115 before = node->prev; 116 after = node->next; 117 118 if ( before ) 119 before->next = after; 120 else 121 list->head = after; 122 123 if ( after ) 124 after->prev = before; 125 else 126 list->tail = before; 127 } 128 129 130 /* documentation is in ftlist.h */ 131 132 FT_EXPORT_DEF( void ) FT_List_Up(FT_List list,FT_ListNode node)133 FT_List_Up( FT_List list, 134 FT_ListNode node ) 135 { 136 FT_ListNode before, after; 137 138 139 before = node->prev; 140 after = node->next; 141 142 /* check whether we are already on top of the list */ 143 if ( !before ) 144 return; 145 146 before->next = after; 147 148 if ( after ) 149 after->prev = before; 150 else 151 list->tail = before; 152 153 node->prev = 0; 154 node->next = list->head; 155 list->head->prev = node; 156 list->head = node; 157 } 158 159 160 /* documentation is in ftlist.h */ 161 162 FT_EXPORT_DEF( FT_Error ) FT_List_Iterate(FT_List list,FT_List_Iterator iterator,void * user)163 FT_List_Iterate( FT_List list, 164 FT_List_Iterator iterator, 165 void* user ) 166 { 167 FT_ListNode cur = list->head; 168 FT_Error error = FT_Err_Ok; 169 170 171 while ( cur ) 172 { 173 FT_ListNode next = cur->next; 174 175 176 error = iterator( cur, user ); 177 if ( error ) 178 break; 179 180 cur = next; 181 } 182 183 return error; 184 } 185 186 187 /* documentation is in ftlist.h */ 188 189 FT_EXPORT_DEF( void ) FT_List_Finalize(FT_List list,FT_List_Destructor destroy,FT_Memory memory,void * user)190 FT_List_Finalize( FT_List list, 191 FT_List_Destructor destroy, 192 FT_Memory memory, 193 void* user ) 194 { 195 FT_ListNode cur; 196 197 198 cur = list->head; 199 while ( cur ) 200 { 201 FT_ListNode next = cur->next; 202 void* data = cur->data; 203 204 205 if ( destroy ) 206 destroy( memory, data, user ); 207 208 FT_FREE( cur ); 209 cur = next; 210 } 211 212 list->head = 0; 213 list->tail = 0; 214 } 215 216 217 /* END */ 218