more cleaning up the interface.

This commit is contained in:
Lee Thomason 2012-01-26 18:32:34 -08:00
parent 18d68bdf6b
commit 7c913cd6db
2 changed files with 85 additions and 33 deletions

View File

@ -31,26 +31,6 @@ static const Entity entities[NUM_ENTITIES] =
}; };
#if 0
// --------- CharBuffer ----------- //
/*static*/ CharBuffer* CharBuffer::Construct( const char* in )
{
size_t len = strlen( in );
size_t size = len + sizeof( CharBuffer );
CharBuffer* cb = (CharBuffer*) malloc( size );
cb->length = len;
strcpy( cb->mem, in );
return cb;
}
/*static*/ void CharBuffer::Free( CharBuffer* cb )
{
free( cb );
}
#endif
const char* StrPair::GetStr() const char* StrPair::GetStr()
{ {
if ( flags & NEEDS_FLUSH ) { if ( flags & NEEDS_FLUSH ) {
@ -237,6 +217,9 @@ XMLNode::XMLNode( XMLDocument* doc ) :
XMLNode::~XMLNode() XMLNode::~XMLNode()
{ {
ClearChildren(); ClearChildren();
if ( parent ) {
parent->Unlink( this );
}
} }
@ -547,7 +530,7 @@ void XMLDocument::InitDocument()
} }
bool XMLDocument::Parse( const char* p ) int XMLDocument::Parse( const char* p )
{ {
ClearChildren(); ClearChildren();
InitDocument(); InitDocument();
@ -561,7 +544,7 @@ bool XMLDocument::Parse( const char* p )
XMLNode* node = 0; XMLNode* node = 0;
char* q = ParseDeep( charBuffer ); char* q = ParseDeep( charBuffer );
return true; return errorID;
} }
@ -607,7 +590,7 @@ void StringStack::Push( const char* str ) {
int needed = strlen( str ) + 1; int needed = strlen( str ) + 1;
if ( needed > 1 ) if ( needed > 1 )
nPositive++; nPositive++;
if ( inUse+needed > allocated ) { if ( inUse+needed >= allocated ) {
// fixme: power of 2 // fixme: power of 2
// less stupid allocation // less stupid allocation
int more = inUse+needed + 1000; int more = inUse+needed + 1000;
@ -639,6 +622,55 @@ const char* StringStack::Pop() {
} }
StringPtrStack::StringPtrStack()
{
*pool = 0;
mem = pool;
inUse = 0;
allocated = INIT;
nPositive = 0;
}
StringPtrStack::~StringPtrStack()
{
if ( mem != pool ) {
delete [] mem;
}
}
void StringPtrStack::Push( const char* str ) {
int needed = inUse + 1;
if ( str )
nPositive++;
if ( inUse+needed >= allocated ) {
// fixme: power of 2
// less stupid allocation
int more = inUse+needed + 1000;
char** newMem = new char*[more];
memcpy( newMem, mem, inUse*sizeof(char*) );
if ( mem != pool ) {
delete [] mem;
}
mem = newMem;
}
mem[inUse] = (char*)str;
inUse++;
}
const char* StringPtrStack::Pop() {
TIXMLASSERT( inUse > 0 );
inUse--;
const char* result = mem[inUse];
if ( result )
nPositive--;
return result;
}
XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ) XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false )
{ {
for( int i=0; i<ENTITY_RANGE; ++i ) { for( int i=0; i<ENTITY_RANGE; ++i ) {

View File

@ -243,16 +243,12 @@ public:
XMLDocument(); XMLDocument();
~XMLDocument(); ~XMLDocument();
bool Parse( const char* ); int Parse( const char* );
bool Load( const char* ); int Load( const char* );
bool Load( FILE* ); int Load( FILE* );
void Print( XMLStreamer* streamer=0 ); void Print( XMLStreamer* streamer=0 );
/*
XMLNode* Root() { return root; }
XMLNode* RootElement();
*/
enum { enum {
NO_ERROR = 0, NO_ERROR = 0,
ERROR_ELEMENT_MISMATCH, ERROR_ELEMENT_MISMATCH,
@ -261,6 +257,7 @@ public:
}; };
void SetError( int error, const char* str1, const char* str2 ); void SetError( int error, const char* str1, const char* str2 );
bool Error() const { return errorID != NO_ERROR; }
int GetErrorID() const { return errorID; } int GetErrorID() const { return errorID; }
const char* GetErrorStr1() const { return errorStr1; } const char* GetErrorStr1() const { return errorStr1; }
const char* GetErrorStr2() const { return errorStr2; } const char* GetErrorStr2() const { return errorStr2; }
@ -269,14 +266,13 @@ private:
XMLDocument( const XMLDocument& ); // intentionally not implemented XMLDocument( const XMLDocument& ); // intentionally not implemented
void InitDocument(); void InitDocument();
bool errorID; int errorID;
const char* errorStr1; const char* errorStr1;
const char* errorStr2; const char* errorStr2;
char* charBuffer; char* charBuffer;
}; };
// FIXME: break out into string pointer stack
class StringStack class StringStack
{ {
public: public:
@ -299,6 +295,30 @@ private:
int nPositive; // number of strings with len > 0 int nPositive; // number of strings with len > 0
}; };
class StringPtrStack
{
public:
StringPtrStack();
~StringPtrStack();
void Push( const char* str );
const char* Pop();
int NumPositive() const { return nPositive; }
private:
enum {
INIT=10 // fixme, super small for testing
};
char** mem;
char* pool[INIT];
int inUse;
int allocated; // bytes allocated
int nPositive; // number of non-null pointers
};
class XMLStreamer class XMLStreamer
{ {
public: public:
@ -325,7 +345,7 @@ private:
}; };
bool entityFlag[ENTITY_RANGE]; bool entityFlag[ENTITY_RANGE];
StringStack stack; StringPtrStack stack;
StringStack text; StringStack text;
}; };