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
+269 -168
View File
@@ -11,12 +11,14 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// 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;
@@ -33,19 +35,21 @@ 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);
@@ -56,11 +60,14 @@ 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);
@@ -68,11 +75,13 @@ int LL_node_Destroy(LL_node *node)
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
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;
@@ -90,21 +99,27 @@ 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);
@@ -121,9 +136,11 @@ 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;
@@ -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;
@@ -310,18 +352,24 @@ 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;
@@ -340,15 +388,20 @@ 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");
@@ -383,18 +436,22 @@ void * LL_DeleteNode(LL *list)
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)
return LL_DeleteNode (list);
} while (LL_Next (list) == 0); } while (LL_Next (list) == 0);
return NULL; return NULL;
@@ -403,10 +460,13 @@ 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);
@@ -416,42 +476,53 @@ 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
{ {
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);
@@ -459,28 +530,36 @@ int LL_Unshift(LL *list, void *add) // Add node to beginning of list
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
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,22 +618,25 @@ 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); i = compare (add, data);
if (i >= 0) // If we're in the right place, add it and exit if (i >= 0) // If we're in the right place, add it and exit
{ {
@@ -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,17 +733,22 @@ 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);
@@ -651,7 +759,8 @@ 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
@@ -659,35 +768,38 @@ int LL_Sort(LL *list, int compare(void *, void *))
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))
return -1; // Find the last node.
last = LL_GetNode (list); 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.
} }
@@ -699,30 +811,19 @@ int LL_Sort(LL *list, int compare(void *, void *))
} }
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);
} }
+2 -4
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;
+33 -27
View File
@@ -31,17 +31,15 @@
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;
} }
@@ -52,7 +50,8 @@ 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;
@@ -61,8 +60,7 @@ int sock_connect(char *host, unsigned short int port)
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;
} }
@@ -70,11 +68,8 @@ int sock_connect(char *host, unsigned short int port)
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));
if(err<0)
{
perror ("sock_connect: connect failed"); perror ("sock_connect: connect failed");
shutdown (sock, 2); shutdown (sock, 2);
return 0; // Normal exit if server doesn't exist... return 0; // Normal exit if server doesn't exist...
@@ -86,23 +81,27 @@ int sock_connect(char *host, unsigned short int port)
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) {
@@ -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);
@@ -178,11 +180,13 @@ 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,
@@ -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;
+12 -11
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;
} }
+1008
View File
File diff suppressed because it is too large Load Diff