# id = xmlOpen("/tmp/file.xml");
# xmlClose(id);
#
-void *xmlOpen(const char *);
-void xmlClose(const void *);
+void *xmlOpen(const char *filename);
+void *xmlInitBuffer(const char *buffer, size_t size);
+void xmlClose(void *xid);
#
# Get the Id of a node at the specified path
# e.g.
# xnid = xmlNodeGet(id, "/path/to/specified/node");
#
-void *xmlNodeGet(const void *, const char *);
-void *xmlNodeCopy(void *, const char *);
+void *xmlNodeGet(const void *xid, const char *path);
+void *xmlNodeCopy(const void *xid, const char *path);
#
# Functions to walk the node tree and process them one by one.
# e.g.
# xmid = xmlMarkId(id);
-# num = xmlNodeGetNum(xmid);
+# num = xmlNodeGetNum(xmid, "node");
# for (i=0; i<num; i++) {
# if (xmlNodeGetPos(id, xmid, "element", i) != 0) {
# if ((s = xmlGetString(xmid)) != 0) {
# }
# free(xmid);
#
-void *xmlMarkId(void *);
-unsigned int xmlNodeGetNum(void *, const char *);
-void *xmlNodeGetPos(const void *, void *, const char *, int);
+void *xmlMarkId(const void *xid);
+unsigned int xmlNodeGetNum(const void *xid, const char *path);
+void *xmlNodeGetPos(const void *pid, void *xid, const char *element, int pos);
#
# Get the name of the current node
#
-char *xmlNodeGetName(void *);
-size_t xmlNodeCopyName(void *, const char *, size_t);
+char *xmlNodeGetName(const void *xid);
+size_t xmlNodeCopyName(const void *xid, const char *buffer, size_t size);
#
# These functions work on the current node.
# xnid = xmlNodeGet(id, "/path/to/specified/node");
# if (xmlCompareString(xnid, "value") == 0) printf("We have a match!\n");
#
-long int xmlGetInt(void *);
-double xmlGetDouble(void *);
-char *xmlGetString(void *);
-size_t xmlCopyString(void *, char *, const size_t);
-int xmlCompareString(const void *, const char *);
+long int xmlGetInt(const void *xid);
+double xmlGetDouble(const void *xid);
+char *xmlGetString(const void *xid);
+size_t xmlCopyString(const void *xid, char *buffer, const size_t size);
+int xmlCompareString(const void *xid, const char *str);
#
# These functions work on a specified node path
# xnid = xmlNodeGet(id, "/path/to");
# i = xmlNodeGetInt(xnid, "node");
#
-long int xmlNodeGetInt(void *, const char *);
-double xmlNodeGetDouble(void *, const char *);
-char *xmlNodeGetString(void *, const char *);
-size_t xmlNodeCopyString(void *, const char *, char *, const size_t);
-int xmlNodeCompareString(const void *, const char *, const char *);
+long int xmlNodeGetInt(const void *xid, const char *path);
+double xmlNodeGetDouble(const void *xid, const char *path);
+char *xmlNodeGetString(const void *xid, const char *path);
+size_t xmlNodeCopyString(const void *xid, const char *path,
+ char *buffer, const size_t size);
+int xmlNodeCompareString(const void *xid, const char *path, const char *str);
#
# These functions work on a specified atribute
# i = xmlAttributeGetInt(id, "n");
#
# or
-# s = xmlNodeGetString(id, "type");
+# s = xmlAttributeGetString(id, "type");
# if (s) printf("node is of type '%s'\n", s);
# free(s);
#
-long int xmlAttributeGetInt(const void *, const char *);
-double xmlAttributeGetDouble(const void *, const char *);
-char *xmlAttributeGetString(const void *, const char *);
-size_t xmlAttributeCopyString(const void *, const char *, const char *, size_t);
-int xmlAttributeCompareString(const void *, const char *, const char *);
+long int xmlAttributeGetInt(const void *xid, const char *attr);
+double xmlAttributeGetDouble(const void *xid, const char *attr);
+char *xmlAttributeGetString(const void *xid, const char *attr);
+size_t xmlAttributeCopyString(const void *xid, const char *attr,
+ const char *buffer, size_t size);
+int xmlAttributeCompareString(const void *xid, const char *attr,
+ const char *str);
#
# Error detection and reporting functions
# int err = xmlErrorGetNo(id, 1); /* clear last error */
# if (err) printf("Error #%i at line %u: '%s'\n", err, err_lineno, err_str);
#
-int xmlErrorGetNo(const void *, int);
-size_t xmlErrorGetLineNo(const void *, int);
-const char *xmlErrorGetString(const void *, int);
+int xmlErrorGetNo(const void *xid, int clear);
+size_t xmlErrorGetLineNo(const void *xid, int clear);
+size_t xmlErrorGetColumnNo(const void *xid, int clear);
+const char *xmlErrorGetString(const void *xid, int clear);
#endif
#include <stdlib.h> /* free, malloc */
#include <string.h>
+#ifndef _MSC_VER
+#include <strings.h> /* strncasecmp */
+#else
+# define strncasecmp strnicmp
+#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
-#ifndef _MSC_VER
-# include <strings.h> /* strncasecmp */
-#else
-# define strncasecmp strnicmp
-#endif
#ifndef XML_NONVALIDATING
#include "xml.h"
return (void *)rid;
}
+void *
+xmlInitBuffer(const char *buffer, size_t size)
+{
+ struct _root_id *rid = 0;
+
+ if (buffer && (size>0))
+ {
+ rid->fd = 0;
+ rid->start = (char *)buffer;
+ rid->len = size;
+ rid->name = 0;
+#ifndef XML_NONVALIDATING
+ rid->info = 0;
+#endif
+ }
+
+ return (void *)rid;
+}
+
void
xmlClose(void *id)
{
assert(rid != 0);
assert(rid->name == 0);
- munmap(rid->start, rid->len);
- close(rid->fd);
+ if (rid->fd)
+ {
+ munmap(rid->start, rid->len);
+ close(rid->fd);
+ }
if (rid->info) free(rid->info);
free(rid);
while (ps<pe)
{
new = memchr(ps, '\n', pe-ps);
- if (new)
- {
- ps = new;
- ret++;
- }
- ps++;
+ if (new) ret++;
+ else break;
+ ps = new+1;
}
if (clear) err->err_no = 0;
return ret;
}
+size_t
+xmlErrorGetColumnNo(const void *id, int clear)
+{
+ size_t ret = 0;
+
+ if (id)
+ {
+ struct _xml_id *xid = (struct _xml_id *)id;
+ struct _root_id *rid;
+
+ if (xid->name) rid = xid->root;
+ else rid = (struct _root_id *)xid;
+
+ assert(rid != 0);
+
+ if (rid->info)
+ {
+ struct _xml_error *err = rid->info;
+ char *ps = rid->start;
+ char *pe = err->pos;
+ char *new;
+
+ while (ps<pe)
+ {
+ new = memchr(ps, '\n', pe-ps);
+ new = memchr(ps, '\n', pe-ps);
+ if (new) ret++;
+ else break;
+ ps = new+1;
+ }
+ ret = pe-ps;
+
+ if (clear) err->err_no = 0;
+ }
+ }
+
+ return ret;
+}
+
const char *
xmlErrorGetString(const void *id, int clear)
{
char *
__xmlNodeGet(const char *start, size_t *len, char **name, size_t *rlen, size_t *nodenum)
{
+ char *element, *open, *start_tag=0;
char *new, *cur, *ne, *ret = 0;
- char *element, *start_tag=0;
size_t restlen, elementlen;
size_t return_len = 0;
int found, num;
/*
* get element name and a pointer to after the opening tag
*/
+ open = cur;
element = *name;
elementlen = *rlen;
len_remaining = restlen;
new = __xmlNodeGet(cur-1, &slen, &node, &nlen, &pos);
if (!new)
{
+ if (nlen == 0) /* error upstream */
+ {
+ *rlen = nlen;
+ *name = node;
+ *len = slen;
+ return 0;
+ }
+
if (slen == restlen)
{
*rlen = 0;
if (found == num)
{
- if (start_tag)
- {
- *len = new-ret-1;
- *name = start_tag;
- }
- else /* report error */
- {
- *rlen = 0;
- *name = new;
- *len = XML_ELEMENT_NO_OPENING_TAG;
- return 0;
- }
+ if (start_tag)
+ {
+ *len = new-ret-1;
+ *name = start_tag;
+ }
+ else /* report error */
+ {
+ *rlen = 0;
+ *name = new;
+ *len = XML_ELEMENT_NO_OPENING_TAG;
+ return 0;
+ }
}
found++;
}
- /* else proper closing tag not yet found, continue. */
- /* TODO: could be a bad match to the opening tag though */
- /* like: <test></teft> */
+#ifndef XML_NONVALIDATING
+ /* strcmp is a heavy operation when not required */
+ else if (strncmp(open, new+1, elementlen))
+ {
+ *rlen = 0;
+ *name = new+1;
+ *len = XML_ELEMENT_NO_CLOSING_TAG;
+ return 0;
+ }
+#endif
new = memchr(cur, '>', restlen);
if (!new)