CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
xml_parser.h
Go to the documentation of this file.
1
14/*
15 ****************************************************************************
16 * <P> XML.c - implementation file for basic XML parser written in ANSI C++
17 * for portability. It works by using recursion and a node tree for breaking
18 * down the elements of an XML document. </P>
19 *
20 * @version V2.31
21 * @author Frank Vanden Berghen
22 *
23 * BSD license:
24 * Copyright (c) 2002, Frank Vanden Berghen
25 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are met:
28 *
29 * * Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * * Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * * Neither the name of the Frank Vanden Berghen nor the
35 * names of its contributors may be used to endorse or promote products
36 * derived from this software without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
39 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
42 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 ****************************************************************************
50 */
51#ifndef __INCLUDE_XML_NODE__
52#define __INCLUDE_XML_NODE__
53
54#include <stdlib.h>
55
56#ifdef _UNICODE
57// If you comment the next "define" line then the library will never "switch to" _UNICODE (wchar_t*) mode (16/32 bits per characters).
58// This is useful when you get error messages like:
59// 'XMLNode::openFileHelper' : cannot convert parameter 2 from 'const char [5]' to 'const wchar_t *'
60// The _XMLWIDECHAR preprocessor variable force the XMLParser library into either utf16/32-mode (the proprocessor variable
61// must be defined) or utf8-mode(the pre-processor variable must be undefined).
62//#define _XMLWIDECHAR
63#endif
64
65#if defined(WIN32) || defined(UNDER_CE)
66// comment the next line if you are under windows and the compiler is not Microsoft Visual Studio (6.0 or .NET)
67#define _XMLWINDOWS
68#endif
69
70//#define _USE_XMLPARSER_DLL
71
72#ifdef XMLDLLENTRY
73#undef XMLDLLENTRY
74#endif
75#ifdef _USE_XMLPARSER_DLL
76#ifdef CORELIBRARY_EXPORTS
77#define XMLDLLENTRY __declspec(dllexport)
78#else
79#define XMLDLLENTRY __declspec(dllimport)
80#endif
81#else
82#define XMLDLLENTRY
83#endif
84
85// 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)
86//#define XML_NO_WIDE_CHAR
87
88#ifdef XML_NO_WIDE_CHAR
89#undef _XMLWINDOWS
90#undef _XMLWIDECHAR
91#endif
92
93#ifdef _XMLWINDOWS
94#include <tchar.h>
95#else
96#undef XMLDLLENTRY
97#define XMLDLLENTRY
98#ifndef XML_NO_WIDE_CHAR
99#include <wchar.h> // to have 'wcsrtombs' for ANSI version
100 // to have 'mbsrtowcs' for WIDECHAR version
101#endif
102#endif
103
104// Some common types for char set portable code
105#ifdef _XMLWIDECHAR
106 #ifndef _X
107 #define _X(c) L ## c
108 #endif
109 #define XMLCSTR const wchar_t *
110 #define XMLSTR wchar_t *
111 #define XMLCHAR wchar_t
112#else
113 #ifndef _X
114 #define _X(c) c
115 #endif
116 #define XMLCSTR const char *
117 #define XMLSTR char *
118 #define XMLCHAR char
119#endif
120#ifndef FALSE
121 #define FALSE 0
122#endif /* FALSE */
123#ifndef TRUE
124 #define TRUE 1
125#endif /* TRUE */
126
127namespace cmlabs{
128// Enumeration for XML parse errors.
153
154
155// Enumeration used to manage type of data. Use in conjunction with structure XMLNodeContents
164
165// Structure used to obtain error details if the parse fails.
171
172// Structure for XML clear (unformatted) node (usually comments)
176
177// Structure for XML attribute.
181
182struct XMLNodeContents;
183
184typedef struct XMLDLLENTRY XMLNode
185{
186 private:
187
188 struct XMLNodeDataTag;
189
190 // protected constructors: use one of these four methods to get your first instance of XMLNode:
191 // - parseString
192 // - parseFile
193 // - openFileHelper
194 // - createXMLTopNode
195 XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDeclaration);
196 XMLNode(struct XMLNodeDataTag *p);
197
198 public:
199
200 // You can create your first instance of XMLNode with these 4 functions:
201 // (see complete explanation of parameters below)
202
203 static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE);
204 static XMLNode parseString (XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
205 static XMLNode parseFile (XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
206 static XMLNode openFileHelper(XMLCSTR filename, XMLCSTR tag=NULL );
207
208 // The tag parameter should be the name of the first tag inside the XML file.
209 // If the tag parameter is omitted, the 3 functions return a node that represents
210 // the head of the xml document including the declaration term (<? ... ?>).
211
212 // The "openFileHelper" reports to the screen all the warnings & errors that occurred during
213 // parsing of the XML file. Since each application has its own way to report and deal with errors,
214 // you should rather use the "parseFile" function to parse XML files and program yourself thereafter
215 // an "error reporting" tailored for your needs (instead of using the very crude "error reporting"
216 // mechanism included inside the "openFileHelper" function).
217
218 // If the XML document is corrupted:
219 // * The "openFileHelper" method will:
220 // - display an error message on the console (or inside a messageBox for windows).
221 // - stop execution (exit).
222 // I suggest that you write your own "openFileHelper" method tailored to your needs.
223 // * The 2 other methods will initialize the "pResults" variable with some information that
224 // can be used to trace the error.
225 // * If you still want to parse the file, you can use the APPROXIMATE_PARSING option as
226 // explained inside the note at the beginning of the "xmlParser.cpp" file.
227 // You can have a user-friendly explanation of the parsing error with this function:
228 static XMLCSTR getError(XMLError error);
230
231 XMLCSTR getName() const; // name of the node
232 XMLCSTR getText(int i=0) const; // return ith text field
233 int nText() const; // nbr of text field
234 XMLNode getParentNode() const; // return the parent node
235 XMLNode getChildNode(int i=0) const; // return ith child node
236 XMLNode getChildNode(XMLCSTR name, int i) const; // return ith child node with specific name
237 // (return an empty node if failing)
238 XMLNode getChildNode(XMLCSTR name, int *i=NULL) const; // return next child node with specific name
239 // (return an empty node if failing)
240 XMLNode getChildNodeWithAttribute(XMLCSTR tagName, // return child node with specific name/attribute
241 XMLCSTR attributeName, // (return an empty node if failing)
242 XMLCSTR attributeValue=NULL, //
243 int *i=NULL) const; //
244 int nChildNode(XMLCSTR name) const; // return the number of child node with specific name
245 int nChildNode() const; // nbr of child node
246 XMLAttribute getAttribute(int i=0) const; // return ith attribute
247 XMLCSTR getAttributeName(int i=0) const; // return ith attribute name
248 XMLCSTR getAttributeValue(int i=0) const; // return ith attribute value
249 char isAttributeSet(XMLCSTR name) const; // test if an attribute with a specific name is given
250 XMLCSTR getAttribute(XMLCSTR name, int i) const; // return ith attribute content with specific name
251 // (return a NULL if failing)
252 XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const; // return next attribute content with specific name
253 // (return a NULL if failing)
254 int nAttribute() const; // nbr of attribute
255 XMLClear getClear(int i=0) const; // return ith clear field (comments)
256 int nClear() const; // nbr of clear field
257 XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const; // create XML string starting from current XMLNode
258 // if nFormat==0, no formatting is required
259 // otherwise this returns an user friendly XML string from a
260 // given element with appropriate white spaces and carriage returns.
261 // if pnSize is given it returns the size in character of the string.
262 XMLError writeToFile(XMLCSTR filename, const char *encoding=NULL, char nFormat=1) const;
263 // Save the content of an xmlNode inside a file.
264 // The nFormat parameter has the same meaning as in the
265 // createXMLString function. If the global parameter
266 // "characterEncoding==encoding_UTF8", then the "encoding" parameter is
267 // ignored and always set to "utf-8". If the global parameter
268 // "characterEncoding==encoding_ShiftJIS", then the "encoding" parameter
269 // is ignored and always set to "SHIFT-JIS". If "_XMLWIDECHAR=1", then
270 // the "encoding" parameter is ignored and always set to "utf-16".
271 // If no "encoding" parameter is given the "ISO-8859-1" encoding is used.
272 XMLNodeContents enumContents(int i) const; // enumerate all the different contents (attribute,child,text,
273 // clear) of the current XMLNode. The order is reflecting
274 // the order of the original file/string.
275 // NOTE: 0 <= i < nElement();
276 int nElement() const; // nbr of different contents for current node
277 char isEmpty() const; // is this node Empty?
278 char isDeclaration() const; // is this node a declaration <? .... ?>
279 static XMLNode emptyNode(); // return XMLNode::emptyXMLNode;
280
281 #pragma warning(disable: 4800) // warning: forcing value to bool
282 bool operator !(){ return isEmpty(); }
283
284// to allow shallow/fast copy:
286 XMLNode(const XMLNode &A);
287 XMLNode& operator=( const XMLNode& A );
288
289 XMLNode(): d(NULL){};
290 static XMLNode emptyXMLNode;
293
294 // The following functions allows you to create from scratch (or update) a XMLNode structure
295 // Start by creating your top node with the "createXMLTopNode" function and then add new nodes with the "addChild" function.
296 // The parameter 'pos' gives the position where the childNode, the text or the XMLClearTag will be inserted.
297 // The default value (pos=-1) inserts at the end. The value (pos=0) insert at the beginning (Insertion at the beginning is slower than at the end).
298 // REMARK: 0 <= pos < nChild()+nText()+nClear()
299 XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, int pos=-1);
300 XMLAttribute *addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev);
301 XMLCSTR addText(XMLCSTR lpszValue, int pos=-1);
302 XMLClear *addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, int pos=-1);
303 // default values: lpszOpen ="<![CDATA["
304 // lpszClose="]]>"
305 XMLNode addChild(XMLNode nodeToAdd, int pos=-1); // If the "nodeToAdd" has some parents, it will be detached
306 // from it's parents before being attached to the current XMLNode
307 // Some update functions:
308 XMLCSTR updateName(XMLCSTR lpszName); // change node's name
309 XMLAttribute *updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute); // if the attribute to update is missing, a new one will be added
310 XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName=NULL,int i=0); // if the attribute to update is missing, a new one will be added
311 XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName); // set lpszNewName=NULL if you don't want to change the name of the attribute
312 // if the attribute to update is missing, a new one will be added
313 XMLCSTR updateText(XMLCSTR lpszNewValue, int i=0); // if the text to update is missing, a new one will be added
314 XMLCSTR updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue); // if the text to update is missing, a new one will be added
315 XMLClear *updateClear(XMLCSTR lpszNewContent, int i=0); // if the clearTag to update is missing, a new one will be added
316 XMLClear *updateClear(XMLClear *newP,XMLClear *oldP); // if the clearTag to update is missing, a new one will be added
317 XMLClear *updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue); // if the clearTag to update is missing, a new one will be added
318
319 // Some deletion functions:
320 void deleteNodeContent(); // delete the content of this XMLNode and the subtree.
321 void deleteAttribute(XMLCSTR lpszName);
322 void deleteAttribute(int i=0);
323 void deleteAttribute(XMLAttribute *anAttribute);
324 void deleteText(int i=0);
325 void deleteText(XMLCSTR lpszValue);
326 void deleteClear(int i=0);
328 void deleteClear(XMLCSTR lpszValue);
329
330 // The strings given as parameters for the following add and update methods (all these methods have
331 // a name with the postfix "_WOSD" that means "WithOut String Duplication" ) will be free'd by the
332 // XMLNode class. For example, it means that this is incorrect:
333 // xNode.addText_WOSD("foo");
334 // xNode.updateAttribute_WOSD("#newcolor" ,NULL,"color");
335 // In opposition, this is correct:
336 // xNode.addText("foo");
337 // xNode.addText_WOSD(stringDup("foo"));
338 // xNode.updateAttribute("#newcolor" ,NULL,"color");
339 // xNode.updateAttribute_WOSD(stringDup("#newcolor"),NULL,"color");
340 // Typically, you will never do:
341 // char *b=(char*)malloc(...);
342 // xNode.addText(b);
343 // free(b);
344 // ... but rather:
345 // char *b=(char*)malloc(...);
346 // xNode.addText_WOSD(b);
347 // ('free(b)' is performed by the XMLNode class)
348
349 static XMLNode createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration=FALSE);
350 XMLNode addChild_WOSD(XMLSTR lpszName, char isDeclaration=FALSE, int pos=-1);
352 XMLCSTR addText_WOSD(XMLSTR lpszValue, int pos=-1);
353 XMLClear *addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, int pos=-1);
354
357 XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName=NULL,int i=0);
358 XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName);
359 XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, int i=0);
360 XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);
361 XMLClear *updateClear_WOSD(XMLSTR lpszNewContent, int i=0);
363 XMLClear *updateClear_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);
364
365 // These are some useful functions when you want to insert a childNode, a text or a XMLClearTag in the
366 // middle (at a specified position) of a XMLNode tree already constructed. The value returned by these
367 // methods is to be used as last parameter (parameter 'pos') of addChild, addText or addClear.
368 int positionOfText(int i=0) const;
369 int positionOfText(XMLCSTR lpszValue) const;
370 int positionOfClear(int i=0) const;
371 int positionOfClear(XMLCSTR lpszValue) const;
373 int positionOfChildNode(int i=0) const;
374 int positionOfChildNode(XMLNode x) const;
375 int positionOfChildNode(XMLCSTR name, int i=0) const; // return the position of the ith childNode with the specified name
376 // if (name==NULL) return the position of the ith childNode
377
378 // The setGlobalOptions function allows you to change tree global parameters that affect string&file
379 // parsing. First of all, you most-probably will never have to change these 3 global parameters.
380 // The return value of the setGlobalOptions function is "0" when there are no errors. If you try to
381 // set an unrecognized encoding then the return value will be "1" to signal an error.
382 //
383 // About the "guessWideCharChars" parameter:
384 // If "guessWideCharChars=1" and if this library is compiled in WideChar mode, then the
385 // "parseFile" and "openFileHelper" functions will test if the file contains ASCII
386 // characters. If this is the case, then the file will be loaded and converted in memory to
387 // WideChar before being parsed. If "guessWideCharChars=0", no conversion will
388 // be performed.
389 //
390 // If "guessWideCharChars=1" and if this library is compiled in ASCII/UTF8/char* mode, then the
391 // "parseFile" and "openFileHelper" functions will test if the file contains WideChar
392 // characters. If this is the case, then the file will be loaded and converted in memory to
393 // ASCII/UTF8/char* before being parsed. If "guessWideCharChars=0", no conversion will
394 // be performed
395 //
396 // Sometime, it's useful to set "guessWideCharChars=0" to disable any conversion
397 // because the test to detect the file-type (ASCII/UTF8/char* or WideChar) may fail (rarely).
398 //
399 // About the "characterEncoding" parameter:
400 // This parameter is only meaningful when compiling in char* mode (multibyte character mode).
401 // In wchar_t* (wide char mode), this parameter is ignored. This parameter should be one of the
402 // three currently recognized encodings: XMLNode::encoding_UTF8, XMLNode::encoding_ascii,
403 // XMLNode::encoding_ShiftJIS.
404 //
405 // About the "dropWhiteSpace" parameter:
406 // In most situations, text fields containing only white spaces (and carriage returns)
407 // are useless. Even more, these "empty" text fields are annoying because they increase the
408 // complexity of the user's code for parsing. So, 99% of the time, it's better to drop
409 // the "empty" text fields. However The XML specification indicates that no white spaces
410 // should be lost when parsing the file. So to be perfectly XML-compliant, you should set
411 // dropWhiteSpace=0. A note of caution: if you set "dropWhiteSpace=0", the parser will be
412 // slower and your code will be more complex.
413
414 // Enumeration for XML character encoding.
415 typedef enum XMLCharEncoding { encoding_UTF8=1, encoding_ascii=2, encoding_ShiftJIS=3 } XMLCharEncoding;
416
417 static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::encoding_UTF8, char guessWideCharChars=1, char dropWhiteSpace=1);
418
419 // The next function try to guess the character encoding. You most-probably will never
420 // have to use this function. It then returns the appropriate value of the global parameter
421 // "characterEncoding" described above. The guess is based on the content of a buffer of length
422 // "bufLen" bytes that contains the first bytes (minimum 25 bytes; 200 bytes is a good value) of the
423 // file to be parsed. The "openFileHelper" function is using this function to automatically compute
424 // the value of the "characterEncoding" global parameter. There are several heuristics used to do the
425 // guess. One of the heuristic is based on the "encoding" attribute. The original XML specifications
426 // forbids to use this attribute to do the guess but you can still use it if you set
427 // "useXMLEncodingAttribute" to 1 (this is the default behavior and the behavior of most parsers).
428 // If an inconsistency in the encoding is detected, then the return value is "0".
429
430 static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1);
431
432 // Self-contained unit test for the XML parser. Returns true on success.
433 static bool UnitTest();
434
435 private:
436
437// these are functions and structures used internally by the XMLNode class (don't bother about them):
438
439 typedef struct XMLNodeDataTag // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
440 {
441 XMLCSTR lpszName; // Element name (=NULL if root)
442 int nChild, // Number of child nodes
443 nText, // Number of text fields
444 nClear, // Number of Clear fields (comments)
445 nAttribute; // Number of attributes
446 char isDeclaration; // Whether node is an XML declaration - '<?xml ?>'
447 struct XMLNodeDataTag *pParent; // Pointer to parent element (=NULL if root)
448 XMLNode *pChild; // Array of child nodes
449 XMLCSTR *pText; // Array of text fields
450 XMLClear *pClear; // Array of clear fields
451 XMLAttribute *pAttribute; // Array of attributes
452 int *pOrder; // order of the child_nodes,text_fields,clear_fields
453 int ref_count; // for garbage collection (smart pointers)
454 } XMLNodeData;
455 XMLNodeData *d;
456
457 char parseClearTag(void *px, void *pa);
458 char maybeAddTxT(void *pa, XMLCSTR tokenPStr);
459 int ParseXMLElement(void *pXML);
460 void *addToOrder(int memInc, int *_pos, int nc, void *p, int size, XMLElementType xtype);
461 int indexText(XMLCSTR lpszValue) const;
462 int indexClear(XMLCSTR lpszValue) const;
463 XMLNode addChild_priv(int,XMLSTR,char,int);
464 XMLAttribute *addAttribute_priv(int,XMLSTR,XMLSTR);
465 XMLCSTR addText_priv(int,XMLSTR,int);
466 XMLClear *addClear_priv(int,XMLSTR,XMLCSTR,XMLCSTR,int);
467 void deleteNodeContent_priv(char,char);
468 static inline int findPosition(XMLNodeData *d, int index, XMLElementType xtype);
469 static int CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat);
470 static int removeOrderElement(XMLNodeData *d, XMLElementType t, int index);
471 static void exactMemory(XMLNodeData *d);
472 static int detachFromParent(XMLNodeData *d);
474
475// This structure is given by the function "enumContents".
476typedef struct XMLNodeContents
477{
478 // This dictates what's the content of the XMLNodeContent
480 // should be an union to access the appropriate data.
481 // compiler does not allow union of object with constructor... too bad.
486
488
489XMLDLLENTRY void freeXMLString(XMLSTR t); // {free(t);}
490
491// Duplicate (copy in a new allocated buffer) the source string. This is
492// a very handy function when used with all the "XMLNode::*_WOSD" functions.
493// (If (cbData!=0) then cbData is the number of chars to duplicate)
494XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=0);
495
496// The following class is processing strings so that all the characters
497// &,",',<,> are replaced by their XML equivalent: &amp;, &quot;, &apos;, &lt;, &gt;.
498// This class is useful when creating from scratch an XML file using the
499// "printf", "fprintf", "cout",... functions. If you are creating from scratch an
500// XML file using the provided XMLNode class you must not use the "ToXMLStringTool"
501// class (the "XMLNode" class does the processing job for you during rendering).
502// Using the "ToXMLStringTool class" and the "fprintf function" is THE most efficient
503// way to produce VERY large XML documents VERY fast.
505{
506public:
507 ToXMLStringTool(): buf(NULL),buflen(0){}
510
512
513 // The next function is deprecated because there is a possibility of
514 // "destination-buffer-overflow". It converts the string
515 // "source" to the string "dest".
516 static XMLSTR toXMLUnSafe(XMLSTR dest,XMLCSTR source);
517
518private:
519 XMLSTR buf;
520 int buflen;
522
523// Below is a class that allows you to include any binary data (images, sounds,...)
524// into an XML document using "Base64 encoding". This class is completely
525// separated from the rest of the xmlParser library and can be removed without any problem.
526// To include some binary data into an XML file, you must convert the binary data into
527// standard text (using "encode"). To retrieve the original binary data from the
528// b64-encoded text included inside the XML file use "decode". Alternatively, these
529// functions can also be used to "encrypt/decrypt" some critical data contained inside
530// the XML (it's not a strong encryption at all, but sometimes it can be useful).
531
533{
534public:
535 XMLParserBase64Tool(): buf(NULL),buflen(0){}
538
539 // returns the length of the base64 string that encodes a data buffer of size inBufLen bytes.
540 // If "formatted" parameter is true, some space will be reserved for a carriage-return every 72 chars.
541 static int encodeLength(int inBufLen, char formatted=0);
542
543 // The "base64Encode" function returns a string containing the base64 encoding of "inByteLen" bytes
544 // from "inByteBuf". If "formatted" parameter is true, then there will be a carriage-return every 72 chars.
545 // The string will be free'd when the XMLParserBase64Tool object is deleted.
546 // All returned strings are sharing the same memory space.
547 XMLSTR encode(unsigned char *inByteBuf, unsigned int inByteLen, char formatted=0);
548
549 // returns the number of bytes which will be decoded from "inString".
550 static unsigned int decodeSize(XMLCSTR inString, XMLError *xe=NULL);
551
552 // returns a pointer to a buffer containing the binary data decoded from "inString"
553 // If "inString" is malformed NULL will be returned
554 // The output buffer will be free'd when the XMLParserBase64Tool object is deleted.
555 // All output buffer are sharing the same memory space.
556 unsigned char* decode(XMLCSTR inString, int *outByteLen=NULL, XMLError *xe=NULL);
557
558 // The next function is deprecated.
559 // decodes data from "inString" to "outByteBuf". You need to provide the size (in byte) of "outByteBuf"
560 // in "inMaxByteOutBuflen". If "outByteBuf" is not large enough or if data is malformed, then "FALSE"
561 // will be returned; otherwise "TRUE".
562 static unsigned char decode(XMLCSTR inString, unsigned char *outByteBuf, int inMaxByteOutBuflen, XMLError *xe=NULL);
563
564private:
565 void *buf;
566 int buflen;
567 void alloc(int newsize);
569}
570#undef XMLDLLENTRY
571
572#endif
XMLDLLENTRY void freeXMLString(XMLSTR t)
struct cmlabs::XMLResults XMLResults
struct XMLDLLENTRY cmlabs::XMLParserBase64Tool XMLParserBase64Tool
struct cmlabs::XMLNodeContents XMLNodeContents
struct XMLDLLENTRY cmlabs::XMLNode XMLNode
XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=0)
@ eXMLErrorCannotOpenWriteFile
Definition xml_parser.h:145
@ eXMLErrorUnexpectedToken
Definition xml_parser.h:139
@ eXMLErrorFirstTagNotFound
Definition xml_parser.h:142
@ eXMLErrorNoXMLTagFound
Definition xml_parser.h:133
@ eXMLErrorCannotWriteFile
Definition xml_parser.h:146
@ eXMLErrorBase64DecodeBufferTooSmall
Definition xml_parser.h:151
@ eXMLErrorNone
Definition xml_parser.h:131
@ eXMLErrorBase64DecodeIllegalCharacter
Definition xml_parser.h:149
@ eXMLErrorFileNotFound
Definition xml_parser.h:141
@ eXMLErrorUnmatchedEndClearTag
Definition xml_parser.h:138
@ eXMLErrorBase64DataSizeIsNotMultipleOf4
Definition xml_parser.h:148
@ eXMLErrorMissingEndTag
Definition xml_parser.h:132
@ eXMLErrorUnknownCharacterEntity
Definition xml_parser.h:143
@ eXMLErrorMissingTagName
Definition xml_parser.h:135
@ eXMLErrorEmpty
Definition xml_parser.h:134
@ eXMLErrorBase64DecodeTruncatedData
Definition xml_parser.h:150
@ eXMLErrorMissingEndTagName
Definition xml_parser.h:136
@ eXMLErrorUnmatchedEndTag
Definition xml_parser.h:137
@ eXMLErrorCharConversionError
Definition xml_parser.h:144
@ eXMLErrorNoElements
Definition xml_parser.h:140
struct XMLDLLENTRY cmlabs::ToXMLStringTool ToXMLStringTool
struct cmlabs::XMLClear XMLClear
struct cmlabs::XMLAttribute XMLAttribute
@ eNodeText
Definition xml_parser.h:160
@ eNodeNULL
Definition xml_parser.h:162
@ eNodeChild
Definition xml_parser.h:158
@ eNodeClear
Definition xml_parser.h:161
@ eNodeAttribute
Definition xml_parser.h:159
static XMLSTR toXMLUnSafe(XMLSTR dest, XMLCSTR source)
XMLSTR toXML(XMLCSTR source)
XMLCSTR lpszOpenTag
Definition xml_parser.h:174
XMLCSTR lpszCloseTag
Definition xml_parser.h:174
enum XMLElementType type
Definition xml_parser.h:479
XMLAttribute * updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName, XMLCSTR lpszOldName)
XMLNodeContents enumContents(int i) const
static XMLCSTR getVersion()
XMLAttribute * updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue)
int positionOfChildNode(XMLNode x) const
XMLCSTR addText_WOSD(XMLSTR lpszValue, int pos=-1)
int positionOfClear(int i=0) const
XMLAttribute * updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName, XMLCSTR lpszOldName)
int nText() const
static XMLNode parseString(XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL)
XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, int i=0)
XMLClear getClear(int i=0) const
int nClear() const
char isDeclaration() const
static XMLNode emptyXMLNode
Definition xml_parser.h:290
static XMLNode openFileHelper(XMLCSTR filename, XMLCSTR tag=NULL)
static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1)
void deleteAttribute(int i=0)
XMLClear * updateClear_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue)
XMLCSTR updateText(XMLCSTR lpszNewValue, int i=0)
XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, int pos=-1)
XMLClear * updateClear_WOSD(XMLClear *newP, XMLClear *oldP)
XMLNode(const XMLNode &A)
XMLNode getChildNodeWithAttribute(XMLCSTR tagName, XMLCSTR attributeName, XMLCSTR attributeValue=NULL, int *i=NULL) const
void deleteAttribute(XMLCSTR lpszName)
XMLCSTR getName() const
XMLNode & operator=(const XMLNode &A)
static XMLNode createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration=FALSE)
int positionOfClear(XMLCSTR lpszValue) const
XMLAttribute getAttribute(int i=0) const
XMLNode addChild(XMLNode nodeToAdd, int pos=-1)
XMLNode getChildNode(int i=0) const
int positionOfText(int i=0) const
XMLNode getChildNode(XMLCSTR name, int *i=NULL) const
void deleteNodeContent()
void deleteText(int i=0)
char isAttributeSet(XMLCSTR name) const
XMLCSTR getAttributeValue(int i=0) const
int nChildNode() const
XMLCSTR updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
XMLClear * addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, int pos=-1)
void deleteText(XMLCSTR lpszValue)
static XMLCSTR getError(XMLError error)
static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::encoding_UTF8, char guessWideCharChars=1, char dropWhiteSpace=1)
XMLCSTR getAttribute(XMLCSTR name, int i) const
XMLNode addChild_WOSD(XMLSTR lpszName, char isDeclaration=FALSE, int pos=-1)
void deleteClear(XMLCSTR lpszValue)
int nChildNode(XMLCSTR name) const
int positionOfClear(XMLClear *a) const
XMLClear * updateClear_WOSD(XMLSTR lpszNewContent, int i=0)
XMLCSTR getAttributeName(int i=0) const
XMLAttribute * updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName=NULL, int i=0)
XMLNode getChildNode(XMLCSTR name, int i) const
XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const
XMLClear * updateClear(XMLCSTR lpszNewContent, int i=0)
static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE)
XMLCSTR addText(XMLCSTR lpszValue, int pos=-1)
static XMLNode emptyNode()
XMLAttribute * addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev)
XMLCSTR updateName_WOSD(XMLSTR lpszName)
XMLNode getParentNode() const
int positionOfChildNode(XMLCSTR name, int i=0) const
XMLClear * updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
int nAttribute() const
XMLClear * updateClear(XMLClear *newP, XMLClear *oldP)
XMLAttribute * updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName=NULL, int i=0)
int positionOfText(XMLCSTR lpszValue) const
void deleteAttribute(XMLAttribute *anAttribute)
XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const
static XMLNode parseFile(XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL)
void deleteClear(XMLClear *p)
int positionOfChildNode(int i=0) const
XMLClear * addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, int pos=-1)
void deleteClear(int i=0)
XMLError writeToFile(XMLCSTR filename, const char *encoding=NULL, char nFormat=1) const
static bool UnitTest()
XMLCSTR updateName(XMLCSTR lpszName)
XMLCSTR getText(int i=0) const
static XMLAttribute emptyXMLAttribute
Definition xml_parser.h:292
XMLAttribute * addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValue)
char isEmpty() const
XMLAttribute * updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
int nElement() const
static XMLClear emptyXMLClear
Definition xml_parser.h:291
unsigned char * decode(XMLCSTR inString, int *outByteLen=NULL, XMLError *xe=NULL)
XMLSTR encode(unsigned char *inByteBuf, unsigned int inByteLen, char formatted=0)
static unsigned int decodeSize(XMLCSTR inString, XMLError *xe=NULL)
static int encodeLength(int inBufLen, char formatted=0)
static unsigned char decode(XMLCSTR inString, unsigned char *outByteBuf, int inMaxByteOutBuflen, XMLError *xe=NULL)
enum XMLError error
Definition xml_parser.h:168
#define XMLSTR
Definition xml_parser.h:117
#define XMLDLLENTRY
Definition xml_parser.h:82
#define FALSE
Definition xml_parser.h:121
#define XMLCSTR
Definition xml_parser.h:116