Instrument Neutral Distributed Interface INDI  1.9.2
Classes | Macros | Enumerations | Functions
lilxml.c File Reference
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lilxml.h"

Go to the source code of this file.

Classes

struct  String
 
struct  LilXML_
 
struct  xml_ele_
 
struct  xml_att_
 

Macros

#define MINMEM   64 /* starting string length */
 
#define PRINDENT   4 /* sample print indent each level */
 

Enumerations

enum  State {
  LOOK4START = 0, LOOK4TAG, INTAG, LOOK4ATTRN,
  INATTRN, LOOK4ATTRV, SAWSLASH, INATTRV,
  ENTINATTRV, LOOK4CON, INCON, ENTINCON,
  SAWLTINCON, LOOK4CLOSETAG, INCLOSETAG
}
 

Functions

void lilxmlMalloc (void *(*newmalloc)(size_t size), void *(*newrealloc)(void *ptr, size_t size), void(*newfree)(void *ptr))
 
LilXMLnewLilXML ()
 Create a new lilxml parser. More...
 
void delLilXML (LilXML *lp)
 Delete a lilxml parser. More...
 
void delXMLEle (XMLEle *ep)
 delXMLEle Delete XML element. More...
 
XMLEle ** parseXMLChunk (LilXML *lp, char *buf, int size, char ynot[])
 Process an XML chunk. More...
 
XMLElereadXMLEle (LilXML *lp, int newc, char ynot[])
 Process an XML one char at a time. More...
 
XMLEleparseXML (char buf[], char ynot[])
 
XMLElecloneXMLEle (XMLEle *ep)
 
XMLAttfindXMLAtt (XMLEle *ep, const char *name)
 Find an XML attribute within an XML element. More...
 
XMLElefindXMLEle (XMLEle *ep, const char *tag)
 Find an XML element within an XML element. More...
 
XMLElenextXMLEle (XMLEle *ep, int init)
 Iterate an XML element for a list of nesetd XML elements. More...
 
XMLAttnextXMLAtt (XMLEle *ep, int init)
 Iterate an XML element for a list of XML attributes. More...
 
XMLEleparentXMLEle (XMLEle *ep)
 Return the parent of an XML element. More...
 
XMLEleparentXMLAtt (XMLAtt *ap)
 Return the parent of an XML attribute. More...
 
char * tagXMLEle (XMLEle *ep)
 Return the tag of an XML element. More...
 
char * pcdataXMLEle (XMLEle *ep)
 Return the pcdata of an XML element. More...
 
int pcdatalenXMLEle (XMLEle *ep)
 Return the number of characters in pcdata in an XML element. More...
 
char * nameXMLAtt (XMLAtt *ap)
 Return the name of an XML attribute. More...
 
char * valuXMLAtt (XMLAtt *ap)
 Return the value of an XML attribute. More...
 
int nXMLEle (XMLEle *ep)
 Return the number of nested XML elements in a parent XML element. More...
 
int nXMLAtt (XMLEle *ep)
 Return the number of XML attributes in a parent XML element. More...
 
const char * findXMLAttValu (XMLEle *ep, const char *name)
 Find an XML element's attribute value. More...
 
XMLElereadXMLFile (FILE *fp, LilXML *lp, char ynot[])
 Handy wrapper to read one xml file. More...
 
XMLEleaddXMLEle (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. More...
 
void appXMLEle (XMLEle *ep, XMLEle *newep)
 
void editXMLEle (XMLEle *ep, const char *pcdata)
 set the pcdata of the given element More...
 
XMLAttaddXMLAtt (XMLEle *ep, const char *name, const char *valu)
 Add an XML attribute to an existing XML element. More...
 
void rmXMLAtt (XMLEle *ep, const char *name)
 Remove an XML attribute from an XML element. More...
 
void editXMLAtt (XMLAtt *ap, const char *str)
 change the value of an attribute to str. More...
 
void prXMLEle (FILE *fp, XMLEle *ep, int level)
 Print an XML element. More...
 
int 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. N.B. set level = 0 on first call. More...
 
int sprlXMLEle (XMLEle *ep, int level)
 return number of bytes in a string guaranteed able to hold result of sprXLMEle(ep) (sans trailing \0@). N.B. set level = 0 on first call. More...
 
char * entityXML (char *s)
 return a string with all xml-sensitive characters within the passed string replaced with their entity sequence equivalents. N.B. caller must use the returned string before calling us again. More...
 

Macro Definition Documentation

◆ MINMEM

#define MINMEM   64 /* starting string length */

Definition at line 55 of file lilxml.c.

◆ PRINDENT

#define PRINDENT   4 /* sample print indent each level */

Definition at line 698 of file lilxml.c.

Enumeration Type Documentation

◆ State

enum State
Enumerator
LOOK4START 
LOOK4TAG 
INTAG 
LOOK4ATTRN 
INATTRN 
LOOK4ATTRV 
SAWSLASH 
INATTRV 
ENTINATTRV 
LOOK4CON 
INCON 
ENTINCON 
SAWLTINCON 
LOOK4CLOSETAG 
INCLOSETAG 

Definition at line 72 of file lilxml.c.

Function Documentation

◆ addXMLAtt()

XMLAtt* addXMLAtt ( XMLEle ep,
const char *  name,
const char *  value 
)

Add an XML attribute to an existing XML element.

Parameters
eppointer to an XML element
namethe name of the XML attribute to add.
valuethe value of the XML attribute to add.

Definition at line 664 of file lilxml.c.

◆ addXMLEle()

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.

Returns
if parent is NULL, a new root is returned, otherwise, parent is returned.

Definition at line 639 of file lilxml.c.

◆ appXMLEle()

void appXMLEle ( XMLEle ep,
XMLEle newep 
)

Definition at line 649 of file lilxml.c.

◆ cloneXMLEle()

XMLEle* cloneXMLEle ( XMLEle ep)

Definition at line 476 of file lilxml.c.

◆ delLilXML()

void delLilXML ( LilXML lp)

Delete a lilxml parser.

Parameters
lpa pointer to a lilxml parser to be deleted.

Definition at line 157 of file lilxml.c.

◆ delXMLEle()

void delXMLEle ( XMLEle e)

delXMLEle Delete XML element.

Parameters
ePointer to XML element to delete. If nullptr, no action is taken.

Definition at line 165 of file lilxml.c.

◆ editXMLAtt()

void editXMLAtt ( XMLAtt ap,
const char *  str 
)

change the value of an attribute to str.

Parameters
appointer to XML attribute
strnew attribute value

Definition at line 689 of file lilxml.c.

◆ editXMLEle()

void editXMLEle ( XMLEle ep,
const char *  pcdata 
)

set the pcdata of the given element

Parameters
eppointer to an XML element.
pcdatapcdata to set.

Definition at line 656 of file lilxml.c.

◆ entityXML()

char* entityXML ( char *  s)

return a string with all xml-sensitive characters within the passed string replaced with their entity sequence equivalents. N.B. caller must use the returned string before calling us again.

Definition at line 815 of file lilxml.c.

◆ findXMLAtt()

XMLAtt* findXMLAtt ( XMLEle e,
const char *  name 
)

Find an XML attribute within an XML element.

Parameters
ea pointer to the XML element to search.
namethe attribute name to search for.
Returns
A pointer to the XML attribute if found or NULL on failure.

Definition at line 493 of file lilxml.c.

◆ findXMLAttValu()

const char* findXMLAttValu ( XMLEle ep,
const char *  name 
)

Find an XML element's attribute value.

Parameters
epa pointer to an XML element.
namethe name of the XML attribute to retrieve its value.
Returns
the value string of an XML element on success. NULL on failure.

Definition at line 613 of file lilxml.c.

◆ findXMLEle()

XMLEle* findXMLEle ( XMLEle e,
const char *  tag 
)

Find an XML element within an XML element.

Parameters
ea pointer to the XML element to search.
tagthe element tag to search for.
Returns
A pointer to the XML element if found or NULL on failure.

Definition at line 506 of file lilxml.c.

◆ lilxmlMalloc()

void lilxmlMalloc ( void *(*)(size_t size)  newmalloc,
void *(*)(void *ptr, size_t size)  newrealloc,
void(*)(void *ptr)  newfree 
)

Definition at line 139 of file lilxml.c.

◆ nameXMLAtt()

char* nameXMLAtt ( XMLAtt ap)

Return the name of an XML attribute.

Parameters
apa pointer to an XML attribute.
Returns
the name string of the attribute.

Definition at line 587 of file lilxml.c.

◆ newLilXML()

LilXML* newLilXML ( )

Create a new lilxml parser.

Returns
a pointer to the lilxml parser on success. NULL on failure.

Definition at line 148 of file lilxml.c.

◆ nextXMLAtt()

XMLAtt* nextXMLAtt ( XMLEle ep,
int  first 
)

Iterate an XML element for a list of XML attributes.

Parameters
epa pointer to the XML element to iterate.
firstthe index of the starting XML attribute. Pass 1 to start iteration from the beginning of the XML element. Pass 0 to get the next attribute thereater.
Returns
On success, a pointer to the next XML attribute is returned. NULL when there are no more attributes.

Definition at line 541 of file lilxml.c.

◆ nextXMLEle()

XMLEle* nextXMLEle ( XMLEle ep,
int  first 
)

Iterate an XML element for a list of nesetd XML elements.

Parameters
epa pointer to the XML element to iterate.
firstthe index of the starting XML element. Pass 1 to start iteration from the beginning of the XML element. Pass 0 to get the next element thereater.
Returns
On success, a pointer to the next XML element is returned. NULL when there are no more elements.

Definition at line 524 of file lilxml.c.

◆ nXMLAtt()

int nXMLAtt ( XMLEle ep)

Return the number of XML attributes in a parent XML element.

Parameters
epa pointer to an XML element.
Returns
the number of XML attributes within the XML element.

Definition at line 605 of file lilxml.c.

◆ nXMLEle()

int nXMLEle ( XMLEle ep)

Return the number of nested XML elements in a parent XML element.

Parameters
epa pointer to an XML element.
Returns
the number of nested XML elements.

Definition at line 599 of file lilxml.c.

◆ parentXMLAtt()

XMLEle* parentXMLAtt ( XMLAtt ap)

Return the parent of an XML attribute.

Returns
a pointer to the XML element parent.

Definition at line 561 of file lilxml.c.

◆ parentXMLEle()

XMLEle* parentXMLEle ( XMLEle ep)

Return the parent of an XML element.

Returns
a pointer to the XML element parent.

Definition at line 555 of file lilxml.c.

◆ parseXML()

XMLEle* parseXML ( char  buf[],
char  ynot[] 
)

Definition at line 459 of file lilxml.c.

◆ parseXMLChunk()

XMLEle** parseXMLChunk ( LilXML lp,
char *  buf,
int  size,
char  errmsg[] 
)

Process an XML chunk.

Parameters
lpa pointer to a lilxml parser.
bufbuffer to process.
sizesize of buf
errmsga buffer to store error messages if an error in parsing is encountered.
Returns
return a pointer to a NULL terminated array of parsed XML elements. An array of size 1 with on a NULL element means there is nothing to parse or a parsing is still in progress. A NULL pointer may be returned if a parsing error occurs. Check errmsg for errors if NULL is returned.

Definition at line 213 of file lilxml.c.

◆ pcdatalenXMLEle()

int pcdatalenXMLEle ( XMLEle ep)

Return the number of characters in pcdata in an XML element.

Parameters
epa pointer to an XML element.
Returns
the length of the pcdata string.

Definition at line 581 of file lilxml.c.

◆ pcdataXMLEle()

char* pcdataXMLEle ( XMLEle ep)

Return the pcdata of an XML element.

Parameters
epa pointer to an XML element.
Returns
the pcdata string on success.

Definition at line 575 of file lilxml.c.

◆ prXMLEle()

void prXMLEle ( FILE *  fp,
XMLEle e,
int  level 
)

Print an XML element.

Parameters
fpa pointer to FILE where the print output is directed.
ethe XML element to print.
levelthe printing level, set to 0 to print the whole element.

Definition at line 699 of file lilxml.c.

◆ readXMLEle()

XMLEle* readXMLEle ( LilXML lp,
int  c,
char  errmsg[] 
)

Process an XML one char at a time.

Parameters
lpa pointer to a lilxml parser.
cone character to process.
errmsga buffer to store error messages if an error in parsing is encounterd.
Returns
When the function parses a complete valid XML element, it will return a pointer to the XML element. A NULL is returned when parsing the element is still in progress, or if a parsing error occurs. Check errmsg for errors if NULL is returned.

Definition at line 383 of file lilxml.c.

◆ readXMLFile()

XMLEle* readXMLFile ( FILE *  fp,
LilXML lp,
char  errmsg[] 
)

Handy wrapper to read one xml file.

Parameters
fppointer to FILE to read.
lppointer to lilxml parser.
errmsga buffer to store error messages on failure.
Returns
root element else NULL with report in errmsg[].

Definition at line 622 of file lilxml.c.

◆ rmXMLAtt()

void rmXMLAtt ( XMLEle ep,
const char *  name 
)

Remove an XML attribute from an XML element.

Parameters
eppointer to an XML element.
namethe name of the XML attribute to remove

Definition at line 673 of file lilxml.c.

◆ sprlXMLEle()

int sprlXMLEle ( XMLEle ep,
int  level 
)

return number of bytes in a string guaranteed able to hold result of sprXLMEle(ep) (sans trailing \0@). N.B. set level = 0 on first call.

Definition at line 776 of file lilxml.c.

◆ sprXMLEle()

int 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. N.B. set level = 0 on first call.

Returns
return length of resulting string (sans trailing \0@)

Definition at line 735 of file lilxml.c.

◆ tagXMLEle()

char* tagXMLEle ( XMLEle ep)

Return the tag of an XML element.

Parameters
epa pointer to an XML element.
Returns
the tag string.

Definition at line 569 of file lilxml.c.

◆ valuXMLAtt()

char* valuXMLAtt ( XMLAtt ap)

Return the value of an XML attribute.

Parameters
apa pointer to an XML attribute.
Returns
the value string of the attribute.

Definition at line 593 of file lilxml.c.