summaryrefslogtreecommitdiff
path: root/lib/shlist.c
blob: 44919ef04606841f59aea4c6e89564cd0e0d8a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*-------------------------------------------------------------------*/
/*                         List  Functionality                       */
/*-------------------------------------------------------------------*/
/* #define SH_LIST_DEBUG */
/*-------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "shlist.h"
/*-------------------------------------------------------------------*/
void shListInitList( SHLIST *listPtr )
{
  listPtr->data = (void *)0L;
  listPtr->next = listPtr;
  listPtr->prev = listPtr;
}

SHLIST *shListFindItem( SHLIST *head, void *val, shListEqual func )
{
  SHLIST *item;

  for(item=head->next;( item != head );item=item->next)
    if( func ) {
      if( func( val, item->data ) ) {
        return( item );
      }
    }
    else {
      if( item->data == val ) {
        return( item );
      }
    }
  return( NULL );
}

SHLIST *shListGetLastItem( SHLIST *head )
{
  if( head->prev != head )
    return( head->prev );
  return( NULL );
}

SHLIST *shListGetFirstItem( SHLIST *head )
{
  if( head->next != head )
    return( head->next );
  return( NULL );
}

SHLIST *shListGetNItem( SHLIST *head, unsigned long num )
{
  SHLIST *item;
  unsigned long i;

  for(i=0,item=head->next;( (i < num) && (item != head) );i++,item=item->next);
  if( item != head )
    return( item );
  return( NULL );
}

SHLIST *shListGetNextItem( SHLIST *head, SHLIST *item )
{
  if( item == NULL )
    return( NULL );
  if( item->next != head )
    return( item->next );
  return( NULL );
}

SHLIST *shListGetPrevItem( SHLIST *head, SHLIST *item )
{
  if( item == NULL )
    return( NULL );
  if( item->prev != head )
    return( item->prev );
  return( NULL );
}

void shListDelItem( SHLIST *head, SHLIST *item, shListFree func )
{
  if( item == NULL )
    return;
#ifdef SH_LIST_DEBUG
  fprintf(stderr, "Del %lx\n", (unsigned long)(item->data));
#endif
  (item->prev)->next = item->next;
  (item->next)->prev = item->prev;
  if( func && item->data ) {
    func( (void *)(item->data) );
  }
  free( item );
  head->data = (void *)((unsigned long)(head->data) - 1);
}

void shListInsFirstItem( SHLIST *head, void *val )
{ /* Insert to the beginning of the list */
  SHLIST *item;

  item = (SHLIST *)malloc( sizeof(SHLIST) );
  if( item == NULL )
    return;
  item->data = val;
  item->next = head->next;
  item->prev = head;
  (head->next)->prev = item;
  head->next = item;
#ifdef SH_LIST_DEBUG
  fprintf(stderr, "Ins First %lx\n", (unsigned long)(item->data));
#endif
  head->data = (void *)((unsigned long)(head->data) + 1);
}

void shListInsLastItem( SHLIST *head, void *val )
{ /* Insert to the end of the list */
  SHLIST *item;

  item = (SHLIST *)malloc( sizeof(SHLIST) );
  if( item == NULL )
    return;
  item->data = val;
  item->next = head;
  item->prev = head->prev;
  (head->prev)->next = item;
  head->prev = item;
#ifdef SH_LIST_DEBUG
  fprintf(stderr, "Ins Last %lx\n", (unsigned long)(item->data));
#endif
  head->data = (void *)((unsigned long)(head->data) + 1);
}

void shListInsBeforeItem( SHLIST *head, void *val, void *etal, 
                          shListCmp func )
{
  SHLIST *item, *iptr;

  if( func == NULL )
    shListInsFirstItem( head, val );
  else {
    item = (SHLIST *)malloc( sizeof(SHLIST) );
    if( item == NULL )
      return;
    item->data = val;
    for(iptr=head->next;( iptr != head );iptr=iptr->next)
      if( func( val, iptr->data, etal ) )
         break;
    item->next = iptr;
    item->prev = iptr->prev;
    (iptr->prev)->next = item;
    iptr->prev = item;
#ifdef SH_LIST_DEBUG
    fprintf(stderr, "Ins Before %lx\n", (unsigned long)(item->data));
#endif
    head->data = (void *)((unsigned long)(head->data) + 1);
  }
}

void shListDelAllItems( SHLIST *head, shListFree func )
{
  SHLIST *item;

  for(item=head->next;( item != head );) {
    shListDelItem( head, item, func );
    item = head->next;
  }
  head->data = (void *)0L;
}

void shListPrintAllItems( SHLIST *head, shListPrint func )
{
#ifdef SH_LIST_DEBUG
  SHLIST *item;

  for(item=head->next;( item != head );item=item->next)
    if( func ) {
      func(item->data);
    }
    else {
      fprintf(stderr, "Item: %lx\n",(unsigned long)(item->data));
    }
#endif
}

unsigned long shListGetCount( SHLIST *head )
{
  return( (unsigned long)(head->data) );
}