server/src/util/lists.c

130 lines
2.8 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
2015-01-30 22:10:29 +01:00
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
2010-08-08 10:06:34 +02:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2017-12-28 10:50:08 +01:00
ACTION OF CONTRACT, NEGLIGENCE OR OTH19ER TORTIOUS ACTION, ARISING OUT OF
2010-08-08 10:06:34 +02:00
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#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;
}
}