Annotation of libwww/Library/src/HTList.c, revision 2.14
2.8 frystyk 1: /* HTList.c
2: ** MANAGEMENT OF LINKED LISTS
3: **
2.12 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.8 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.1 timbl 6: **
7: ** A list is represented as a sequence of linked nodes of type HTList.
8: ** The first node is a header which contains no object.
9: ** New nodes are inserted between the header and the rest of the list.
10: */
11:
2.10 frystyk 12: /* Library include files */
13: #include "tcp.h"
14: #include "HTUtils.h"
1.1 timbl 15: #include "HTList.h"
16:
2.14 ! frystyk 17: HTList * HTList_new (void)
1.1 timbl 18: {
19: HTList *newList = (HTList *)malloc (sizeof (HTList));
20: if (newList == NULL) outofmem(__FILE__, "HTList_new");
21: newList->object = NULL;
22: newList->next = NULL;
23: return newList;
24: }
25:
2.14 ! frystyk 26: void HTList_delete (HTList * me)
1.1 timbl 27: {
28: HTList *current;
2.6 luotonen 29: while ((current = me)) {
2.1 timbl 30: me = me->next;
1.1 timbl 31: free (current);
32: }
33: }
34:
2.14 ! frystyk 35: BOOL HTList_addObject (HTList * me, void * newObject)
1.1 timbl 36: {
2.11 frystyk 37: if (me) {
38: HTList *newNode = (HTList *)malloc (sizeof (HTList));
39: if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
40: newNode->object = newObject;
41: newNode->next = me->next;
42: me->next = newNode;
43: return YES;
44: } else {
2.13 frystyk 45: if (WWWTRACE)
2.11 frystyk 46: fprintf(TDEST,
47: "HTList...... Can not add object %p to nonexisting list\n",
48: newObject);
49: }
50: return NO;
1.1 timbl 51: }
52:
2.14 ! frystyk 53: BOOL HTList_removeObject (HTList * me, void * oldObject)
1.1 timbl 54: {
2.11 frystyk 55: if (me) {
56: HTList *previous;
57: while (me->next) {
58: previous = me;
59: me = me->next;
60: if (me->object == oldObject) { /* HWL: sigbus error */
61: previous->next = me->next;
62: free (me);
63: return YES; /* Success */
64: }
65: }
1.1 timbl 66: }
2.11 frystyk 67: return NO; /* object not found or NULL list */
1.1 timbl 68: }
69:
2.14 ! frystyk 70: void * HTList_removeLastObject (HTList * me)
1.1 timbl 71: {
2.1 timbl 72: if (me && me->next) {
73: HTList *lastNode = me->next;
1.1 timbl 74: void * lastObject = lastNode->object;
2.1 timbl 75: me->next = lastNode->next;
1.1 timbl 76: free (lastNode);
77: return lastObject;
78: } else /* Empty list */
79: return NULL;
80: }
81:
2.14 ! frystyk 82: void * HTList_removeFirstObject (HTList * me)
1.1 timbl 83: {
2.1 timbl 84: if (me && me->next) {
1.1 timbl 85: HTList * prevNode;
86: void *firstObject;
2.1 timbl 87: while (me->next) {
88: prevNode = me;
89: me = me->next;
1.1 timbl 90: }
2.1 timbl 91: firstObject = me->object;
1.1 timbl 92: prevNode->next = NULL;
2.1 timbl 93: free (me);
1.1 timbl 94: return firstObject;
95: } else /* Empty list */
96: return NULL;
97: }
98:
2.14 ! frystyk 99: int HTList_count (HTList * me)
1.1 timbl 100: {
101: int count = 0;
2.1 timbl 102: if (me)
2.6 luotonen 103: while ((me = me->next))
1.1 timbl 104: count++;
105: return count;
106: }
107:
2.14 ! frystyk 108: int HTList_indexOf (HTList * me, void * object)
1.1 timbl 109: {
2.1 timbl 110: if (me) {
1.1 timbl 111: int position = 0;
2.6 luotonen 112: while ((me = me->next)) {
2.1 timbl 113: if (me->object == object)
1.1 timbl 114: return position;
115: position++;
116: }
117: }
118: return -1; /* Object not in the list */
119: }
120:
2.14 ! frystyk 121: void * HTList_objectAt (HTList * me, int position)
1.1 timbl 122: {
123: if (position < 0)
124: return NULL;
2.1 timbl 125: if (me) {
2.6 luotonen 126: while ((me = me->next)) {
1.1 timbl 127: if (position == 0)
2.1 timbl 128: return me->object;
1.1 timbl 129: position--;
130: }
131: }
132: return NULL; /* Reached the end of the list */
133: }
2.10 frystyk 134:
135:
2.14 ! frystyk 136: void * HTList_removeObjectAt (HTList * me, int position)
2.10 frystyk 137: {
138: if (position < 0)
139: return NULL;
140: if (me) {
141: HTList * prevNode;
142: prevNode = me;
143: while ((me = me->next)) {
144: if (position == 0) {
145: prevNode->next = me->next;
146: return me->object;
147: }
148: prevNode = me;
149: position--;
150: }
151: }
152: return NULL; /* Reached the end of the list */
153: }
154:
Webmaster