@@ -422,16 +422,19 @@ void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
422422 TIXML_SNPRINTF ( buffer, bufferSize, " %d" , v ? 1 : 0 );
423423}
424424
425-
425+ /*
426+ ToStr() of a number is a very tricky topic.
427+ https://github.com/leethomason/tinyxml2/issues/106
428+ */
426429void XMLUtil::ToStr ( float v, char * buffer, int bufferSize )
427430{
428- TIXML_SNPRINTF ( buffer, bufferSize, " %f " , v );
431+ TIXML_SNPRINTF ( buffer, bufferSize, " %.8g " , v );
429432}
430433
431434
432435void XMLUtil::ToStr ( double v, char * buffer, int bufferSize )
433436{
434- TIXML_SNPRINTF ( buffer, bufferSize, " %f " , v );
437+ TIXML_SNPRINTF ( buffer, bufferSize, " %.17g " , v );
435438}
436439
437440
@@ -497,12 +500,7 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
497500 }
498501
499502 // What is this thing?
500- // - Elements start with a letter or underscore, but xml is reserved.
501- // - Comments: <!--
502- // - Declaration: <?
503- // - Everything else is unknown to tinyxml.
504- //
505-
503+ // These strings define the matching patters:
506504 static const char * xmlHeader = { " <?" };
507505 static const char * commentHeader = { " <!--" };
508506 static const char * dtdHeader = { " <!" };
@@ -1262,6 +1260,57 @@ const char* XMLElement::GetText() const
12621260}
12631261
12641262
1263+ void XMLElement::SetText ( const char * inText )
1264+ {
1265+ if ( FirstChild () && FirstChild ()->ToText () )
1266+ FirstChild ()->SetValue ( inText );
1267+ else {
1268+ XMLText* theText = GetDocument ()->NewText ( inText );
1269+ InsertFirstChild ( theText );
1270+ }
1271+ }
1272+
1273+
1274+ void XMLElement::SetText ( int v )
1275+ {
1276+ char buf[BUF_SIZE];
1277+ XMLUtil::ToStr ( v, buf, BUF_SIZE );
1278+ SetText ( buf );
1279+ }
1280+
1281+
1282+ void XMLElement::SetText ( unsigned v )
1283+ {
1284+ char buf[BUF_SIZE];
1285+ XMLUtil::ToStr ( v, buf, BUF_SIZE );
1286+ SetText ( buf );
1287+ }
1288+
1289+
1290+ void XMLElement::SetText ( bool v )
1291+ {
1292+ char buf[BUF_SIZE];
1293+ XMLUtil::ToStr ( v, buf, BUF_SIZE );
1294+ SetText ( buf );
1295+ }
1296+
1297+
1298+ void XMLElement::SetText ( float v )
1299+ {
1300+ char buf[BUF_SIZE];
1301+ XMLUtil::ToStr ( v, buf, BUF_SIZE );
1302+ SetText ( buf );
1303+ }
1304+
1305+
1306+ void XMLElement::SetText ( double v )
1307+ {
1308+ char buf[BUF_SIZE];
1309+ XMLUtil::ToStr ( v, buf, BUF_SIZE );
1310+ SetText ( buf );
1311+ }
1312+
1313+
12651314XMLError XMLElement::QueryIntText ( int * ival ) const
12661315{
12671316 if ( FirstChild () && FirstChild ()->ToText () ) {
@@ -1639,6 +1688,13 @@ XMLError XMLDocument::LoadFile( FILE* fp )
16391688{
16401689 Clear ();
16411690
1691+ fseek ( fp, 0 , SEEK_SET );
1692+ fgetc ( fp );
1693+ if ( ferror ( fp ) != 0 ) {
1694+ SetError ( XML_ERROR_FILE_READ_ERROR, 0 , 0 );
1695+ return _errorID;
1696+ }
1697+
16421698 fseek ( fp, 0 , SEEK_END );
16431699 size_t size = ftell ( fp );
16441700 fseek ( fp, 0 , SEEK_SET );
@@ -1702,7 +1758,7 @@ XMLError XMLDocument::Parse( const char* p, size_t len )
17021758 const char * start = p;
17031759 Clear ();
17041760
1705- if ( !p || !*p ) {
1761+ if ( len == 0 || !p || !*p ) {
17061762 SetError ( XML_ERROR_EMPTY_DOCUMENT, 0 , 0 );
17071763 return _errorID;
17081764 }
@@ -1884,17 +1940,17 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
18841940}
18851941
18861942
1887- void XMLPrinter::OpenElement ( const char * name )
1943+ void XMLPrinter::OpenElement ( const char * name, bool compactMode )
18881944{
18891945 if ( _elementJustOpened ) {
18901946 SealElement ();
18911947 }
18921948 _stack.Push ( name );
18931949
1894- if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
1950+ if ( _textDepth < 0 && !_firstElement && !compactMode ) {
18951951 Print ( " \n " );
18961952 }
1897- if ( !_compactMode ) {
1953+ if ( !compactMode ) {
18981954 PrintSpace ( _depth );
18991955 }
19001956
@@ -1946,7 +2002,7 @@ void XMLPrinter::PushAttribute( const char* name, double v )
19462002}
19472003
19482004
1949- void XMLPrinter::CloseElement ()
2005+ void XMLPrinter::CloseElement ( bool compactMode )
19502006{
19512007 --_depth;
19522008 const char * name = _stack.Pop ();
@@ -1955,7 +2011,7 @@ void XMLPrinter::CloseElement()
19552011 Print ( " />" );
19562012 }
19572013 else {
1958- if ( _textDepth < 0 && !_compactMode ) {
2014+ if ( _textDepth < 0 && !compactMode ) {
19592015 Print ( " \n " );
19602016 PrintSpace ( _depth );
19612017 }
@@ -1965,7 +2021,7 @@ void XMLPrinter::CloseElement()
19652021 if ( _textDepth == _depth ) {
19662022 _textDepth = -1 ;
19672023 }
1968- if ( _depth == 0 && !_compactMode ) {
2024+ if ( _depth == 0 && !compactMode ) {
19692025 Print ( " \n " );
19702026 }
19712027 _elementJustOpened = false ;
@@ -2090,7 +2146,9 @@ bool XMLPrinter::VisitEnter( const XMLDocument& doc )
20902146
20912147bool XMLPrinter::VisitEnter ( const XMLElement& element, const XMLAttribute* attribute )
20922148{
2093- OpenElement ( element.Name () );
2149+ const XMLElement* parentElem = element.Parent ()->ToElement ();
2150+ bool compactMode = parentElem ? CompactMode (*parentElem) : _compactMode;
2151+ OpenElement ( element.Name (), compactMode );
20942152 while ( attribute ) {
20952153 PushAttribute ( attribute->Name (), attribute->Value () );
20962154 attribute = attribute->Next ();
@@ -2099,9 +2157,9 @@ bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attr
20992157}
21002158
21012159
2102- bool XMLPrinter::VisitExit ( const XMLElement& )
2160+ bool XMLPrinter::VisitExit ( const XMLElement& element )
21032161{
2104- CloseElement ();
2162+ CloseElement ( CompactMode (element) );
21052163 return true ;
21062164}
21072165
0 commit comments