Annotation of libwww/Library/src/HTList.c, revision 2.24
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.
2.24 ! frystyk 6: ** @(#) $Id: HTList.c,v 2.23 1996/04/12 17:47:30 frystyk Exp $
1.1 timbl 7: **
8: ** A list is represented as a sequence of linked nodes of type HTList.
9: ** The first node is a header which contains no object.
10: ** New nodes are inserted between the header and the rest of the list.
11: */
12:
2.10 frystyk 13: /* Library include files */
2.22 frystyk 14: #include "sysdep.h"
2.10 frystyk 15: #include "HTUtils.h"
1.1 timbl 16: #include "HTList.h"
17:
2.17 frystyk 18: PUBLIC HTList * HTList_new (void)
1.1 timbl 19: {
2.20 frystyk 20: HTList *newList;
21: if ((newList = (HTList *) HT_CALLOC(1, sizeof (HTList))) == NULL)
22: HT_OUTOFMEM("HTList_new");
2.17 frystyk 23: newList->object = NULL;
24: newList->next = NULL;
25: return newList;
1.1 timbl 26: }
27:
2.17 frystyk 28: PUBLIC BOOL HTList_delete (HTList * me)
1.1 timbl 29: {
2.15 frystyk 30: if (me) {
31: HTList *current;
32: while ((current = me)) {
33: me = me->next;
2.20 frystyk 34: HT_FREE(current);
2.15 frystyk 35: }
36: return YES;
37: }
38: return NO;
1.1 timbl 39: }
40:
2.17 frystyk 41: PUBLIC BOOL HTList_addObject (HTList * me, void * newObject)
1.1 timbl 42: {
2.11 frystyk 43: if (me) {
2.20 frystyk 44: HTList *newNode;
45: if ((newNode = (HTList *) HT_CALLOC(1, sizeof(HTList))) == NULL)
46: HT_OUTOFMEM("HTList_addObject");
2.11 frystyk 47: newNode->object = newObject;
48: newNode->next = me->next;
49: me->next = newNode;
50: return YES;
51: } else {
2.13 frystyk 52: if (WWWTRACE)
2.21 eric 53: HTTrace(
2.11 frystyk 54: "HTList...... Can not add object %p to nonexisting list\n",
55: newObject);
56: }
57: return NO;
1.1 timbl 58: }
59:
2.18 frystyk 60: PUBLIC BOOL HTList_appendObject (HTList * me, void * newObject)
61: {
62: if (me) {
63: while (me->next) me = me->next;
64: return HTList_addObject(me, newObject);
65: }
66: return NO;
67: }
68:
2.17 frystyk 69: PUBLIC BOOL HTList_removeObject (HTList * me, void * oldObject)
1.1 timbl 70: {
2.11 frystyk 71: if (me) {
72: HTList *previous;
73: while (me->next) {
74: previous = me;
75: me = me->next;
2.19 frystyk 76: if (me->object == oldObject) {
2.11 frystyk 77: previous->next = me->next;
2.20 frystyk 78: HT_FREE(me);
2.11 frystyk 79: return YES; /* Success */
80: }
81: }
1.1 timbl 82: }
2.11 frystyk 83: return NO; /* object not found or NULL list */
1.1 timbl 84: }
85:
2.17 frystyk 86: PUBLIC void * HTList_removeLastObject (HTList * me)
1.1 timbl 87: {
2.17 frystyk 88: if (me && me->next) {
89: HTList *lastNode = me->next;
90: void * lastObject = lastNode->object;
91: me->next = lastNode->next;
2.20 frystyk 92: HT_FREE(lastNode);
2.17 frystyk 93: return lastObject;
94: } else /* Empty list */
95: return NULL;
96: }
97:
98: PUBLIC void * HTList_removeFirstObject (HTList * me)
99: {
100: if (me && me->next) {
101: HTList * prevNode;
102: void *firstObject;
103: while (me->next) {
104: prevNode = me;
105: me = me->next;
106: }
107: firstObject = me->object;
108: prevNode->next = NULL;
2.20 frystyk 109: HT_FREE(me);
2.17 frystyk 110: return firstObject;
2.24 ! frystyk 111: } else /* Empty list */
! 112: return NULL;
! 113: }
! 114:
! 115: PUBLIC void * HTList_firstObject (HTList * me)
! 116: {
! 117: if (me && me->next) {
! 118: HTList * prevNode;
! 119: while (me->next) {
! 120: prevNode = me;
! 121: me = me->next;
! 122: }
! 123: return me->object;
2.17 frystyk 124: } else /* Empty list */
125: return NULL;
1.1 timbl 126: }
127:
2.17 frystyk 128: PUBLIC int HTList_count (HTList * me)
1.1 timbl 129: {
2.17 frystyk 130: int count = 0;
131: if (me)
132: while ((me = me->next))
133: count++;
134: return count;
1.1 timbl 135: }
136:
2.17 frystyk 137: PUBLIC int HTList_indexOf (HTList * me, void * object)
1.1 timbl 138: {
2.17 frystyk 139: if (me) {
140: int position = 0;
141: while ((me = me->next)) {
142: if (me->object == object)
143: return position;
144: position++;
145: }
1.1 timbl 146: }
2.17 frystyk 147: return -1;
1.1 timbl 148: }
149:
2.17 frystyk 150: PUBLIC void * HTList_objectAt (HTList * me, int position)
1.1 timbl 151: {
2.17 frystyk 152: if (position < 0)
153: return NULL;
154: if (me) {
155: while ((me = me->next)) {
156: if (position == 0)
157: return me->object;
158: position--;
159: }
1.1 timbl 160: }
2.17 frystyk 161: return NULL; /* Reached the end of the list */
1.1 timbl 162: }
2.10 frystyk 163:
2.17 frystyk 164: PUBLIC void * HTList_removeObjectAt (HTList * me, int position)
2.10 frystyk 165: {
2.17 frystyk 166: if (position < 0)
167: return NULL;
168: if (me) {
169: HTList * prevNode;
170: prevNode = me;
171: while ((me = me->next)) {
172: if (position == 0) {
173: prevNode->next = me->next;
174: return me->object;
175: }
176: prevNode = me;
177: position--;
178: }
2.10 frystyk 179: }
2.17 frystyk 180: return NULL; /* Reached the end of the list */
2.10 frystyk 181: }
Webmaster