ftlist.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* This file implements functions relative to list processing. Its */
  20. /* data structures are defined in `freetype/internal/ftlist.h'. */
  21. /* */
  22. /*************************************************************************/
  23. #include <ft2build.h>
  24. #include FT_LIST_H
  25. #include FT_INTERNAL_DEBUG_H
  26. #include FT_INTERNAL_OBJECTS_H
  27. /*************************************************************************/
  28. /* */
  29. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  30. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  31. /* messages during execution. */
  32. /* */
  33. #undef FT_COMPONENT
  34. #define FT_COMPONENT trace_list
  35. /* documentation is in ftlist.h */
  36. FT_EXPORT_DEF( FT_ListNode )
  37. FT_List_Find( FT_List list,
  38. void* data )
  39. {
  40. FT_ListNode cur;
  41. cur = list->head;
  42. while ( cur )
  43. {
  44. if ( cur->data == data )
  45. return cur;
  46. cur = cur->next;
  47. }
  48. return (FT_ListNode)0;
  49. }
  50. /* documentation is in ftlist.h */
  51. FT_EXPORT_DEF( void )
  52. FT_List_Add( FT_List list,
  53. FT_ListNode node )
  54. {
  55. FT_ListNode before = list->tail;
  56. node->next = 0;
  57. node->prev = before;
  58. if ( before )
  59. before->next = node;
  60. else
  61. list->head = node;
  62. list->tail = node;
  63. }
  64. /* documentation is in ftlist.h */
  65. FT_EXPORT_DEF( void )
  66. FT_List_Insert( FT_List list,
  67. FT_ListNode node )
  68. {
  69. FT_ListNode after = list->head;
  70. node->next = after;
  71. node->prev = 0;
  72. if ( !after )
  73. list->tail = node;
  74. else
  75. after->prev = node;
  76. list->head = node;
  77. }
  78. /* documentation is in ftlist.h */
  79. FT_EXPORT_DEF( void )
  80. FT_List_Remove( FT_List list,
  81. FT_ListNode node )
  82. {
  83. FT_ListNode before, after;
  84. before = node->prev;
  85. after = node->next;
  86. if ( before )
  87. before->next = after;
  88. else
  89. list->head = after;
  90. if ( after )
  91. after->prev = before;
  92. else
  93. list->tail = before;
  94. }
  95. /* documentation is in ftlist.h */
  96. FT_EXPORT_DEF( void )
  97. FT_List_Up( FT_List list,
  98. FT_ListNode node )
  99. {
  100. FT_ListNode before, after;
  101. before = node->prev;
  102. after = node->next;
  103. /* check whether we are already on top of the list */
  104. if ( !before )
  105. return;
  106. before->next = after;
  107. if ( after )
  108. after->prev = before;
  109. else
  110. list->tail = before;
  111. node->prev = 0;
  112. node->next = list->head;
  113. list->head->prev = node;
  114. list->head = node;
  115. }
  116. /* documentation is in ftlist.h */
  117. FT_EXPORT_DEF( FT_Error )
  118. FT_List_Iterate( FT_List list,
  119. FT_List_Iterator iterator,
  120. void* user )
  121. {
  122. FT_ListNode cur = list->head;
  123. FT_Error error = FT_Err_Ok;
  124. while ( cur )
  125. {
  126. FT_ListNode next = cur->next;
  127. error = iterator( cur, user );
  128. if ( error )
  129. break;
  130. cur = next;
  131. }
  132. return error;
  133. }
  134. /* documentation is in ftlist.h */
  135. FT_EXPORT_DEF( void )
  136. FT_List_Finalize( FT_List list,
  137. FT_List_Destructor destroy,
  138. FT_Memory memory,
  139. void* user )
  140. {
  141. FT_ListNode cur;
  142. cur = list->head;
  143. while ( cur )
  144. {
  145. FT_ListNode next = cur->next;
  146. void* data = cur->data;
  147. if ( destroy )
  148. destroy( memory, data, user );
  149. FT_FREE( cur );
  150. cur = next;
  151. }
  152. list->head = 0;
  153. list->tail = 0;
  154. }
  155. /* END */