Merge branch 'master' of https://git.maemo.org/projects/erwise
[erwise] / Cl / WWWLibrary / HTList.c
1 /*      A small List class                                            HTList.c
2 **      ==================
3 **
4 **      A list is represented as a sequence of linked nodes of type HTList.
5 **      The first node is a header which contains no object.
6 **      New nodes are inserted between the header and the rest of the list.
7 */
8
9 #include "HTList.h"
10
11 #ifdef ERWISE
12 #include <stdio.h>
13 #endif
14
15 HTList * HTList_new NOARGS
16 {
17   HTList *newList = (HTList *)malloc (sizeof (HTList));
18   if (newList == NULL) outofmem(__FILE__, "HTList_new");
19   newList->object = NULL;
20   newList->next = NULL;
21   return newList;
22 }
23
24 void HTList_delete ARGS1(HTList *,this)
25 {
26   HTList *current;
27   while (current = this) {
28     this = this->next;
29     free (current);
30   }
31 }
32
33 void HTList_addObject ARGS2(HTList *,this, void *,newObject)
34 {
35   if (this) {
36     HTList *newNode = (HTList *)malloc (sizeof (HTList));
37     if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
38     newNode->object = newObject;
39     newNode->next = this->next;
40     this->next = newNode;
41   }
42   else
43     if (TRACE) printf ("HTList: Trying to add object %p to a nonexisting list\n",
44                        newObject);
45 }
46
47 BOOL HTList_removeObject ARGS2(HTList *,this, void *,oldObject)
48 {
49   if (this) {
50     HTList *previous;
51     while (this->next) {
52       previous = this;
53       this = this->next;
54       if (this->object == oldObject) {
55         previous->next = this->next;
56         free (this);
57         return YES;  /* Success */
58       }
59     }
60   }
61   return NO;  /* object not found or NULL list */
62 }
63
64 void * HTList_removeLastObject ARGS1 (HTList *,this)
65 {
66   if (this && this->next) {
67     HTList *lastNode = this->next;
68     void * lastObject = lastNode->object;
69     this->next = lastNode->next;
70     free (lastNode);
71     return lastObject;
72   } else  /* Empty list */
73     return NULL;
74 }
75
76 void * HTList_removeFirstObject ARGS1 (HTList *,this)
77 {
78   if (this && this->next) {
79     HTList * prevNode;
80     void *firstObject;
81     while (this->next) {
82       prevNode = this;
83       this = this->next;
84     }
85     firstObject = this->object;
86     prevNode->next = NULL;
87     free (this);
88     return firstObject;
89   } else  /* Empty list */
90     return NULL;
91 }
92
93 int HTList_count ARGS1 (HTList *,this)
94 {
95   int count = 0;
96   if (this)
97     while (this = this->next)
98       count++;
99   return count;
100 }
101
102 int HTList_indexOf ARGS2(HTList *,this, void *,object)
103 {
104   if (this) {
105     int position = 0;
106     while (this = this->next) {
107       if (this->object == object)
108         return position;
109       position++;
110     }
111   }
112   return -1;  /* Object not in the list */
113 }
114
115 void * HTList_objectAt ARGS2 (HTList *,this, int,position)
116 {
117   if (position < 0)
118     return NULL;
119   if (this) {
120     while (this = this->next) {
121       if (position == 0)
122         return this->object;
123       position--;
124     }
125   }
126   return NULL;  /* Reached the end of the list */
127 }