tinyxmlparser.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #include <ctype.h>
  20. #include <stddef.h>
  21. #include "tinyxml.h"
  22. //#define DEBUG_PARSER
  23. #if defined( DEBUG_PARSER )
  24. # if defined( DEBUG ) && defined( _MSC_VER )
  25. # include <windows.h>
  26. # define TIXML_LOG OutputDebugString
  27. # else
  28. # define TIXML_LOG printf
  29. # endif
  30. #endif
  31. // Note tha "PutString" hardcodes the same list. This
  32. // is less flexible than it appears. Changing the entries
  33. // or order will break putstring.
  34. TiXmlBase::Entity TiXmlBase::entity[ NUM_ENTITY ] =
  35. {
  36. { "&amp;", 5, '&' },
  37. { "&lt;", 4, '<' },
  38. { "&gt;", 4, '>' },
  39. { "&quot;", 6, '\"' },
  40. { "&apos;", 6, '\'' }
  41. };
  42. // Bunch of unicode info at:
  43. // http://www.unicode.org/faq/utf_bom.html
  44. // Including the basic of this table, which determines the #bytes in the
  45. // sequence from the lead byte. 1 placed for invalid sequences --
  46. // although the result will be junk, pass it through as much as possible.
  47. // Beware of the non-characters in UTF-8:
  48. // ef bb bf (Microsoft "lead bytes")
  49. // ef bf be
  50. // ef bf bf
  51. const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  52. const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  53. const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  54. const int TiXmlBase::utf8ByteTable[256] =
  55. {
  56. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  57. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
  58. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
  59. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
  61. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
  62. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
  63. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
  64. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70 End of ASCII range
  65. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80 0x80 to 0xc1 invalid
  66. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90
  67. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0
  68. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0
  69. 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0 0xc2 to 0xdf 2 byte
  70. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0
  71. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xe0 0xe0 to 0xef 3 byte
  72. 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid
  73. };
  74. void TiXmlBase::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  75. {
  76. const unsigned long BYTE_MASK = 0xBF;
  77. const unsigned long BYTE_MARK = 0x80;
  78. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  79. if (input < 0x80)
  80. *length = 1;
  81. else if ( input < 0x800 )
  82. *length = 2;
  83. else if ( input < 0x10000 )
  84. *length = 3;
  85. else if ( input < 0x200000 )
  86. *length = 4;
  87. else
  88. { *length = 0; return; } // This code won't covert this correctly anyway.
  89. output += *length;
  90. // Scary scary fall throughs.
  91. switch (*length)
  92. {
  93. case 4:
  94. --output;
  95. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  96. input >>= 6;
  97. case 3:
  98. --output;
  99. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  100. input >>= 6;
  101. case 2:
  102. --output;
  103. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  104. input >>= 6;
  105. case 1:
  106. --output;
  107. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  108. }
  109. }
  110. /*static*/ int TiXmlBase::IsAlpha( unsigned char anyByte, TiXmlEncoding /*encoding*/ )
  111. {
  112. // This will only work for low-ascii, everything else is assumed to be a valid
  113. // letter. I'm not sure this is the best approach, but it is quite tricky trying
  114. // to figure out alhabetical vs. not across encoding. So take a very
  115. // conservative approach.
  116. // if ( encoding == TIXML_ENCODING_UTF8 )
  117. // {
  118. if ( anyByte < 127 )
  119. return isalpha( anyByte );
  120. else
  121. return 1; // What else to do? The unicode set is huge...get the english ones right.
  122. // }
  123. // else
  124. // {
  125. // return isalpha( anyByte );
  126. // }
  127. }
  128. /*static*/ int TiXmlBase::IsAlphaNum( unsigned char anyByte, TiXmlEncoding /*encoding*/ )
  129. {
  130. // This will only work for low-ascii, everything else is assumed to be a valid
  131. // letter. I'm not sure this is the best approach, but it is quite tricky trying
  132. // to figure out alhabetical vs. not across encoding. So take a very
  133. // conservative approach.
  134. // if ( encoding == TIXML_ENCODING_UTF8 )
  135. // {
  136. if ( anyByte < 127 )
  137. return isalnum( anyByte );
  138. else
  139. return 1; // What else to do? The unicode set is huge...get the english ones right.
  140. // }
  141. // else
  142. // {
  143. // return isalnum( anyByte );
  144. // }
  145. }
  146. class TiXmlParsingData
  147. {
  148. friend class TiXmlDocument;
  149. public:
  150. void Stamp( const char* now, TiXmlEncoding encoding );
  151. const TiXmlCursor& Cursor() { return cursor; }
  152. private:
  153. // Only used by the document!
  154. TiXmlParsingData( const char* start, int _tabsize, int row, int col )
  155. {
  156. assert( start );
  157. stamp = start;
  158. tabsize = _tabsize;
  159. cursor.row = row;
  160. cursor.col = col;
  161. }
  162. TiXmlCursor cursor;
  163. const char* stamp;
  164. int tabsize;
  165. };
  166. void TiXmlParsingData::Stamp( const char* now, TiXmlEncoding encoding )
  167. {
  168. assert( now );
  169. // Do nothing if the tabsize is 0.
  170. if ( tabsize < 1 )
  171. {
  172. return;
  173. }
  174. // Get the current row, column.
  175. int row = cursor.row;
  176. int col = cursor.col;
  177. const char* p = stamp;
  178. assert( p );
  179. while ( p < now )
  180. {
  181. // Treat p as unsigned, so we have a happy compiler.
  182. const unsigned char* pU = (const unsigned char*)p;
  183. // Code contributed by Fletcher Dunn: (modified by lee)
  184. switch (*pU) {
  185. case 0:
  186. // We *should* never get here, but in case we do, don't
  187. // advance past the terminating null character, ever
  188. return;
  189. case '\r':
  190. // bump down to the next line
  191. ++row;
  192. col = 0;
  193. // Eat the character
  194. ++p;
  195. // Check for \r\n sequence, and treat this as a single character
  196. if (*p == '\n') {
  197. ++p;
  198. }
  199. break;
  200. case '\n':
  201. // bump down to the next line
  202. ++row;
  203. col = 0;
  204. // Eat the character
  205. ++p;
  206. // Check for \n\r sequence, and treat this as a single
  207. // character. (Yes, this bizarre thing does occur still
  208. // on some arcane platforms...)
  209. if (*p == '\r') {
  210. ++p;
  211. }
  212. break;
  213. case '\t':
  214. // Eat the character
  215. ++p;
  216. // Skip to next tab stop
  217. col = (col / tabsize + 1) * tabsize;
  218. break;
  219. case TIXML_UTF_LEAD_0:
  220. if ( encoding == TIXML_ENCODING_UTF8 )
  221. {
  222. if ( *(p+1) && *(p+2) )
  223. {
  224. // In these cases, don't advance the column. These are
  225. // 0-width spaces.
  226. if ( *(pU+1)==TIXML_UTF_LEAD_1 && *(pU+2)==TIXML_UTF_LEAD_2 )
  227. p += 3;
  228. else if ( *(pU+1)==0xbfU && *(pU+2)==0xbeU )
  229. p += 3;
  230. else if ( *(pU+1)==0xbfU && *(pU+2)==0xbfU )
  231. p += 3;
  232. else
  233. { p +=3; ++col; } // A normal character.
  234. }
  235. }
  236. else
  237. {
  238. ++p;
  239. ++col;
  240. }
  241. break;
  242. default:
  243. if ( encoding == TIXML_ENCODING_UTF8 )
  244. {
  245. // Eat the 1 to 4 byte utf8 character.
  246. int step = TiXmlBase::utf8ByteTable[*((const unsigned char*)p)];
  247. if ( step == 0 )
  248. step = 1; // Error case from bad encoding, but handle gracefully.
  249. p += step;
  250. // Just advance one column, of course.
  251. ++col;
  252. }
  253. else
  254. {
  255. ++p;
  256. ++col;
  257. }
  258. break;
  259. }
  260. }
  261. cursor.row = row;
  262. cursor.col = col;
  263. assert( cursor.row >= -1 );
  264. assert( cursor.col >= -1 );
  265. stamp = p;
  266. assert( stamp );
  267. }
  268. const char* TiXmlBase::SkipWhiteSpace( const char* p, TiXmlEncoding encoding )
  269. {
  270. if ( !p || !*p )
  271. {
  272. return 0;
  273. }
  274. if ( encoding == TIXML_ENCODING_UTF8 )
  275. {
  276. while ( *p )
  277. {
  278. const unsigned char* pU = (const unsigned char*)p;
  279. // Skip the stupid Microsoft UTF-8 Byte order marks
  280. if ( *(pU+0)==TIXML_UTF_LEAD_0
  281. && *(pU+1)==TIXML_UTF_LEAD_1
  282. && *(pU+2)==TIXML_UTF_LEAD_2 )
  283. {
  284. p += 3;
  285. continue;
  286. }
  287. else if(*(pU+0)==TIXML_UTF_LEAD_0
  288. && *(pU+1)==0xbfU
  289. && *(pU+2)==0xbeU )
  290. {
  291. p += 3;
  292. continue;
  293. }
  294. else if(*(pU+0)==TIXML_UTF_LEAD_0
  295. && *(pU+1)==0xbfU
  296. && *(pU+2)==0xbfU )
  297. {
  298. p += 3;
  299. continue;
  300. }
  301. if ( IsWhiteSpace( *p ) ) // Still using old rules for white space.
  302. ++p;
  303. else
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. while ( *p && IsWhiteSpace( *p ) )
  310. ++p;
  311. }
  312. return p;
  313. }
  314. #ifdef TIXML_USE_STL
  315. /*static*/ bool TiXmlBase::StreamWhiteSpace( std::istream * in, TIXML_STRING * tag )
  316. {
  317. for( ;; )
  318. {
  319. if ( !in->good() ) return false;
  320. int c = in->peek();
  321. // At this scope, we can't get to a document. So fail silently.
  322. if ( !IsWhiteSpace( c ) || c <= 0 )
  323. return true;
  324. *tag += (char) in->get();
  325. }
  326. }
  327. /*static*/ bool TiXmlBase::StreamTo( std::istream * in, int character, TIXML_STRING * tag )
  328. {
  329. //assert( character > 0 && character < 128 ); // else it won't work in utf-8
  330. while ( in->good() )
  331. {
  332. int c = in->peek();
  333. if ( c == character )
  334. return true;
  335. if ( c <= 0 ) // Silent failure: can't get document at this scope
  336. return false;
  337. in->get();
  338. *tag += (char) c;
  339. }
  340. return false;
  341. }
  342. #endif
  343. // One of TinyXML's more performance demanding functions. Try to keep the memory overhead down. The
  344. // "assign" optimization removes over 10% of the execution time.
  345. //
  346. const char* TiXmlBase::ReadName( const char* p, TIXML_STRING * name, TiXmlEncoding encoding )
  347. {
  348. // Oddly, not supported on some comilers,
  349. //name->clear();
  350. // So use this:
  351. *name = "";
  352. assert( p );
  353. // Names start with letters or underscores.
  354. // Of course, in unicode, tinyxml has no idea what a letter *is*. The
  355. // algorithm is generous.
  356. //
  357. // After that, they can be letters, underscores, numbers,
  358. // hyphens, or colons. (Colons are valid ony for namespaces,
  359. // but tinyxml can't tell namespaces from names.)
  360. if ( p && *p
  361. && ( IsAlpha( (unsigned char) *p, encoding ) || *p == '_' ) )
  362. {
  363. const char* start = p;
  364. while( p && *p
  365. && ( IsAlphaNum( (unsigned char ) *p, encoding )
  366. || *p == '_'
  367. || *p == '-'
  368. || *p == '.'
  369. || *p == ':' ) )
  370. {
  371. //(*name) += *p; // expensive
  372. ++p;
  373. }
  374. if ( p-start > 0 ) {
  375. name->assign( start, p-start );
  376. }
  377. return p;
  378. }
  379. return 0;
  380. }
  381. const char* TiXmlBase::GetEntity( const char* p, char* value, int* length, TiXmlEncoding encoding )
  382. {
  383. // Presume an entity, and pull it out.
  384. TIXML_STRING ent;
  385. int i;
  386. *length = 0;
  387. if ( *(p+1) && *(p+1) == '#' && *(p+2) )
  388. {
  389. unsigned long ucs = 0;
  390. ptrdiff_t delta = 0;
  391. unsigned mult = 1;
  392. if ( *(p+2) == 'x' )
  393. {
  394. // Hexadecimal.
  395. if ( !*(p+3) ) return 0;
  396. const char* q = p+3;
  397. q = strchr( q, ';' );
  398. if ( !q || !*q ) return 0;
  399. delta = q-p;
  400. --q;
  401. while ( *q != 'x' )
  402. {
  403. if ( *q >= '0' && *q <= '9' )
  404. ucs += mult * (*q - '0');
  405. else if ( *q >= 'a' && *q <= 'f' )
  406. ucs += mult * (*q - 'a' + 10);
  407. else if ( *q >= 'A' && *q <= 'F' )
  408. ucs += mult * (*q - 'A' + 10 );
  409. else
  410. return 0;
  411. mult *= 16;
  412. --q;
  413. }
  414. }
  415. else
  416. {
  417. // Decimal.
  418. if ( !*(p+2) ) return 0;
  419. const char* q = p+2;
  420. q = strchr( q, ';' );
  421. if ( !q || !*q ) return 0;
  422. delta = q-p;
  423. --q;
  424. while ( *q != '#' )
  425. {
  426. if ( *q >= '0' && *q <= '9' )
  427. ucs += mult * (*q - '0');
  428. else
  429. return 0;
  430. mult *= 10;
  431. --q;
  432. }
  433. }
  434. if ( encoding == TIXML_ENCODING_UTF8 )
  435. {
  436. // convert the UCS to UTF-8
  437. ConvertUTF32ToUTF8( ucs, value, length );
  438. }
  439. else
  440. {
  441. *value = (char)ucs;
  442. *length = 1;
  443. }
  444. return p + delta + 1;
  445. }
  446. // Now try to match it.
  447. for( i=0; i<NUM_ENTITY; ++i )
  448. {
  449. if ( strncmp( entity[i].str, p, entity[i].strLength ) == 0 )
  450. {
  451. assert( strlen( entity[i].str ) == entity[i].strLength );
  452. *value = entity[i].chr;
  453. *length = 1;
  454. return ( p + entity[i].strLength );
  455. }
  456. }
  457. // So it wasn't an entity, its unrecognized, or something like that.
  458. *value = *p; // Don't put back the last one, since we return it!
  459. //*length = 1; // Leave unrecognized entities - this doesn't really work.
  460. // Just writes strange XML.
  461. return p+1;
  462. }
  463. bool TiXmlBase::StringEqual( const char* p,
  464. const char* tag,
  465. bool ignoreCase,
  466. TiXmlEncoding encoding )
  467. {
  468. assert( p );
  469. assert( tag );
  470. if ( !p || !*p )
  471. {
  472. assert( 0 );
  473. return false;
  474. }
  475. const char* q = p;
  476. if ( ignoreCase )
  477. {
  478. while ( *q && *tag && ToLower( *q, encoding ) == ToLower( *tag, encoding ) )
  479. {
  480. ++q;
  481. ++tag;
  482. }
  483. if ( *tag == 0 )
  484. return true;
  485. }
  486. else
  487. {
  488. while ( *q && *tag && *q == *tag )
  489. {
  490. ++q;
  491. ++tag;
  492. }
  493. if ( *tag == 0 ) // Have we found the end of the tag, and everything equal?
  494. return true;
  495. }
  496. return false;
  497. }
  498. const char* TiXmlBase::ReadText( const char* p,
  499. TIXML_STRING * text,
  500. bool trimWhiteSpace,
  501. const char* endTag,
  502. bool caseInsensitive,
  503. TiXmlEncoding encoding )
  504. {
  505. *text = "";
  506. if ( !trimWhiteSpace // certain tags always keep whitespace
  507. || !condenseWhiteSpace ) // if true, whitespace is always kept
  508. {
  509. // Keep all the white space.
  510. while ( p && *p
  511. && !StringEqual( p, endTag, caseInsensitive, encoding )
  512. )
  513. {
  514. int len;
  515. char cArr[4] = { 0, 0, 0, 0 };
  516. p = GetChar( p, cArr, &len, encoding );
  517. text->append( cArr, len );
  518. }
  519. }
  520. else
  521. {
  522. bool whitespace = false;
  523. // Remove leading white space:
  524. p = SkipWhiteSpace( p, encoding );
  525. while ( p && *p
  526. && !StringEqual( p, endTag, caseInsensitive, encoding ) )
  527. {
  528. if ( *p == '\r' || *p == '\n' )
  529. {
  530. whitespace = true;
  531. ++p;
  532. }
  533. else if ( IsWhiteSpace( *p ) )
  534. {
  535. whitespace = true;
  536. ++p;
  537. }
  538. else
  539. {
  540. // If we've found whitespace, add it before the
  541. // new character. Any whitespace just becomes a space.
  542. if ( whitespace )
  543. {
  544. (*text) += ' ';
  545. whitespace = false;
  546. }
  547. int len;
  548. char cArr[4] = { 0, 0, 0, 0 };
  549. p = GetChar( p, cArr, &len, encoding );
  550. if ( len == 1 )
  551. (*text) += cArr[0]; // more efficient
  552. else
  553. text->append( cArr, len );
  554. }
  555. }
  556. }
  557. if ( p && *p )
  558. p += strlen( endTag );
  559. return p;
  560. }
  561. #ifdef TIXML_USE_STL
  562. void TiXmlDocument::StreamIn( std::istream * in, TIXML_STRING * tag )
  563. {
  564. // The basic issue with a document is that we don't know what we're
  565. // streaming. Read something presumed to be a tag (and hope), then
  566. // identify it, and call the appropriate stream method on the tag.
  567. //
  568. // This "pre-streaming" will never read the closing ">" so the
  569. // sub-tag can orient itself.
  570. if ( !StreamTo( in, '<', tag ) )
  571. {
  572. SetError( TIXML_ERROR_PARSING_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
  573. return;
  574. }
  575. while ( in->good() )
  576. {
  577. int tagIndex = (int) tag->length();
  578. while ( in->good() && in->peek() != '>' )
  579. {
  580. int c = in->get();
  581. if ( c <= 0 )
  582. {
  583. SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  584. break;
  585. }
  586. (*tag) += (char) c;
  587. }
  588. if ( in->good() )
  589. {
  590. // We now have something we presume to be a node of
  591. // some sort. Identify it, and call the node to
  592. // continue streaming.
  593. TiXmlNode* node = Identify( tag->c_str() + tagIndex, TIXML_DEFAULT_ENCODING );
  594. if ( node )
  595. {
  596. node->StreamIn( in, tag );
  597. bool isElement = node->ToElement() != 0;
  598. delete node;
  599. node = 0;
  600. // If this is the root element, we're done. Parsing will be
  601. // done by the >> operator.
  602. if ( isElement )
  603. {
  604. return;
  605. }
  606. }
  607. else
  608. {
  609. SetError( TIXML_ERROR, 0, 0, TIXML_ENCODING_UNKNOWN );
  610. return;
  611. }
  612. }
  613. }
  614. // We should have returned sooner.
  615. SetError( TIXML_ERROR, 0, 0, TIXML_ENCODING_UNKNOWN );
  616. }
  617. #endif
  618. const char* TiXmlDocument::Parse( const char* p, TiXmlParsingData* prevData, TiXmlEncoding encoding )
  619. {
  620. ClearError();
  621. // Parse away, at the document level. Since a document
  622. // contains nothing but other tags, most of what happens
  623. // here is skipping white space.
  624. if ( !p || !*p )
  625. {
  626. SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
  627. return 0;
  628. }
  629. // Note that, for a document, this needs to come
  630. // before the while space skip, so that parsing
  631. // starts from the pointer we are given.
  632. location.Clear();
  633. if ( prevData )
  634. {
  635. location.row = prevData->cursor.row;
  636. location.col = prevData->cursor.col;
  637. }
  638. else
  639. {
  640. location.row = 0;
  641. location.col = 0;
  642. }
  643. TiXmlParsingData data( p, TabSize(), location.row, location.col );
  644. location = data.Cursor();
  645. if ( encoding == TIXML_ENCODING_UNKNOWN )
  646. {
  647. // Check for the Microsoft UTF-8 lead bytes.
  648. const unsigned char* pU = (const unsigned char*)p;
  649. if ( *(pU+0) && *(pU+0) == TIXML_UTF_LEAD_0
  650. && *(pU+1) && *(pU+1) == TIXML_UTF_LEAD_1
  651. && *(pU+2) && *(pU+2) == TIXML_UTF_LEAD_2 )
  652. {
  653. encoding = TIXML_ENCODING_UTF8;
  654. useMicrosoftBOM = true;
  655. }
  656. }
  657. p = SkipWhiteSpace( p, encoding );
  658. if ( !p )
  659. {
  660. SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
  661. return 0;
  662. }
  663. while ( p && *p )
  664. {
  665. TiXmlNode* node = Identify( p, encoding );
  666. if ( node )
  667. {
  668. p = node->Parse( p, &data, encoding );
  669. LinkEndChild( node );
  670. }
  671. else
  672. {
  673. break;
  674. }
  675. // Did we get encoding info?
  676. if ( encoding == TIXML_ENCODING_UNKNOWN
  677. && node->ToDeclaration() )
  678. {
  679. TiXmlDeclaration* dec = node->ToDeclaration();
  680. const char* enc = dec->Encoding();
  681. assert( enc );
  682. if ( *enc == 0 )
  683. encoding = TIXML_ENCODING_UTF8;
  684. else if ( StringEqual( enc, "UTF-8", true, TIXML_ENCODING_UNKNOWN ) )
  685. encoding = TIXML_ENCODING_UTF8;
  686. else if ( StringEqual( enc, "UTF8", true, TIXML_ENCODING_UNKNOWN ) )
  687. encoding = TIXML_ENCODING_UTF8; // incorrect, but be nice
  688. else
  689. encoding = TIXML_ENCODING_LEGACY;
  690. }
  691. p = SkipWhiteSpace( p, encoding );
  692. }
  693. // Was this empty?
  694. if ( !firstChild ) {
  695. SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, encoding );
  696. return 0;
  697. }
  698. // All is well.
  699. return p;
  700. }
  701. void TiXmlDocument::SetError( int err, const char* pError, TiXmlParsingData* data, TiXmlEncoding encoding )
  702. {
  703. // The first error in a chain is more accurate - don't set again!
  704. if ( error )
  705. return;
  706. assert( err > 0 && err < TIXML_ERROR_STRING_COUNT );
  707. error = true;
  708. errorId = err;
  709. errorDesc = errorString[ errorId ];
  710. errorLocation.Clear();
  711. if ( pError && data )
  712. {
  713. data->Stamp( pError, encoding );
  714. errorLocation = data->Cursor();
  715. }
  716. }
  717. TiXmlNode* TiXmlNode::Identify( const char* p, TiXmlEncoding encoding )
  718. {
  719. TiXmlNode* returnNode = 0;
  720. p = SkipWhiteSpace( p, encoding );
  721. if( !p || !*p || *p != '<' )
  722. {
  723. return 0;
  724. }
  725. p = SkipWhiteSpace( p, encoding );
  726. if ( !p || !*p )
  727. {
  728. return 0;
  729. }
  730. // What is this thing?
  731. // - Elements start with a letter or underscore, but xml is reserved.
  732. // - Comments: <!--
  733. // - Decleration: <?xml
  734. // - Everthing else is unknown to tinyxml.
  735. //
  736. const char* xmlHeader = { "<?xml" };
  737. const char* commentHeader = { "<!--" };
  738. const char* dtdHeader = { "<!" };
  739. const char* cdataHeader = { "<![CDATA[" };
  740. if ( StringEqual( p, xmlHeader, true, encoding ) )
  741. {
  742. #ifdef DEBUG_PARSER
  743. TIXML_LOG( "XML parsing Declaration\n" );
  744. #endif
  745. returnNode = new TiXmlDeclaration();
  746. }
  747. else if ( StringEqual( p, commentHeader, false, encoding ) )
  748. {
  749. #ifdef DEBUG_PARSER
  750. TIXML_LOG( "XML parsing Comment\n" );
  751. #endif
  752. returnNode = new TiXmlComment();
  753. }
  754. else if ( StringEqual( p, cdataHeader, false, encoding ) )
  755. {
  756. #ifdef DEBUG_PARSER
  757. TIXML_LOG( "XML parsing CDATA\n" );
  758. #endif
  759. TiXmlText* text = new TiXmlText( "" );
  760. text->SetCDATA( true );
  761. returnNode = text;
  762. }
  763. else if ( StringEqual( p, dtdHeader, false, encoding ) )
  764. {
  765. #ifdef DEBUG_PARSER
  766. TIXML_LOG( "XML parsing Unknown(1)\n" );
  767. #endif
  768. returnNode = new TiXmlUnknown();
  769. }
  770. else if ( IsAlpha( *(p+1), encoding )
  771. || *(p+1) == '_' )
  772. {
  773. #ifdef DEBUG_PARSER
  774. TIXML_LOG( "XML parsing Element\n" );
  775. #endif
  776. returnNode = new TiXmlElement( "" );
  777. }
  778. else
  779. {
  780. #ifdef DEBUG_PARSER
  781. TIXML_LOG( "XML parsing Unknown(2)\n" );
  782. #endif
  783. returnNode = new TiXmlUnknown();
  784. }
  785. if ( returnNode )
  786. {
  787. // Set the parent, so it can report errors
  788. returnNode->parent = this;
  789. }
  790. return returnNode;
  791. }
  792. #ifdef TIXML_USE_STL
  793. void TiXmlElement::StreamIn (std::istream * in, TIXML_STRING * tag)
  794. {
  795. // We're called with some amount of pre-parsing. That is, some of "this"
  796. // element is in "tag". Go ahead and stream to the closing ">"
  797. while( in->good() )
  798. {
  799. int c = in->get();
  800. if ( c <= 0 )
  801. {
  802. TiXmlDocument* document = GetDocument();
  803. if ( document )
  804. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  805. return;
  806. }
  807. (*tag) += (char) c ;
  808. if ( c == '>' )
  809. break;
  810. }
  811. if ( tag->length() < 3 ) return;
  812. // Okay...if we are a "/>" tag, then we're done. We've read a complete tag.
  813. // If not, identify and stream.
  814. if ( tag->at( tag->length() - 1 ) == '>'
  815. && tag->at( tag->length() - 2 ) == '/' )
  816. {
  817. // All good!
  818. return;
  819. }
  820. else if ( tag->at( tag->length() - 1 ) == '>' )
  821. {
  822. // There is more. Could be:
  823. // text
  824. // cdata text (which looks like another node)
  825. // closing tag
  826. // another node.
  827. for ( ;; )
  828. {
  829. StreamWhiteSpace( in, tag );
  830. // Do we have text?
  831. if ( in->good() && in->peek() != '<' )
  832. {
  833. // Yep, text.
  834. TiXmlText text( "" );
  835. text.StreamIn( in, tag );
  836. // What follows text is a closing tag or another node.
  837. // Go around again and figure it out.
  838. continue;
  839. }
  840. // We now have either a closing tag...or another node.
  841. // We should be at a "<", regardless.
  842. if ( !in->good() ) return;
  843. assert( in->peek() == '<' );
  844. int tagIndex = (int) tag->length();
  845. bool closingTag = false;
  846. bool firstCharFound = false;
  847. for( ;; )
  848. {
  849. if ( !in->good() )
  850. return;
  851. int c = in->peek();
  852. if ( c <= 0 )
  853. {
  854. TiXmlDocument* document = GetDocument();
  855. if ( document )
  856. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  857. return;
  858. }
  859. if ( c == '>' )
  860. break;
  861. *tag += (char) c;
  862. in->get();
  863. // Early out if we find the CDATA id.
  864. if ( c == '[' && tag->size() >= 9 )
  865. {
  866. size_t len = tag->size();
  867. const char* start = tag->c_str() + len - 9;
  868. if ( strcmp( start, "<![CDATA[" ) == 0 ) {
  869. assert( !closingTag );
  870. break;
  871. }
  872. }
  873. if ( !firstCharFound && c != '<' && !IsWhiteSpace( c ) )
  874. {
  875. firstCharFound = true;
  876. if ( c == '/' )
  877. closingTag = true;
  878. }
  879. }
  880. // If it was a closing tag, then read in the closing '>' to clean up the input stream.
  881. // If it was not, the streaming will be done by the tag.
  882. if ( closingTag )
  883. {
  884. if ( !in->good() )
  885. return;
  886. int c = in->get();
  887. if ( c <= 0 )
  888. {
  889. TiXmlDocument* document = GetDocument();
  890. if ( document )
  891. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  892. return;
  893. }
  894. assert( c == '>' );
  895. *tag += (char) c;
  896. // We are done, once we've found our closing tag.
  897. return;
  898. }
  899. else
  900. {
  901. // If not a closing tag, id it, and stream.
  902. const char* tagloc = tag->c_str() + tagIndex;
  903. TiXmlNode* node = Identify( tagloc, TIXML_DEFAULT_ENCODING );
  904. if ( !node )
  905. return;
  906. node->StreamIn( in, tag );
  907. delete node;
  908. node = 0;
  909. // No return: go around from the beginning: text, closing tag, or node.
  910. }
  911. }
  912. }
  913. }
  914. #endif
  915. const char* TiXmlElement::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  916. {
  917. p = SkipWhiteSpace( p, encoding );
  918. TiXmlDocument* document = GetDocument();
  919. if ( !p || !*p )
  920. {
  921. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, 0, 0, encoding );
  922. return 0;
  923. }
  924. if ( data )
  925. {
  926. data->Stamp( p, encoding );
  927. location = data->Cursor();
  928. }
  929. if ( *p != '<' )
  930. {
  931. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, p, data, encoding );
  932. return 0;
  933. }
  934. p = SkipWhiteSpace( p+1, encoding );
  935. // Read the name.
  936. const char* pErr = p;
  937. p = ReadName( p, &value, encoding );
  938. if ( !p || !*p )
  939. {
  940. if ( document ) document->SetError( TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME, pErr, data, encoding );
  941. return 0;
  942. }
  943. TIXML_STRING endTag ("</");
  944. endTag += value;
  945. // Check for and read attributes. Also look for an empty
  946. // tag or an end tag.
  947. while ( p && *p )
  948. {
  949. pErr = p;
  950. p = SkipWhiteSpace( p, encoding );
  951. if ( !p || !*p )
  952. {
  953. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
  954. return 0;
  955. }
  956. if ( *p == '/' )
  957. {
  958. ++p;
  959. // Empty tag.
  960. if ( *p != '>' )
  961. {
  962. if ( document ) document->SetError( TIXML_ERROR_PARSING_EMPTY, p, data, encoding );
  963. return 0;
  964. }
  965. return (p+1);
  966. }
  967. else if ( *p == '>' )
  968. {
  969. // Done with attributes (if there were any.)
  970. // Read the value -- which can include other
  971. // elements -- read the end tag, and return.
  972. ++p;
  973. p = ReadValue( p, data, encoding ); // Note this is an Element method, and will set the error if one happens.
  974. if ( !p || !*p ) {
  975. // We were looking for the end tag, but found nothing.
  976. // Fix for [ 1663758 ] Failure to report error on bad XML
  977. if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding );
  978. return 0;
  979. }
  980. // We should find the end tag now
  981. // note that:
  982. // </foo > and
  983. // </foo>
  984. // are both valid end tags.
  985. if ( StringEqual( p, endTag.c_str(), false, encoding ) )
  986. {
  987. p += endTag.length();
  988. p = SkipWhiteSpace( p, encoding );
  989. if ( p && *p && *p == '>' ) {
  990. ++p;
  991. return p;
  992. }
  993. if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding );
  994. return 0;
  995. }
  996. else
  997. {
  998. if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding );
  999. return 0;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. // Try to read an attribute:
  1005. TiXmlAttribute* attrib = new TiXmlAttribute();
  1006. if ( !attrib )
  1007. {
  1008. return 0;
  1009. }
  1010. attrib->SetDocument( document );
  1011. pErr = p;
  1012. p = attrib->Parse( p, data, encoding );
  1013. if ( !p || !*p )
  1014. {
  1015. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, pErr, data, encoding );
  1016. delete attrib;
  1017. return 0;
  1018. }
  1019. // Handle the strange case of double attributes:
  1020. #ifdef TIXML_USE_STL
  1021. TiXmlAttribute* node = attributeSet.Find( attrib->NameTStr() );
  1022. #else
  1023. TiXmlAttribute* node = attributeSet.Find( attrib->Name() );
  1024. #endif
  1025. if ( node )
  1026. {
  1027. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, pErr, data, encoding );
  1028. delete attrib;
  1029. return 0;
  1030. }
  1031. attributeSet.Add( attrib );
  1032. }
  1033. }
  1034. return p;
  1035. }
  1036. const char* TiXmlElement::ReadValue( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1037. {
  1038. TiXmlDocument* document = GetDocument();
  1039. // Read in text and elements in any order.
  1040. const char* pWithWhiteSpace = p;
  1041. p = SkipWhiteSpace( p, encoding );
  1042. while ( p && *p )
  1043. {
  1044. if ( *p != '<' )
  1045. {
  1046. // Take what we have, make a text element.
  1047. TiXmlText* textNode = new TiXmlText( "" );
  1048. if ( !textNode )
  1049. {
  1050. return 0;
  1051. }
  1052. if ( TiXmlBase::IsWhiteSpaceCondensed() )
  1053. {
  1054. p = textNode->Parse( p, data, encoding );
  1055. }
  1056. else
  1057. {
  1058. // Special case: we want to keep the white space
  1059. // so that leading spaces aren't removed.
  1060. p = textNode->Parse( pWithWhiteSpace, data, encoding );
  1061. }
  1062. if ( !textNode->Blank() )
  1063. LinkEndChild( textNode );
  1064. else
  1065. delete textNode;
  1066. }
  1067. else
  1068. {
  1069. // We hit a '<'
  1070. // Have we hit a new element or an end tag? This could also be
  1071. // a TiXmlText in the "CDATA" style.
  1072. if ( StringEqual( p, "</", false, encoding ) )
  1073. {
  1074. return p;
  1075. }
  1076. else
  1077. {
  1078. TiXmlNode* node = Identify( p, encoding );
  1079. if ( node )
  1080. {
  1081. p = node->Parse( p, data, encoding );
  1082. LinkEndChild( node );
  1083. }
  1084. else
  1085. {
  1086. return 0;
  1087. }
  1088. }
  1089. }
  1090. pWithWhiteSpace = p;
  1091. p = SkipWhiteSpace( p, encoding );
  1092. }
  1093. if ( !p )
  1094. {
  1095. if ( document ) document->SetError( TIXML_ERROR_READING_ELEMENT_VALUE, 0, 0, encoding );
  1096. }
  1097. return p;
  1098. }
  1099. #ifdef TIXML_USE_STL
  1100. void TiXmlUnknown::StreamIn( std::istream * in, TIXML_STRING * tag )
  1101. {
  1102. while ( in->good() )
  1103. {
  1104. int c = in->get();
  1105. if ( c <= 0 )
  1106. {
  1107. TiXmlDocument* document = GetDocument();
  1108. if ( document )
  1109. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1110. return;
  1111. }
  1112. (*tag) += (char) c;
  1113. if ( c == '>' )
  1114. {
  1115. // All is well.
  1116. return;
  1117. }
  1118. }
  1119. }
  1120. #endif
  1121. const char* TiXmlUnknown::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1122. {
  1123. TiXmlDocument* document = GetDocument();
  1124. p = SkipWhiteSpace( p, encoding );
  1125. if ( data )
  1126. {
  1127. data->Stamp( p, encoding );
  1128. location = data->Cursor();
  1129. }
  1130. if ( !p || !*p || *p != '<' )
  1131. {
  1132. if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, p, data, encoding );
  1133. return 0;
  1134. }
  1135. ++p;
  1136. value = "";
  1137. while ( p && *p && *p != '>' )
  1138. {
  1139. value += *p;
  1140. ++p;
  1141. }
  1142. if ( !p )
  1143. {
  1144. if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, 0, 0, encoding );
  1145. }
  1146. if ( *p == '>' )
  1147. return p+1;
  1148. return p;
  1149. }
  1150. #ifdef TIXML_USE_STL
  1151. void TiXmlComment::StreamIn( std::istream * in, TIXML_STRING * tag )
  1152. {
  1153. while ( in->good() )
  1154. {
  1155. int c = in->get();
  1156. if ( c <= 0 )
  1157. {
  1158. TiXmlDocument* document = GetDocument();
  1159. if ( document )
  1160. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1161. return;
  1162. }
  1163. (*tag) += (char) c;
  1164. if ( c == '>'
  1165. && tag->at( tag->length() - 2 ) == '-'
  1166. && tag->at( tag->length() - 3 ) == '-' )
  1167. {
  1168. // All is well.
  1169. return;
  1170. }
  1171. }
  1172. }
  1173. #endif
  1174. const char* TiXmlComment::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1175. {
  1176. TiXmlDocument* document = GetDocument();
  1177. value = "";
  1178. p = SkipWhiteSpace( p, encoding );
  1179. if ( data )
  1180. {
  1181. data->Stamp( p, encoding );
  1182. location = data->Cursor();
  1183. }
  1184. const char* startTag = "<!--";
  1185. const char* endTag = "-->";
  1186. if ( !StringEqual( p, startTag, false, encoding ) )
  1187. {
  1188. document->SetError( TIXML_ERROR_PARSING_COMMENT, p, data, encoding );
  1189. return 0;
  1190. }
  1191. p += strlen( startTag );
  1192. // [ 1475201 ] TinyXML parses entities in comments
  1193. // Oops - ReadText doesn't work, because we don't want to parse the entities.
  1194. // p = ReadText( p, &value, false, endTag, false, encoding );
  1195. //
  1196. // from the XML spec:
  1197. /*
  1198. [Definition: Comments may appear anywhere in a document outside other markup; in addition,
  1199. they may appear within the document type declaration at places allowed by the grammar.
  1200. They are not part of the document's character data; an XML processor MAY, but need not,
  1201. make it possible for an application to retrieve the text of comments. For compatibility,
  1202. the string "--" (double-hyphen) MUST NOT occur within comments.] Parameter entity
  1203. references MUST NOT be recognized within comments.
  1204. An example of a comment:
  1205. <!-- declarations for <head> & <body> -->
  1206. */
  1207. value = "";
  1208. // Keep all the white space.
  1209. while ( p && *p && !StringEqual( p, endTag, false, encoding ) )
  1210. {
  1211. value.append( p, 1 );
  1212. ++p;
  1213. }
  1214. if ( p && *p )
  1215. p += strlen( endTag );
  1216. return p;
  1217. }
  1218. const char* TiXmlAttribute::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1219. {
  1220. p = SkipWhiteSpace( p, encoding );
  1221. if ( !p || !*p ) return 0;
  1222. if ( data )
  1223. {
  1224. data->Stamp( p, encoding );
  1225. location = data->Cursor();
  1226. }
  1227. // Read the name, the '=' and the value.
  1228. const char* pErr = p;
  1229. p = ReadName( p, &name, encoding );
  1230. if ( !p || !*p )
  1231. {
  1232. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
  1233. return 0;
  1234. }
  1235. p = SkipWhiteSpace( p, encoding );
  1236. if ( !p || !*p || *p != '=' )
  1237. {
  1238. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1239. return 0;
  1240. }
  1241. ++p; // skip '='
  1242. p = SkipWhiteSpace( p, encoding );
  1243. if ( !p || !*p )
  1244. {
  1245. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1246. return 0;
  1247. }
  1248. const char* end;
  1249. const char SINGLE_QUOTE = '\'';
  1250. const char DOUBLE_QUOTE = '\"';
  1251. if ( *p == SINGLE_QUOTE )
  1252. {
  1253. ++p;
  1254. end = "\'"; // single quote in string
  1255. p = ReadText( p, &value, false, end, false, encoding );
  1256. }
  1257. else if ( *p == DOUBLE_QUOTE )
  1258. {
  1259. ++p;
  1260. end = "\""; // double quote in string
  1261. p = ReadText( p, &value, false, end, false, encoding );
  1262. }
  1263. else
  1264. {
  1265. // All attribute values should be in single or double quotes.
  1266. // But this is such a common error that the parser will try
  1267. // its best, even without them.
  1268. value = "";
  1269. while ( p && *p // existence
  1270. && !IsWhiteSpace( *p ) // whitespace
  1271. && *p != '/' && *p != '>' ) // tag end
  1272. {
  1273. if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
  1274. // [ 1451649 ] Attribute values with trailing quotes not handled correctly
  1275. // We did not have an opening quote but seem to have a
  1276. // closing one. Give up and throw an error.
  1277. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1278. return 0;
  1279. }
  1280. value += *p;
  1281. ++p;
  1282. }
  1283. }
  1284. return p;
  1285. }
  1286. #ifdef TIXML_USE_STL
  1287. void TiXmlText::StreamIn( std::istream * in, TIXML_STRING * tag )
  1288. {
  1289. while ( in->good() )
  1290. {
  1291. int c = in->peek();
  1292. if ( !cdata && (c == '<' ) )
  1293. {
  1294. return;
  1295. }
  1296. if ( c <= 0 )
  1297. {
  1298. TiXmlDocument* document = GetDocument();
  1299. if ( document )
  1300. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1301. return;
  1302. }
  1303. (*tag) += (char) c;
  1304. in->get(); // "commits" the peek made above
  1305. if ( cdata && c == '>' && tag->size() >= 3 ) {
  1306. size_t len = tag->size();
  1307. if ( (*tag)[len-2] == ']' && (*tag)[len-3] == ']' ) {
  1308. // terminator of cdata.
  1309. return;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. #endif
  1315. const char* TiXmlText::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1316. {
  1317. value = "";
  1318. TiXmlDocument* document = GetDocument();
  1319. if ( data )
  1320. {
  1321. data->Stamp( p, encoding );
  1322. location = data->Cursor();
  1323. }
  1324. const char* const startTag = "<![CDATA[";
  1325. const char* const endTag = "]]>";
  1326. if ( cdata || StringEqual( p, startTag, false, encoding ) )
  1327. {
  1328. cdata = true;
  1329. if ( !StringEqual( p, startTag, false, encoding ) )
  1330. {
  1331. document->SetError( TIXML_ERROR_PARSING_CDATA, p, data, encoding );
  1332. return 0;
  1333. }
  1334. p += strlen( startTag );
  1335. // Keep all the white space, ignore the encoding, etc.
  1336. while ( p && *p
  1337. && !StringEqual( p, endTag, false, encoding )
  1338. )
  1339. {
  1340. value += *p;
  1341. ++p;
  1342. }
  1343. TIXML_STRING dummy;
  1344. p = ReadText( p, &dummy, false, endTag, false, encoding );
  1345. return p;
  1346. }
  1347. else
  1348. {
  1349. bool ignoreWhite = true;
  1350. const char* end = "<";
  1351. p = ReadText( p, &value, ignoreWhite, end, false, encoding );
  1352. if ( p )
  1353. return p-1; // don't truncate the '<'
  1354. return 0;
  1355. }
  1356. }
  1357. #ifdef TIXML_USE_STL
  1358. void TiXmlDeclaration::StreamIn( std::istream * in, TIXML_STRING * tag )
  1359. {
  1360. while ( in->good() )
  1361. {
  1362. int c = in->get();
  1363. if ( c <= 0 )
  1364. {
  1365. TiXmlDocument* document = GetDocument();
  1366. if ( document )
  1367. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1368. return;
  1369. }
  1370. (*tag) += (char) c;
  1371. if ( c == '>' )
  1372. {
  1373. // All is well.
  1374. return;
  1375. }
  1376. }
  1377. }
  1378. #endif
  1379. const char* TiXmlDeclaration::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding _encoding )
  1380. {
  1381. p = SkipWhiteSpace( p, _encoding );
  1382. // Find the beginning, find the end, and look for
  1383. // the stuff in-between.
  1384. TiXmlDocument* document = GetDocument();
  1385. if ( !p || !*p || !StringEqual( p, "<?xml", true, _encoding ) )
  1386. {
  1387. if ( document ) document->SetError( TIXML_ERROR_PARSING_DECLARATION, 0, 0, _encoding );
  1388. return 0;
  1389. }
  1390. if ( data )
  1391. {
  1392. data->Stamp( p, _encoding );
  1393. location = data->Cursor();
  1394. }
  1395. p += 5;
  1396. version = "";
  1397. encoding = "";
  1398. standalone = "";
  1399. while ( p && *p )
  1400. {
  1401. if ( *p == '>' )
  1402. {
  1403. ++p;
  1404. return p;
  1405. }
  1406. p = SkipWhiteSpace( p, _encoding );
  1407. if ( StringEqual( p, "version", true, _encoding ) )
  1408. {
  1409. TiXmlAttribute attrib;
  1410. p = attrib.Parse( p, data, _encoding );
  1411. version = attrib.Value();
  1412. }
  1413. else if ( StringEqual( p, "encoding", true, _encoding ) )
  1414. {
  1415. TiXmlAttribute attrib;
  1416. p = attrib.Parse( p, data, _encoding );
  1417. encoding = attrib.Value();
  1418. }
  1419. else if ( StringEqual( p, "standalone", true, _encoding ) )
  1420. {
  1421. TiXmlAttribute attrib;
  1422. p = attrib.Parse( p, data, _encoding );
  1423. standalone = attrib.Value();
  1424. }
  1425. else
  1426. {
  1427. // Read over whatever it is.
  1428. while( p && *p && *p != '>' && !IsWhiteSpace( *p ) )
  1429. ++p;
  1430. }
  1431. }
  1432. return 0;
  1433. }
  1434. bool TiXmlText::Blank() const
  1435. {
  1436. for ( unsigned i=0; i<value.length(); i++ )
  1437. if ( !IsWhiteSpace( value[i] ) )
  1438. return false;
  1439. return true;
  1440. }