Instrument Neutral Distributed Interface INDI 2.2.5
lilxml.h
Go to the documentation of this file.
1#if 0
2liblilxml
3Copyright (C) 2003 Elwood C. Downey
42022 Ludovic Pollet
5
6This library is free software;
7you can redistribute it and / or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation;
10either
11version 2.1 of the License, or (at your option) any later version.
12
13This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY;
15without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public
20License along with this library;
21if not, write to the Free Software
22Foundation, 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/* size every caller must allocate for the errmsg[]/ynot[] buffer passed to
70 * parseXMLChunk(), readXMLEle(), readXMLFile() and parseXML(). Error text
71 * is always truncated to fit this size, no matter how long the offending
72 * XML token is. */
73#define XML_ERROR_SIZE 1024
74
75#ifdef __cplusplus
76extern "C" {
77#endif
78
79/* opaque handle types */
80typedef struct xml_att_ XMLAtt;
81typedef struct xml_ele_ XMLEle;
82typedef struct LilXML_ LilXML;
83
88
89/* creation and destruction functions */
90
94extern LilXML *newLilXML();
95
99extern void delLilXML(LilXML *lp);
100
105extern void delXMLEle(XMLEle *e);
106
114extern XMLEle **parseXMLChunk(LilXML *lp, char *buf, int size, char errmsg[]);
115
122extern XMLEle *readXMLEle(LilXML *lp, int c, char errmsg[]);
123
124/* search functions */
130extern XMLAtt *findXMLAtt(XMLEle *e, const char *name);
131
137extern XMLEle *findXMLEle(XMLEle *e, const char *tag);
138
139/* iteration functions */
145extern XMLEle *nextXMLEle(XMLEle *ep, int first);
146
152extern XMLAtt *nextXMLAtt(XMLEle *ep, int first);
153
154/* tree functions */
158extern XMLEle *parentXMLEle(XMLEle *ep);
159
163extern XMLEle *parentXMLAtt(XMLAtt *ap);
164
165/* access functions */
170extern char *tagXMLEle(XMLEle *ep);
171
176extern char *pcdataXMLEle(XMLEle *ep);
177
182extern char *nameXMLAtt(XMLAtt *ap);
183
188extern char *valuXMLAtt(XMLAtt *ap);
189
194extern int pcdatalenXMLEle(XMLEle *ep);
195
200extern int nXMLEle(XMLEle *ep);
201
206extern int nXMLAtt(XMLEle *ep);
207
208/* editing functions */
212extern XMLEle *addXMLEle(XMLEle *parent, const char *tag);
213
214
219extern XMLEle *setXMLEleTag(XMLEle *ep, const char * tag);
220
225extern void editXMLEle(XMLEle *ep, const char *pcdata);
226
232extern XMLAtt *addXMLAtt(XMLEle *ep, const char *name, const char *value);
233
238extern void rmXMLAtt(XMLEle *ep, const char *name);
239
244extern void editXMLAtt(XMLAtt *ap, const char *str);
245
250extern char *entityXML(char *str);
251
252/* convenience functions */
258extern const char *findXMLAttValu(XMLEle *ep, const char *name);
259
264extern XMLEle *shallowCloneXMLEle(XMLEle * ele);
265
273extern XMLEle * cloneXMLEle(XMLEle * ep, int (*replace)(void * self, XMLEle * source, XMLEle * * replace), void * self);
274
281extern XMLEle *readXMLFile(FILE *fp, LilXML *lp, char errmsg[]);
282
288extern void prXMLEle(FILE *fp, XMLEle *e, int level);
289
295extern size_t sprXMLEle(char *s, XMLEle *ep, int level);
296
300extern size_t sprlXMLEle(XMLEle *ep, int level);
301
305extern size_t sprXMLCDataOffset(XMLEle * root, XMLEle * child, int level);
306
307/* install alternatives to malloc/realloc/free */
308extern void indi_xmlMalloc(void *(*newmalloc)(size_t size), void *(*newrealloc)(void *ptr, size_t size),
309 void (*newfree)(void *ptr));
310
313#ifdef __cplusplus
314}
315#endif
316
317/* examples.
318
319 initialize a lil xml context and read an XML file in a root element
320
321 LilXML *lp = newLilXML();
322 char errmsg[1024];
323 XMLEle *root, *ep;
324 int c;
325
326 while ((c = fgetc(stdin)) != EOF) {
327 root = readXMLEle (lp, c, errmsg);
328 if (root)
329 break;
330 if (errmsg[0])
331 error ("Error: %s\n", errmsg);
332 }
333
334 print the tag and pcdata content of each child element within the root
335
336 for (ep = nextXMLEle (root, 1); ep != NULL; ep = nextXMLEle (root, 0))
337 printf ("%s: %s\n", tagXMLEle(ep), pcdataXMLEle(ep));
338
339
340 finished with root element and with lil xml context
341
342 delXMLEle (root);
343 delLilXML (lp);
344 */
345
346/* For RCS Only -- Do Not Edit
347 * @(#) $RCSfile$ $Date: 2007-09-17 16:34:48 +0300 (Mon, 17 Sep 2007) $ $Revision: 713418 $ $Name: $
348 */
const char * findXMLAttValu(XMLEle *ep, const char *name)
Find an XML element's attribute value.
Definition lilxml.cpp:644
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
XMLEle * parentXMLEle(XMLEle *ep)
Return the parent of an XML element.
Definition lilxml.cpp:586
int nXMLEle(XMLEle *ep)
Return the number of nested XML elements in a parent XML element.
Definition lilxml.cpp:630
XMLEle * readXMLEle(LilXML *lp, int c, char errmsg[])
Process an XML one char at a time.
Definition lilxml.cpp:385
XMLEle * nextXMLEle(XMLEle *ep, int first)
Iterate an XML element for a list of nesetd XML elements.
Definition lilxml.cpp:555
LilXML * newLilXML()
Create a new lilxml parser.
Definition lilxml.cpp:150
XMLEle ** parseXMLChunk(LilXML *lp, char *buf, int size, char errmsg[])
Process an XML chunk.
Definition lilxml.cpp:215
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
char * tagXMLEle(XMLEle *ep)
Return the tag of an XML element.
Definition lilxml.cpp:600
char * nameXMLAtt(XMLAtt *ap)
Return the name of an XML attribute.
Definition lilxml.cpp:618
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
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
XMLAtt * findXMLAtt(XMLEle *e, const char *name)
Find an XML attribute within an XML element.
Definition lilxml.cpp:524
void prXMLEle(FILE *fp, XMLEle *e, int level)
Print an XML element.
Definition lilxml.cpp:844
XMLEle * parentXMLAtt(XMLAtt *ap)
Return the parent of an XML attribute.
Definition lilxml.cpp:592
XMLAtt * nextXMLAtt(XMLEle *ep, int first)
Iterate an XML element for a list of XML attributes.
Definition lilxml.cpp:572
char * entityXML(char *str)
return a string with all xml-sensitive characters within the passed string replaced with their entity...
Definition lilxml.cpp:975
char * valuXMLAtt(XMLAtt *ap)
Return the value of an XML attribute.
Definition lilxml.cpp:624
void editXMLEle(XMLEle *ep, const char *pcdata)
set the pcdata of the given element
Definition lilxml.cpp:698
XMLEle * setXMLEleTag(XMLEle *ep, const char *tag)
Update the tag of an element.
Definition lilxml.cpp:688
void rmXMLAtt(XMLEle *ep, const char *name)
Remove an XML attribute from an XML element.
Definition lilxml.cpp:715
XMLEle * findXMLEle(XMLEle *e, const char *tag)
Find an XML element within an XML element.
Definition lilxml.cpp:537
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))
void delXMLEle(XMLEle *e)
delXMLEle Delete XML element.
Definition lilxml.cpp:167
char * pcdataXMLEle(XMLEle *ep)
Return the pcdata of an XML element.
Definition lilxml.cpp:606
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 * shallowCloneXMLEle(XMLEle *ele)
return a surface copy of a node. Don't copy childs or cdata.
Definition lilxml.cpp:731
XMLEle * readXMLFile(FILE *fp, LilXML *lp, char errmsg[])
Handy wrapper to read one xml file.
Definition lilxml.cpp:653
int pcdatalenXMLEle(XMLEle *ep)
Return the number of characters in pcdata in an XML element.
Definition lilxml.cpp:612
XMLAtt * addXMLAtt(XMLEle *ep, const char *name, const char *value)
Add an XML attribute to an existing XML element.
Definition lilxml.cpp:706