ref: 4e09200f7e4bf8791c9c720922734e0759206b72
dir: /libxpath/fns.c/
#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 (p->name && e->name && 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) { Elem *e; for (e = ep->child; e; e = e->next) { if (e->name && e->name[0] == '?') goto Found; } return; Found: buildsingleelem(r, e); } void fcomment(XpResult *r, Elem *ep) { Elem *e; XpResult t; for (e = ep->child; e; e = e->next) { if (!e->name) { if (r->type != Xstring) { r->type = Xstring; r->num = 0; } buildsinglestring(&t, e->pcdata); appendresult(r, t); } } } 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 }