Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
xml2.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
26#ifdef FUN_WITH_XML2
27#include <libxml/parser.h>
28#include <libxml/tree.h>
29
36typedef struct {
37 xmlDocPtr doc;
38 int in_use;
39} XmlDocSlot;
46typedef struct {
47 xmlNodePtr node;
48 int in_use;
49} XmlNodeSlot;
50
51static XmlDocSlot g_xml_docs[64];
52static XmlNodeSlot g_xml_nodes[256];
53
62static int xml_doc_alloc(xmlDocPtr d) {
63 for (int i = 1; i < (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])); ++i) {
64 if (!g_xml_docs[i].in_use) {
65 g_xml_docs[i].in_use = 1;
66 g_xml_docs[i].doc = d;
67 return i;
68 }
69 }
70 return 0;
71}
78static xmlDocPtr xml_doc_get(int h) {
79 if (h > 0 && h < (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])) && g_xml_docs[h].in_use) return g_xml_docs[h].doc;
80 return NULL;
81}
88static int xml_doc_free_handle(int h) {
89 if (h <= 0 || h >= (int)(sizeof(g_xml_docs) / sizeof(g_xml_docs[0])) || !g_xml_docs[h].in_use) return 0;
90 if (g_xml_docs[h].doc) xmlFreeDoc(g_xml_docs[h].doc);
91 g_xml_docs[h].doc = NULL;
92 g_xml_docs[h].in_use = 0;
93 return 1;
94}
95
104static int xml_node_alloc(xmlNodePtr n) {
105 for (int i = 1; i < (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])); ++i) {
106 if (!g_xml_nodes[i].in_use) {
107 g_xml_nodes[i].in_use = 1;
108 g_xml_nodes[i].node = n;
109 return i;
110 }
111 }
112 return 0;
113}
120static xmlNodePtr xml_node_get(int h) {
121 if (h > 0 && h < (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])) && g_xml_nodes[h].in_use) return g_xml_nodes[h].node;
122 return NULL;
123}
133static int xml_node_free_handle(int h) {
134 if (h <= 0 || h >= (int)(sizeof(g_xml_nodes) / sizeof(g_xml_nodes[0])) || !g_xml_nodes[h].in_use) return 0;
135 /* nodes are owned by their document; do not free here */
136 g_xml_nodes[h].node = NULL;
137 g_xml_nodes[h].in_use = 0;
138 return 1;
139}
140#endif
int n
Definition insert.c:41