1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/* Copyright (c) 1996--1999 Geoff Pike. */
/* All rights reserved. */
/* Floater is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* This software is provided "as is" and comes with absolutely no */
/* warranties. Geoff Pike is not liable for damages under any */
/* circumstances. Support is not provided. Use at your own risk. */
/* Personal, non-commercial use is allowed. Attempting to make money */
/* from Floater or products or code derived from Floater is not allowed */
/* without prior written consent from Geoff Pike. Anything that remotely */
/* involves commercialism, including (but not limited to) systems that */
/* show advertisements while being used and systems that collect */
/* information on users that is later sold or traded require prior */
/* written consent from Geoff Pike. */
#include "floater.h"
stringlist *member(char *s, stringlist *l)
{
while (l != NULL) if (streq(s, l->s)) return l; else l = l->next;
return NULL;
}
/* free a node; return the contents of its `next' pointer */
/* (somewhat misnamed---perhaps freestringlistnode would be better) */
stringlist *freestringlist(stringlist *l)
{
stringlist *ret;
assert(l != NULL);
ret = l->next;
free(l->s);
free(l);
return ret;
}
/* adjoin */
stringlist *addstringlist(char *s, stringlist *who)
{
stringlist *l;
if (member(s, who)) return who;
l = alloc(stringlist);
l->s = STRDUP(s);
l->next = who;
return l;
}
/* add s on the front without copying it or checking for its presence in who */
stringlist *consstringlist(char *s, stringlist *who)
{
stringlist *l;
l = alloc(stringlist);
l->s = s;
l->next = who;
return l;
}
/* remove first occurrence of s, return the resulting list */
stringlist *removestringlist(char *s, stringlist *who)
{
stringlist *l;
if (who == NULL) return NULL;
if (streq(who->s, s)) return freestringlist(who);
for (l=who; l->next != NULL; l = l->next)
if (streq(l->next->s, s)) {
l->next = freestringlist(l->next);
return who;
}
return who;
}
int length(stringlist *l)
{
int i;
for (i = 0; l != NULL; l = l->next) i++;
return i;
}
|