tinyxmlparser.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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 ) || *p == '\n' || *p =='\r' ) // Still using old rules for white space.
  302. ++p;
  303. else
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. while ( *p && (IsWhiteSpace( *p ) || (*p == '\n') || (*p =='\r')))
  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 )
  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. TiXmlDocument* doc = GetDocument();
  726. p = SkipWhiteSpace( p, encoding );
  727. if ( !p || !*p )
  728. {
  729. return 0;
  730. }
  731. // What is this thing?
  732. // - Elements start with a letter or underscore, but xml is reserved.
  733. // - Comments: <!--
  734. // - Decleration: <?xml
  735. // - Everthing else is unknown to tinyxml.
  736. //
  737. const char* xmlHeader = { "<?xml" };
  738. const char* commentHeader = { "<!--" };
  739. const char* dtdHeader = { "<!" };
  740. const char* cdataHeader = { "<![CDATA[" };
  741. if ( StringEqual( p, xmlHeader, true, encoding ) )
  742. {
  743. #ifdef DEBUG_PARSER
  744. TIXML_LOG( "XML parsing Declaration\n" );
  745. #endif
  746. returnNode = new TiXmlDeclaration();
  747. }
  748. else if ( StringEqual( p, commentHeader, false, encoding ) )
  749. {
  750. #ifdef DEBUG_PARSER
  751. TIXML_LOG( "XML parsing Comment\n" );
  752. #endif
  753. returnNode = new TiXmlComment();
  754. }
  755. else if ( StringEqual( p, cdataHeader, false, encoding ) )
  756. {
  757. #ifdef DEBUG_PARSER
  758. TIXML_LOG( "XML parsing CDATA\n" );
  759. #endif
  760. TiXmlText* text = new TiXmlText( "" );
  761. text->SetCDATA( true );
  762. returnNode = text;
  763. }
  764. else if ( StringEqual( p, dtdHeader, false, encoding ) )
  765. {
  766. #ifdef DEBUG_PARSER
  767. TIXML_LOG( "XML parsing Unknown(1)\n" );
  768. #endif
  769. returnNode = new TiXmlUnknown();
  770. }
  771. else if ( IsAlpha( *(p+1), encoding )
  772. || *(p+1) == '_' )
  773. {
  774. #ifdef DEBUG_PARSER
  775. TIXML_LOG( "XML parsing Element\n" );
  776. #endif
  777. returnNode = new TiXmlElement( "" );
  778. }
  779. else
  780. {
  781. #ifdef DEBUG_PARSER
  782. TIXML_LOG( "XML parsing Unknown(2)\n" );
  783. #endif
  784. returnNode = new TiXmlUnknown();
  785. }
  786. if ( returnNode )
  787. {
  788. // Set the parent, so it can report errors
  789. returnNode->parent = this;
  790. }
  791. else
  792. {
  793. if ( doc )
  794. doc->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, TIXML_ENCODING_UNKNOWN );
  795. }
  796. return returnNode;
  797. }
  798. #ifdef TIXML_USE_STL
  799. void TiXmlElement::StreamIn (std::istream * in, TIXML_STRING * tag)
  800. {
  801. // We're called with some amount of pre-parsing. That is, some of "this"
  802. // element is in "tag". Go ahead and stream to the closing ">"
  803. while( in->good() )
  804. {
  805. int c = in->get();
  806. if ( c <= 0 )
  807. {
  808. TiXmlDocument* document = GetDocument();
  809. if ( document )
  810. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  811. return;
  812. }
  813. (*tag) += (char) c ;
  814. if ( c == '>' )
  815. break;
  816. }
  817. if ( tag->length() < 3 ) return;
  818. // Okay...if we are a "/>" tag, then we're done. We've read a complete tag.
  819. // If not, identify and stream.
  820. if ( tag->at( tag->length() - 1 ) == '>'
  821. && tag->at( tag->length() - 2 ) == '/' )
  822. {
  823. // All good!
  824. return;
  825. }
  826. else if ( tag->at( tag->length() - 1 ) == '>' )
  827. {
  828. // There is more. Could be:
  829. // text
  830. // cdata text (which looks like another node)
  831. // closing tag
  832. // another node.
  833. for ( ;; )
  834. {
  835. StreamWhiteSpace( in, tag );
  836. // Do we have text?
  837. if ( in->good() && in->peek() != '<' )
  838. {
  839. // Yep, text.
  840. TiXmlText text( "" );
  841. text.StreamIn( in, tag );
  842. // What follows text is a closing tag or another node.
  843. // Go around again and figure it out.
  844. continue;
  845. }
  846. // We now have either a closing tag...or another node.
  847. // We should be at a "<", regardless.
  848. if ( !in->good() ) return;
  849. assert( in->peek() == '<' );
  850. int tagIndex = (int) tag->length();
  851. bool closingTag = false;
  852. bool firstCharFound = false;
  853. for( ;; )
  854. {
  855. if ( !in->good() )
  856. return;
  857. int c = in->peek();
  858. if ( c <= 0 )
  859. {
  860. TiXmlDocument* document = GetDocument();
  861. if ( document )
  862. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  863. return;
  864. }
  865. if ( c == '>' )
  866. break;
  867. *tag += (char) c;
  868. in->get();
  869. // Early out if we find the CDATA id.
  870. if ( c == '[' && tag->size() >= 9 )
  871. {
  872. size_t len = tag->size();
  873. const char* start = tag->c_str() + len - 9;
  874. if ( strcmp( start, "<![CDATA[" ) == 0 ) {
  875. assert( !closingTag );
  876. break;
  877. }
  878. }
  879. if ( !firstCharFound && c != '<' && !IsWhiteSpace( c ) )
  880. {
  881. firstCharFound = true;
  882. if ( c == '/' )
  883. closingTag = true;
  884. }
  885. }
  886. // If it was a closing tag, then read in the closing '>' to clean up the input stream.
  887. // If it was not, the streaming will be done by the tag.
  888. if ( closingTag )
  889. {
  890. if ( !in->good() )
  891. return;
  892. int c = in->get();
  893. if ( c <= 0 )
  894. {
  895. TiXmlDocument* document = GetDocument();
  896. if ( document )
  897. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  898. return;
  899. }
  900. assert( c == '>' );
  901. *tag += (char) c;
  902. // We are done, once we've found our closing tag.
  903. return;
  904. }
  905. else
  906. {
  907. // If not a closing tag, id it, and stream.
  908. const char* tagloc = tag->c_str() + tagIndex;
  909. TiXmlNode* node = Identify( tagloc, TIXML_DEFAULT_ENCODING );
  910. if ( !node )
  911. return;
  912. node->StreamIn( in, tag );
  913. delete node;
  914. node = 0;
  915. // No return: go around from the beginning: text, closing tag, or node.
  916. }
  917. }
  918. }
  919. }
  920. #endif
  921. const char* TiXmlElement::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  922. {
  923. p = SkipWhiteSpace( p, encoding );
  924. TiXmlDocument* document = GetDocument();
  925. if ( !p || !*p )
  926. {
  927. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, 0, 0, encoding );
  928. return 0;
  929. }
  930. if ( data )
  931. {
  932. data->Stamp( p, encoding );
  933. location = data->Cursor();
  934. }
  935. if ( *p != '<' )
  936. {
  937. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, p, data, encoding );
  938. return 0;
  939. }
  940. p = SkipWhiteSpace( p+1, encoding );
  941. // Read the name.
  942. const char* pErr = p;
  943. p = ReadName( p, &value, encoding );
  944. if ( !p || !*p )
  945. {
  946. if ( document ) document->SetError( TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME, pErr, data, encoding );
  947. return 0;
  948. }
  949. TIXML_STRING endTag ("</");
  950. endTag += value;
  951. endTag += ">";
  952. // Check for and read attributes. Also look for an empty
  953. // tag or an end tag.
  954. while ( p && *p )
  955. {
  956. pErr = p;
  957. p = SkipWhiteSpace( p, encoding );
  958. if ( !p || !*p )
  959. {
  960. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
  961. return 0;
  962. }
  963. if ( *p == '/' )
  964. {
  965. ++p;
  966. // Empty tag.
  967. if ( *p != '>' )
  968. {
  969. if ( document ) document->SetError( TIXML_ERROR_PARSING_EMPTY, p, data, encoding );
  970. return 0;
  971. }
  972. return (p+1);
  973. }
  974. else if ( *p == '>' )
  975. {
  976. // Done with attributes (if there were any.)
  977. // Read the value -- which can include other
  978. // elements -- read the end tag, and return.
  979. ++p;
  980. p = ReadValue( p, data, encoding ); // Note this is an Element method, and will set the error if one happens.
  981. if ( !p || !*p )
  982. return 0;
  983. // We should find the end tag now
  984. if ( StringEqual( p, endTag.c_str(), false, encoding ) )
  985. {
  986. p += endTag.length();
  987. return p;
  988. }
  989. else
  990. {
  991. if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding );
  992. return 0;
  993. }
  994. }
  995. else
  996. {
  997. // Try to read an attribute:
  998. TiXmlAttribute* attrib = new TiXmlAttribute();
  999. if ( !attrib )
  1000. {
  1001. if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, pErr, data, encoding );
  1002. return 0;
  1003. }
  1004. attrib->SetDocument( document );
  1005. pErr = p;
  1006. p = attrib->Parse( p, data, encoding );
  1007. if ( !p || !*p )
  1008. {
  1009. if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, pErr, data, encoding );
  1010. delete attrib;
  1011. return 0;
  1012. }
  1013. // Handle the strange case of double attributes:
  1014. #ifdef TIXML_USE_STL
  1015. TiXmlAttribute* node = attributeSet.Find( attrib->NameTStr() );
  1016. #else
  1017. TiXmlAttribute* node = attributeSet.Find( attrib->Name() );
  1018. #endif
  1019. if ( node )
  1020. {
  1021. node->SetValue( attrib->Value() );
  1022. delete attrib;
  1023. return 0;
  1024. }
  1025. attributeSet.Add( attrib );
  1026. }
  1027. }
  1028. return p;
  1029. }
  1030. const char* TiXmlElement::ReadValue( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1031. {
  1032. TiXmlDocument* document = GetDocument();
  1033. // Read in text and elements in any order.
  1034. const char* pWithWhiteSpace = p;
  1035. p = SkipWhiteSpace( p, encoding );
  1036. while ( p && *p )
  1037. {
  1038. if ( *p != '<' )
  1039. {
  1040. // Take what we have, make a text element.
  1041. TiXmlText* textNode = new TiXmlText( "" );
  1042. if ( !textNode )
  1043. {
  1044. if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, encoding );
  1045. return 0;
  1046. }
  1047. if ( TiXmlBase::IsWhiteSpaceCondensed() )
  1048. {
  1049. p = textNode->Parse( p, data, encoding );
  1050. }
  1051. else
  1052. {
  1053. // Special case: we want to keep the white space
  1054. // so that leading spaces aren't removed.
  1055. p = textNode->Parse( pWithWhiteSpace, data, encoding );
  1056. }
  1057. if ( !textNode->Blank() )
  1058. LinkEndChild( textNode );
  1059. else
  1060. delete textNode;
  1061. }
  1062. else
  1063. {
  1064. // We hit a '<'
  1065. // Have we hit a new element or an end tag? This could also be
  1066. // a TiXmlText in the "CDATA" style.
  1067. if ( StringEqual( p, "</", false, encoding ) )
  1068. {
  1069. return p;
  1070. }
  1071. else
  1072. {
  1073. TiXmlNode* node = Identify( p, encoding );
  1074. if ( node )
  1075. {
  1076. p = node->Parse( p, data, encoding );
  1077. LinkEndChild( node );
  1078. }
  1079. else
  1080. {
  1081. return 0;
  1082. }
  1083. }
  1084. }
  1085. pWithWhiteSpace = p;
  1086. p = SkipWhiteSpace( p, encoding );
  1087. }
  1088. if ( !p )
  1089. {
  1090. if ( document ) document->SetError( TIXML_ERROR_READING_ELEMENT_VALUE, 0, 0, encoding );
  1091. }
  1092. return p;
  1093. }
  1094. #ifdef TIXML_USE_STL
  1095. void TiXmlUnknown::StreamIn( std::istream * in, TIXML_STRING * tag )
  1096. {
  1097. while ( in->good() )
  1098. {
  1099. int c = in->get();
  1100. if ( c <= 0 )
  1101. {
  1102. TiXmlDocument* document = GetDocument();
  1103. if ( document )
  1104. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1105. return;
  1106. }
  1107. (*tag) += (char) c;
  1108. if ( c == '>' )
  1109. {
  1110. // All is well.
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. #endif
  1116. const char* TiXmlUnknown::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1117. {
  1118. TiXmlDocument* document = GetDocument();
  1119. p = SkipWhiteSpace( p, encoding );
  1120. if ( data )
  1121. {
  1122. data->Stamp( p, encoding );
  1123. location = data->Cursor();
  1124. }
  1125. if ( !p || !*p || *p != '<' )
  1126. {
  1127. if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, p, data, encoding );
  1128. return 0;
  1129. }
  1130. ++p;
  1131. value = "";
  1132. while ( p && *p && *p != '>' )
  1133. {
  1134. value += *p;
  1135. ++p;
  1136. }
  1137. if ( !p )
  1138. {
  1139. if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, 0, 0, encoding );
  1140. }
  1141. if ( *p == '>' )
  1142. return p+1;
  1143. return p;
  1144. }
  1145. #ifdef TIXML_USE_STL
  1146. void TiXmlComment::StreamIn( std::istream * in, TIXML_STRING * tag )
  1147. {
  1148. while ( in->good() )
  1149. {
  1150. int c = in->get();
  1151. if ( c <= 0 )
  1152. {
  1153. TiXmlDocument* document = GetDocument();
  1154. if ( document )
  1155. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1156. return;
  1157. }
  1158. (*tag) += (char) c;
  1159. if ( c == '>'
  1160. && tag->at( tag->length() - 2 ) == '-'
  1161. && tag->at( tag->length() - 3 ) == '-' )
  1162. {
  1163. // All is well.
  1164. return;
  1165. }
  1166. }
  1167. }
  1168. #endif
  1169. const char* TiXmlComment::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1170. {
  1171. TiXmlDocument* document = GetDocument();
  1172. value = "";
  1173. p = SkipWhiteSpace( p, encoding );
  1174. if ( data )
  1175. {
  1176. data->Stamp( p, encoding );
  1177. location = data->Cursor();
  1178. }
  1179. const char* startTag = "<!--";
  1180. const char* endTag = "-->";
  1181. if ( !StringEqual( p, startTag, false, encoding ) )
  1182. {
  1183. document->SetError( TIXML_ERROR_PARSING_COMMENT, p, data, encoding );
  1184. return 0;
  1185. }
  1186. p += strlen( startTag );
  1187. p = ReadText( p, &value, false, endTag, false, encoding );
  1188. return p;
  1189. }
  1190. const char* TiXmlAttribute::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1191. {
  1192. p = SkipWhiteSpace( p, encoding );
  1193. if ( !p || !*p ) return 0;
  1194. // int tabsize = 4;
  1195. // if ( document )
  1196. // tabsize = document->TabSize();
  1197. if ( data )
  1198. {
  1199. data->Stamp( p, encoding );
  1200. location = data->Cursor();
  1201. }
  1202. // Read the name, the '=' and the value.
  1203. const char* pErr = p;
  1204. p = ReadName( p, &name, encoding );
  1205. if ( !p || !*p )
  1206. {
  1207. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
  1208. return 0;
  1209. }
  1210. p = SkipWhiteSpace( p, encoding );
  1211. if ( !p || !*p || *p != '=' )
  1212. {
  1213. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1214. return 0;
  1215. }
  1216. ++p; // skip '='
  1217. p = SkipWhiteSpace( p, encoding );
  1218. if ( !p || !*p )
  1219. {
  1220. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1221. return 0;
  1222. }
  1223. const char* end;
  1224. const char SINGLE_QUOTE = '\'';
  1225. const char DOUBLE_QUOTE = '\"';
  1226. if ( *p == SINGLE_QUOTE )
  1227. {
  1228. ++p;
  1229. end = "\'"; // single quote in string
  1230. p = ReadText( p, &value, false, end, false, encoding );
  1231. }
  1232. else if ( *p == DOUBLE_QUOTE )
  1233. {
  1234. ++p;
  1235. end = "\""; // double quote in string
  1236. p = ReadText( p, &value, false, end, false, encoding );
  1237. }
  1238. else
  1239. {
  1240. // All attribute values should be in single or double quotes.
  1241. // But this is such a common error that the parser will try
  1242. // its best, even without them.
  1243. value = "";
  1244. while ( p && *p // existence
  1245. && !IsWhiteSpace( *p ) && *p != '\n' && *p != '\r' // whitespace
  1246. && *p != '/' && *p != '>' ) // tag end
  1247. {
  1248. if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
  1249. // [ 1451649 ] Attribute values with trailing quotes not handled correctly
  1250. // We did not have an opening quote but seem to have a
  1251. // closing one. Give up and throw an error.
  1252. if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
  1253. return 0;
  1254. }
  1255. value += *p;
  1256. ++p;
  1257. }
  1258. }
  1259. return p;
  1260. }
  1261. #ifdef TIXML_USE_STL
  1262. void TiXmlText::StreamIn( std::istream * in, TIXML_STRING * tag )
  1263. {
  1264. while ( in->good() )
  1265. {
  1266. int c = in->peek();
  1267. if ( !cdata && (c == '<' ) )
  1268. {
  1269. return;
  1270. }
  1271. if ( c <= 0 )
  1272. {
  1273. TiXmlDocument* document = GetDocument();
  1274. if ( document )
  1275. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1276. return;
  1277. }
  1278. (*tag) += (char) c;
  1279. in->get(); // "commits" the peek made above
  1280. if ( cdata && c == '>' && tag->size() >= 3 ) {
  1281. size_t len = tag->size();
  1282. if ( (*tag)[len-2] == ']' && (*tag)[len-3] == ']' ) {
  1283. // terminator of cdata.
  1284. return;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. #endif
  1290. const char* TiXmlText::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
  1291. {
  1292. value = "";
  1293. TiXmlDocument* document = GetDocument();
  1294. if ( data )
  1295. {
  1296. data->Stamp( p, encoding );
  1297. location = data->Cursor();
  1298. }
  1299. const char* const startTag = "<![CDATA[";
  1300. const char* const endTag = "]]>";
  1301. if ( cdata || StringEqual( p, startTag, false, encoding ) )
  1302. {
  1303. cdata = true;
  1304. if ( !StringEqual( p, startTag, false, encoding ) )
  1305. {
  1306. document->SetError( TIXML_ERROR_PARSING_CDATA, p, data, encoding );
  1307. return 0;
  1308. }
  1309. p += strlen( startTag );
  1310. // Keep all the white space, ignore the encoding, etc.
  1311. while ( p && *p
  1312. && !StringEqual( p, endTag, false, encoding )
  1313. )
  1314. {
  1315. value += *p;
  1316. ++p;
  1317. }
  1318. TIXML_STRING dummy;
  1319. p = ReadText( p, &dummy, false, endTag, false, encoding );
  1320. return p;
  1321. }
  1322. else
  1323. {
  1324. bool ignoreWhite = true;
  1325. const char* end = "<";
  1326. p = ReadText( p, &value, ignoreWhite, end, false, encoding );
  1327. if ( p )
  1328. return p-1; // don't truncate the '<'
  1329. return 0;
  1330. }
  1331. }
  1332. #ifdef TIXML_USE_STL
  1333. void TiXmlDeclaration::StreamIn( std::istream * in, TIXML_STRING * tag )
  1334. {
  1335. while ( in->good() )
  1336. {
  1337. int c = in->get();
  1338. if ( c <= 0 )
  1339. {
  1340. TiXmlDocument* document = GetDocument();
  1341. if ( document )
  1342. document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
  1343. return;
  1344. }
  1345. (*tag) += (char) c;
  1346. if ( c == '>' )
  1347. {
  1348. // All is well.
  1349. return;
  1350. }
  1351. }
  1352. }
  1353. #endif
  1354. const char* TiXmlDeclaration::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding _encoding )
  1355. {
  1356. p = SkipWhiteSpace( p, _encoding );
  1357. // Find the beginning, find the end, and look for
  1358. // the stuff in-between.
  1359. TiXmlDocument* document = GetDocument();
  1360. if ( !p || !*p || !StringEqual( p, "<?xml", true, _encoding ) )
  1361. {
  1362. if ( document ) document->SetError( TIXML_ERROR_PARSING_DECLARATION, 0, 0, _encoding );
  1363. return 0;
  1364. }
  1365. if ( data )
  1366. {
  1367. data->Stamp( p, _encoding );
  1368. location = data->Cursor();
  1369. }
  1370. p += 5;
  1371. version = "";
  1372. encoding = "";
  1373. standalone = "";
  1374. while ( p && *p )
  1375. {
  1376. if ( *p == '>' )
  1377. {
  1378. ++p;
  1379. return p;
  1380. }
  1381. p = SkipWhiteSpace( p, _encoding );
  1382. if ( StringEqual( p, "version", true, _encoding ) )
  1383. {
  1384. TiXmlAttribute attrib;
  1385. p = attrib.Parse( p, data, _encoding );
  1386. version = attrib.Value();
  1387. }
  1388. else if ( StringEqual( p, "encoding", true, _encoding ) )
  1389. {
  1390. TiXmlAttribute attrib;
  1391. p = attrib.Parse( p, data, _encoding );
  1392. encoding = attrib.Value();
  1393. }
  1394. else if ( StringEqual( p, "standalone", true, _encoding ) )
  1395. {
  1396. TiXmlAttribute attrib;
  1397. p = attrib.Parse( p, data, _encoding );
  1398. standalone = attrib.Value();
  1399. }
  1400. else
  1401. {
  1402. // Read over whatever it is.
  1403. while( p && *p && *p != '>' && !IsWhiteSpace( *p ) )
  1404. ++p;
  1405. }
  1406. }
  1407. return 0;
  1408. }
  1409. bool TiXmlText::Blank() const
  1410. {
  1411. for ( unsigned i=0; i<value.length(); i++ )
  1412. if ( !IsWhiteSpace( value[i] ) )
  1413. return false;
  1414. return true;
  1415. }