sfeed

Simple RSS and Atom feed parser
git clone https://git.sinitax.com/codemadness/sfeed
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

sfeed_xmlenc.c (1335B)


      1#include <stdio.h>
      2#include <stdlib.h>
      3#include <strings.h>
      4
      5#include "util.h"
      6#include "xml.h"
      7
      8static XMLParser parser;
      9static int tags;
     10
     11static void
     12xmltagstart(XMLParser *p, const char *t, size_t tl)
     13{
     14	/* optimization: try to find a processing instruction only at the
     15	   start of the data at the first few starting tags. */
     16	if (tags++ > 3)
     17		exit(0);
     18}
     19
     20static void
     21xmlattr(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
     22	const char *v, size_t vl)
     23{
     24	if (strcasecmp(t, "?xml") || strcasecmp(n, "encoding"))
     25		return;
     26
     27	for (; *v; v++) {
     28		if (ISALPHA((unsigned char)*v) ||
     29		    ISDIGIT((unsigned char)*v) ||
     30		    *v == '.' || *v == ':' || *v == '-' || *v == '_')
     31			putchar(TOLOWER((unsigned char)*v));
     32	}
     33}
     34
     35static void
     36xmlattrend(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl)
     37{
     38	if (strcasecmp(t, "?xml") || strcasecmp(n, "encoding"))
     39		return;
     40	putchar('\n');
     41	exit(0);
     42}
     43
     44int
     45main(void)
     46{
     47	if (pledge("stdio", NULL) == -1)
     48		err(1, "pledge");
     49
     50	parser.xmlattr = xmlattr;
     51	parser.xmlattrentity = xmlattr; /* no entity conversion */
     52	parser.xmlattrend = xmlattrend;
     53	parser.xmltagstart = xmltagstart;
     54
     55	/* NOTE: GETNEXT is defined in xml.h for inline optimization */
     56	xml_parse(&parser);
     57
     58	checkfileerror(stdin, "<stdin>", 'r');
     59	checkfileerror(stdout, "<stdout>", 'w');
     60
     61	return 0;
     62}