server/src/util/lists.c

112 lines
2 KiB
C
Raw Normal View History

#ifdef _MSC_VER
#include <platform.h>
#endif
2016-08-30 21:22:32 +02:00
#include "lists.h"
#include "strings.h"
2010-08-08 10:06:34 +02:00
#include <stdlib.h>
2016-08-30 21:15:28 +02:00
#include <string.h>
2010-08-08 10:06:34 +02:00
#include <assert.h>
typedef struct void_list {
struct void_list *next;
void *data;
} void_list;
2011-03-07 08:02:35 +01:00
void addlist(void *l1, void *p1)
2010-08-08 10:06:34 +02:00
{
/* add entry p to the end of list l */
2010-08-08 10:06:34 +02:00
void_list **l;
void_list *p, *q;
2010-08-08 10:06:34 +02:00
l = (void_list **)l1;
p = (void_list *)p1;
assert(p->next == 0);
2010-08-08 10:06:34 +02:00
if (*l) {
for (q = *l; q->next; q = q->next)
assert(q);
q->next = p;
}
else
*l = p;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void choplist(void *a, void *b)
2010-08-08 10:06:34 +02:00
{
void_list **l = (void_list **)a, *p = (void_list *)b;
/* remove entry p from list l - when called, a pointer to p must be
* kept in order to use (and free) p; if omitted, this will be a
* memory leak */
void_list **q;
for (q = l; *q; q = &((*q)->next)) {
if (*q == p) {
*q = p->next;
p->next = 0;
break;
}
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void translist(void *l1, void *l2, void *p)
2010-08-08 10:06:34 +02:00
{
/* remove entry p from list l1 and add it at the end of list l2 */
2010-08-08 10:06:34 +02:00
choplist(l1, p);
addlist(l2, p);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void freelist(void *p1)
2010-08-08 10:06:34 +02:00
{
/* remove all entries following and including entry p from a listlist */
2010-08-08 10:06:34 +02:00
void_list *p, *p2;
2010-08-08 10:06:34 +02:00
p = (void_list *)p1;
2010-08-08 10:06:34 +02:00
while (p) {
p2 = p->next;
free(p);
p = p2;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
unsigned int listlen(void *l)
2010-08-08 10:06:34 +02:00
{
/* count entries p in list l */
2010-08-08 10:06:34 +02:00
unsigned int i;
void_list *p;
2010-08-08 10:06:34 +02:00
for (p = (void_list *)l, i = 0; p; p = p->next, i++);
return i;
2010-08-08 10:06:34 +02:00
}
/* - String Listen --------------------------------------------- */
void addstrlist(strlist ** SP, const char *s)
{
strlist *slist = malloc(sizeof(strlist));
if (!slist) abort();
slist->next = NULL;
slist->s = str_strdup(s);
addlist(SP, slist);
}
void freestrlist(strlist * s)
{
strlist *q, *p = s;
while (p) {
q = p->next;
free(p->s);
free(p);
p = q;
}
}