shithub: xml-9atom

ref: 524f6890a72aa3629cc91ecf5c505ffa52f7eda4
dir: /libxpath/fns.c/

View raw version
#include <u.h>
#include <libc.h>
#include <xml.h>
#include <xpath.h>
#include "dat.h"

int
position(Elem *e)
{
	Elem *p;
	p = e->parent;
	int i;
	
	i = 0;
	for (p = p->child; p; p = p->next) {
		if (strcmp(p->name, e->name) == 0)
			i++;
		if (p == e)
			return i;
	}
	return i;
}

int
last(Elem *e)
{
	Elem *p;
	p = e->parent;
	int i;
	
	i = 0;
	for (p = p->child; p; p = p->next) {
		if (strcmp(p->name, e->name) == 0)
			i++;
	}
	return i;
}

void
ftext(XpResult *r, Elem *ep)
{
	buildsinglestring(r, ep->pcdata);
}

void
fposition(XpResult *r, Elem *ep)
{
	buildsinglenum(r, position(ep));
}

void
fprocinst(XpResult *r, Elem *ep)
{
	fprint(2, "function processing-instruction()");
}

void
fcomment(XpResult *r, Elem *ep)
{
	fprint(2, "function comment()");
}

void
fnode(XpResult *r, Elem *ep)
{
	buildsingleelem(r, ep);
}

void
flast(XpResult *r, Elem *ep)
{
	buildsinglenum(r, last(ep));
}

void
initfuncs()
{
#define F(name, func) addfunc(addname(name), func)

	F("text", ftext);
	F("position", fposition);
	F("processing-instruction", fprocinst);
	F("comment", fcomment);
	F("node", fnode);
	F("last", flast);
	
#undef F
}