Added a tags file. I dunno if this is something that's really supposed to

be in a CVS repository, but for now, it is, so there. :)
This commit is contained in:
William Ferrell
2000-03-30 20:28:01 +00:00
parent f5a5e9653b
commit a98c8109a8
8 changed files with 1762 additions and 648 deletions
+332 -231
View File
@@ -11,19 +11,21 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Creates a new list... // Creates a new list...
LL * LL_new() LL *
LL_new ()
{ {
LL *list; LL *list;
list = malloc(sizeof(LL)); list = malloc (sizeof (LL));
if(!list) return NULL; if (!list)
return NULL;
list->head.data=NULL; list->head.data = NULL;
list->head.prev=NULL; list->head.prev = NULL;
list->head.next=&list->tail; list->head.next = &list->tail;
list->tail.data=NULL; list->tail.data = NULL;
list->tail.prev=&list->head; list->tail.prev = &list->head;
list->tail.next=NULL; list->tail.next = NULL;
list->current = &list->head; list->current = &list->head;
return list; return list;
@@ -33,22 +35,24 @@ LL * LL_new()
// TODO: test this function // TODO: test this function
// Destroys the entire list // Destroys the entire list
// Warning! Does not free the list data! (only the list itself) // Warning! Does not free the list data! (only the list itself)
int LL_Destroy(LL *list) int
LL_Destroy (LL * list)
{ {
LL_node *node, *next; LL_node *node, *next;
if(!list) return -1; if (!list)
return -1;
node = &list->head; node = &list->head;
for(node = node->next; node && node->next; node = next) for (node = node->next; node && node->next; node = next) {
{
// Avoid accessing "node" after it's freed.. :) // Avoid accessing "node" after it's freed.. :)
next = node->next; next = node->next;
if(LL_node_Destroy(node) < 0) return -1; if (LL_node_Destroy (node) < 0)
return -1;
} }
free(list); free (list);
return 0; return 0;
} }
@@ -56,30 +60,35 @@ int LL_Destroy(LL *list)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// TODO: test this function // TODO: test this function
// Warning! This does not assert that the node data is free! // Warning! This does not assert that the node data is free!
int LL_node_Destroy(LL_node *node) int
LL_node_Destroy (LL_node * node)
{ {
if(!node) return -1; if (!node)
return -1;
if(LL_node_Unlink(node) < 0) return -1; if (LL_node_Unlink (node) < 0)
return -1;
free(node); free (node);
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_node_Unlink(LL_node *node) int
LL_node_Unlink (LL_node * node)
{ {
LL_node *next, *prev; LL_node *next, *prev;
if(!node) return -1; if (!node)
return -1;
next = node->next; next = node->next;
prev = node->prev; prev = node->prev;
if(next) if (next)
next->prev = prev; next->prev = prev;
if(prev) if (prev)
prev->next = next; prev->next = next;
node->next = NULL; node->next = NULL;
@@ -90,28 +99,34 @@ int LL_node_Unlink(LL_node *node)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Frees the data in a list node, if not NULL... // Frees the data in a list node, if not NULL...
int LL_node_DestroyData(LL_node *node) int
LL_node_DestroyData (LL_node * node)
{ {
if(!node) return -1; if (!node)
return -1;
if(node->data) free(node->data); if (node->data)
else return -1; free (node->data);
else
return -1;
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Returns to the beginning of the list... // Returns to the beginning of the list...
int LL_Rewind(LL *list) int
LL_Rewind (LL * list)
{ {
if(!list) return -1; if (!list)
return -1;
/* /*
printf("LL_Rewind: list=%8x\n", list); printf("LL_Rewind: list=%8x\n", list);
printf("LL_Rewind: list.head=%8x\n", &list->head); printf("LL_Rewind: list.head=%8x\n", &list->head);
printf("LL_Rewind: list.tail=%8x\n", &list->tail); printf("LL_Rewind: list.tail=%8x\n", &list->tail);
*/ */
if(list->head.next != &list->tail) if (list->head.next != &list->tail)
list->current = list->head.next; list->current = list->head.next;
else else
list->current = &list->head; list->current = &list->head;
@@ -121,11 +136,13 @@ int LL_Rewind(LL *list)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Goes to the end of the list... // Goes to the end of the list...
int LL_End(LL *list) int
LL_End (LL * list)
{ {
if(!list) return -1; if (!list)
return -1;
if(list->tail.prev != &list->head) if (list->tail.prev != &list->head)
list->current = list->tail.prev; list->current = list->tail.prev;
else else
list->current = &list->tail; list->current = &list->tail;
@@ -135,36 +152,36 @@ int LL_End(LL *list)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Go to the next node // Go to the next node
int LL_Next(LL *list) int
LL_Next (LL * list)
{ {
if(!list) return -1; if (!list)
if(!list->current) return -1; return -1;
if (!list->current)
return -1;
if(list->current->next != &list->tail) if (list->current->next != &list->tail) {
{
list->current = list->current->next; list->current = list->current->next;
return 0; return 0;
} } else {
else
{
return -1; return -1;
} }
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Go to the previous node // Go to the previous node
int LL_Prev(LL *list) int
LL_Prev (LL * list)
{ {
if(!list) return -1; if (!list)
if(!list->current) return -1; return -1;
if (!list->current)
return -1;
if(list->current->prev != &list->head) if (list->current->prev != &list->head) {
{
list->current = list->current->prev; list->current = list->current->prev;
return 0; return 0;
} } else {
else
{
return -1; return -1;
} }
} }
@@ -172,19 +189,25 @@ int LL_Prev(LL *list)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Data manipulation // Data manipulation
void * LL_Get(LL *list) void *
LL_Get (LL * list)
{ {
if(!list) return NULL; if (!list)
if(!list->current) return NULL; return NULL;
if (!list->current)
return NULL;
return list->current->data; return list->current->data;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_Put(LL *list, void *data) int
LL_Put (LL * list, void *data)
{ {
if(!list) return -1; if (!list)
if(!list->current) return -1; return -1;
if (!list->current)
return -1;
list->current->data = data; list->current->data = data;
@@ -193,19 +216,24 @@ int LL_Put(LL *list, void *data)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
LL_node * LL_GetNode(LL *list) LL_node *
LL_GetNode (LL * list)
{ {
if(!list) return NULL; if (!list)
return NULL;
return list->current; return list->current;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Don't use this unless you know what you're doing. // Don't use this unless you know what you're doing.
int LL_PutNode(LL *list, LL_node *node) int
LL_PutNode (LL * list, LL_node * node)
{ {
if(!list) return -1; if (!list)
if(!node) return -1; return -1;
if (!node)
return -1;
list->current = node; list->current = node;
@@ -214,68 +242,83 @@ int LL_PutNode(LL *list, LL_node *node)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_GetFirst(LL *list) // gets data from first node void *
LL_GetFirst (LL * list) // gets data from first node
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0> LL_Rewind(list)) return NULL; if (0 > LL_Rewind (list))
return NULL;
return LL_Get(list); return LL_Get (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// //
void * LL_GetNext (LL *list) // ... next node void *
LL_GetNext (LL * list) // ... next node
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0 > LL_Next(list)) return NULL; if (0 > LL_Next (list))
return NULL;
return LL_Get(list); return LL_Get (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_GetPrev (LL *list) // ... prev node void *
LL_GetPrev (LL * list) // ... prev node
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0 > LL_Prev(list)) return NULL; if (0 > LL_Prev (list))
return NULL;
return LL_Get(list); return LL_Get (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_GetLast (LL *list) // ... last node void *
LL_GetLast (LL * list) // ... last node
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0 > LL_End(list)) return NULL; if (0 > LL_End (list))
return NULL;
return LL_Get(list); return LL_Get (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_AddNode(LL *list, void * add) // Adds node AFTER current one int
LL_AddNode (LL * list, void *add) // Adds node AFTER current one
{ {
LL_node *node; LL_node *node;
if(!list) return -1; if (!list)
return -1;
//if(!add) return -1; // Nevermind.. NULL entries can be good... //if(!add) return -1; // Nevermind.. NULL entries can be good...
if(!list->current) return -1; if (!list->current)
return -1;
//LL_dprint(list); //LL_dprint(list);
node = malloc(sizeof(LL_node)); node = malloc (sizeof (LL_node));
if(!node) return -1; if (!node)
return -1;
//printf("Allocated node\n"); //printf("Allocated node\n");
/* printf("Current: prev: %8x\tnode: %8x\tnext: %8x\n", */ /* printf("Current: prev: %8x\tnode: %8x\tnext: %8x\n", */
/* (int)list->current->prev, */ /* (int)list->current->prev, */
/* (int)list->current, */ /* (int)list->current, */
/* (int)list->current->next); */ /* (int)list->current->next); */
if(list->current == &list->tail) if (list->current == &list->tail) {
{
list->current = list->current->prev; list->current = list->current->prev;
/* printf("Was at end of list...\n"); */ /* printf("Was at end of list...\n"); */
/* printf("Current: prev: %8x\tnode: %8x\tnext: %8x\n", */ /* printf("Current: prev: %8x\tnode: %8x\tnext: %8x\n", */
@@ -283,7 +326,6 @@ int LL_AddNode(LL *list, void * add) // Adds node AFTER current one
/* (int)list->current, */ /* (int)list->current, */
/* (int)list->current->next); */ /* (int)list->current->next); */
} }
// printf("Setting node data\n"); // printf("Setting node data\n");
node->next = list->current->next; node->next = list->current->next;
node->prev = list->current; node->prev = list->current;
@@ -295,7 +337,7 @@ int LL_AddNode(LL *list, void * add) // Adds node AFTER current one
/* (int)node->next); */ /* (int)node->next); */
// printf("Relinking...\n"); // printf("Relinking...\n");
if(node->next) if (node->next)
node->next->prev = node; node->next->prev = node;
// printf("...\n"); // printf("...\n");
list->current->next = node; list->current->next = node;
@@ -310,24 +352,30 @@ int LL_AddNode(LL *list, void * add) // Adds node AFTER current one
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_InsertNode(LL *list, void * add)// Adds node BEFORE current one int
LL_InsertNode (LL * list, void *add) // Adds node BEFORE current one
{ {
LL_node *node; LL_node *node;
if(!list) return -1; if (!list)
if(!add) return -1; return -1;
if(!list->current) return -1; if (!add)
return -1;
if (!list->current)
return -1;
node = malloc(sizeof(LL_node)); node = malloc (sizeof (LL_node));
if(!node) return -1; if (!node)
return -1;
if(list->current == &list->head) list->current = list->current->next; if (list->current == &list->head)
list->current = list->current->next;
node->next = list->current; node->next = list->current;
node->prev = list->current->prev; node->prev = list->current->prev;
node->data = add; node->data = add;
if(list->current->prev) if (list->current->prev)
list->current->prev->next = node; list->current->prev->next = node;
list->current->prev = node; list->current->prev = node;
@@ -340,30 +388,35 @@ int LL_InsertNode(LL *list, void * add)// Adds node BEFORE current one
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Removes a node from the link // Removes a node from the link
// ... and advances one node forward // ... and advances one node forward
void * LL_DeleteNode(LL *list) void *
LL_DeleteNode (LL * list)
{ {
LL_node *next, *prev; LL_node *next, *prev;
void *data; void *data;
if(!list) return NULL; if (!list)
if(!list->current) return NULL; return NULL;
if(list->current == &list->head) return NULL; if (!list->current)
if(list->current == &list->tail) return NULL; return NULL;
if (list->current == &list->head)
return NULL;
if (list->current == &list->tail)
return NULL;
#ifdef DEBUG #ifdef DEBUG
printf("LL_DeleteNode: Before...\n"); printf ("LL_DeleteNode: Before...\n");
LL_dprint(list); LL_dprint (list);
#endif #endif
next = list->current->next; next = list->current->next;
prev = list->current->prev; prev = list->current->prev;
data = list->current->data; data = list->current->data;
if(prev) if (prev)
prev->next = next; prev->next = next;
if(next) if (next)
next->prev = prev; next->prev = prev;
list->current->prev = NULL; list->current->prev = NULL;
@@ -372,30 +425,34 @@ void * LL_DeleteNode(LL *list)
//if(list->current->data) free(list->current->data); //if(list->current->data) free(list->current->data);
list->current->data = NULL; list->current->data = NULL;
free(list->current); free (list->current);
list->current = next; list->current = next;
#ifdef DEBUG #ifdef DEBUG
printf("LL_DeleteNode: After...\n"); printf ("LL_DeleteNode: After...\n");
LL_dprint(list); LL_dprint (list);
#endif #endif
return data; return data;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Removes a specific node... // Removes a specific node...
void * LL_Remove(LL *list, void * data) void *
LL_Remove (LL * list, void *data)
{ {
void *find; void *find;
if(!list) return NULL; if (!list)
return NULL;
LL_Rewind(list); LL_Rewind (list);
do { do {
find = LL_Get(list); find = LL_Get (list);
if(find == data) return LL_DeleteNode(list); if (find == data)
} while (LL_Next(list) == 0); return LL_DeleteNode (list);
} while (LL_Next (list) == 0);
return NULL; return NULL;
} }
@@ -403,84 +460,106 @@ void * LL_Remove(LL *list, void * data)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Stack operations // Stack operations
int LL_Push(LL *list, void *add) // Add node to end of list int
LL_Push (LL * list, void *add) // Add node to end of list
{ {
if(!list) return -1; if (!list)
if(!add) return -1; return -1;
if (!add)
return -1;
// printf("Going to end of list...\n"); // printf("Going to end of list...\n");
LL_End(list); LL_End (list);
// printf("Adding node...\n"); // printf("Adding node...\n");
return LL_AddNode(list, add); return LL_AddNode (list, add);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_Pop(LL *list) // Remove node from end of list void *
LL_Pop (LL * list) // Remove node from end of list
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0 > LL_End(list)) return NULL; if (0 > LL_End (list))
return NULL;
return LL_DeleteNode(list); return LL_DeleteNode (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_Top(LL *list) // Peek at end node void *
LL_Top (LL * list) // Peek at end node
{ {
return LL_GetLast(list); return LL_GetLast (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_Shift(LL *list) // Remove node from start of list void *
LL_Shift (LL * list) // Remove node from start of list
{ {
if(!list) return NULL; if (!list)
return NULL;
if(0 > LL_Rewind(list)) return NULL; if (0 > LL_Rewind (list))
return NULL;
return LL_DeleteNode(list); return LL_DeleteNode (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
void * LL_Look(LL *list) // Peek at first node void *
LL_Look (LL * list) // Peek at first node
{ {
return LL_GetFirst(list); return LL_GetFirst (list);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_Unshift(LL *list, void *add) // Add node to beginning of list int
LL_Unshift (LL * list, void *add) // Add node to beginning of list
{ {
if(!list) return -1; if (!list)
if(!add) return -1; return -1;
if (!add)
return -1;
LL_Rewind(list); LL_Rewind (list);
return LL_InsertNode(list, add); return LL_InsertNode (list, add);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_Roll(LL *list) // Make last node first int
LL_Roll (LL * list) // Make last node first
{ {
LL_node *node, *next; LL_node *node, *next;
if(!list) return -1; if (!list)
return -1;
//if(!list->current) return -1; //if(!list->current) return -1;
if(0 > LL_End(list)) return -1; if (0 > LL_End (list))
return -1;
// Avoid rolling an empty list, or unlinking the head/tail... // Avoid rolling an empty list, or unlinking the head/tail...
if(list->current == &list->head) list->current = list->current->next; if (list->current == &list->head)
if(list->current == &list->tail) list->current = list->current->prev; list->current = list->current->next;
if (list->current == &list->tail)
list->current = list->current->prev;
// List is empty // List is empty
if(list->current == &list->head) return 0; if (list->current == &list->head)
return 0;
// List has one item // List has one item
if(list->current->prev == &list->head) return 0; if (list->current->prev == &list->head)
return 0;
node = list->current; node = list->current;
LL_node_Unlink(node); LL_node_Unlink (node);
if(0 > LL_Rewind(list)) return -1; if (0 > LL_Rewind (list))
return -1;
next = list->head.next; next = list->head.next;
@@ -493,28 +572,36 @@ int LL_Roll(LL *list) // Make last node first
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_UnRoll(LL *list)// Roll the other way... int
LL_UnRoll (LL * list) // Roll the other way...
{ {
LL_node *node, *prev; LL_node *node, *prev;
if(!list) return -1; if (!list)
return -1;
//if(!list->current) return -1; //if(!list->current) return -1;
if(0 > LL_Rewind(list)) return -1; if (0 > LL_Rewind (list))
return -1;
// Avoid rolling an empty list, or unlinking the head/tail... // Avoid rolling an empty list, or unlinking the head/tail...
if(list->current == &list->tail) list->current = list->current->prev; if (list->current == &list->tail)
if(list->current == &list->head) list->current = list->current->next; list->current = list->current->prev;
if (list->current == &list->head)
list->current = list->current->next;
// List is empty // List is empty
if(list->current == &list->tail) return 0; if (list->current == &list->tail)
return 0;
// List has one item // List has one item
if(list->current->next == &list->tail) return 0; if (list->current->next == &list->tail)
return 0;
node = list->current; node = list->current;
LL_node_Unlink(node); LL_node_Unlink (node);
if(0 > LL_End(list)) return -1; if (0 > LL_End (list))
return -1;
prev = list->tail.prev; prev = list->tail.prev;
@@ -531,33 +618,36 @@ int LL_UnRoll(LL *list)// Roll the other way...
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Add an item to the end of its "priority group" // Add an item to the end of its "priority group"
// The list is assumed to be sorted already... // The list is assumed to be sorted already...
int LL_PriorityEnqueue(LL *list, void *add, int compare(void *, void *)) int
LL_PriorityEnqueue (LL * list, void *add, int compare (void *, void *))
{ {
void *data; void *data;
int i; int i;
if(!list) return -1; if (!list)
if(!add) return -1; return -1;
if(!compare) return -1; if (!add)
return -1;
if (!compare)
return -1;
// From the end of the list, keep searching while we're "less than" // From the end of the list, keep searching while we're "less than"
// the given nodes... // the given nodes...
LL_End(list); LL_End (list);
do { do {
data = LL_Get(list); data = LL_Get (list);
if(data) if (data) {
i = compare (add, data);
if (i >= 0) // If we're in the right place, add it and exit
{ {
i = compare(add, data); LL_AddNode (list, add);
if(i >= 0) // If we're in the right place, add it and exit
{
LL_AddNode(list, add);
return 0; return 0;
} }
} }
} while(LL_Prev(list) == 0); } while (LL_Prev (list) == 0);
// If we're less than *everything*, put it at the beginning // If we're less than *everything*, put it at the beginning
LL_Unshift(list, add); LL_Unshift (list, add);
return 0; return 0;
} }
@@ -565,57 +655,70 @@ int LL_PriorityEnqueue(LL *list, void *add, int compare(void *, void *))
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_SwapNodes(LL_node *one, LL_node *two) // Switch two nodes positions... int
LL_SwapNodes (LL_node * one, LL_node * two) // Switch two nodes positions...
{ {
LL_node *firstprev, *firstnext; LL_node *firstprev, *firstnext;
LL_node *secondprev, *secondnext; LL_node *secondprev, *secondnext;
if(!one || !two) return -1; if (!one || !two)
if(one == two) return 0; // Do nothing return -1;
if (one == two)
return 0; // Do nothing
firstprev = one->prev; // Look up the nodes neighbors... firstprev = one->prev; // Look up the nodes neighbors...
firstnext = one->next; firstnext = one->next;
secondprev = two->prev; secondprev = two->prev;
secondnext = two->next; secondnext = two->next;
if(firstprev != NULL) firstprev->next = two; // Swap the neighboring if (firstprev != NULL)
if(firstnext != NULL) firstnext->prev = two; // nodes pointers... firstprev->next = two; // Swap the neighboring
if(secondprev != NULL) secondprev->next = one; if (firstnext != NULL)
if(secondprev != NULL) secondnext->prev = one; firstnext->prev = two; // nodes pointers...
if (secondprev != NULL)
secondprev->next = one;
if (secondprev != NULL)
secondnext->prev = one;
one->next = secondnext; // Swap the nodes pointers one->next = secondnext; // Swap the nodes pointers
one->prev = secondprev; one->prev = secondprev;
two->next = firstnext; two->next = firstnext;
two->prev = firstprev; two->prev = firstprev;
if(firstnext == two) one->prev = two; // Fix things in case if (firstnext == two)
if(firstprev == two) one->next = two; // they were next to one->prev = two; // Fix things in case
if(secondprev == one) two->next = one; // each other... if (firstprev == two)
if(secondnext == one) two->prev = one; one->next = two; // they were next to
if (secondprev == one)
two->next = one; // each other...
if (secondnext == one)
two->prev = one;
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_nSwapNodes(int one, int two) // Switch two nodes positions... int
LL_nSwapNodes (int one, int two) // Switch two nodes positions...
{ {
return -1; return -1;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
int LL_Length(LL *list) // Returns # of nodes in entire list int
LL_Length (LL * list) // Returns # of nodes in entire list
{ {
LL_node *node; LL_node *node;
int num = 0; int num = 0;
if(!list) return -1; if (!list)
return -1;
node = &list->head; node = &list->head;
for(num = -1; for (num = -1; node != &list->tail; num++)
node != &list->tail;
num++)
node = node->next; node = node->next;
return num; return num;
@@ -630,19 +733,24 @@ int LL_Length(LL *list) // Returns # of nodes in entire list
// //
// Note that this does *not* rewind the list first! You should do // Note that this does *not* rewind the list first! You should do
// it yourself if you want to start from the beginning! // it yourself if you want to start from the beginning!
void * LL_Find(LL *list, int compare(void *, void *), void *value) void *
LL_Find (LL * list, int compare (void *, void *), void *value)
{ {
void *data; void *data;
if(!list) return NULL; if (!list)
if(!compare) return NULL; return NULL;
if(!value) return NULL; if (!compare)
return NULL;
if (!value)
return NULL;
do{ do {
data = LL_Get(list); data = LL_Get (list);
if( 0 == compare(data, value) ) return data; if (0 == compare (data, value))
return data;
} while(LL_Next(list) == 0); } while (LL_Next (list) == 0);
return NULL; return NULL;
} }
@@ -651,78 +759,71 @@ void * LL_Find(LL *list, int compare(void *, void *), void *value)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Sorts the list, then rewinds it... // Sorts the list, then rewinds it...
// //
int LL_Sort(LL *list, int compare(void *, void *)) int
LL_Sort (LL * list, int compare (void *, void *))
{ {
int i,j; // Junk / loop variables int i, j; // Junk / loop variables
int numnodes; // number of nodes in list int numnodes; // number of nodes in list
LL_node *best, *last; // best match and last node in the list LL_node *best, *last; // best match and last node in the list
LL_node *current; LL_node *current;
if(!list) return -1; if (!list)
if(!compare) return -1; return -1;
if (!compare)
return -1;
numnodes = LL_Length(list); // get the number of nodes... numnodes = LL_Length (list); // get the number of nodes...
if(0 > LL_End(list)) return -1; // Find the last node. if (0 > LL_End (list))
last = LL_GetNode(list); return -1; // Find the last node.
last = LL_GetNode (list);
if(numnodes < 2) return 0; if (numnodes < 2)
return 0;
for(i=numnodes-1; i>0; i--) for (i = numnodes - 1; i > 0; i--) {
{ LL_Rewind (list); // get the first node again
LL_Rewind(list); // get the first node again
best = last; // reset our "best" node best = last; // reset our "best" node
for(j=0; j<i; j++) for (j = 0; j < i; j++) {
{ current = LL_GetNode (list);
current = LL_GetNode(list);
// If we found a better match... // If we found a better match...
if(compare(current->data, best->data) > 0) if (compare (current->data, best->data) > 0) {
{
best = current; // keep track of the "best" match best = current; // keep track of the "best" match
} }
LL_Next(list); // Go to the next node. LL_Next (list); // Go to the next node.
} }
LL_SwapNodes(last, best); // Switch two nodes... LL_SwapNodes (last, best); // Switch two nodes...
if(best) last = best->prev; if (best)
else return -1; last = best->prev;
else
return -1;
//last = LL_FindPrev(best); // And go backwards by one node. //last = LL_FindPrev(best); // And go backwards by one node.
} }
//return LLFindFirst(current); // return pointer to the first node. //return LLFindFirst(current); // return pointer to the first node.
LL_Rewind(list); LL_Rewind (list);
return 0; return 0;
} }
void LL_dprint(LL *list) void
LL_dprint (LL * list)
{ {
LL_node *current; LL_node *current;
current = &list->head; current = &list->head;
printf("Head: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", printf ("Head: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", (int) list->head.prev, (int) &list->head, (int) list->head.next);
(int)list->head.prev,
(int)&list->head,
(int)list->head.next);
for(current = current->next; for (current = current->next; current != &list->tail; current = current->next) {
current != &list->tail; printf ("node: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", (int) current->prev, (int) current, (int) current->next);
current = current->next)
{
printf("node: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n",
(int)current->prev,
(int)current,
(int)current->next);
} }
printf("Tail: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", printf ("Tail: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", (int) list->tail.prev, (int) &list->tail, (int) list->tail.next);
(int)list->tail.prev,
(int)&list->tail,
(int)list->tail.next);
} }
+38 -40
View File
@@ -118,14 +118,12 @@
// See LL.c for more detailed descriptions of these functions. // See LL.c for more detailed descriptions of these functions.
typedef struct LL_node typedef struct LL_node {
{
struct LL_node *next, *prev; struct LL_node *next, *prev;
void *data; void *data;
} LL_node; } LL_node;
typedef struct LL typedef struct LL {
{
LL_node head, tail; LL_node head, tail;
LL_node *current; LL_node *current;
} LL; } LL;
@@ -133,51 +131,51 @@ typedef struct LL
// Creates a new list... // Creates a new list...
LL * LL_new(); LL *LL_new ();
// Destroying lists... // Destroying lists...
int LL_Destroy(LL *list); int LL_Destroy (LL * list);
int LL_node_Destroy(LL_node *node); int LL_node_Destroy (LL_node * node);
int LL_node_Unlink(LL_node *node); int LL_node_Unlink (LL_node * node);
int LL_node_DestroyData(LL_node *node); int LL_node_DestroyData (LL_node * node);
// Returns to the beginning of the list... // Returns to the beginning of the list...
int LL_Rewind(LL *list); int LL_Rewind (LL * list);
// Goes to the end of the list... // Goes to the end of the list...
int LL_End(LL *list); int LL_End (LL * list);
// Go to the next node // Go to the next node
int LL_Next(LL *list); int LL_Next (LL * list);
// Go to the previous node // Go to the previous node
int LL_Prev(LL *list); int LL_Prev (LL * list);
// Data manipulation // Data manipulation
void * LL_Get(LL *list); void *LL_Get (LL * list);
int LL_Put(LL *list, void *data); int LL_Put (LL * list, void *data);
// Don't use these next two unless you really know what you're doing. // Don't use these next two unless you really know what you're doing.
LL_node * LL_GetNode(LL *list); LL_node *LL_GetNode (LL * list);
int LL_PutNode(LL *list, LL_node *node); int LL_PutNode (LL * list, LL_node * node);
void * LL_GetFirst(LL *list); // gets data from first node void *LL_GetFirst (LL * list); // gets data from first node
void * LL_GetNext (LL *list); // ... next node void *LL_GetNext (LL * list); // ... next node
void * LL_GetPrev (LL *list); // ... prev node void *LL_GetPrev (LL * list); // ... prev node
void * LL_GetLast (LL *list); // ... last node void *LL_GetLast (LL * list); // ... last node
int LL_AddNode(LL *list, void * add); // Adds node AFTER current one int LL_AddNode (LL * list, void *add); // Adds node AFTER current one
int LL_InsertNode(LL *list, void * add);// Adds node BEFORE current one int LL_InsertNode (LL * list, void *add); // Adds node BEFORE current one
// Removes a node from the link; returns the data from the node // Removes a node from the link; returns the data from the node
void * LL_DeleteNode(LL *list); void *LL_DeleteNode (LL * list);
// Removes a specific node... // Removes a specific node...
void * LL_Remove(LL *list, void * data); void *LL_Remove (LL * list, void *data);
// Stack operations // Stack operations
int LL_Push(LL *list, void *add); // Add node to end of list int LL_Push (LL * list, void *add); // Add node to end of list
void * LL_Pop(LL *list); // Remove node from end of list void *LL_Pop (LL * list); // Remove node from end of list
void * LL_Top(LL *list); // Peek at end node void *LL_Top (LL * list); // Peek at end node
void * LL_Shift(LL *list); // Remove node from start of list void *LL_Shift (LL * list); // Remove node from start of list
void * LL_Look(LL *list); // Peek at first node void *LL_Look (LL * list); // Peek at first node
int LL_Unshift(LL *list, void *add); // Add node to beginning of list int LL_Unshift (LL * list, void *add); // Add node to beginning of list
int LL_Roll(LL *list); // Make first node last int LL_Roll (LL * list); // Make first node last
int LL_UnRoll(LL *list);// Roll the other way... int LL_UnRoll (LL * list); // Roll the other way...
// Queue operations... // Queue operations...
//int LL_Enqueue(LL *list, void *add); //int LL_Enqueue(LL *list, void *add);
@@ -188,22 +186,22 @@ int LL_UnRoll(LL *list);// Roll the other way...
#define LL_Dequeue(list) LL_Shift(list) #define LL_Dequeue(list) LL_Shift(list)
int LL_PriorityEnqueue(LL *list, void *add, int compare(void *, void *)); int LL_PriorityEnqueue (LL * list, void *add, int compare (void *, void *));
int LL_SwapNodes(LL_node *one, LL_node *two); // Switch two nodes positions... int LL_SwapNodes (LL_node * one, LL_node * two); // Switch two nodes positions...
int LL_nSwapNodes(int one, int two); // Switch two nodes positions... int LL_nSwapNodes (int one, int two); // Switch two nodes positions...
int LL_Length(LL *list); // Returns # of nodes in entire list int LL_Length (LL * list); // Returns # of nodes in entire list
// Searching... // Searching...
void * LL_Find(LL *list, int compare(void *, void *), void *value); void *LL_Find (LL * list, int compare (void *, void *), void *value);
// Sorts the list... // Sorts the list...
int LL_Sort(LL *list, int compare(void *, void *)); int LL_Sort (LL * list, int compare (void *, void *));
// Debugging... // Debugging...
void LL_dprint(LL *list); void LL_dprint (LL * list);
#endif #endif
+52 -46
View File
@@ -31,18 +31,16 @@
typedef struct sockaddr_in sockaddr_in; typedef struct sockaddr_in sockaddr_in;
static int sock_init_sockaddr (sockaddr_in *name, static int
const char *hostname, sock_init_sockaddr (sockaddr_in * name, const char *hostname, unsigned short int port)
unsigned short int port)
{ {
struct hostent *hostinfo; struct hostent *hostinfo;
name->sin_family = AF_INET; name->sin_family = AF_INET;
name->sin_port = htons (port); name->sin_port = htons (port);
hostinfo = gethostbyname (hostname); hostinfo = gethostbyname (hostname);
if (hostinfo == NULL) if (hostinfo == NULL) {
{ fprintf (stderr, "sock_init_sockaddr: Unknown host %s.\n", hostname);
fprintf (stderr,"sock_init_sockaddr: Unknown host %s.\n", hostname);
return -1; return -1;
} }
name->sin_addr = *(struct in_addr *) hostinfo->h_addr; name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
@@ -52,67 +50,68 @@ static int sock_init_sockaddr (sockaddr_in *name,
} }
// Client functions... // Client functions...
int sock_connect(char *host, unsigned short int port) int
sock_connect (char *host, unsigned short int port)
{ {
struct sockaddr_in servername; struct sockaddr_in servername;
int sock; int sock;
int err=0; int err = 0;
debug("sock_connect: Creating socket\n"); debug ("sock_connect: Creating socket\n");
sock = socket(PF_INET, SOCK_STREAM, 0); sock = socket (PF_INET, SOCK_STREAM, 0);
if(sock < 0) if (sock < 0) {
{ perror ("sock_connect: Error creating socket");
perror("sock_connect: Error creating socket");
return sock; return sock;
} }
debug("sock_connect: Created socket (%i)\n", sock); debug ("sock_connect: Created socket (%i)\n", sock);
sock_init_sockaddr(&servername, host, port); sock_init_sockaddr (&servername, host, port);
err = connect (sock, err = connect (sock, (struct sockaddr *) &servername, sizeof (servername));
(struct sockaddr *) &servername, if (err < 0) {
sizeof (servername)); perror ("sock_connect: connect failed");
if(err<0) shutdown (sock, 2);
{
perror("sock_connect: connect failed");
shutdown(sock, 2);
return 0; // Normal exit if server doesn't exist... return 0; // Normal exit if server doesn't exist...
} }
fcntl(sock, F_SETFL, O_NONBLOCK); fcntl (sock, F_SETFL, O_NONBLOCK);
return sock; return sock;
} }
int sock_close(int fd) int
sock_close (int fd)
{ {
int err; int err;
err=shutdown(fd, 2); err = shutdown (fd, 2);
if (!err) close (fd); if (!err)
close (fd);
return err; return err;
} }
// Send/receive lines of text // Send/receive lines of text
int sock_send_string(int fd, char *string) int
sock_send_string (int fd, char *string)
{ {
int len; int len;
int offset=0; int offset = 0;
if (!string) return -1; if (!string)
return -1;
len = strlen (string) + 1; len = strlen (string) + 1;
while (offset != len) { while (offset != len) {
// write isn't guaranteed to send the entire string at once, // write isn't guaranteed to send the entire string at once,
// so we have to sent it in a loop like this // so we have to sent it in a loop like this
int sent = write (fd, string+offset, len-offset); int sent = write (fd, string + offset, len - offset);
if (sent == -1) { if (sent == -1) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
perror ("sock_send_string: socket write error"); perror ("sock_send_string: socket write error");
printf("Message was: %s\n", string); printf ("Message was: %s\n", string);
//shutdown(fd, 2); //shutdown(fd, 2);
return sent; return sent;
} }
@@ -120,7 +119,7 @@ int sock_send_string(int fd, char *string)
} else if (sent == 0) { } else if (sent == 0) {
// when this returns zero, it generally means // when this returns zero, it generally means
// we got disconnected // we got disconnected
return sent+offset; return sent + offset;
} }
offset += sent; offset += sent;
@@ -130,13 +129,16 @@ int sock_send_string(int fd, char *string)
} }
// Recv gives only one line per call... // Recv gives only one line per call...
int sock_recv_string(int fd, char *dest, size_t maxlen) int
sock_recv_string (int fd, char *dest, size_t maxlen)
{ {
char *ptr = dest; char *ptr = dest;
int recv = 0; int recv = 0;
if (!dest) return -1; if (!dest)
if (maxlen <= 0) return 0; return -1;
if (maxlen <= 0)
return 0;
while (1) { while (1) {
int err = read (fd, ptr, 1); int err = read (fd, ptr, 1);
@@ -170,7 +172,7 @@ int sock_recv_string(int fd, char *dest, size_t maxlen)
return 0; return 0;
} }
if (recv < maxlen-1) { if (recv < maxlen - 1) {
dest[recv] = 0; dest[recv] = 0;
} }
@@ -178,16 +180,18 @@ int sock_recv_string(int fd, char *dest, size_t maxlen)
} }
// Send/receive raw data // Send/receive raw data
int sock_send(int fd, void *src, size_t size) int
sock_send (int fd, void *src, size_t size)
{ {
int offset=0; int offset = 0;
if (!src) return -1; if (!src)
return -1;
while (offset != size) { while (offset != size) {
// write isn't guaranteed to send the entire string at once, // write isn't guaranteed to send the entire string at once,
// so we have to sent it in a loop like this // so we have to sent it in a loop like this
int sent = write (fd, ((char*)src)+offset, size-offset); int sent = write (fd, ((char *) src) + offset, size - offset);
if (sent == -1) { if (sent == -1) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
perror ("sock_send: socket write error"); perror ("sock_send: socket write error");
@@ -198,7 +202,7 @@ int sock_send(int fd, void *src, size_t size)
} else if (sent == 0) { } else if (sent == 0) {
// when this returns zero, it generally means // when this returns zero, it generally means
// we got disconnected // we got disconnected
return sent+offset; return sent + offset;
} }
offset += sent; offset += sent;
@@ -207,16 +211,18 @@ int sock_send(int fd, void *src, size_t size)
return offset; return offset;
} }
int sock_recv(int fd, void *dest, size_t maxlen) int
sock_recv (int fd, void *dest, size_t maxlen)
{ {
int err; int err;
if(!dest) return -1; if (!dest)
if(maxlen <= 0) return 0; return -1;
if (maxlen <= 0)
return 0;
err = read (fd, dest, maxlen); err = read (fd, dest, maxlen);
if (err < 0) if (err < 0) {
{
//fprintf (stderr,"sock_recv: socket read error\n"); //fprintf (stderr,"sock_recv: socket read error\n");
//shutdown(fd, 2); //shutdown(fd, 2);
return err; return err;
+6 -6
View File
@@ -22,15 +22,15 @@
// Client functions... // Client functions...
int sock_connect(char *host, unsigned short int port); int sock_connect (char *host, unsigned short int port);
int sock_close(int fd); int sock_close (int fd);
// Send/receive lines of text // Send/receive lines of text
int sock_send_string(int fd, char *string); int sock_send_string (int fd, char *string);
// Recv gives only one line per call... // Recv gives only one line per call...
int sock_recv_string(int fd, char *dest, size_t maxlen); int sock_recv_string (int fd, char *dest, size_t maxlen);
// Send/receive raw data // Send/receive raw data
int sock_send(int fd, void *src, size_t size); int sock_send (int fd, void *src, size_t size);
int sock_recv(int fd, void *dest, size_t maxlen); int sock_recv (int fd, void *dest, size_t maxlen);
// Er, ignore the rest of this file. I'll clean it up sometime... // Er, ignore the rest of this file. I'll clean it up sometime...
+13 -12
View File
@@ -5,30 +5,31 @@
#include "debug.h" #include "debug.h"
#include "str.h" #include "str.h"
int get_args(char **argv, char *str, int max_args) int
get_args (char **argv, char *str, int max_args)
{ {
char *delimiters = " \n\0"; char *delimiters = " \n\0";
char *item; char *item;
int i=0; int i = 0;
if(!argv) return -1; if (!argv)
if(!str) return 0; return -1;
if(max_args < 1) return 0; if (!str)
return 0;
if (max_args < 1)
return 0;
//debug("get_args(%i): string=%s\n", max_args, str); //debug("get_args(%i): string=%s\n", max_args, str);
// Parse the command line... // Parse the command line...
for(item = strtok(str, delimiters); item; item=strtok(NULL, delimiters)) for (item = strtok (str, delimiters); item; item = strtok (NULL, delimiters)) {
{
//debug("get_args: item=%s\n", item); //debug("get_args: item=%s\n", item);
if(i < max_args) if (i < max_args) {
{
argv[i] = item; argv[i] = item;
i++; i++;
} } else
else return i; return i;
} }
return i; return i;
} }
+1 -1
View File
@@ -1,6 +1,6 @@
#ifndef STR_H #ifndef STR_H
#define STR_H #define STR_H
int get_args(char **argv, char *str, int max_args); int get_args (char **argv, char *str, int max_args);
#endif #endif
+1008
View File
File diff suppressed because it is too large Load Diff