#ifndef LIBXML2_COMPAT_H
#define LIBXML2_COMPAT_H
#include "xmloxide.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef xmloxide_document xmlDoc;
typedef xmloxide_document *xmlDocPtr;
typedef struct {
xmloxide_document *doc;
uint32_t id;
} xmlNode;
typedef xmlNode *xmlNodePtr;
typedef xmloxide_xpath_value xmlXPathObject;
typedef xmloxide_xpath_value *xmlXPathObjectPtr;
static inline void xmlInitParser(void) {}
static inline void xmlCleanupParser(void) {}
static inline void xmlMemoryDump(void) {}
static inline xmlDocPtr xmlParseDoc(const char *input) {
return xmloxide_parse_str(input);
}
static inline xmlDocPtr xmlReadMemory(const char *buffer, int size,
const char *url, const char *encoding,
int options) {
(void)url; (void)encoding; (void)options;
return xmloxide_parse_bytes((const uint8_t *)buffer, (size_t)size);
}
static inline xmlDocPtr xmlReadFile(const char *filename, const char *encoding,
int options) {
(void)encoding; (void)options;
return xmloxide_parse_file(filename);
}
static inline xmlDocPtr htmlReadMemory(const char *buffer, int size,
const char *url, const char *encoding,
int options) {
(void)size; (void)url; (void)encoding; (void)options;
return xmloxide_parse_html(buffer);
}
static inline xmlDocPtr htmlReadMemory5(const char *buffer, int size,
const char *url, const char *encoding,
int options) {
(void)size; (void)url; (void)encoding; (void)options;
return xmloxide_parse_html5(buffer);
}
static inline void xmlFreeDoc(xmlDocPtr doc) {
xmloxide_free_doc(doc);
}
static inline xmlNodePtr xmloxide_compat_make_node(xmlDocPtr doc, uint32_t id) {
if (id == 0) return NULL;
xmlNodePtr node = (xmlNodePtr)malloc(sizeof(xmlNode));
if (node) {
node->doc = doc;
node->id = id;
}
return node;
}
static inline void xmlFreeNode(xmlNodePtr node) {
free(node);
}
static inline xmlNodePtr xmlDocGetRootElement(xmlDocPtr doc) {
return xmloxide_compat_make_node(doc, xmloxide_doc_root_element(doc));
}
static inline xmlNodePtr xmlNodeGetParent(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_compat_make_node(node->doc,
xmloxide_node_parent(node->doc, node->id));
}
static inline xmlNodePtr xmlNodeGetChildren(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_compat_make_node(node->doc,
xmloxide_node_first_child(node->doc, node->id));
}
static inline xmlNodePtr xmlNodeGetNext(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_compat_make_node(node->doc,
xmloxide_node_next_sibling(node->doc, node->id));
}
static inline xmlNodePtr xmlNodeGetPrev(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_compat_make_node(node->doc,
xmloxide_node_prev_sibling(node->doc, node->id));
}
static inline int xmlNodeGetType(xmlNodePtr node) {
if (!node) return -1;
return xmloxide_node_type(node->doc, node->id);
}
static inline char *xmlNodeGetName(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_node_name(node->doc, node->id);
}
static inline char *xmlNodeGetContent(xmlNodePtr node) {
if (!node) return NULL;
return xmloxide_node_text_content(node->doc, node->id);
}
static inline char *xmlGetProp(xmlNodePtr node, const char *name) {
if (!node) return NULL;
return xmloxide_node_attribute(node->doc, node->id, name);
}
static inline int xmlSetProp(xmlNodePtr node, const char *name,
const char *value) {
if (!node) return 0;
return xmloxide_set_attribute(node->doc, node->id, name, value);
}
static inline int xmlUnsetProp(xmlNodePtr node, const char *name) {
if (!node) return 0;
return xmloxide_remove_attribute(node->doc, node->id, name);
}
static inline void xmlDocDumpMemory(xmlDocPtr doc, char **mem, int *size) {
if (!doc || !mem) return;
char *s = xmloxide_serialize(doc);
*mem = s;
if (size) *size = s ? (int)strlen(s) : 0;
}
static inline void xmlDocDumpFormatMemory(xmlDocPtr doc, char **mem,
int *size, int format) {
(void)format;
if (!doc || !mem) return;
char *s = xmloxide_serialize_pretty(doc);
*mem = s;
if (size) *size = s ? (int)strlen(s) : 0;
}
static inline void xmlFree(void *ptr) {
xmloxide_free_string((char *)ptr);
}
static inline xmlXPathObjectPtr xmlXPathEval(const char *expr,
xmlNodePtr context) {
if (!context) return NULL;
return xmloxide_xpath_eval(context->doc, context->id, expr);
}
static inline void xmlXPathFreeObject(xmlXPathObjectPtr obj) {
xmloxide_xpath_free_result(obj);
}
static inline int xmlXPathNodeSetGetLength(xmlXPathObjectPtr obj) {
return (int)xmloxide_xpath_nodeset_count(obj);
}
static inline xmlNodePtr xmlXPathNodeSetItem(xmlXPathObjectPtr obj,
int index,
xmlDocPtr doc) {
uint32_t id = xmloxide_xpath_nodeset_item(obj, (size_t)index);
return xmloxide_compat_make_node(doc, id);
}
static inline const char *xmlGetLastError(void) {
return xmloxide_last_error();
}
static inline void xmlSetGenericErrorFunc(void *ctx,
void (*handler)(void *, const char *, ...)) {
(void)ctx; (void)handler;
}
#ifdef __cplusplus
}
#endif
#endif