75#ifndef _CRT_SECURE_NO_DEPRECATE
76#define _CRT_SECURE_NO_DEPRECATE
85#define WIN32_LEAN_AND_MEAN
101XMLCSTR XMLNode::getVersion() {
return _X(
"v2.30"); }
104static XMLNode::XMLCharEncoding characterEncoding=XMLNode::encoding_UTF8;
105static char guessWideCharChars=1, dropWhiteSpace=1;
107inline int mmin(
const int t1,
const int t2 ) {
return t1 < t2 ? t1 : t2; }
113typedef struct {
XMLCSTR lpszOpen;
int openTagLen;
XMLCSTR lpszClose;} ALLXMLClearTag;
114static ALLXMLClearTag XMLClearTags[] =
116 {
_X(
"<![CDATA["),9,
_X(
"]]>") },
117 {
_X(
"<!DOCTYPE"),9,
_X(
">") },
118 {
_X(
"<PRE>") ,5,
_X(
"</PRE>") },
119 {
_X(
"<Script>") ,8,
_X(
"</Script>")},
120 {
_X(
"<!--") ,4,
_X(
"-->") },
129static XMLCharacterEntity XMLEntities[] =
131 {
_X(
"&" ), 5,
_X(
'&' )},
132 {
_X(
"<" ), 4,
_X(
'<' )},
133 {
_X(
">" ), 4,
_X(
'>' )},
134 {
_X(
"""), 6,
_X(
'\"')},
135 {
_X(
"'"), 6,
_X(
'\'')},
142#define INDENTCHAR _X('\t')
172 return _X(
"Unknown");
184#ifdef XML_NO_WIDE_CHAR
185char myIsTextWideChar(
const void *b,
int len) {
return FALSE; }
187 #if defined (UNDER_CE) || !defined(_XMLWINDOWS)
188 char myIsTextWideChar(
const void *b,
int len)
192 if ((((
unsigned long)b)%
sizeof(
wchar_t))!=0)
return FALSE;
194 const wchar_t *s=(
const wchar_t*)b;
197 if (len<(
int)
sizeof(
wchar_t))
return FALSE;
200 if (len&1)
return FALSE;
203 len=mmin(256,len/
sizeof(
wchar_t));
206 if (*((
unsigned short*)s) == 0xFFFE)
return TRUE;
207 if (*((
unsigned short*)s) == 0xFEFF)
return TRUE;
211 for (i=0; i<len; i++)
if (s[i]<=(
unsigned short)255) stats++;
212 if (stats>len/2)
return TRUE;
215 for (i=0; i<len; i++)
if (!s[i])
return TRUE;
220 char myIsTextWideChar(
const void *b,
int l) {
return (
char)IsTextUnicode((CONST LPVOID)b,l,NULL); };
227 wchar_t *myMultiByteToWideChar(
const char *s)
230 if (characterEncoding==XMLNode::encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,NULL,0);
231 else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,NULL,0);
232 if (i<0)
return NULL;
233 wchar_t *d=(
wchar_t *)malloc((i+1)*
sizeof(
XMLCHAR));
234 if (characterEncoding==XMLNode::encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,d,i);
235 else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,d,i);
239 static inline FILE *xfopen(
XMLCSTR filename,
XMLCSTR mode) {
return _wfopen(filename,mode); }
240 static inline int xstrlen(
XMLCSTR c) {
return (
int)wcslen(c); }
241 static inline int xstrnicmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return _wcsnicmp(c1,c2,l);}
242 static inline int xstrncmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return wcsncmp(c1,c2,l);}
243 static inline int xstricmp(
XMLCSTR c1,
XMLCSTR c2) {
return _wcsicmp(c1,c2); }
247 char *myWideCharToMultiByte(
const wchar_t *s)
249 UINT codePage=CP_ACP;
if (characterEncoding==XMLNode::encoding_UTF8) codePage=CP_UTF8;
250 int i=(int)WideCharToMultiByte(codePage,
259 if (i<0)
return NULL;
260 char *d=(
char*)malloc(i+1);
261 WideCharToMultiByte(codePage,
273 static inline FILE *xfopen(
XMLCSTR filename,
XMLCSTR mode) {
return fopen(filename,mode); }
274 static inline int xstrlen(
XMLCSTR c) {
return (
int)strlen(c); }
276 static inline int xstrncmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return strncmp(c1,c2,l);}
282 static inline int _strnicmp(
char *c1,
char *c2,
int l){
return strnicmp(c1,c2,l);}
286 #ifdef XML_NO_WIDE_CHAR
287 char *myWideCharToMultiByte(
const wchar_t *s) {
return NULL; }
289 char *myWideCharToMultiByte(
const wchar_t *s)
292 int i=(int)wcsrtombs(NULL,&ss,0,NULL);
293 if (i<0)
return NULL;
294 char *d=(
char *)malloc(i+1);
295 wcsrtombs(d,&s,i,NULL);
301 wchar_t *myMultiByteToWideChar(
const char *s)
304 int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
305 if (i<0)
return NULL;
306 wchar_t *d=(
wchar_t *)malloc((i+1)*
sizeof(
wchar_t));
307 mbsrtowcs(d,&s,i,NULL);
311 int xstrlen(
XMLCSTR c) {
return wcslen(c); }
315 static inline int xstrnicmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return wsncasecmp(c1,c2,l);}
316 static inline int xstrncmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return wsncmp(c1,c2,l);}
317 static inline int xstricmp(
XMLCSTR c1,
XMLCSTR c2) {
return wscasecmp(c1,c2); }
320 static inline int xstrnicmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return wcsncasecmp(c1,c2,l);}
321 static inline int xstrncmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return wcsncmp(c1,c2,l);}
322 static inline int xstricmp(
XMLCSTR c1,
XMLCSTR c2) {
return wcscasecmp(c1,c2); }
328 char *filenameAscii=myWideCharToMultiByte(filename);
330 if (mode[0]==
_X(
'r')) f=fopen(filenameAscii,
"rb");
331 else f=fopen(filenameAscii,
"wb");
336 static inline FILE *xfopen(
XMLCSTR filename,
XMLCSTR mode) {
return fopen(filename,mode); }
337 static inline int xstrlen(
XMLCSTR c) {
return strlen(c); }
338 static inline int xstrnicmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return strncasecmp(c1,c2,l);}
339 static inline int xstrncmp(
XMLCSTR c1,
XMLCSTR c2,
int l) {
return strncmp(c1,c2,l);}
340 static inline int xstricmp(
XMLCSTR c1,
XMLCSTR c2) {
return strcasecmp(c1,c2); }
346 static inline int _strnicmp(
const char *c1,
const char *c2,
int l) { return ::strncasecmp(c1,c2,(
size_t)l);}
359 FILE *f=xfopen(filename,
_X(
"rb"));
363 int l=(int)fread(bb,1,200,f);
370 XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
376 char message[2000],*s1=(
char*)
"",*s3=(
char*)
"";
XMLCSTR s2=
_X(
"");
379#if defined(_XMLWINDOWS)
380 sprintf_s(message, 2000,
382 snprintf(message, 2000,
385 "XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
387 "XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
389 ,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3);
392#
if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
393 MessageBoxA(NULL,message,
"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST);
395 printf(
"%s",message);
415static const char XML_utf8ByteTable[256] =
418 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
419 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
420 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
421 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
422 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
423 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
424 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
425 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
426 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
427 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
428 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
429 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
430 1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
431 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
432 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
433 4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1
435static const char XML_asciiByteTable[256] =
437 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
438 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
439 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
440 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
441 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
442 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
444static const char XML_sjisByteTable[256] =
447 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
448 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
449 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
450 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
451 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
452 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
453 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
454 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
455 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
456 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
457 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
458 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
459 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
460 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
461 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
462 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
464static const char *XML_ByteTable=(
const char *)XML_utf8ByteTable;
469XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL};
473typedef enum XMLTokenTypeTag
482 eTokenShortHandClose,
492 int nIndex,nIndexMissigEndTag;
503 ALLXMLClearTag *pClr;
523XMLError XMLNode::writeToFile(
XMLCSTR filename,
const char *encoding,
char nFormat)
const
526 FILE *f=xfopen(filename,
_X(
"wb"));
529 unsigned char h[2]={ 0xFF, 0xFE };
533 if (!fwrite(
_X(
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n"),
sizeof(
wchar_t)*40,1,f))
546 if (!encoding) encoding=
"ISO-8859-1";
567 if (lpszData==NULL)
return NULL;
570 if (cbData==0) cbData=(int)xstrlen(lpszData);
574 memcpy(lpszNew, lpszData, (cbData) *
sizeof(
XMLCHAR));
575 lpszNew[cbData] = (
XMLCHAR)NULL;
584 XMLCharacterEntity *entity;
590 if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++;
goto out_of_loop1; }
594 *(dest++)=*(source++);
596 switch(XML_ByteTable[(
unsigned char)ch])
598 case 4: *(dest++)=*(source++);
599 case 3: *(dest++)=*(source++);
600 case 2: *(dest++)=*(source++);
601 case 1: *(dest++)=*(source++);
612int lengthXMLString(
XMLCSTR source)
615 XMLCharacterEntity *entity;
622 if (ch==entity->c) { r+=entity->l; source++;
goto out_of_loop1; }
628 ch=XML_ByteTable[(
unsigned char)ch]; r+=ch; source+=ch;
636ToXMLStringTool::~ToXMLStringTool(){
freeBuffer(); }
637void ToXMLStringTool::freeBuffer(){
if (buf) free(buf); buf=NULL; buflen=0; }
640 int l=lengthXMLString(source)+1;
641 if (l>buflen) { buflen=l; buf=(
XMLSTR)realloc(buf,l*
sizeof(
XMLCHAR)); }
642 return toXMLStringUnSafe(buf,source);
660 XMLCharacterEntity *entity;
665 if ((lo>2)&&(s[1]==
_X(
'#')))
668 if ((*s==
_X(
'X'))||(*s==
_X(
'x'))) { s++; lo--; }
669 while ((*s)&&(*s!=
_X(
';'))&&((lo--)>0)) s++;
681 if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l;
break; }
695 j=XML_ByteTable[(
unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
710 if ((*ss==
_X(
'X'))||(*ss==
_X(
'x')))
715 if ((*ss>=
_X(
'0'))&&(*ss<=
_X(
'9'))) j=(j<<4)+*ss-
_X(
'0');
716 else if ((*ss>=
_X(
'A'))&&(*ss<=
_X(
'F'))) j=(j<<4)+*ss-
_X(
'A')+10;
717 else if ((*ss>=
_X(
'a'))&&(*ss<=
_X(
'f'))) j=(j<<4)+*ss-
_X(
'a')+10;
725 if ((*ss>=
_X(
'0'))&&(*ss<=
_X(
'9'))) j=(j*10)+*ss-
_X(
'0');
736 if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l;
break; }
745 switch(XML_ByteTable[(
unsigned char)*ss])
747 case 4: *(d++)=*(ss++); ll--;
748 case 3: *(d++)=*(ss++); ll--;
749 case 2: *(d++)=*(ss++); ll--;
750 case 1: *(d++)=*(ss++);
759#define XML_isSPACECHAR(ch) ((ch==_X('\n'))||(ch==_X(' '))||(ch== _X('\t'))||(ch==_X('\r')))
767 if (!cclose)
return 1;
768 int l=(int)xstrlen(cclose);
769 if (xstrnicmp(cclose, copen, l)!=0)
return 1;
771 if (XML_isSPACECHAR(c)||
775 (c==
_X(
'=' )))
return 0;
780static inline XMLCHAR getNextChar(XML *pXML)
782 XMLCHAR ch = pXML->lpXML[pXML->nIndex];
784 if (ch!=0) pXML->nIndex++;
786 pXML->nIndex+=XML_ByteTable[(
unsigned char)ch];
793static NextToken GetNextToken(XML *pXML,
int *pcbToken,
enum XMLTokenTypeTag *pType)
798 int indexStart,nFoundMatch,nIsText=
FALSE;
802 do { indexStart=pXML->nIndex; ch=getNextChar(pXML); }
while XML_isSPACECHAR(ch);
807 result.pStr = &pXML->lpXML[indexStart];
811 ALLXMLClearTag *ctag=XMLClearTags;
814 if (xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen)==0)
817 pXML->nIndex+=ctag->openTagLen-1;
822 }
while(ctag->lpszOpen);
831 *pType = eTokenQuotedText;
838 while((ch = getNextChar(pXML)))
840 if (ch==chTemp) { nFoundMatch =
TRUE;
break; }
841 if (ch==
_X(
'<'))
break;
845 if (nFoundMatch ==
FALSE)
847 pXML->nIndex=indexStart+1;
859 *pType = eTokenEquals;
864 *pType = eTokenCloseTag;
872 chTemp = pXML->lpXML[pXML->nIndex];
875 if (chTemp ==
_X(
'/'))
879 *pType = eTokenTagEnd;
883 else if (chTemp ==
_X(
'?'))
888 *pType = eTokenDeclaration;
894 *pType = eTokenTagStart;
902 chTemp = pXML->lpXML[pXML->nIndex];
905 if (chTemp ==
_X(
'>'))
909 *pType = eTokenShortHandClose;
926 while((ch = getNextChar(pXML)))
928 if XML_isSPACECHAR(ch)
932 }
else if (ch==
_X(
'/'))
936 ch=pXML->lpXML[pXML->nIndex];
938 if (ch==
_X(
'>')) { pXML->nIndex--;
break; }
940 }
else if ((ch==
_X(
'<'))||(ch==
_X(
'>'))||(ch==
_X(
'=')))
942 pXML->nIndex--;
break;
946 *pcbToken = pXML->nIndex-indexStart;
951 *pType = eTokenError;
960 if (!d) { free(lpszName);
return NULL; }
961 if (d->lpszName&&(lpszName!=d->lpszName)) free((
void*)d->lpszName);
962 d->lpszName=lpszName;
967XMLNode::XMLNode(
struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
968XMLNode::XMLNode(XMLNodeData *pParent,
XMLSTR lpszName,
char isDeclaration)
970 d=(XMLNodeData*)malloc(
sizeof(XMLNodeData));
981 d->pParent = pParent;
994#define MEMORYINCREASE 50
996static inline void myFree(
void *p) {
if (p) free(p); };
997static inline void *myRealloc(
void *p,
int newsize,
int memInc,
int sizeofElem)
999 if (p==NULL) {
if (memInc)
return malloc(memInc*sizeofElem);
return malloc(sizeofElem); }
1000 if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
1009int XMLNode::findPosition(XMLNodeData *d,
int index,
XMLElementType xtype)
1011 if (index<0)
return -1;
1012 int i=0,j=(int)((index<<2)+xtype),*o=d->pOrder;
while (o[i]!=j) i++;
return i;
1017int XMLNode::removeOrderElement(XMLNodeData *d,
XMLElementType t,
int index)
1019 int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
1020 memmove(o+i, o+i+1, (n-i)*
sizeof(
int));
1022 if ((o[i]&3)==(
int)t) o[i]-=4;
1030void *XMLNode::addToOrder(
int memoryIncrease,
int *_pos,
int nc,
void *p,
int size,
XMLElementType xtype)
1034 p=myRealloc(p,(nc+1),memoryIncrease,size);
1035 int n=d->nChild+d->nText+d->nClear;
1036 d->pOrder=(
int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,
sizeof(
int));
1037 int pos=*_pos,*o=d->pOrder;
1039 if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype);
return p; }
1042 memmove(o+i+1, o+i, (n-i)*
sizeof(
int));
1044 while ((pos<n)&&((o[pos]&3)!=(
int)xtype)) pos++;
1045 if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype);
return p; }
1048 for (i=pos+1;i<=n;i++)
if ((o[i]&3)==(
int)xtype) o[i]+=4;
1050 *_pos=pos=o[pos]>>2;
1051 memmove(((
char*)p)+(pos+1)*size,((
char*)p)+pos*size,(nc-pos)*size);
1057XMLNode XMLNode::addChild_priv(
int memoryIncrease,
XMLSTR lpszName,
char isDeclaration,
int pos)
1061 d->pChild[pos].d=NULL;
1064 return d->pChild[pos];
1072 int nc=d->nAttribute;
1075 pAttr->lpszName = lpszName;
1076 pAttr->lpszValue = lpszValuev;
1082XMLCSTR XMLNode::addText_priv(
int memoryIncrease,
XMLSTR lpszValue,
int pos)
1084 if (!lpszValue)
return NULL;
1085 if (!d) { myFree(lpszValue);
return NULL; }
1087 d->pText[pos]=lpszValue;
1099 pNewClear->lpszValue = lpszValue;
1100 if (!lpszOpen) lpszOpen=XMLClearTags->lpszOpen;
1101 if (!lpszClose) lpszClose=XMLClearTags->lpszClose;
1102 pNewClear->lpszOpenTag = lpszOpen;
1103 pNewClear->lpszCloseTag = lpszClose;
1110char XMLNode::parseClearTag(
void *px,
void *_pClear)
1112 XML *pXML=(XML *)px;
1113 ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear);
1116 XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
1121 if (pClear.lpszOpen==XMLClearTags[1].lpszOpen)
1126 if (*pCh==
_X(
'<')) { pClear.lpszClose=docTypeEnd; lpszTemp=xstrstr(lpXML,docTypeEnd);
break; }
1127 else if (*pCh==
_X(
'>')) { lpszTemp=pCh;
break; }
1131 pCh+=XML_ByteTable[(
unsigned char)(*pCh)];
1134 }
else lpszTemp=xstrstr(lpXML, pClear.lpszClose);
1139 cbTemp = (int)(lpszTemp - lpXML);
1141 pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
1144 addClear_priv(MEMORYINCREASE,
stringDup(lpXML,cbTemp), pClear.lpszOpen, pClear.lpszClose,-1);
1153void XMLNode::exactMemory(XMLNodeData *d)
1155 if (d->pOrder) d->pOrder=(
int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*
sizeof(
int));
1156 if (d->pChild) d->pChild=(
XMLNode*)realloc(d->pChild,d->nChild*
sizeof(
XMLNode));
1158 if (d->pText) d->pText=(
XMLCSTR*)realloc(d->pText,d->nText*
sizeof(
XMLSTR));
1159 if (d->pClear) d->pClear=(
XMLClear *)realloc(d->pClear,d->nClear*
sizeof(
XMLClear));
1162char XMLNode::maybeAddTxT(
void *pa,
XMLCSTR tokenPStr)
1164 XML *pXML=(XML *)pa;
1165 XMLCSTR lpszText=pXML->lpszText;
1166 if (!lpszText)
return 0;
1167 if (dropWhiteSpace)
while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
1168 int cbText = (int)(tokenPStr - lpszText);
1169 if (!cbText) { pXML->lpszText=NULL;
return 0; }
1170 if (dropWhiteSpace) { cbText--;
while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
1171 if (!cbText) { pXML->lpszText=NULL;
return 0; }
1172 XMLSTR lpt=fromXMLString(lpszText,cbText,pXML);
1174 addText_priv(MEMORYINCREASE,lpt,-1);
1175 pXML->lpszText=NULL;
1180int XMLNode::ParseXMLElement(
void *pa)
1182 XML *pXML=(XML *)pa;
1184 enum XMLTokenTypeTag xtype;
1191 enum Attrib attrib = eAttribName;
1199 pXML->nFirst =
FALSE;
1200 status = eOutsideTag;
1204 status = eInsideTag;
1211 token = GetNextToken(pXML, &cbToken, &xtype);
1213 if (xtype != eTokenError)
1227 case eTokenCloseTag:
1228 case eTokenShortHandClose:
1229 case eTokenQuotedText:
1234 case eTokenTagStart:
1235 case eTokenDeclaration:
1238 nDeclaration = (xtype == eTokenDeclaration);
1241 if (maybeAddTxT(pXML,token.pStr))
return FALSE;
1244 token = GetNextToken(pXML, &cbToken, &xtype);
1248 if (xtype != eTokenText)
1257#ifdef APPROXIMATE_PARSING
1259 myTagCompare(d->lpszName, token.pStr) == 0)
1263 pXML->lpNewElement = token.pStr;
1264 pXML->cbNewElement = cbToken;
1272 pNew = addChild_priv(MEMORYINCREASE,
stringDup(token.pStr,cbToken), nDeclaration,-1);
1274 while (!pNew.isEmpty())
1280 if (!pNew.ParseXMLElement(pXML))
return FALSE;
1303 if (myTagCompare(d->lpszName, pXML->lpEndTag)==0)
1310 if (pXML->cbNewElement)
1320 if (myTagCompare(d->lpszName, pXML->lpNewElement)==0)
1326 pNew = addChild_priv(MEMORYINCREASE,
stringDup(pXML->lpNewElement,pXML->cbNewElement),0,-1);
1327 pXML->cbNewElement = 0;
1344 if (maybeAddTxT(pXML,token.pStr))
return FALSE;
1347 token = GetNextToken(pXML, &cbTemp, &xtype);
1350 if (xtype != eTokenText)
1355 lpszTemp = token.pStr;
1358 token = GetNextToken(pXML, &cbToken, &xtype);
1359 if (xtype != eTokenCloseTag)
1364 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1369 if (myTagCompare(d->lpszName, lpszTemp) != 0)
1370#ifdef STRICT_PARSING
1373 pXML->nIndexMissigEndTag=pXML->nIndex;
1379 pXML->nIndexMissigEndTag=pXML->nIndex;
1380 pXML->lpEndTag = lpszTemp;
1381 pXML->cbEndTag = cbTemp;
1392 if (maybeAddTxT(pXML,token.pStr))
return FALSE;
1393 if (parseClearTag(pXML, token.pClr))
return FALSE;
1394 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1420 lpszTemp = token.pStr;
1422 attrib = eAttribEquals;
1427 case eTokenCloseTag:
1429 status = eOutsideTag;
1430 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1435 case eTokenShortHandClose:
1437 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1441 case eTokenQuotedText:
1442 case eTokenTagStart:
1445 case eTokenDeclaration:
1462 addAttribute_priv(MEMORYINCREASE,
stringDup(lpszTemp,cbTemp), NULL);
1465 lpszTemp = token.pStr;
1471 case eTokenShortHandClose:
1472 case eTokenCloseTag:
1475 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1477 if (d->isDeclaration &&
1478 (lpszTemp[cbTemp-1]) ==
_X(
'?'))
1486 addAttribute_priv(MEMORYINCREASE,
stringDup(lpszTemp,cbTemp), NULL);
1490 if (xtype == eTokenShortHandClose)
1497 status = eOutsideTag;
1505 attrib = eAttribValue;
1509 case eTokenQuotedText:
1510 case eTokenTagStart:
1512 case eTokenDeclaration:
1529 case eTokenQuotedText:
1532 if (d->isDeclaration &&
1533 (token.pStr[cbToken-1]) ==
_X(
'?'))
1541 if (xtype==eTokenQuotedText) { token.pStr++; cbToken-=2; }
1545 attrVal=fromXMLString(attrVal,cbToken,pXML);
1546 if (!attrVal)
return FALSE;
1548 addAttribute_priv(MEMORYINCREASE,
stringDup(lpszTemp,cbTemp),attrVal);
1552 attrib = eAttribName;
1556 case eTokenTagStart:
1558 case eTokenCloseTag:
1559 case eTokenShortHandClose:
1561 case eTokenDeclaration:
1574 if ((!d->isDeclaration)&&(d->pParent))
1576#ifdef STRICT_PARSING
1581 pXML->nIndexMissigEndTag=pXML->nIndex;
1583 maybeAddTxT(pXML,pXML->lpXML+pXML->nIndex);
1598 pResults->nLine = 1;
1599 pResults->nColumn = 1;
1600 while (xml.nIndex<nUpto)
1602 ch = getNextChar(&xml);
1603 if (ch !=
_X(
'\n')) pResults->nColumn++;
1607 pResults->nColumn=1;
1621 pResults->nColumn=0;
1627 struct XML xml={ lpszXML, lpszXML, 0, 0,
eXMLErrorNone, NULL, 0, NULL, 0,
TRUE };
1630 xnode.ParseXMLElement(&xml);
1633 if ((xnode.nChildNode()==1)&&(xnode.nElement()==1)) xnode=xnode.getChildNode();
1639 if (tag&&xstrlen(tag)&&((!name)||(xstricmp(xnode.getName(),tag))))
1643 while (i<xnode.nChildNode())
1645 nodeTmp=xnode.getChildNode(i);
1646 if (xstricmp(nodeTmp.getName(),tag)==0)
break;
1647 if (nodeTmp.isDeclaration()) { xnode=nodeTmp; i=0; }
else i++;
1649 if (i>=xnode.nChildNode())
1655 pResults->nColumn=0;
1671 pResults->error = error;
1678 CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
1686 if (pResults) { pResults->nLine=0; pResults->nColumn=0; }
1687 FILE *f=xfopen(filename,
_X(
"rb"));
1689 fseek(f,0,SEEK_END);
1690 int l=ftell(f),headerSz=0;
1692 fseek(f,0,SEEK_SET);
1693 unsigned char *buf=(
unsigned char*)malloc(l+4);
1694 size_t dummy = fread(buf,l,1,f);
1696 buf[l]=0;buf[l+1]=0;buf[l+2]=0;buf[l+3]=0;
1698 if (guessWideCharChars)
1700 if (!myIsTextWideChar(buf,l))
1702 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1703 XMLSTR b2=myMultiByteToWideChar((
const char*)(buf+headerSz));
1704 free(buf); buf=(
unsigned char*)b2; headerSz=0;
1707 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1708 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1712 if (guessWideCharChars)
1714 if (myIsTextWideChar(buf,l))
1717 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1718 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1719 char *b2=myWideCharToMultiByte((
const wchar_t*)(buf+headerSz));
1720 free(buf); buf=(
unsigned char*)b2; headerSz=0;
1723 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1734static inline void charmemset(
XMLSTR dest,
XMLCHAR c,
int l) {
while (l--) *(dest++)=c; }
1741int XMLNode::CreateXMLStringR(XMLNodeData *pEntry,
XMLSTR lpszMarker,
int nFormat)
1746 int nChildFormat=-1;
1747 int nElementI=pEntry->nChild+pEntry->nText+pEntry->nClear;
1752#define LENSTR(lpsz) (lpsz ? xstrlen(lpsz) : 0)
1755 cbElement = (int)LENSTR(pEntry->lpszName);
1760 cb = nFormat == -1 ? 0 : nFormat;
1764 if (cb) charmemset(lpszMarker, INDENTCHAR,
sizeof(
XMLCHAR)*cb);
1766 lpszMarker[nResult++]=
_X(
'<');
1767 if (pEntry->isDeclaration) lpszMarker[nResult++]=
_X(
'?');
1768 xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
1770 lpszMarker[nResult++]=
_X(
' ');
1774 nResult+=cbElement+2+cb;
1775 if (pEntry->isDeclaration) nResult++;
1780 for (i=0; i<pEntry->nAttribute; i++)
1783 cb = (int)LENSTR(pAttr->lpszName);
1786 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pAttr->lpszName);
1789 if (pAttr->lpszValue)
1791 cb=(int)lengthXMLString(pAttr->lpszValue);
1794 lpszMarker[nResult]=
_X(
'=');
1795 lpszMarker[nResult+1]=
_X(
'"');
1796 if (cb) toXMLStringUnSafe(&lpszMarker[nResult+2],pAttr->lpszValue);
1797 lpszMarker[nResult+cb+2]=
_X(
'"');
1801 if (lpszMarker) lpszMarker[nResult] =
_X(
' ');
1807 if (pEntry->isDeclaration)
1811 lpszMarker[nResult-1]=
_X(
'?');
1812 lpszMarker[nResult]=
_X(
'>');
1817 if (lpszMarker) lpszMarker[nResult]=
_X(
'\n');
1824 if (lpszMarker) lpszMarker[nResult-1]=
_X(
'>');
1827 if (lpszMarker) lpszMarker[nResult]=
_X(
'\n');
1837 if (cbElement&&(!pEntry->isDeclaration)) nChildFormat=nFormat+1;
1838 else nChildFormat=nFormat;
1842 for (i=0; i<nElementI; i++)
1844 j=pEntry->pOrder[i];
1851 XMLCSTR pChild=pEntry->pText[j>>2];
1852 cb = (int)lengthXMLString(pChild);
1859 charmemset(&lpszMarker[nResult],INDENTCHAR,
sizeof(
XMLCHAR)*(nFormat + 1));
1860 toXMLStringUnSafe(&lpszMarker[nResult+nFormat+1],pChild);
1861 lpszMarker[nResult+nFormat+1+cb]=
_X(
'\n');
1863 nResult+=cb+nFormat+2;
1866 if (lpszMarker) toXMLStringUnSafe(&lpszMarker[nResult], pChild);
1876 XMLClear *pChild=pEntry->pClear+(j>>2);
1878 cb = (int)LENSTR(pChild->lpszOpenTag);
1885 charmemset(&lpszMarker[nResult], INDENTCHAR,
sizeof(
XMLCHAR)*(nFormat + 1));
1886 xstrcpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag);
1888 nResult+=cb+nFormat+1;
1892 if (lpszMarker)xstrcpy(&lpszMarker[nResult], pChild->lpszOpenTag);
1898 cb = (int)LENSTR(pChild->lpszValue);
1901 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszValue);
1906 cb = (int)LENSTR(pChild->lpszCloseTag);
1909 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszCloseTag);
1915 if (lpszMarker) lpszMarker[nResult] =
_X(
'\n');
1925 nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
1932 if ((cbElement)&&(!pEntry->isDeclaration))
1945 charmemset(&lpszMarker[nResult], INDENTCHAR,
sizeof(
XMLCHAR)*nFormat);
1950 xstrcpy(&lpszMarker[nResult],
_X(
"</"));
1952 xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
1953 nResult += cbElement;
1957 xstrcpy(&lpszMarker[nResult],
_X(
">"));
1961 xstrcpy(&lpszMarker[nResult],
_X(
">\n"));
1966 if (nFormat != -1) nResult+=cbElement+4+nFormat;
1967 else nResult+=cbElement+3;
1978 xstrcpy(&lpszMarker[nResult],
_X(
"/>"));
1983 xstrcpy(&lpszMarker[nResult],
_X(
"/>\n"));
1989 nResult += nFormat == -1 ? 2 : 3;
2008XMLSTR XMLNode::createXMLString(
int nFormat,
int *pnSize)
const
2010 if (!d) {
if (pnSize) *pnSize=0;
return NULL; }
2012 XMLSTR lpszResult = NULL;
2016 if (!dropWhiteSpace) nFormat=0;
2017 nFormat = nFormat ? 0 : -1;
2018 cbStr = CreateXMLStringR(d, 0, nFormat);
2023 CreateXMLStringR(d, lpszResult, nFormat);
2024 if (pnSize) *pnSize = cbStr;
2028int XMLNode::detachFromParent(XMLNodeData *d)
2030 XMLNode *pa=d->pParent->pChild;
2032 while (((
void*)(pa[i].d))!=((
void*)d)) i++;
2033 d->pParent->nChild--;
2037 if (d->pParent->nChild) memmove((
void*)(pa+i),(
void*)(pa+i+1),(d->pParent->nChild-i)*
sizeof(
XMLNode));
2038 else { free(pa); d->pParent->pChild=NULL; }
2039 return removeOrderElement(d->pParent,
eNodeChild,i);
2042XMLNode::~XMLNode() { deleteNodeContent_priv(1,0); }
2043void XMLNode::deleteNodeContent(){ deleteNodeContent_priv(0,1); }
2044void XMLNode::deleteNodeContent_priv(
char isInDestuctor,
char force)
2047 if (isInDestuctor) (d->ref_count)--;
2048 if ((d->ref_count==0)||force)
2051 if (d->pParent) detachFromParent(d);
2052 for(i=0; i<d->nChild; i++) { d->pChild[i].d->pParent=NULL; d->pChild[i].deleteNodeContent_priv(1,force); }
2054 for(i=0; i<d->nText; i++) free((
void*)d->pText[i]);
2056 for(i=0; i<d->nClear; i++) free((
void*)d->pClear[i].lpszValue);
2058 for(i=0; i<d->nAttribute; i++)
2060 free((
void*)d->pAttribute[i].lpszName);
2061 if (d->pAttribute[i].lpszValue) free((
void*)d->pAttribute[i].lpszValue);
2063 myFree(d->pAttribute);
2065 myFree((
void*)d->lpszName);
2066 d->nChild=0; d->nText=0; d->nClear=0; d->nAttribute=0;
2067 d->pChild=NULL; d->pText=NULL; d->pClear=NULL; d->pAttribute=NULL;
2068 d->pOrder=NULL; d->lpszName=NULL; d->pParent=NULL;
2070 if (d->ref_count==0)
2079 XMLNodeData *dc=childNode.d;
2080 if ((!dc)||(!d))
return childNode;
2081 if (dc->pParent) {
if ((detachFromParent(dc)<=pos)&&(dc->pParent==d)) pos--; }
else dc->ref_count++;
2086 d->pChild[pos].d=dc;
2091void XMLNode::deleteAttribute(
int i)
2093 if ((!d)||(i<0)||(i>=d->nAttribute))
return;
2096 free((
void*)p->lpszName);
2097 if (p->lpszValue) free((
void*)p->lpszValue);
2098 if (d->nAttribute) memmove(p,p+1,(d->nAttribute-i)*
sizeof(
XMLAttribute));
else { free(p); d->pAttribute=NULL; }
2102void XMLNode::deleteAttribute(
XMLCSTR lpszName)
2111 if (!d) {
if (lpszNewValue) free(lpszNewValue);
if (lpszNewName) free(lpszNewName);
return NULL; }
2112 if (i>=d->nAttribute)
2118 if (p->lpszValue&&p->lpszValue!=lpszNewValue) free((
void*)p->lpszValue);
2119 p->lpszValue=lpszNewValue;
2120 if (lpszNewName&&p->lpszName!=lpszNewName) { free((
void*)p->lpszName); p->lpszName=lpszNewName; };
2142int XMLNode::indexText(
XMLCSTR lpszValue)
const
2146 if (!lpszValue) {
if (l)
return 0;
return -1; }
2148 for (i=0; i<l; i++)
if (lpszValue==p[i])
return i;
2152void XMLNode::deleteText(
int i)
2154 if ((!d)||(i<0)||(i>=d->nText))
return;
2158 if (d->nText) memmove(p,p+1,(d->nText-i)*
sizeof(
XMLCSTR));
else { free(p); d->pText=NULL; }
2162void XMLNode::deleteText(
XMLCSTR lpszValue) {
deleteText(indexText(lpszValue)); }
2166 if (!d) {
if (lpszNewValue) free(lpszNewValue);
return NULL; }
2169 if (*p!=lpszNewValue) { free((
void*)*p); *p=lpszNewValue; }
2170 return lpszNewValue;
2175 if (!d) {
if (lpszNewValue) free(lpszNewValue);
return NULL; }
2176 int i=indexText(lpszOldValue);
2181void XMLNode::deleteClear(
int i)
2183 if ((!d)||(i<0)||(i>=d->nClear))
return;
2186 free((
void*)p->lpszValue);
2187 if (d->nClear) memmove(p,p+1,(d->nClear-i)*
sizeof(
XMLClear));
else { free(p); d->pClear=NULL; }
2191int XMLNode::indexClear(
XMLCSTR lpszValue)
const
2195 if (!lpszValue) {
if (l)
return 0;
return -1; }
2197 for (i=0; i<l; i++)
if (lpszValue==p[i].lpszValue)
return i;
2206 if (!d) {
if (lpszNewContent) free(lpszNewContent);
return NULL; }
2209 if (lpszNewContent!=p->lpszValue) { free((
void*)p->lpszValue); p->lpszValue=lpszNewContent; }
2215 if (!d) {
if (lpszNewContent) free(lpszNewContent);
return NULL; }
2216 int i=indexClear(lpszOldValue);
2232 deleteNodeContent_priv(1,0);
2234 if (d) (d->ref_count) ++ ;
2239XMLNode::XMLNode(
const XMLNode &A)
2243 if (d) (d->ref_count)++ ;
2246int XMLNode::nChildNode(
XMLCSTR name)
const
2249 int i,j=0,n=d->nChild;
2253 if (xstricmp(pc->d->lpszName, name)==0) j++;
2262 int i=0,n=d->nChild;
2267 if (xstricmp(pc->d->lpszName, name)==0)
2285int XMLNode::positionOfText (
int i)
const {
if (i>=d->nText ) i=d->nText-1;
return findPosition(d,i,
eNodeText ); }
2286int XMLNode::positionOfClear (
int i)
const {
if (i>=d->nClear) i=d->nClear-1;
return findPosition(d,i,
eNodeClear); }
2287int XMLNode::positionOfChildNode(
int i)
const {
if (i>=d->nChild) i=d->nChild-1;
return findPosition(d,i,
eNodeChild); }
2288int XMLNode::positionOfText (
XMLCSTR lpszValue)
const {
return positionOfText (indexText (lpszValue)); }
2291int XMLNode::positionOfChildNode(
XMLNode x)
const
2293 if ((!d)||(!x.d))
return -1;
2294 XMLNodeData *dd=x.d;
2297 while (i--)
if (pc[i].d==dd)
return findPosition(d,i,
eNodeChild);
2300int XMLNode::positionOfChildNode(
XMLCSTR name,
int count)
const
2304 do {
getChildNode(name,&j);
if (j<0)
return -1; }
while (count--);
2324 t=x.getAttribute(attributeName,&j);
2325 if (t&&(xstricmp(attributeValue,t)==0)) {
if (k) *k=i+1;
return x; }
2329 if (x.isAttributeSet(attributeName)) {
if (k) *k=i+1;
return x; }
2332 }
while (!x.isEmpty());
2339 if (!d)
return NULL;
2340 int i=0,n=d->nAttribute;
2345 if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2348 return pAttr->lpszValue;
2355char XMLNode::isAttributeSet(
XMLCSTR lpszAttrib)
const
2357 if (!d)
return FALSE;
2358 int i,n=d->nAttribute;
2362 if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2373 if (!d)
return NULL;
2386 c.attrib=d->pAttribute[i];
2391 i=(d->pOrder[i])>>2;
2394 case eNodeChild: c.child = d->pChild[i];
break;
2395 case eNodeText: c.text = d->pText[i];
break;
2396 case eNodeClear: c.clear = d->pClear[i];
break;
2402XMLCSTR XMLNode::getName()
const {
if (!d)
return NULL;
return d->lpszName; }
2403int XMLNode::nText()
const {
if (!d)
return 0;
return d->nText; }
2404int XMLNode::nChildNode()
const {
if (!d)
return 0;
return d->nChild; }
2405int XMLNode::nAttribute()
const {
if (!d)
return 0;
return d->nAttribute; }
2406int XMLNode::nClear()
const {
if (!d)
return 0;
return d->nClear; }
2407int XMLNode::nElement()
const {
if (!d)
return 0;
return d->nAttribute+d->nChild+d->nText+d->nClear; }
2408XMLClear XMLNode::getClear (
int i)
const {
if ((!d)||(i>=d->nClear ))
return emptyXMLClear;
return d->pClear[i]; }
2410XMLCSTR XMLNode::getAttributeName (
int i)
const {
if ((!d)||(i>=d->nAttribute))
return NULL;
return d->pAttribute[i].lpszName; }
2411XMLCSTR XMLNode::getAttributeValue(
int i)
const {
if ((!d)||(i>=d->nAttribute))
return NULL;
return d->pAttribute[i].lpszValue; }
2412XMLCSTR XMLNode::getText (
int i)
const {
if ((!d)||(i>=d->nText ))
return NULL;
return d->pText[i]; }
2413XMLNode XMLNode::getChildNode (
int i)
const {
if ((!d)||(i>=d->nChild ))
return emptyXMLNode;
return d->pChild[i]; }
2415char XMLNode::isDeclaration ( )
const {
if (!d)
return 0;
return d->isDeclaration; }
2416char XMLNode::isEmpty ( )
const {
return (d==NULL); }
2417XMLNode XMLNode::emptyNode ( ) {
return XMLNode::emptyXMLNode; }
2419XMLNode XMLNode::addChild(
XMLCSTR lpszName,
char isDeclaration,
int pos)
2421XMLNode XMLNode::addChild_WOSD(
XMLSTR lpszName,
char isDeclaration,
int pos)
2426 {
return addAttribute_priv(0,lpszName,lpszValuev); }
2428 {
return addText_priv(0,
stringDup(lpszValue),pos); }
2430 {
return addText_priv(0,lpszValue,pos); }
2432 {
return addClear_priv(0,
stringDup(lpszValue),lpszOpen,lpszClose,pos); }
2434 {
return addClear_priv(0,lpszValue,lpszOpen,lpszClose,pos); }
2454char XMLNode::setGlobalOptions(XMLCharEncoding _characterEncoding,
char _guessWideCharChars,
char _dropWhiteSpace)
2456 guessWideCharChars=_guessWideCharChars; dropWhiteSpace=_dropWhiteSpace;
2458 if (_characterEncoding) characterEncoding=_characterEncoding;
2460 switch(_characterEncoding)
2462 case encoding_UTF8: characterEncoding=_characterEncoding; XML_ByteTable=XML_utf8ByteTable;
break;
2463 case encoding_ascii: characterEncoding=_characterEncoding; XML_ByteTable=XML_asciiByteTable;
break;
2464 case encoding_ShiftJIS: characterEncoding=_characterEncoding; XML_ByteTable=XML_sjisByteTable;
break;
2471XMLNode::XMLCharEncoding XMLNode::guessCharEncoding(
void *buf,
int l,
char useXMLEncodingAttribute)
2477 if (guessWideCharChars&&(myIsTextWideChar(buf,l)))
return (
XMLCharEncoding)0;
2478 unsigned char *b=(
unsigned char*)buf;
2479 if ((b[0]==0xef)&&(b[1]==0xbb)&&(b[2]==0xbf))
return encoding_UTF8;
2485 switch (XML_utf8ByteTable[b[i]])
2487 case 4: i++;
if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=
encoding_ascii; i=l; }
2488 case 3: i++;
if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=
encoding_ascii; i=l; }
2489 case 2: i++;
if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=
encoding_ascii; i=l; }
2493 if (!useXMLEncodingAttribute)
return bestGuess;
2500 b=(
unsigned char*)strstr(bb,
"encoding");
2501 if (!b)
return bestGuess;
2502 b+=8;
while XML_isSPACECHAR(*b) b++;
if (*b!=
'=')
return bestGuess;
2503 b++;
while XML_isSPACECHAR(*b) b++;
if ((*b!=
'\'')&&(*b!=
'"'))
return bestGuess;
2504 b++;
while XML_isSPACECHAR(*b) b++;
2506 if ((
_strnicmp((
char*)b,
"utf-8",5)==0)||
2513 if ((
_strnicmp((
char*)b,
"shiftjis",8)==0)||
2514 (
_strnicmp((
char*)b,
"shift-jis",9)==0)||
2520#undef XML_isSPACECHAR
2526static const char base64Fillchar =
_X(
'=');
2529XMLCSTR base64EncodeTable=
_X(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2533const unsigned char base64DecodeTable[] = {
2534 99,98,98,98,98,98,98,98,98,97, 97,98,98,97,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98,
2535 98,98,97,98,98,98,98,98,98,98, 98,98,98,62,98,98,98,63,52,53, 54,55,56,57,58,59,60,61,98,98,
2536 98,96,98,98,98, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 15,16,17,18,19,20,21,22,23,24,
2537 25,98,98,98,98,98,98,26,27,28, 29,30,31,32,33,34,35,36,37,38, 39,40,41,42,43,44,45,46,47,48,
2538 49,50,51,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98,
2539 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98,
2540 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98,
2541 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98,
2542 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98
2545XMLParserBase64Tool::~XMLParserBase64Tool(){
freeBuffer(); }
2547void XMLParserBase64Tool::freeBuffer(){
if (buf) free(buf); buf=NULL; buflen=0; }
2549int XMLParserBase64Tool::encodeLength(
int inlen,
char formatted)
2551 unsigned int i=((inlen-1)/3*4+4+1);
2552 if (formatted) i+=inlen/54;
2556XMLSTR XMLParserBase64Tool::encode(
unsigned char *inbuf,
unsigned int inlen,
char formatted)
2558 int i=
encodeLength(inlen,formatted),k=17,eLen=inlen/3,j;
2564 j=(inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
2566 *(curr++)=base64EncodeTable[ j>>18 ];
2567 *(curr++)=base64EncodeTable[(j>>12)&0x3f];
2568 *(curr++)=base64EncodeTable[(j>> 6)&0x3f];
2569 *(curr++)=base64EncodeTable[(j )&0x3f];
2570 if (formatted) {
if (!k) { *(curr++)=
_X(
'\n'); k=18; } k--; }
2575 *(curr++)=base64EncodeTable[ inbuf[0]>>2 ];
2576 *(curr++)=base64EncodeTable[(inbuf[0]<<4)&0x3F];
2577 *(curr++)=base64Fillchar;
2578 *(curr++)=base64Fillchar;
2581 j=(inbuf[0]<<8)|inbuf[1];
2582 *(curr++)=base64EncodeTable[ j>>10 ];
2583 *(curr++)=base64EncodeTable[(j>> 4)&0x3f];
2584 *(curr++)=base64EncodeTable[(j<< 2)&0x3f];
2585 *(curr++)=base64Fillchar;
2591unsigned int XMLParserBase64Tool::decodeSize(
XMLCSTR data,
XMLError *xe)
2602 c=base64DecodeTable[(
unsigned char)(*data)];
2608 if (size==0)
return 0;
2609 do { data--; size--; }
while(*data==base64Fillchar); size++;
2610 return (
unsigned int)((size*3)/4);
2613unsigned char XMLParserBase64Tool::decode(
XMLCSTR data,
unsigned char *buf,
int len,
XMLError *xe)
2622#define BASE64DECODE_READ_NEXT_CHAR(c) \
2624 if (data[i]>255){ c=98; break; } \
2625 c=base64DecodeTable[(unsigned char)data[i++]]; \
2627 if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2629#define BASE64DECODE_READ_NEXT_CHAR(c) \
2630 do { c=base64DecodeTable[(unsigned char)data[i++]]; }while (c==97); \
2631 if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2634 BASE64DECODE_READ_NEXT_CHAR(c)
2635 if (c==99) {
return 2; }
2638 if (p==(
int)len)
return 2;
2643 BASE64DECODE_READ_NEXT_CHAR(d)
2646 buf[p++]=(
unsigned char)((c<<2)|((d>>4)&0x3));
2648 BASE64DECODE_READ_NEXT_CHAR(c)
2652 if (c==96)
return 2;
2657 buf[p++]=(
unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf));
2659 BASE64DECODE_READ_NEXT_CHAR(d)
2663 if (d==96)
return 2;
2668 buf[p++]=(
unsigned char)(((c<<6)&0xc0)|d);
2671#undef BASE64DECODE_READ_NEXT_CHAR
2673void XMLParserBase64Tool::alloc(
int newsize)
2675 if ((!buf)&&(newsize)) { buf=malloc(newsize); buflen=newsize;
return; }
2676 if (newsize>buflen) { buf=realloc(buf,newsize); buflen=newsize; }
2679unsigned char *XMLParserBase64Tool::decode(
XMLCSTR data,
int *outlen,
XMLError *xe)
2683 if (outlen) *outlen=len;
2684 if (!len)
return NULL;
2686 if(!
decode(data,(
unsigned char*)buf,len,xe)){
return NULL; }
2687 return (
unsigned char*)buf;
2692bool XMLNode::UnitTest()
2698 "<library name=\"central\" open=\"true\">"
2699 "<book id=\"b1\" lang=\"en\">"
2700 "<title>Hello World</title>"
2701 "<author>Ada</author>"
2703 "<book id=\"b2\" lang=\"fr\">"
2704 "<title>Bonjour</title>"
2709 XMLNode root = XMLNode::parseString(doc,
"library", &results);
2712 XMLNode::getError(results.error), results.nLine, results.nColumn);
2715 if (root.isEmpty()) {
2723 if (!root.getName() || strcmp(root.getName(),
"library") != 0) {
2724 unittest::fail(
"root name mismatch: expected 'library', got '%s'",
2725 root.getName() ? root.getName() :
"(null)");
2730 if (root.nAttribute() != 2) {
2731 unittest::fail(
"root attribute count mismatch: expected 2, got %d", root.nAttribute());
2734 XMLCSTR nameAttr = root.getAttribute(
"name");
2735 if (!nameAttr || strcmp(nameAttr,
"central") != 0) {
2736 unittest::fail(
"root @name mismatch: expected 'central', got '%s'", nameAttr ? nameAttr :
"(null)");
2739 XMLCSTR openAttr = root.getAttribute(
"open");
2740 if (!openAttr || strcmp(openAttr,
"true") != 0) {
2741 unittest::fail(
"root @open mismatch: expected 'true', got '%s'", openAttr ? openAttr :
"(null)");
2744 if (!root.isAttributeSet(
"name")) {
2752 if (root.nChildNode() != 2) {
2753 unittest::fail(
"root child count mismatch: expected 2, got %d", root.nChildNode());
2756 if (root.nChildNode(
"book") != 2) {
2757 unittest::fail(
"root book-child count mismatch: expected 2, got %d", root.nChildNode(
"book"));
2761 XMLNode book1 = root.getChildNode(
"book", 0);
2762 if (book1.isEmpty() || !book1.getName() || strcmp(book1.getName(),
"book") != 0) {
2766 XMLCSTR id1 = book1.getAttribute(
"id");
2767 if (!id1 || strcmp(id1,
"b1") != 0) {
2768 unittest::fail(
"book[0] @id mismatch: expected 'b1', got '%s'", id1 ? id1 :
"(null)");
2771 XMLCSTR lang1 = book1.getAttribute(
"lang");
2772 if (!lang1 || strcmp(lang1,
"en") != 0) {
2773 unittest::fail(
"book[0] @lang mismatch: expected 'en', got '%s'", lang1 ? lang1 :
"(null)");
2778 if (book1.nChildNode() != 2) {
2779 unittest::fail(
"book[0] child count mismatch: expected 2, got %d", book1.nChildNode());
2785 XMLNode title1 = book1.getChildNode(
"title", 0);
2786 if (title1.isEmpty()) {
2790 if (title1.nText() < 1 || !title1.getText(0) || strcmp(title1.getText(0),
"Hello World") != 0) {
2791 unittest::fail(
"book[0]/title text mismatch: expected 'Hello World', got '%s'",
2792 title1.getText(0) ? title1.getText(0) :
"(null)");
2795 XMLNode author1 = book1.getChildNode(
"author", 0);
2796 if (author1.isEmpty() || !author1.getText(0) || strcmp(author1.getText(0),
"Ada") != 0) {
2797 unittest::fail(
"book[0]/author text mismatch: expected 'Ada', got '%s'",
2798 author1.getText(0) ? author1.getText(0) :
"(null)");
2803 XMLNode book2 = root.getChildNode(
"book", 1);
2804 XMLCSTR id2 = book2.getAttribute(
"id");
2805 if (!id2 || strcmp(id2,
"b2") != 0) {
2806 unittest::fail(
"book[1] @id mismatch: expected 'b2', got '%s'", id2 ? id2 :
"(null)");
2809 if (book2.nChildNode() != 1) {
2810 unittest::fail(
"book[1] child count mismatch: expected 1, got %d", book2.nChildNode());
2813 XMLNode title2 = book2.getChildNode(
"title", 0);
2814 if (title2.isEmpty() || !title2.getText(0) || strcmp(title2.getText(0),
"Bonjour") != 0) {
2815 unittest::fail(
"book[1]/title text mismatch: expected 'Bonjour', got '%s'",
2816 title2.getText(0) ? title2.getText(0) :
"(null)");
2822 XMLNode::parseString(
"<a><b></a>",
"a", &badResults);
2824 unittest::fail(
"malformed XML <a><b></a> was accepted without error");
2827 unittest::detail(
"malformed-doc error correctly reported: %s", XMLNode::getError(badResults.error));
2833 const char* docs[4] = {
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><a/>",
2834 "<?xml version=\"1.0\" encoding=\"Shift-JIS\"?><a/>",
"<?xml version=\"1.0\" encoding=\"utf-8\"?><a/>",
2835 "<?xml version=\"1.0\" encoding=\"UTF8\"?><a/>" };
2837 for (
int e = 0; e < 4; e++) {
2839 if (got != want[e]) {
2840 unittest::fail(
"guessCharEncoding(%s) = %d, expected %d", docs[e], (
int)got, (
int)want[e]);
2844 unittest::detail(
"encoding guess: ISO-8859-1 -> ascii, Shift-JIS -> ShiftJIS, utf-8/UTF8 -> UTF8");
2850 const int iterations = 20000;
2851 const size_t docBytes = strlen(doc);
2853 for (
int i = 0; i < iterations; i++) {
2854 XMLNode n = XMLNode::parseString(doc,
"library");
2856 if (n.nChildNode() != 2) {
2857 unittest::fail(
"throughput iteration %d produced wrong child count", i);
2862 if (us <= 0.0) us = 1.0;
2863 double parsesPerSec = (double)iterations / us * 1e6;
2864 double mbPerSec = ((double)iterations * (double)docBytes) / us;
2865 unittest::detail(
"parsed %d docs of %lu bytes in %.0f us", iterations, (
unsigned long)docBytes, us);
2868 unittest::metric(
"avg_parse_latency", us / (
double)iterations,
"us",
false);
2876 "xml", XMLNode::UnitTest,
2877 "XML parser: parse nested elements/attributes/text and parse throughput",
"util");
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Core fixed-width scalar typedefs and the PsyType / PsyContext hierarchical identifiers.
Small, dependency-free unit test harness used by all CMSDK object tests.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true, uint32 timeoutSec=0)
Register a test with the runner.
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
XMLDLLENTRY void freeXMLString(XMLSTR t)
void Register_XMLParser_Tests()
struct cmlabs::XMLResults XMLResults
struct cmlabs::XMLNodeContents XMLNodeContents
struct XMLDLLENTRY cmlabs::XMLNode XMLNode
XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=0)
@ eXMLErrorCannotOpenWriteFile
@ eXMLErrorUnexpectedToken
@ eXMLErrorFirstTagNotFound
@ eXMLErrorCannotWriteFile
@ eXMLErrorBase64DecodeBufferTooSmall
@ eXMLErrorBase64DecodeIllegalCharacter
@ eXMLErrorUnmatchedEndClearTag
@ eXMLErrorBase64DataSizeIsNotMultipleOf4
@ eXMLErrorUnknownCharacterEntity
@ eXMLErrorMissingTagName
@ eXMLErrorBase64DecodeTruncatedData
@ eXMLErrorMissingEndTagName
@ eXMLErrorUnmatchedEndTag
@ eXMLErrorCharConversionError
struct cmlabs::XMLClear XMLClear
struct cmlabs::XMLAttribute XMLAttribute
XMLCSTR addText_WOSD(XMLSTR lpszValue, int pos=-1)
int positionOfClear(int i=0) const
static XMLNode parseString(XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL)
XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, int i=0)
char isDeclaration() const
static XMLNode emptyXMLNode
static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1)
void deleteAttribute(XMLCSTR lpszName)
XMLAttribute getAttribute(int i=0) const
XMLNode getChildNode(int i=0) const
int positionOfText(int i=0) const
XMLClear * addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, int pos=-1)
static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::encoding_UTF8, char guessWideCharChars=1, char dropWhiteSpace=1)
XMLClear * updateClear_WOSD(XMLSTR lpszNewContent, int i=0)
XMLCSTR updateName_WOSD(XMLSTR lpszName)
XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const
int positionOfChildNode(int i=0) const
void deleteClear(int i=0)
static XMLAttribute emptyXMLAttribute
XMLAttribute * addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValue)
XMLAttribute * updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
static XMLClear emptyXMLClear
Small recursive XML DOM parser (XMLNode) used by CMSDK for all PsySpec XML parsing.