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
|
/*
* Routines to check for the occurrence of a class.
*
* Part of HTML-XML-utils, see:
* http://www.w3.org/Tools/HTML-XML-utils/
*
* Author: Bert Bos <bert@w3.org>
* Created: 20 Aug 2000
*
**/
#define _GNU_SOURCE /* We need strcasestr() */
#include "config.h"
#include <assert.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
# ifndef HAVE_STRSTR
# include "strstr.e"
# endif
#endif
#include <ctype.h>
#include "export.h"
#include "types.e"
/* contains -- check if s contains word (case-insenstive), return pointer */
EXPORT conststring contains(const conststring s, const conststring word)
{
conststring t = s;
unsigned char c;
while ((t = strcasestr(t, word))) {
if ((c = *(t + strlen(word))) && !isspace(c)) t++; /* Not end of word */
else if (t != s && !isspace(*(t - 1))) t++; /* Not beginning of word */
else return t; /* Found it */
}
return NULL; /* Not found */
}
/* has_class -- check for class=word in list of attributes */
EXPORT Boolean has_class(pairlist attribs, const string word)
{
pairlist p;
for (p = attribs; p; p = p->next) {
if (strcasecmp(p->name, "class") == 0 && contains(p->value, word))
return True;
}
return False;
}
/* in_list -- check if word occurs in array list */
static Boolean in_list(const string word, const string *list)
{
int i;
for (i = 0; list[i]; i++) if (contains(word, list[i])) return True;
return False;
}
/* has_class_in_list -- check for class=word for all words in array c */
EXPORT Boolean has_class_in_list(const pairlist attribs, const string *c)
{
pairlist p;
assert(c);
for (p = attribs; p; p = p->next) {
if (strcasecmp(p->name, "class") == 0 && in_list(p->value, c))
return True;
}
return False;
}
|