xmlParser  2.44
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xmlParser.h
Go to the documentation of this file.
1 /****************************************************************************/
85 #ifndef __INCLUDE_XML_NODE__
86 #define __INCLUDE_XML_NODE__
87 
88 #include <stdlib.h>
89 
90 #if defined(UNICODE) || defined(_UNICODE)
91 // If you comment the next "define" line then the library will never "switch to" _UNICODE (wchar_t*) mode (16/32 bits per characters).
92 // This is useful when you get error messages like:
93 // 'XMLNode::openFileHelper' : cannot convert parameter 2 from 'const char [5]' to 'const wchar_t *'
94 // The _XMLWIDECHAR preprocessor variable force the XMLParser library into either utf16/32-mode (the proprocessor variable
95 // must be defined) or utf8-mode(the pre-processor variable must be undefined).
96 #define _XMLWIDECHAR
97 #endif
98 
99 #if defined(WIN32) || defined(UNDER_CE) || defined(_WIN32) || defined(WIN64) || defined(__BORLANDC__)
100 // comment the next line if you are under windows and the compiler is not Microsoft Visual Studio (6.0 or .NET) or Borland
101 #define _XMLWINDOWS
102 #endif
103 
104 #ifdef XMLDLLENTRY
105 #undef XMLDLLENTRY
106 #endif
107 #ifdef _USE_XMLPARSER_DLL
108 #ifdef _DLL_EXPORTS_
109 #define XMLDLLENTRY __declspec(dllexport)
110 #else
111 #define XMLDLLENTRY __declspec(dllimport)
112 #endif
113 #else
114 #define XMLDLLENTRY
115 #endif
116 
117 // uncomment the next line if you want no support for wchar_t* (no need for the <wchar.h> or <tchar.h> libraries anymore to compile)
118 //#define XML_NO_WIDE_CHAR
119 
120 #ifdef XML_NO_WIDE_CHAR
121 #undef _XMLWINDOWS
122 #undef _XMLWIDECHAR
123 #endif
124 
125 #ifdef _XMLWINDOWS
126 #include <tchar.h>
127 #else
128 #define XMLDLLENTRY
129 #ifndef XML_NO_WIDE_CHAR
130 #include <wchar.h> // to have 'wcsrtombs' for ANSI version
131  // to have 'mbsrtowcs' for WIDECHAR version
132 #endif
133 #endif
134 
135 // Some common types for char set portable code
136 #ifdef _XMLWIDECHAR
137  #define _CXML(c) L ## c
138  #define XMLCSTR const wchar_t *
139  #define XMLSTR wchar_t *
140  #define XMLCHAR wchar_t
141 #else
142  #define _CXML(c) c
143  #define XMLCSTR const char *
144  #define XMLSTR char *
145  #define XMLCHAR char
146 #endif
147 #ifndef FALSE
148  #define FALSE 0
149 #endif /* FALSE */
150 #ifndef TRUE
151  #define TRUE 1
152 #endif /* TRUE */
153 
154 
156 typedef enum XMLError
157 {
175 
180 } XMLError;
181 
182 
184 typedef enum XMLElementType
185 {
192 
194 typedef struct XMLResults
195 {
198 } XMLResults;
199 
201 typedef struct XMLClear {
203 } XMLClear;
204 
206 typedef struct XMLAttribute {
208 } XMLAttribute;
209 
211 typedef int XMLElementPosition;
212 
213 struct XMLNodeContents;
214 
217 
218 
227 typedef struct XMLDLLENTRY XMLNode
228 {
229  private:
230 
231  struct XMLNodeDataTag;
232 
234  XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDeclaration);
236  XMLNode(struct XMLNodeDataTag *p);
237 
238  public:
239  static XMLCSTR getVersion();
240 
245 
246  static XMLNode parseString (XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
258 
259  static XMLNode parseFile (XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
271 
272  static XMLNode openFileHelper(XMLCSTR filename, XMLCSTR tag=NULL);
290  static XMLCSTR getError(XMLError error);
291 
293  XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const;
299 
300  XMLError writeToFile(XMLCSTR filename,
301  const char *encoding=NULL,
302  char nFormat=1) const;
313  XMLCSTR getName() const;
314  XMLCSTR getText(int i=0) const;
315  int nText() const;
316  XMLNode getParentNode() const;
317  XMLNode getChildNode(int i=0) const;
318  XMLNode getChildNode(XMLCSTR name, int i) const;
319  XMLNode getChildNode(XMLCSTR name, int *i=NULL) const;
320  XMLNode getChildNodeWithAttribute(XMLCSTR tagName,
321  XMLCSTR attributeName,
322  XMLCSTR attributeValue=NULL,
323  int *i=NULL) const;
324  XMLNode getChildNodeByPath(XMLCSTR path, char createNodeIfMissing=0, XMLCHAR sep='/');
326  XMLNode getChildNodeByPathNonConst(XMLSTR path, char createNodeIfMissing=0, XMLCHAR sep='/');
328 
329  int nChildNode(XMLCSTR name) const;
330  int nChildNode() const;
331  XMLAttribute getAttribute(int i=0) const;
332  XMLCSTR getAttributeName(int i=0) const;
333  XMLCSTR getAttributeValue(int i=0) const;
334  char isAttributeSet(XMLCSTR name) const;
335  XMLCSTR getAttribute(XMLCSTR name, int i) const;
336  XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const;
337  int nAttribute() const;
338  XMLClear getClear(int i=0) const;
339  int nClear() const;
340  XMLNodeContents enumContents(XMLElementPosition i) const;
341  int nElement() const;
342  char isEmpty() const;
343  char isDeclaration() const;
344  XMLNode deepCopy() const;
345  static XMLNode emptyNode();
346 
348  ~XMLNode();
349  XMLNode(const XMLNode &A);
350  XMLNode& operator=( const XMLNode& A );
351 
352  XMLNode(): d(NULL){};
353  static XMLNode emptyXMLNode;
356 
370  static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE);
371  XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1);
372  XMLNode addChild(XMLNode nodeToAdd, XMLElementPosition pos=-1);
373  XMLAttribute *addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev);
374  XMLCSTR addText(XMLCSTR lpszValue, XMLElementPosition pos=-1);
375  XMLClear *addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1);
387  XMLCSTR updateName(XMLCSTR lpszName);
388  XMLAttribute *updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute);
389  XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName=NULL,int i=0);
390  XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName);
391  XMLCSTR updateText(XMLCSTR lpszNewValue, int i=0);
392  XMLCSTR updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue);
393  XMLClear *updateClear(XMLCSTR lpszNewContent, int i=0);
394  XMLClear *updateClear(XMLClear *newP,XMLClear *oldP);
395  XMLClear *updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue);
396 
403 
404  void deleteNodeContent();
406  void deleteAttribute(int i=0);
407  void deleteAttribute(XMLCSTR lpszName);
408  void deleteAttribute(XMLAttribute *anAttribute);
409  void deleteText(int i=0);
410  void deleteText(XMLCSTR lpszValue);
411  void deleteClear(int i=0);
412  void deleteClear(XMLCSTR lpszValue);
413  void deleteClear(XMLClear *p);
414 
445  static XMLNode createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration=FALSE);
446  XMLNode addChild_WOSD(XMLSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1);
447  XMLAttribute *addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValue);
448  XMLCSTR addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos=-1);
449  XMLClear *addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1);
450 
451  XMLCSTR updateName_WOSD(XMLSTR lpszName);
452  XMLAttribute *updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute);
453  XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName=NULL,int i=0);
454  XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName);
455  XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, int i=0);
456  XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);
457  XMLClear *updateClear_WOSD(XMLSTR lpszNewContent, int i=0);
458  XMLClear *updateClear_WOSD(XMLClear *newP,XMLClear *oldP);
459  XMLClear *updateClear_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);
460 
468  XMLElementPosition positionOfText(int i=0) const;
469  XMLElementPosition positionOfText(XMLCSTR lpszValue) const;
470  XMLElementPosition positionOfClear(int i=0) const;
471  XMLElementPosition positionOfClear(XMLCSTR lpszValue) const;
472  XMLElementPosition positionOfClear(XMLClear *a) const;
473  XMLElementPosition positionOfChildNode(int i=0) const;
474  XMLElementPosition positionOfChildNode(XMLNode x) const;
475  XMLElementPosition positionOfChildNode(XMLCSTR name, int i=0) const;
476 
478 
479  typedef enum XMLCharEncoding
480  {
481  char_encoding_error=0,
482  char_encoding_UTF8=1,
483  char_encoding_legacy=2,
484  char_encoding_ShiftJIS=3,
485  char_encoding_GB2312=4,
486  char_encoding_Big5=5,
487  char_encoding_GBK=6 // this is actually the same as Big5
488  } XMLCharEncoding;
489 
493 
494  static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8, char guessWideCharChars=1,
495  char dropWhiteSpace=1, char removeCommentsInMiddleOfText=1);
546 
547  static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1);
560  private:
561  // these are functions and structures used internally by the XMLNode class (don't bother about them):
562 
563  typedef struct XMLNodeDataTag // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
564  {
565  XMLCSTR lpszName; // Element name (=NULL if root)
566  int nChild, // Number of child nodes
567  nText, // Number of text fields
568  nClear, // Number of Clear fields (comments)
569  nAttribute; // Number of attributes
570  char isDeclaration; // Whether node is an XML declaration - '<?xml ?>'
571  struct XMLNodeDataTag *pParent; // Pointer to parent element (=NULL if root)
572  XMLNode *pChild; // Array of child nodes
573  XMLCSTR *pText; // Array of text fields
574  XMLClear *pClear; // Array of clear fields
575  XMLAttribute *pAttribute; // Array of attributes
576  int *pOrder; // order of the child_nodes,text_fields,clear_fields
577  int ref_count; // for garbage collection (smart pointers)
578  } XMLNodeData;
579  XMLNodeData *d;
580 
581  char parseClearTag(void *px, void *pa);
582  char maybeAddTxT(void *pa, XMLCSTR tokenPStr);
583  int ParseXMLElement(void *pXML);
584  void *addToOrder(int memInc, int *_pos, int nc, void *p, int size, XMLElementType xtype);
585  int indexText(XMLCSTR lpszValue) const;
586  int indexClear(XMLCSTR lpszValue) const;
587  XMLNode addChild_priv(int,XMLSTR,char,int);
588  XMLAttribute *addAttribute_priv(int,XMLSTR,XMLSTR);
589  XMLCSTR addText_priv(int,XMLSTR,int);
590  XMLClear *addClear_priv(int,XMLSTR,XMLCSTR,XMLCSTR,int);
591  void emptyTheNode(char force);
592  static inline XMLElementPosition findPosition(XMLNodeData *d, int index, XMLElementType xtype);
593  static int CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat);
594  static int removeOrderElement(XMLNodeData *d, XMLElementType t, int index);
595  static void exactMemory(XMLNodeData *d);
596  static int detachFromParent(XMLNodeData *d);
597 } XMLNode;
598 
600 typedef struct XMLNodeContents
601 {
609 
611 
615 
616 XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=-1);
622 
623 XMLDLLENTRY void freeXMLString(XMLSTR t); // {free(t);}
635 XMLDLLENTRY char xmltob(XMLCSTR xmlString,char defautValue=0);
636 XMLDLLENTRY int xmltoi(XMLCSTR xmlString,int defautValue=0);
637 XMLDLLENTRY long long xmltol(XMLCSTR xmlString,long long defautValue=0);
638 XMLDLLENTRY double xmltof(XMLCSTR xmlString,double defautValue=.0);
639 XMLDLLENTRY XMLCSTR xmltoa(XMLCSTR xmlString,XMLCSTR defautValue=_CXML(""));
640 XMLDLLENTRY XMLCHAR xmltoc(XMLCSTR xmlString,const XMLCHAR defautValue=_CXML('\0'));
646 
647 
657 {
658 public:
659  ToXMLStringTool(): buf(NULL),buflen(0){}
660  ~ToXMLStringTool();
661  void freeBuffer();
662 
663  XMLSTR toXML(XMLCSTR source);
664 
668  static XMLSTR toXMLUnSafe(XMLSTR dest,XMLCSTR source);
669  static int lengthXMLString(XMLCSTR source);
670 
671 private:
672  XMLSTR buf;
673  int buflen;
680 
681 
690 {
691 public:
692  XMLParserBase64Tool(): buf(NULL),buflen(0){}
694  void freeBuffer();
695 
698  static int encodeLength(int inBufLen, char formatted=0);
699 
705  XMLSTR encode(unsigned char *inByteBuf, unsigned int inByteLen, char formatted=0);
706 
708  static unsigned int decodeSize(XMLCSTR inString, XMLError *xe=NULL);
709 
715  unsigned char* decode(XMLCSTR inString, int *outByteLen=NULL, XMLError *xe=NULL);
716 
721  static unsigned char decode(XMLCSTR inString, unsigned char *outByteBuf, int inMaxByteOutBuflen, XMLError *xe=NULL);
722 
723 private:
724  void *buf;
725  int buflen;
726  void alloc(int newsize);
730 #undef XMLDLLENTRY
731 
732 #endif