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:
+332
-231
@@ -11,19 +11,21 @@
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Creates a new list...
|
||||
LL * LL_new()
|
||||
LL *
|
||||
LL_new ()
|
||||
{
|
||||
LL *list;
|
||||
|
||||
list = malloc(sizeof(LL));
|
||||
if(!list) return NULL;
|
||||
list = malloc (sizeof (LL));
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
list->head.data=NULL;
|
||||
list->head.prev=NULL;
|
||||
list->head.next=&list->tail;
|
||||
list->tail.data=NULL;
|
||||
list->tail.prev=&list->head;
|
||||
list->tail.next=NULL;
|
||||
list->head.data = NULL;
|
||||
list->head.prev = NULL;
|
||||
list->head.next = &list->tail;
|
||||
list->tail.data = NULL;
|
||||
list->tail.prev = &list->head;
|
||||
list->tail.next = NULL;
|
||||
list->current = &list->head;
|
||||
|
||||
return list;
|
||||
@@ -33,22 +35,24 @@ LL * LL_new()
|
||||
// TODO: test this function
|
||||
// Destroys the entire list
|
||||
// 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;
|
||||
|
||||
if(!list) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
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.. :)
|
||||
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;
|
||||
}
|
||||
@@ -56,30 +60,35 @@ int LL_Destroy(LL *list)
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// TODO: test this function
|
||||
// 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;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
int LL_node_Unlink(LL_node *node)
|
||||
int
|
||||
LL_node_Unlink (LL_node * node)
|
||||
{
|
||||
LL_node *next, *prev;
|
||||
|
||||
if(!node) return -1;
|
||||
if (!node)
|
||||
return -1;
|
||||
|
||||
next = node->next;
|
||||
prev = node->prev;
|
||||
|
||||
if(next)
|
||||
if (next)
|
||||
next->prev = prev;
|
||||
if(prev)
|
||||
if (prev)
|
||||
prev->next = next;
|
||||
|
||||
node->next = NULL;
|
||||
@@ -90,28 +99,34 @@ int LL_node_Unlink(LL_node *node)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// 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);
|
||||
else return -1;
|
||||
if (node->data)
|
||||
free (node->data);
|
||||
else
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// 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.head=%8x\n", &list->head);
|
||||
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;
|
||||
else
|
||||
list->current = &list->head;
|
||||
@@ -121,11 +136,13 @@ int LL_Rewind(LL *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;
|
||||
else
|
||||
list->current = &list->tail;
|
||||
@@ -135,36 +152,36 @@ int LL_End(LL *list)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Go to the next node
|
||||
int LL_Next(LL *list)
|
||||
int
|
||||
LL_Next (LL * list)
|
||||
{
|
||||
if(!list) return -1;
|
||||
if(!list->current) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!list->current)
|
||||
return -1;
|
||||
|
||||
if(list->current->next != &list->tail)
|
||||
{
|
||||
if (list->current->next != &list->tail) {
|
||||
list->current = list->current->next;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Go to the previous node
|
||||
int LL_Prev(LL *list)
|
||||
int
|
||||
LL_Prev (LL * list)
|
||||
{
|
||||
if(!list) return -1;
|
||||
if(!list->current) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!list->current)
|
||||
return -1;
|
||||
|
||||
if(list->current->prev != &list->head)
|
||||
{
|
||||
if (list->current->prev != &list->head) {
|
||||
list->current = list->current->prev;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -172,19 +189,25 @@ int LL_Prev(LL *list)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Data manipulation
|
||||
void * LL_Get(LL *list)
|
||||
void *
|
||||
LL_Get (LL * list)
|
||||
{
|
||||
if(!list) return NULL;
|
||||
if(!list->current) return NULL;
|
||||
if (!list)
|
||||
return NULL;
|
||||
if (!list->current)
|
||||
return NULL;
|
||||
|
||||
return list->current->data;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
int LL_Put(LL *list, void *data)
|
||||
int
|
||||
LL_Put (LL * list, void *data)
|
||||
{
|
||||
if(!list) return -1;
|
||||
if(!list->current) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!list->current)
|
||||
return -1;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// 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(!node) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!node)
|
||||
return -1;
|
||||
|
||||
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;
|
||||
|
||||
if(!list) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
//if(!add) return -1; // Nevermind.. NULL entries can be good...
|
||||
if(!list->current) return -1;
|
||||
if (!list->current)
|
||||
return -1;
|
||||
|
||||
//LL_dprint(list);
|
||||
|
||||
|
||||
node = malloc(sizeof(LL_node));
|
||||
if(!node) return -1;
|
||||
node = malloc (sizeof (LL_node));
|
||||
if (!node)
|
||||
return -1;
|
||||
//printf("Allocated node\n");
|
||||
|
||||
/* printf("Current: prev: %8x\tnode: %8x\tnext: %8x\n", */
|
||||
/* (int)list->current->prev, */
|
||||
/* (int)list->current, */
|
||||
/* (int)list->current->next); */
|
||||
if(list->current == &list->tail)
|
||||
{
|
||||
if (list->current == &list->tail) {
|
||||
list->current = list->current->prev;
|
||||
/* printf("Was at end of list...\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->next); */
|
||||
}
|
||||
|
||||
// printf("Setting node data\n");
|
||||
node->next = list->current->next;
|
||||
node->prev = list->current;
|
||||
@@ -295,7 +337,7 @@ int LL_AddNode(LL *list, void * add) // Adds node AFTER current one
|
||||
/* (int)node->next); */
|
||||
|
||||
// printf("Relinking...\n");
|
||||
if(node->next)
|
||||
if (node->next)
|
||||
node->next->prev = node;
|
||||
// printf("...\n");
|
||||
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;
|
||||
|
||||
if(!list) return -1;
|
||||
if(!add) return -1;
|
||||
if(!list->current) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!add)
|
||||
return -1;
|
||||
if (!list->current)
|
||||
return -1;
|
||||
|
||||
node = malloc(sizeof(LL_node));
|
||||
if(!node) return -1;
|
||||
node = malloc (sizeof (LL_node));
|
||||
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->prev = list->current->prev;
|
||||
node->data = add;
|
||||
|
||||
if(list->current->prev)
|
||||
if (list->current->prev)
|
||||
list->current->prev->next = 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
|
||||
// ... and advances one node forward
|
||||
void * LL_DeleteNode(LL *list)
|
||||
void *
|
||||
LL_DeleteNode (LL * list)
|
||||
{
|
||||
LL_node *next, *prev;
|
||||
void *data;
|
||||
|
||||
if(!list) return NULL;
|
||||
if(!list->current) return NULL;
|
||||
if(list->current == &list->head) return NULL;
|
||||
if(list->current == &list->tail) return NULL;
|
||||
if (!list)
|
||||
return NULL;
|
||||
if (!list->current)
|
||||
return NULL;
|
||||
if (list->current == &list->head)
|
||||
return NULL;
|
||||
if (list->current == &list->tail)
|
||||
return NULL;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("LL_DeleteNode: Before...\n");
|
||||
LL_dprint(list);
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
printf ("LL_DeleteNode: Before...\n");
|
||||
LL_dprint (list);
|
||||
#endif
|
||||
|
||||
|
||||
next = list->current->next;
|
||||
prev = list->current->prev;
|
||||
data = list->current->data;
|
||||
|
||||
if(prev)
|
||||
if (prev)
|
||||
prev->next = next;
|
||||
|
||||
if(next)
|
||||
if (next)
|
||||
next->prev = prev;
|
||||
|
||||
list->current->prev = NULL;
|
||||
@@ -372,30 +425,34 @@ void * LL_DeleteNode(LL *list)
|
||||
//if(list->current->data) free(list->current->data);
|
||||
list->current->data = NULL;
|
||||
|
||||
free(list->current);
|
||||
free (list->current);
|
||||
|
||||
list->current = next;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("LL_DeleteNode: After...\n");
|
||||
LL_dprint(list);
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
printf ("LL_DeleteNode: After...\n");
|
||||
LL_dprint (list);
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Removes a specific node...
|
||||
void * LL_Remove(LL *list, void * data)
|
||||
void *
|
||||
LL_Remove (LL * list, void *data)
|
||||
{
|
||||
void *find;
|
||||
|
||||
if(!list) return NULL;
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
LL_Rewind(list);
|
||||
LL_Rewind (list);
|
||||
do {
|
||||
find = LL_Get(list);
|
||||
if(find == data) return LL_DeleteNode(list);
|
||||
} while (LL_Next(list) == 0);
|
||||
find = LL_Get (list);
|
||||
if (find == data)
|
||||
return LL_DeleteNode (list);
|
||||
} while (LL_Next (list) == 0);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -403,84 +460,106 @@ void * LL_Remove(LL *list, void * data)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// 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(!add) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!add)
|
||||
return -1;
|
||||
|
||||
// printf("Going to end of list...\n");
|
||||
LL_End(list);
|
||||
LL_End (list);
|
||||
|
||||
// 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(!add) return -1;
|
||||
if (!list)
|
||||
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;
|
||||
|
||||
if(!list) return -1;
|
||||
if (!list)
|
||||
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...
|
||||
if(list->current == &list->head) list->current = list->current->next;
|
||||
if(list->current == &list->tail) list->current = list->current->prev;
|
||||
if (list->current == &list->head)
|
||||
list->current = list->current->next;
|
||||
if (list->current == &list->tail)
|
||||
list->current = list->current->prev;
|
||||
// List is empty
|
||||
if(list->current == &list->head) return 0;
|
||||
if (list->current == &list->head)
|
||||
return 0;
|
||||
// List has one item
|
||||
if(list->current->prev == &list->head) return 0;
|
||||
if (list->current->prev == &list->head)
|
||||
return 0;
|
||||
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
if(!list) return -1;
|
||||
if (!list)
|
||||
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...
|
||||
if(list->current == &list->tail) list->current = list->current->prev;
|
||||
if(list->current == &list->head) list->current = list->current->next;
|
||||
if (list->current == &list->tail)
|
||||
list->current = list->current->prev;
|
||||
if (list->current == &list->head)
|
||||
list->current = list->current->next;
|
||||
// List is empty
|
||||
if(list->current == &list->tail) return 0;
|
||||
if (list->current == &list->tail)
|
||||
return 0;
|
||||
// List has one item
|
||||
if(list->current->next == &list->tail) return 0;
|
||||
if (list->current->next == &list->tail)
|
||||
return 0;
|
||||
|
||||
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;
|
||||
|
||||
@@ -531,33 +618,36 @@ int LL_UnRoll(LL *list)// Roll the other way...
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Add an item to the end of its "priority group"
|
||||
// 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;
|
||||
int i;
|
||||
|
||||
if(!list) return -1;
|
||||
if(!add) return -1;
|
||||
if(!compare) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!add)
|
||||
return -1;
|
||||
if (!compare)
|
||||
return -1;
|
||||
|
||||
// From the end of the list, keep searching while we're "less than"
|
||||
// the given nodes...
|
||||
LL_End(list);
|
||||
LL_End (list);
|
||||
do {
|
||||
data = LL_Get(list);
|
||||
if(data)
|
||||
data = LL_Get (list);
|
||||
if (data) {
|
||||
i = compare (add, data);
|
||||
if (i >= 0) // If we're in the right place, add it and exit
|
||||
{
|
||||
i = compare(add, data);
|
||||
if(i >= 0) // If we're in the right place, add it and exit
|
||||
{
|
||||
LL_AddNode(list, add);
|
||||
LL_AddNode (list, add);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} while(LL_Prev(list) == 0);
|
||||
} while (LL_Prev (list) == 0);
|
||||
|
||||
// If we're less than *everything*, put it at the beginning
|
||||
LL_Unshift(list, add);
|
||||
LL_Unshift (list, add);
|
||||
|
||||
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 *secondprev, *secondnext;
|
||||
|
||||
if(!one || !two) return -1;
|
||||
if(one == two) return 0; // Do nothing
|
||||
if (!one || !two)
|
||||
return -1;
|
||||
if (one == two)
|
||||
return 0; // Do nothing
|
||||
|
||||
firstprev = one->prev; // Look up the nodes neighbors...
|
||||
firstnext = one->next;
|
||||
secondprev = two->prev;
|
||||
secondnext = two->next;
|
||||
|
||||
if(firstprev != NULL) firstprev->next = two; // Swap the neighboring
|
||||
if(firstnext != NULL) firstnext->prev = two; // nodes pointers...
|
||||
if(secondprev != NULL) secondprev->next = one;
|
||||
if(secondprev != NULL) secondnext->prev = one;
|
||||
if (firstprev != NULL)
|
||||
firstprev->next = two; // Swap the neighboring
|
||||
if (firstnext != NULL)
|
||||
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->prev = secondprev;
|
||||
two->next = firstnext;
|
||||
two->prev = firstprev;
|
||||
|
||||
if(firstnext == two) one->prev = two; // Fix things in case
|
||||
if(firstprev == two) one->next = two; // they were next to
|
||||
if(secondprev == one) two->next = one; // each other...
|
||||
if(secondnext == one) two->prev = one;
|
||||
if (firstnext == two)
|
||||
one->prev = two; // Fix things in case
|
||||
if (firstprev == two)
|
||||
one->next = two; // they were next to
|
||||
if (secondprev == one)
|
||||
two->next = one; // each other...
|
||||
if (secondnext == one)
|
||||
two->prev = one;
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
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;
|
||||
int num = 0;
|
||||
|
||||
if(!list) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
|
||||
node = &list->head;
|
||||
|
||||
for(num = -1;
|
||||
node != &list->tail;
|
||||
num++)
|
||||
for (num = -1; node != &list->tail; num++)
|
||||
node = node->next;
|
||||
|
||||
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
|
||||
// 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;
|
||||
|
||||
if(!list) return NULL;
|
||||
if(!compare) return NULL;
|
||||
if(!value) return NULL;
|
||||
if (!list)
|
||||
return NULL;
|
||||
if (!compare)
|
||||
return NULL;
|
||||
if (!value)
|
||||
return NULL;
|
||||
|
||||
do{
|
||||
data = LL_Get(list);
|
||||
if( 0 == compare(data, value) ) return data;
|
||||
do {
|
||||
data = LL_Get (list);
|
||||
if (0 == compare (data, value))
|
||||
return data;
|
||||
|
||||
} while(LL_Next(list) == 0);
|
||||
} while (LL_Next (list) == 0);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -651,78 +759,71 @@ void * LL_Find(LL *list, int compare(void *, void *), void *value)
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// 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
|
||||
LL_node *best, *last; // best match and last node in the list
|
||||
LL_node *current;
|
||||
|
||||
|
||||
if(!list) return -1;
|
||||
if(!compare) return -1;
|
||||
if (!list)
|
||||
return -1;
|
||||
if (!compare)
|
||||
return -1;
|
||||
|
||||
numnodes = LL_Length(list); // get the number of nodes...
|
||||
if(0 > LL_End(list)) return -1; // Find the last node.
|
||||
last = LL_GetNode(list);
|
||||
numnodes = LL_Length (list); // get the number of nodes...
|
||||
if (0 > LL_End (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--)
|
||||
{
|
||||
LL_Rewind(list); // get the first node again
|
||||
for (i = numnodes - 1; i > 0; i--) {
|
||||
LL_Rewind (list); // get the first node again
|
||||
best = last; // reset our "best" node
|
||||
|
||||
for(j=0; j<i; j++)
|
||||
{
|
||||
current = LL_GetNode(list);
|
||||
for (j = 0; j < i; j++) {
|
||||
current = LL_GetNode (list);
|
||||
// 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
|
||||
}
|
||||
LL_Next(list); // Go to the next node.
|
||||
LL_Next (list); // Go to the next node.
|
||||
}
|
||||
|
||||
LL_SwapNodes(last, best); // Switch two nodes...
|
||||
if(best) last = best->prev;
|
||||
else return -1;
|
||||
LL_SwapNodes (last, best); // Switch two nodes...
|
||||
if (best)
|
||||
last = best->prev;
|
||||
else
|
||||
return -1;
|
||||
|
||||
//last = LL_FindPrev(best); // And go backwards by one node.
|
||||
}
|
||||
|
||||
//return LLFindFirst(current); // return pointer to the first node.
|
||||
LL_Rewind(list);
|
||||
LL_Rewind (list);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void LL_dprint(LL *list)
|
||||
void
|
||||
LL_dprint (LL * list)
|
||||
{
|
||||
LL_node *current;
|
||||
|
||||
current = &list->head;
|
||||
|
||||
printf("Head: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n",
|
||||
(int)list->head.prev,
|
||||
(int)&list->head,
|
||||
(int)list->head.next);
|
||||
printf ("Head: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", (int) list->head.prev, (int) &list->head, (int) list->head.next);
|
||||
|
||||
for(current = current->next;
|
||||
current != &list->tail;
|
||||
current = current->next)
|
||||
{
|
||||
printf("node: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n",
|
||||
(int)current->prev,
|
||||
(int)current,
|
||||
(int)current->next);
|
||||
for (current = current->next; current != &list->tail; 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",
|
||||
(int)list->tail.prev,
|
||||
(int)&list->tail,
|
||||
(int)list->tail.next);
|
||||
printf ("Tail: prev:\t0x%8x\taddr:\t0x%8x\tnext:\t0x%8x\n", (int) list->tail.prev, (int) &list->tail, (int) list->tail.next);
|
||||
}
|
||||
|
||||
+38
-40
@@ -118,14 +118,12 @@
|
||||
|
||||
// See LL.c for more detailed descriptions of these functions.
|
||||
|
||||
typedef struct LL_node
|
||||
{
|
||||
typedef struct LL_node {
|
||||
struct LL_node *next, *prev;
|
||||
void *data;
|
||||
} LL_node;
|
||||
|
||||
typedef struct LL
|
||||
{
|
||||
typedef struct LL {
|
||||
LL_node head, tail;
|
||||
LL_node *current;
|
||||
} LL;
|
||||
@@ -133,51 +131,51 @@ typedef struct LL
|
||||
|
||||
|
||||
// Creates a new list...
|
||||
LL * LL_new();
|
||||
LL *LL_new ();
|
||||
// Destroying lists...
|
||||
int LL_Destroy(LL *list);
|
||||
int LL_node_Destroy(LL_node *node);
|
||||
int LL_node_Unlink(LL_node *node);
|
||||
int LL_node_DestroyData(LL_node *node);
|
||||
int LL_Destroy (LL * list);
|
||||
int LL_node_Destroy (LL_node * node);
|
||||
int LL_node_Unlink (LL_node * node);
|
||||
int LL_node_DestroyData (LL_node * node);
|
||||
|
||||
// Returns to the beginning of the list...
|
||||
int LL_Rewind(LL *list);
|
||||
int LL_Rewind (LL * list);
|
||||
// Goes to the end of the list...
|
||||
int LL_End(LL *list);
|
||||
int LL_End (LL * list);
|
||||
// Go to the next node
|
||||
int LL_Next(LL *list);
|
||||
int LL_Next (LL * list);
|
||||
// Go to the previous node
|
||||
int LL_Prev(LL *list);
|
||||
int LL_Prev (LL * list);
|
||||
|
||||
// Data manipulation
|
||||
void * LL_Get(LL *list);
|
||||
int LL_Put(LL *list, void *data);
|
||||
void *LL_Get (LL * list);
|
||||
int LL_Put (LL * list, void *data);
|
||||
// Don't use these next two unless you really know what you're doing.
|
||||
LL_node * LL_GetNode(LL *list);
|
||||
int LL_PutNode(LL *list, LL_node *node);
|
||||
LL_node *LL_GetNode (LL * list);
|
||||
int LL_PutNode (LL * list, LL_node * node);
|
||||
|
||||
void * LL_GetFirst(LL *list); // gets data from first node
|
||||
void * LL_GetNext (LL *list); // ... next node
|
||||
void * LL_GetPrev (LL *list); // ... prev node
|
||||
void * LL_GetLast (LL *list); // ... last node
|
||||
void *LL_GetFirst (LL * list); // gets data from first node
|
||||
void *LL_GetNext (LL * list); // ... next node
|
||||
void *LL_GetPrev (LL * list); // ... prev node
|
||||
void *LL_GetLast (LL * list); // ... last node
|
||||
|
||||
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_AddNode (LL * list, void *add); // Adds node AFTER 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
|
||||
void * LL_DeleteNode(LL *list);
|
||||
void *LL_DeleteNode (LL * list);
|
||||
// Removes a specific node...
|
||||
void * LL_Remove(LL *list, void * data);
|
||||
void *LL_Remove (LL * list, void *data);
|
||||
|
||||
// Stack operations
|
||||
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_Top(LL *list); // Peek at end node
|
||||
void * LL_Shift(LL *list); // Remove node from start of list
|
||||
void * LL_Look(LL *list); // Peek at first node
|
||||
int LL_Unshift(LL *list, void *add); // Add node to beginning 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_Top (LL * list); // Peek at end node
|
||||
void *LL_Shift (LL * list); // Remove node from start of list
|
||||
void *LL_Look (LL * list); // Peek at first node
|
||||
int LL_Unshift (LL * list, void *add); // Add node to beginning of list
|
||||
|
||||
int LL_Roll(LL *list); // Make first node last
|
||||
int LL_UnRoll(LL *list);// Roll the other way...
|
||||
int LL_Roll (LL * list); // Make first node last
|
||||
int LL_UnRoll (LL * list); // Roll the other way...
|
||||
|
||||
// Queue operations...
|
||||
//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)
|
||||
|
||||
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_nSwapNodes(int one, int 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_Length(LL *list); // Returns # of nodes in entire list
|
||||
int LL_Length (LL * list); // Returns # of nodes in entire list
|
||||
|
||||
// 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...
|
||||
int LL_Sort(LL *list, int compare(void *, void *));
|
||||
int LL_Sort (LL * list, int compare (void *, void *));
|
||||
|
||||
|
||||
// Debugging...
|
||||
void LL_dprint(LL *list);
|
||||
void LL_dprint (LL * list);
|
||||
|
||||
#endif
|
||||
|
||||
+52
-46
@@ -31,18 +31,16 @@
|
||||
typedef struct sockaddr_in sockaddr_in;
|
||||
|
||||
|
||||
static int sock_init_sockaddr (sockaddr_in *name,
|
||||
const char *hostname,
|
||||
unsigned short int port)
|
||||
static int
|
||||
sock_init_sockaddr (sockaddr_in * name, const char *hostname, unsigned short int port)
|
||||
{
|
||||
struct hostent *hostinfo;
|
||||
|
||||
name->sin_family = AF_INET;
|
||||
name->sin_port = htons (port);
|
||||
hostinfo = gethostbyname (hostname);
|
||||
if (hostinfo == NULL)
|
||||
{
|
||||
fprintf (stderr,"sock_init_sockaddr: Unknown host %s.\n", hostname);
|
||||
if (hostinfo == NULL) {
|
||||
fprintf (stderr, "sock_init_sockaddr: Unknown host %s.\n", hostname);
|
||||
return -1;
|
||||
}
|
||||
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
|
||||
@@ -52,67 +50,68 @@ static int sock_init_sockaddr (sockaddr_in *name,
|
||||
}
|
||||
|
||||
// Client functions...
|
||||
int sock_connect(char *host, unsigned short int port)
|
||||
int
|
||||
sock_connect (char *host, unsigned short int port)
|
||||
{
|
||||
struct sockaddr_in servername;
|
||||
int sock;
|
||||
int err=0;
|
||||
int err = 0;
|
||||
|
||||
|
||||
debug("sock_connect: Creating socket\n");
|
||||
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if(sock < 0)
|
||||
{
|
||||
perror("sock_connect: Error creating socket");
|
||||
debug ("sock_connect: Creating socket\n");
|
||||
sock = socket (PF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
perror ("sock_connect: Error creating socket");
|
||||
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,
|
||||
(struct sockaddr *) &servername,
|
||||
sizeof (servername));
|
||||
if(err<0)
|
||||
{
|
||||
perror("sock_connect: connect failed");
|
||||
shutdown(sock, 2);
|
||||
err = connect (sock, (struct sockaddr *) &servername, sizeof (servername));
|
||||
if (err < 0) {
|
||||
perror ("sock_connect: connect failed");
|
||||
shutdown (sock, 2);
|
||||
return 0; // Normal exit if server doesn't exist...
|
||||
}
|
||||
|
||||
fcntl(sock, F_SETFL, O_NONBLOCK);
|
||||
fcntl (sock, F_SETFL, O_NONBLOCK);
|
||||
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int sock_close(int fd)
|
||||
int
|
||||
sock_close (int fd)
|
||||
{
|
||||
int err;
|
||||
|
||||
err=shutdown(fd, 2);
|
||||
if (!err) close (fd);
|
||||
err = shutdown (fd, 2);
|
||||
if (!err)
|
||||
close (fd);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// Send/receive lines of text
|
||||
int sock_send_string(int fd, char *string)
|
||||
int
|
||||
sock_send_string (int fd, char *string)
|
||||
{
|
||||
int len;
|
||||
int offset=0;
|
||||
int offset = 0;
|
||||
|
||||
if (!string) return -1;
|
||||
if (!string)
|
||||
return -1;
|
||||
|
||||
len = strlen (string) + 1;
|
||||
while (offset != len) {
|
||||
// write isn't guaranteed to send the entire string at once,
|
||||
// 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 (errno != EAGAIN) {
|
||||
perror ("sock_send_string: socket write error");
|
||||
printf("Message was: %s\n", string);
|
||||
printf ("Message was: %s\n", string);
|
||||
//shutdown(fd, 2);
|
||||
return sent;
|
||||
}
|
||||
@@ -120,7 +119,7 @@ int sock_send_string(int fd, char *string)
|
||||
} else if (sent == 0) {
|
||||
// when this returns zero, it generally means
|
||||
// we got disconnected
|
||||
return sent+offset;
|
||||
return sent + offset;
|
||||
}
|
||||
|
||||
offset += sent;
|
||||
@@ -130,13 +129,16 @@ int sock_send_string(int fd, char *string)
|
||||
}
|
||||
|
||||
// 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;
|
||||
int recv = 0;
|
||||
|
||||
if (!dest) return -1;
|
||||
if (maxlen <= 0) return 0;
|
||||
if (!dest)
|
||||
return -1;
|
||||
if (maxlen <= 0)
|
||||
return 0;
|
||||
|
||||
while (1) {
|
||||
int err = read (fd, ptr, 1);
|
||||
@@ -170,7 +172,7 @@ int sock_recv_string(int fd, char *dest, size_t maxlen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (recv < maxlen-1) {
|
||||
if (recv < maxlen - 1) {
|
||||
dest[recv] = 0;
|
||||
}
|
||||
|
||||
@@ -178,16 +180,18 @@ int sock_recv_string(int fd, char *dest, size_t maxlen)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// write isn't guaranteed to send the entire string at once,
|
||||
// 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 (errno != EAGAIN) {
|
||||
perror ("sock_send: socket write error");
|
||||
@@ -198,7 +202,7 @@ int sock_send(int fd, void *src, size_t size)
|
||||
} else if (sent == 0) {
|
||||
// when this returns zero, it generally means
|
||||
// we got disconnected
|
||||
return sent+offset;
|
||||
return sent + offset;
|
||||
}
|
||||
|
||||
offset += sent;
|
||||
@@ -207,16 +211,18 @@ int sock_send(int fd, void *src, size_t size)
|
||||
return offset;
|
||||
}
|
||||
|
||||
int sock_recv(int fd, void *dest, size_t maxlen)
|
||||
int
|
||||
sock_recv (int fd, void *dest, size_t maxlen)
|
||||
{
|
||||
int err;
|
||||
|
||||
if(!dest) return -1;
|
||||
if(maxlen <= 0) return 0;
|
||||
if (!dest)
|
||||
return -1;
|
||||
if (maxlen <= 0)
|
||||
return 0;
|
||||
|
||||
err = read (fd, dest, maxlen);
|
||||
if (err < 0)
|
||||
{
|
||||
if (err < 0) {
|
||||
//fprintf (stderr,"sock_recv: socket read error\n");
|
||||
//shutdown(fd, 2);
|
||||
return err;
|
||||
|
||||
+6
-6
@@ -22,15 +22,15 @@
|
||||
|
||||
|
||||
// Client functions...
|
||||
int sock_connect(char *host, unsigned short int port);
|
||||
int sock_close(int fd);
|
||||
int sock_connect (char *host, unsigned short int port);
|
||||
int sock_close (int fd);
|
||||
// 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...
|
||||
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
|
||||
int sock_send(int fd, void *src, size_t size);
|
||||
int sock_recv(int fd, void *dest, size_t maxlen);
|
||||
int sock_send (int fd, void *src, size_t size);
|
||||
int sock_recv (int fd, void *dest, size_t maxlen);
|
||||
|
||||
|
||||
// Er, ignore the rest of this file. I'll clean it up sometime...
|
||||
|
||||
+13
-12
@@ -5,30 +5,31 @@
|
||||
#include "debug.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 *item;
|
||||
int i=0;
|
||||
int i = 0;
|
||||
|
||||
if(!argv) return -1;
|
||||
if(!str) return 0;
|
||||
if(max_args < 1) return 0;
|
||||
if (!argv)
|
||||
return -1;
|
||||
if (!str)
|
||||
return 0;
|
||||
if (max_args < 1)
|
||||
return 0;
|
||||
|
||||
//debug("get_args(%i): string=%s\n", max_args, str);
|
||||
|
||||
// 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);
|
||||
if(i < max_args)
|
||||
{
|
||||
if (i < max_args) {
|
||||
argv[i] = item;
|
||||
i++;
|
||||
}
|
||||
else return i;
|
||||
} else
|
||||
return i;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#ifndef 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
|
||||
|
||||
Reference in New Issue
Block a user