tinyxmlparser.cpp 36 KB

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