From 24767b05abe01c8222c3985a9c715126396cda36 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Wed, 25 Jan 2012 17:16:23 -0800 Subject: [PATCH] work on the streamer class. A little optimization to the string class. Formatting work. --- tinyxml2.cpp | 25 ++++++++++++++++++++----- tinyxml2.h | 5 +++-- xmltest.cpp | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 69a6289..3bf058e 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -548,14 +548,22 @@ void XMLDocument::SetError( int error, const char* str1, const char* str2 ) StringStack::StringStack() { - mem = new char[INIT]; - *mem = 0; + *pool = 0; + mem = pool; inUse = 1; // always has a null allocated = INIT; nPositive = 0; } +StringStack::~StringStack() +{ + if ( mem != pool ) { + delete [] mem; + } +} + + void StringStack::Push( const char* str ) { int needed = strlen( str ) + 1; if ( needed > 1 ) @@ -567,7 +575,9 @@ void StringStack::Push( const char* str ) { char* newMem = new char[more]; memcpy( newMem, mem, inUse ); - delete [] mem; + if ( mem != pool ) { + delete [] mem; + } mem = newMem; } strcpy( mem+inUse, str ); @@ -608,10 +618,12 @@ void XMLStreamer::OpenElement( const char* name, bool textParent ) if ( elementJustOpened ) { SealElement(); } + if ( text.NumPositive() == 0 ) { + PrintSpace( depth ); + } stack.Push( name ); text.Push( textParent ? "T" : "" ); - PrintSpace( depth ); fprintf( fp, "<%s", name ); elementJustOpened = true; ++depth; @@ -629,6 +641,7 @@ void XMLStreamer::CloseElement() { --depth; const char* name = stack.Pop(); + int wasPositive = text.NumPositive(); text.Pop(); if ( elementJustOpened ) { @@ -638,7 +651,9 @@ void XMLStreamer::CloseElement() } } else { - PrintSpace( depth ); + if ( wasPositive == 0 ) { + PrintSpace( depth ); + } fprintf( fp, "", name ); if ( text.NumPositive() == 0 ) { fprintf( fp, "\n" ); diff --git a/tinyxml2.h b/tinyxml2.h index ddf285f..7356dc5 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -256,11 +256,12 @@ private: }; +// FIXME: break out into string pointer stack class StringStack { public: StringStack(); - ~StringStack() { delete[] mem; } + ~StringStack(); void Push( const char* str ); const char* Pop(); @@ -272,12 +273,12 @@ private: INIT=10 // fixme, super small for testing }; char* mem; + char pool[INIT]; int inUse; // includes null int allocated; // bytes allocated int nPositive; // number of strings with len > 0 }; - class XMLStreamer { public: diff --git a/xmltest.cpp b/xmltest.cpp index 6376d91..7b12475 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -40,6 +40,7 @@ int main( int argc, const char* argv ) //"Text inside element.", //"", "Text inside and bolded in the element.", + "Text inside and bolded in the element.", 0 }; for( int i=0; test[i]; ++i ) {