Instrument Neutral Distributed Interface INDI  2.0.2
lilxml.h
Go to the documentation of this file.
1 #if 0
2 liblilxml
3 Copyright (C) 2003 Elwood C. Downey
4 2022 Ludovic Pollet
5 
6 This library is free software;
7 you can redistribute it and / or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation;
10 either
11 version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY;
15 without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library;
21 if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
23 
24 #endif
25 
65 #pragma once
66 
67 #include <stdio.h>
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 /* opaque handle types */
74 typedef struct xml_att_ XMLAtt;
75 typedef struct xml_ele_ XMLEle;
76 typedef struct LilXML_ LilXML;
77 
82 
83 /* creation and destruction functions */
84 
88 extern LilXML *newLilXML();
89 
93 extern void delLilXML(LilXML *lp);
94 
99 extern void delXMLEle(XMLEle *e);
100 
108 extern XMLEle **parseXMLChunk(LilXML *lp, char *buf, int size, char errmsg[]);
109 
116 extern XMLEle *readXMLEle(LilXML *lp, int c, char errmsg[]);
117 
118 /* search functions */
124 extern XMLAtt *findXMLAtt(XMLEle *e, const char *name);
125 
131 extern XMLEle *findXMLEle(XMLEle *e, const char *tag);
132 
133 /* iteration functions */
139 extern XMLEle *nextXMLEle(XMLEle *ep, int first);
140 
146 extern XMLAtt *nextXMLAtt(XMLEle *ep, int first);
147 
148 /* tree functions */
152 extern XMLEle *parentXMLEle(XMLEle *ep);
153 
157 extern XMLEle *parentXMLAtt(XMLAtt *ap);
158 
159 /* access functions */
164 extern char *tagXMLEle(XMLEle *ep);
165 
170 extern char *pcdataXMLEle(XMLEle *ep);
171 
176 extern char *nameXMLAtt(XMLAtt *ap);
177 
182 extern char *valuXMLAtt(XMLAtt *ap);
183 
188 extern int pcdatalenXMLEle(XMLEle *ep);
189 
194 extern int nXMLEle(XMLEle *ep);
195 
200 extern int nXMLAtt(XMLEle *ep);
201 
202 /* editing functions */
206 extern XMLEle *addXMLEle(XMLEle *parent, const char *tag);
207 
208 
213 extern XMLEle *setXMLEleTag(XMLEle *ep, const char * tag);
214 
219 extern void editXMLEle(XMLEle *ep, const char *pcdata);
220 
226 extern XMLAtt *addXMLAtt(XMLEle *ep, const char *name, const char *value);
227 
232 extern void rmXMLAtt(XMLEle *ep, const char *name);
233 
238 extern void editXMLAtt(XMLAtt *ap, const char *str);
239 
244 extern char *entityXML(char *str);
245 
246 /* convenience functions */
252 extern const char *findXMLAttValu(XMLEle *ep, const char *name);
253 
258 extern XMLEle *shallowCloneXMLEle(XMLEle * ele);
259 
267 extern XMLEle * cloneXMLEle(XMLEle * ep, int (*replace)(void * self, XMLEle * source, XMLEle * * replace), void * self);
268 
275 extern XMLEle *readXMLFile(FILE *fp, LilXML *lp, char errmsg[]);
276 
282 extern void prXMLEle(FILE *fp, XMLEle *e, int level);
283 
289 extern size_t sprXMLEle(char *s, XMLEle *ep, int level);
290 
294 extern size_t sprlXMLEle(XMLEle *ep, int level);
295 
299 extern size_t sprXMLCDataOffset(XMLEle * root, XMLEle * child, int level);
300 
301 /* install alternatives to malloc/realloc/free */
302 extern void indi_xmlMalloc(void *(*newmalloc)(size_t size), void *(*newrealloc)(void *ptr, size_t size),
303  void (*newfree)(void *ptr));
304 
307 #ifdef __cplusplus
308 }
309 #endif
310 
311 /* examples.
312 
313  initialize a lil xml context and read an XML file in a root element
314 
315  LilXML *lp = newLilXML();
316  char errmsg[1024];
317  XMLEle *root, *ep;
318  int c;
319 
320  while ((c = fgetc(stdin)) != EOF) {
321  root = readXMLEle (lp, c, errmsg);
322  if (root)
323  break;
324  if (errmsg[0])
325  error ("Error: %s\n", errmsg);
326  }
327 
328  print the tag and pcdata content of each child element within the root
329 
330  for (ep = nextXMLEle (root, 1); ep != NULL; ep = nextXMLEle (root, 0))
331  printf ("%s: %s\n", tagXMLEle(ep), pcdataXMLEle(ep));
332 
333 
334  finished with root element and with lil xml context
335 
336  delXMLEle (root);
337  delLilXML (lp);
338  */
339 
340 /* For RCS Only -- Do Not Edit
341  * @(#) $RCSfile$ $Date: 2007-09-17 16:34:48 +0300 (Mon, 17 Sep 2007) $ $Revision: 713418 $ $Name: $
342  */
int nXMLEle(XMLEle *ep)
Return the number of nested XML elements in a parent XML element.
Definition: lilxml.cpp:630
XMLEle * findXMLEle(XMLEle *e, const char *tag)
Find an XML element within an XML element.
Definition: lilxml.cpp:537
XMLEle * parentXMLAtt(XMLAtt *ap)
Return the parent of an XML attribute.
Definition: lilxml.cpp:592
XMLEle * shallowCloneXMLEle(XMLEle *ele)
return a surface copy of a node. Don't copy childs or cdata.
Definition: lilxml.cpp:731
LilXML * newLilXML()
Create a new lilxml parser.
Definition: lilxml.cpp:150
char * entityXML(char *str)
return a string with all xml-sensitive characters within the passed string replaced with their entity...
Definition: lilxml.cpp:975
XMLEle * parentXMLEle(XMLEle *ep)
Return the parent of an XML element.
Definition: lilxml.cpp:586
XMLEle * readXMLFile(FILE *fp, LilXML *lp, char errmsg[])
Handy wrapper to read one xml file.
Definition: lilxml.cpp:653
size_t sprXMLCDataOffset(XMLEle *root, XMLEle *child, int level)
return exact position of cdata of child in printed representation of root N.B. set level = 0 on first...
Definition: lilxml.cpp:930
int nXMLAtt(XMLEle *ep)
Return the number of XML attributes in a parent XML element.
Definition: lilxml.cpp:636
XMLAtt * findXMLAtt(XMLEle *e, const char *name)
Find an XML attribute within an XML element.
Definition: lilxml.cpp:524
XMLAtt * nextXMLAtt(XMLEle *ep, int first)
Iterate an XML element for a list of XML attributes.
Definition: lilxml.cpp:572
size_t sprXMLEle(char *s, XMLEle *ep, int level)
sample print ep to string s. N.B. s must be at least as large as that reported by sprlXMLEle()+1....
Definition: lilxml.cpp:874
const char * findXMLAttValu(XMLEle *ep, const char *name)
Find an XML element's attribute value.
Definition: lilxml.cpp:644
XMLEle ** parseXMLChunk(LilXML *lp, char *buf, int size, char errmsg[])
Process an XML chunk.
Definition: lilxml.cpp:215
void prXMLEle(FILE *fp, XMLEle *e, int level)
Print an XML element.
Definition: lilxml.cpp:844
XMLAtt * addXMLAtt(XMLEle *ep, const char *name, const char *value)
Add an XML attribute to an existing XML element.
Definition: lilxml.cpp:706
char * pcdataXMLEle(XMLEle *ep)
Return the pcdata of an XML element.
Definition: lilxml.cpp:606
void editXMLEle(XMLEle *ep, const char *pcdata)
set the pcdata of the given element
Definition: lilxml.cpp:698
XMLEle * cloneXMLEle(XMLEle *ep, int(*replace)(void *self, XMLEle *source, XMLEle **replace), void *self)
clone (deep) a xmlEle. Optional replacement function can be passed, to replace a whole subtree instea...
Definition: lilxml.cpp:493
char * tagXMLEle(XMLEle *ep)
Return the tag of an XML element.
Definition: lilxml.cpp:600
void rmXMLAtt(XMLEle *ep, const char *name)
Remove an XML attribute from an XML element.
Definition: lilxml.cpp:715
XMLEle * nextXMLEle(XMLEle *ep, int first)
Iterate an XML element for a list of nesetd XML elements.
Definition: lilxml.cpp:555
size_t sprlXMLEle(XMLEle *ep, int level)
return number of bytes in a string guaranteed able to hold result of sprXLMEle(ep) (sans trailing \0@...
Definition: lilxml.cpp:922
void indi_xmlMalloc(void *(*newmalloc)(size_t size), void *(*newrealloc)(void *ptr, size_t size), void(*newfree)(void *ptr))
char * nameXMLAtt(XMLAtt *ap)
Return the name of an XML attribute.
Definition: lilxml.cpp:618
void delXMLEle(XMLEle *e)
delXMLEle Delete XML element.
Definition: lilxml.cpp:167
XMLEle * readXMLEle(LilXML *lp, int c, char errmsg[])
Process an XML one char at a time.
Definition: lilxml.cpp:385
void editXMLAtt(XMLAtt *ap, const char *str)
change the value of an attribute to str.
Definition: lilxml.cpp:745
void delLilXML(LilXML *lp)
Delete a lilxml parser.
Definition: lilxml.cpp:159
XMLEle * setXMLEleTag(XMLEle *ep, const char *tag)
Update the tag of an element.
Definition: lilxml.cpp:688
XMLEle * addXMLEle(XMLEle *parent, const char *tag)
add an element with the given tag to the given element. parent can be NULL to make a new root.
Definition: lilxml.cpp:670
int pcdatalenXMLEle(XMLEle *ep)
Return the number of characters in pcdata in an XML element.
Definition: lilxml.cpp:612
char * valuXMLAtt(XMLAtt *ap)
Return the value of an XML attribute.
Definition: lilxml.cpp:624