From f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 03:49:53 +0000 Subject: Ok, no code is left in src, it's all in src/old. We'll gradually move code back into src as it's fixed and re-org'd. This includes tests, which, I may write a unit test system into libbu++ just to make my life easier. --- src/list.cpp | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/list.cpp (limited to 'src/list.cpp') diff --git a/src/list.cpp b/src/list.cpp deleted file mode 100644 index 18f1a66..0000000 --- a/src/list.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "list.h" - -List::List( ) -{ -} - -List::~List( ) -{ -} - -- cgit v1.2.3 From 033c41ed57348abb3a418166b1fb39bfad3312de Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 11 May 2007 07:51:40 +0000 Subject: Added a list template class, seems to work pretty well for now, I may have forgotten proper cleanup in the deconstructor, but besides that you can do almost everything you need. I'll make a slist/stack next, probably with the same basic code, just a different structure (not doubley-linked). The xml system from old-libbu++ is almost completely converted, I was going to re-write it, but this seemed easier at first, it may not have been, we'll see. It almost parses everything again, and almost outputs again, and it does use streams now. The FString is partway to doing minimum chunk allocations, so that adding single-characters will be really fast up to the minimum chunk size. I also figured out how to add this optimization without any extra variables taking up space, and it's optional in the template, which is cool. You can specify the size of the blocks (default 256 bytes), if it's 0 then they'll be like the old FString, 1 chunk per operation. The next FString update should be allowing efficient removal from the begining of the string by faking it, and simply moving a secondary base pointer ahead, and then optimizing appends after that fact to simply move the existing data around if you shouldn't have to re-allocate (alla FlexBuf). The final fun addition that I'm planning is a simple switch in the template (boolean) that will switch an FString into a thread-safe mode without changing the interface or anything that you can do with them at all. It may increasing memory usage, but they should still be better than std::strings, and totally thread-safe. The best part of that is that if it's done with a boolean template parameter and if statements that only test that parameter controlling flow, the code that you don't want (threadsafe/non-threadsafe) won't be included at all post-optimization. --- src/fstring.h | 14 +++-- src/list.cpp | 2 + src/list.h | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/list.cpp | 22 +++++++ src/tests/xml.cpp | 6 +- src/xmldocument.cpp | 20 +++--- src/xmldocument.h | 8 +-- src/xmlnode.cpp | 106 ++++++++++--------------------- src/xmlnode.h | 101 +++++++++++------------------- src/xmlreader.cpp | 170 +++++++++++++++++++++++++------------------------- src/xmlreader.h | 31 ++++----- src/xmlwriter.cpp | 62 +++++++++--------- src/xmlwriter.h | 16 ++--- 13 files changed, 428 insertions(+), 306 deletions(-) create mode 100644 src/list.cpp create mode 100644 src/list.h create mode 100644 src/tests/list.cpp (limited to 'src/list.cpp') diff --git a/src/fstring.h b/src/fstring.h index 877e5a7..f738f63 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -28,7 +28,7 @@ namespace Bu * data is actually copied. This also means that you never need to put any * FBasicString into a ref-counting container class. */ - template< typename chr, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > + template< typename chr, int nMinSize=256, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > class FBasicString : public Archival { #ifndef VALTEST @@ -36,7 +36,7 @@ namespace Bu #endif private: typedef struct FStringChunk Chunk; - typedef struct FBasicString MyType; + typedef struct FBasicString MyType; public: FBasicString() : @@ -131,6 +131,11 @@ namespace Bu appendChunk( pNew ); } + void append( const chr cData ) + { + append( &cData, 1 ); + } + void prepend( const chr *pData ) { long nLen; @@ -231,8 +236,7 @@ namespace Bu MyType &operator +=( const chr pData ) { - chr tmp[2] = { pData, (chr)0 }; - append( tmp ); + append( &pData, 1 ); return (*this); } @@ -475,7 +479,7 @@ namespace Bu } } - void copyFrom( const FBasicString &rSrc ) + void copyFrom( const FBasicString &rSrc ) { if( rSrc.pFirst == NULL ) return; diff --git a/src/list.cpp b/src/list.cpp new file mode 100644 index 0000000..abe92ad --- /dev/null +++ b/src/list.cpp @@ -0,0 +1,2 @@ +#include "bu/list.h" + diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..ec63496 --- /dev/null +++ b/src/list.h @@ -0,0 +1,176 @@ +#ifndef LIST_H +#define LIST_H + +#include +#include "bu/exceptionbase.h" + +namespace Bu +{ + template + struct ListLink + { + value *pValue; + ListLink *pNext; + ListLink *pPrev; + }; + template, typename linkalloc=std::allocator > > + class List + { + private: + typedef struct ListLink Link; + typedef class List MyType; + + public: + List() : + pFirst( NULL ), + pLast( NULL ) + { + } + + void append( value v ) + { + Link *pNew = la.allocate( sizeof( Link ) ); + pNew->pValue = va.allocate( sizeof( value ) ); + va.construct( pNew->pValue, v ); + if( pFirst == NULL ) + { + // Empty list + pFirst = pLast = pNew; + pNew->pNext = pNew->pPrev = NULL; + } + else + { + pNew->pNext = NULL; + pNew->pPrev = pLast; + pLast->pNext = pNew; + pLast = pNew; + } + } + + void prepend( value v ) + { + Link *pNew = la.allocate( sizeof( Link ) ); + pNew->pValue = va.allocate( sizeof( value ) ); + va.construct( pNew->pValue, v ); + if( pFirst == NULL ) + { + // Empty list + pFirst = pLast = pNew; + pNew->pNext = pNew->pPrev = NULL; + } + else + { + pNew->pNext = pFirst; + pNew->pPrev = NULL; + pFirst->pPrev = pNew; + pFirst = pNew; + } + } + + typedef struct iterator + { + friend class List; + private: + Link *pLink; + iterator() : + pLink( NULL ) + { + } + + iterator( Link *pLink ) : + pLink( pLink ) + { + } + + public: + bool operator==( const iterator &oth ) + { + return ( pLink == oth.pLink ); + } + + bool operator==( const Link *pOth ) + { + return ( pLink == pOth ); + } + + bool operator!=( const iterator &oth ) + { + return ( pLink != oth.pLink ); + } + + bool operator!=( const Link *pOth ) + { + return ( pLink != pOth ); + } + + value &operator*() + { + return *(pLink->pValue); + } + + value *operator->() + { + return pLink->pValue(); + } + + iterator operator++() + { + if( pLink != NULL ) + pLink = pLink->pNext; + return *this; + } + + iterator operator--() + { + if( pLink != NULL ) + pLink = pLink->pPrev; + return *this; + } + + iterator operator++( int ) + { + if( pLink != NULL ) + pLink = pLink->pNext; + return *this; + } + + iterator operator--( int ) + { + if( pLink != NULL ) + pLink = pLink->pPrev; + return *this; + } + + iterator operator=( const iterator &oth ) + { + pLink = oth.pLink; + } + }; + + iterator begin() + { + return iterator( pFirst ); + } + + const Link *end() + { + return NULL; + } + + int getSize() + { + int j = 0; + for( Link *pCur = pFirst; pCur; pCur = pCur->pNext ) + j++; + return j; + } + + private: + Link *pFirst; + Link *pLast; + linkalloc la; + valuealloc va; + }; +} + +#endif diff --git a/src/tests/list.cpp b/src/tests/list.cpp new file mode 100644 index 0000000..34ab656 --- /dev/null +++ b/src/tests/list.cpp @@ -0,0 +1,22 @@ +#include "bu/list.h" + +int main() +{ + Bu::List l; + + l.append( 0 ); + + for( int j = 3; j <= 21; j += 3 ) + { + l.append( j ); + l.prepend( -j ); + } + + for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) + { + printf("%d ", *i ); + } + + printf("\n\n"); +} + diff --git a/src/tests/xml.cpp b/src/tests/xml.cpp index 9ef6a7e..9689a28 100644 --- a/src/tests/xml.cpp +++ b/src/tests/xml.cpp @@ -6,9 +6,9 @@ int main() { Bu::File f("test.xml", "r"); - Bu::XmlReader xr( f ); - - xr.read(); + XmlReader xr( f ); + + //xr.read(); return 0; } diff --git a/src/xmldocument.cpp b/src/xmldocument.cpp index d7867d5..95b9788 100644 --- a/src/xmldocument.cpp +++ b/src/xmldocument.cpp @@ -1,6 +1,6 @@ #include #include -#include "xmlwriter.h" +#include "xmldocument.h" XmlDocument::XmlDocument( XmlNode *pRoot ) { @@ -17,28 +17,23 @@ XmlDocument::~XmlDocument() } } -void XmlDocument::addNode( const char *sName, const char *sContent, bool bClose ) +void XmlDocument::addNode( const Bu::FString &sName ) { if( pRoot == NULL ) { // This is the first node, so ignore position and just insert it. - pCurrent = pRoot = new XmlNode( sName, NULL, sContent ); + pCurrent = pRoot = new XmlNode( sName ); } else { - pCurrent = pCurrent->addChild( sName, sContent ); - } - - if( bClose ) - { - closeNode(); + pCurrent = pCurrent->addChild( sName ); } } - +/* void XmlDocument::setName( const char *sName ) { pCurrent->setName( sName ); -} +}*/ bool XmlDocument::isCompleted() { @@ -143,7 +138,8 @@ void XmlDocument::setContent( const char *sContent ) { if( pCurrent ) { - pCurrent->setContent( sContent ); + printf("XmlDocument::setContent: not yet implemented.\n"); + //pCurrent->setContent( sContent ); } } diff --git a/src/xmldocument.h b/src/xmldocument.h index 6671c41..e0c36eb 100644 --- a/src/xmldocument.h +++ b/src/xmldocument.h @@ -39,13 +39,7 @@ public: * the node and setting the content and name. If this is set to true the * node is appended, but the context node doesn't change. */ - void addNode( const char *sName=NULL, const char *sContent=NULL, bool bClose=false ); - - /** - * Set the name of the current node context. - *@param sName The new name of the node. - */ - void setName( const char *sName ); + void addNode( const Bu::FString &sName ); /** * Close the current node context. This will move the current context to diff --git a/src/xmlnode.cpp b/src/xmlnode.cpp index b1ed9a9..96d5850 100644 --- a/src/xmlnode.cpp +++ b/src/xmlnode.cpp @@ -1,53 +1,15 @@ #include "xmlnode.h" -#include "hashfunctionstring.h" -XmlNode::XmlNode( const char *sName, XmlNode *pParent, const char *sContent ) : - hProperties( new HashFunctionString(), 53, false ), - hChildren( new HashFunctionString(), 53, true ) +XmlNode::XmlNode( const Bu::FString &sName, XmlNode *pParent ) : + sName( sName ), + pParent( pParent ) { - this->pParent = pParent; - if( sName != NULL ) - { - setName( sName ); - } - if( sContent != NULL ) - { - this->sPreContent = new std::string( sContent ); - } - else - { - this->sPreContent = NULL; - } - nCurContent = 0; } XmlNode::~XmlNode() { - for( int j = 0; j < lChildren.getSize(); j++ ) - { - delete (XmlNode *)lChildren[j]; - } - for( int j = 0; j < lPropNames.getSize(); j++ ) - { - delete (std::string *)lPropNames[j]; - } - for( int j = 0; j < lPropValues.getSize(); j++ ) - { - delete (std::string *)lPropValues[j]; - } - for( int j = 0; j < lPostContent.getSize(); j++ ) - { - if( lPostContent[j] != NULL ) - { - delete (std::string *)lPostContent[j]; - } - } - if( sPreContent ) - { - delete sPreContent; - } } - +/* void XmlNode::setName( const char *sName ) { if( pParent ) @@ -120,18 +82,18 @@ const char *XmlNode::getContent( int nIndex ) } return NULL; -} +}*/ -XmlNode *XmlNode::addChild( const char *sName, const char *sContent ) +XmlNode *XmlNode::addChild( const Bu::FString &sName ) { - return addChild( new XmlNode( sName, this, sContent ) ); + return addChild( new XmlNode( sName, this ) ); } XmlNode *XmlNode::addChild( XmlNode *pNode ) { - lChildren.append( pNode ); - lPostContent.append( NULL ); - nCurContent++; + Child c = { typeNode }; + c.pNode = pNode; + lChildren.append( c ); pNode->pParent = this; return pNode; @@ -142,21 +104,16 @@ XmlNode *XmlNode::getParent() return pParent; } -void XmlNode::addProperty( const char *sName, const char *sValue ) +void XmlNode::addProperty( const Bu::FString &sName, const Bu::FString &sValue ) { - std::string *pName = new std::string( sName ); - std::string *pValue = new std::string( sValue ); - - hProperties.insert( pName->c_str(), pValue->c_str() ); - lPropNames.append( pName ); - lPropValues.append( pValue ); + hProperties.insert( sName, sValue ); } int XmlNode::getNumProperties() { - return lPropNames.getSize(); + return hProperties.size(); } - +/* const char *XmlNode::getPropertyName( int nIndex ) { std::string *tmp = ((std::string *)lPropNames[nIndex]); @@ -172,15 +129,12 @@ const char *XmlNode::getProperty( int nIndex ) return NULL; return tmp->c_str(); } - -const char *XmlNode::getProperty( const char *sName ) +*/ +Bu::FString XmlNode::getProperty( const Bu::FString &sName ) { - const char *tmp = (const char *)hProperties[sName]; - if( tmp == NULL ) - return NULL; - return tmp; + return hProperties[sName]; } - +/* void XmlNode::deleteProperty( int nIndex ) { hProperties.del( ((std::string *)lPropNames[nIndex])->c_str() ); @@ -194,29 +148,33 @@ void XmlNode::deleteProperty( int nIndex ) bool XmlNode::hasChildren() { - return lChildren.getSize()>0; -} + return hChildren.getSize()>0; +}*/ int XmlNode::getNumChildren() { return lChildren.getSize(); } - +/* XmlNode *XmlNode::getChild( int nIndex ) { return (XmlNode *)lChildren[nIndex]; } - -XmlNode *XmlNode::getChild( const char *sName, int nSkip ) +*/ +XmlNode *XmlNode::getChild( const Bu::FString &sName, int nSkip ) { - return (XmlNode *)hChildren.get( sName, nSkip ); + if( !hChildren.has( sName ) ) + return NULL; + + Bu::List::iterator i = hChildren[sName]->begin(); + return *i; } -const char *XmlNode::getName() +Bu::FString XmlNode::getName() { - return sName.c_str(); + return sName; } - +/* void XmlNode::deleteNode( int nIndex, const char *sReplacementText ) { XmlNode *xRet = detatchNode( nIndex, sReplacementText ); @@ -442,4 +400,4 @@ void XmlNode::deleteNodeKeepChildren( int nIndex ) void XmlNode::replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ) { } - +*/ diff --git a/src/xmlnode.h b/src/xmlnode.h index 7525306..c895cd8 100644 --- a/src/xmlnode.h +++ b/src/xmlnode.h @@ -2,8 +2,9 @@ #define XMLNODE #include -#include "linkedlist.h" -#include "hashtable.h" +#include "bu/list.h" +#include "bu/hash.h" +#include "bu/fstring.h" /** * Maintains all data pertient to an XML node, including sub-nodes and content. @@ -25,9 +26,8 @@ public: *@param sContent The initial content string. */ XmlNode( - const char *sName=NULL, - XmlNode *pParent = NULL, - const char *sContent=NULL + const Bu::FString &sName, + XmlNode *pParent=NULL ); /** @@ -39,7 +39,7 @@ public: * Change the name of the node. *@param sName The new name of the node. */ - void setName( const char *sName ); + //void setName( const char *sName ); /** * Construct a new node and add it as a child to this node, also return a @@ -48,7 +48,7 @@ public: *@param sContent The initial content of the new node. *@returns A pointer to the newly created child node. */ - XmlNode *addChild( const char *sName, const char *sContent=NULL ); + XmlNode *addChild( const Bu::FString &sName ); /** * Add an already created XmlNode as a child to this node. The new child @@ -65,7 +65,7 @@ public: * in use will overwrite that property. *@param sValue The textual value of the property. */ - void addProperty( const char *sName, const char *sValue ); + void addProperty( const Bu::FString &sName, const Bu::FString &sValue ); /** * Get a pointer to the parent node, if any. @@ -85,14 +85,6 @@ public: */ int getNumChildren(); - /** - * Get a child node at a specific index. - *@param nIndex The zero-based index of the child to retreive. - *@returns A pointer to the child, or NULL if you requested an invalid - * index. - */ - XmlNode *getChild( int nIndex ); - /** * Get a child with the specified name, and possibly skip value. For an * explination of skip values see the HashTable. @@ -101,14 +93,14 @@ public: *@returns A pointer to the child, or NULL if no child with that name was * found. */ - XmlNode *getChild( const char *sName, int nSkip=0 ); + XmlNode *getChild( const Bu::FString &sName, int nSkip=0 ); /** * Get a pointer to the name of this node. Do not change this, use setName * instead. *@returns A pointer to the name of this node. */ - const char *getName(); + Bu::FString getName(); /** * Set the content of this node, optionally at a specific index. Using the @@ -116,14 +108,7 @@ public: *@param sContent The content string to use. *@param nIndex The index of the content. */ - void setContent( const char *sContent, int nIndex=-1 ); - - /** - * Get the content string at a given index, or zero for initial content. - *@param nIndex The index of the content. - *@returns A pointer to the content at that location. - */ - const char *getContent( int nIndex = 0 ); + //void setContent( const char *sContent, int nIndex=-1 ); /** * Get the number of properties in this node. @@ -131,37 +116,13 @@ public: */ int getNumProperties(); - /** - * Get a property's name by index. - *@param nIndex The index of the property to examine. - *@returns A pointer to the name of the property specified, or NULL if none - * found. - */ - const char *getPropertyName( int nIndex ); - - /** - * Get a proprty's value by index. - *@param nIndex The index of the property to examine. - *@returns A pointer to the value of the property specified, or NULL if none - * found. - */ - const char *getProperty( int nIndex ); - /** * Get a propery's value by name. *@param sName The name of the property to examine. *@returns A pointer to the value of the property specified, or NULL if none * found. */ - const char *getProperty( const char *sName ); - - /** - * Delete a property by index. - *@param nIndex The index of the property to delete. - *@returns True if the property was found and deleted, false if it wasn't - * found. - */ - void deleteProperty( int nIndex ); + Bu::FString getProperty( const Bu::FString &sName ); /** * Delete a child node, possibly replacing it with some text. This actually @@ -171,7 +132,7 @@ public: *@returns True of the node was found, and deleted, false if it wasn't * found. */ - void deleteNode( int nIndex, const char *sReplacementText = NULL ); + //void deleteNode( int nIndex, const char *sReplacementText = NULL ); /** * Delete a given node, but move all of it's children and content up to @@ -180,7 +141,7 @@ public: *@param nIndex The node to delete. *@returns True if the node was found and deleted, false if it wasn't. */ - void deleteNodeKeepChildren( int nIndex ); + //void deleteNodeKeepChildren( int nIndex ); /** * Detatch a given child node from this node. This effectively works just @@ -192,7 +153,7 @@ public: *@returns A pointer to the newly detatched node, which then passes * ownership to the caller. */ - XmlNode *detatchNode( int nIndex, const char *sReplacementText = NULL ); + //XmlNode *detatchNode( int nIndex, const char *sReplacementText = NULL ); /** * Replace a given node with a different node that is not currently owned by @@ -201,7 +162,7 @@ public: *@param pNewNode The new node to replace the old node with. *@returns True if the node was found and replaced, false if it wasn't. */ - void replaceNode( int nIndex, XmlNode *pNewNode ); + //void replaceNode( int nIndex, XmlNode *pNewNode ); /** * Replace a given node with the children and content of a given node. @@ -210,24 +171,34 @@ public: * replace the node specified by nIndex. *@returns True if the node was found and replaced, false if it wasn't. */ - void replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ); + //void replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ); /** * Get a copy of this node and all children. getCopy is recursive, so * beware copying large trees of xml. *@returns A newly created copy of this node and all of it's children. */ - XmlNode *getCopy(); + //XmlNode *getCopy(); + + enum ChildType + { + typeNode, + typeContent + }; private: - std::string sName; /**< The name of the node. */ - std::string *sPreContent; /**< The content that goes before any node. */ - LinkedList lChildren; /**< The children. */ - LinkedList lPostContent; /**< The content that comes after children. */ - HashTable hProperties; /**< Property hashtable. */ - HashTable hChildren; /**< Children hashtable. */ - LinkedList lPropNames; /**< List of property names. */ - LinkedList lPropValues; /**< List of property values. */ + typedef struct + { + uint8_t nType; + union { + XmlNode *pNode; + Bu::FString *pContent; + }; + } Child; + Bu::FString sName; /**< The name of the node. */ + Bu::List lChildren; /**< The children. */ + Bu::Hash hProperties; /**< Property hashtable. */ + Bu::Hash > hChildren; /**< Children hashtable. */ XmlNode *pParent; /**< A pointer to the parent of this node. */ int nCurContent; /**< The current content we're on, for using the -1 on setContent. */ diff --git a/src/xmlreader.cpp b/src/xmlreader.cpp index 18df69c..38cad5f 100644 --- a/src/xmlreader.cpp +++ b/src/xmlreader.cpp @@ -1,32 +1,49 @@ -#include "xmlreader.h" -#include "exceptions.h" +#include "bu/xmlreader.h" +#include "bu/exceptions.h" #include -#include "hashfunctionstring.h" -XmlReader::XmlReader( bool bStrip ) : - bStrip( bStrip ), - htEntity( new HashFunctionString(), 11 ) +XmlReader::XmlReader( Bu::Stream &sIn, bool bStrip ) : + sIn( sIn ), + bStrip( bStrip ) { + buildDoc(); } XmlReader::~XmlReader() { - void *i = htEntity.getFirstItemPos(); - while( (i = htEntity.getNextItemPos( i ) ) ) +} + +char XmlReader::getChar( int nIndex ) +{ + if( sBuf.getSize() <= nIndex ) { - free( (char *)(htEntity.getItemID( i )) ); - delete (StaticString *)htEntity.getItemData( i ); + int nInc = nIndex-sBuf.getSize()+1; + char *buf = new char[nInc]; + sIn.read( buf, nInc ); + sBuf.append( buf, nInc ); + delete[] buf; } + + return sBuf[nIndex]; } -void XmlReader::addEntity( const char *name, const char *value ) +void XmlReader::usedChar( int nAmnt ) { - if( htEntity[name] ) return; - - char *sName = strdup( name ); - StaticString *sValue = new StaticString( value ); + if( nAmnt >= sBuf.getSize() ) + { + sBuf.clear(); + } + else + { + char *s = sBuf.getStr(); + memcpy( s, s+nAmnt, sBuf.getSize()-nAmnt ); + sBuf.resize( sBuf.getSize()-nAmnt ); + } +} - htEntity.insert( sName, sValue ); +void XmlReader::addEntity( const Bu::FString &name, const Bu::FString &value ) +{ + htEntity[name] = value; } #define gcall( x ) if( x == false ) return false; @@ -99,7 +116,7 @@ void XmlReader::entity() { usedChar( 2 ); ws(); - std::string buf; + Bu::FString buf; for(;;) { char chr = getChar(); @@ -111,7 +128,7 @@ void XmlReader::entity() if( strcmp( buf.c_str(), "ENTITY") == 0 ) { ws(); - std::string name; + Bu::FString name; for(;;) { char chr = getChar(); @@ -124,21 +141,19 @@ void XmlReader::entity() usedChar(); if( quot != '\'' && quot != '\"' ) { - throw XmlException( + throw Bu::XmlException( "Only quoted entity values are supported." ); } - std::string value; + Bu::FString value; for(;;) { char chr = getChar(); usedChar(); if( chr == '&' ) { - StaticString *tmp = getEscape(); - if( tmp == NULL ) throw XmlException("Entity thing"); - value += tmp->getString(); - delete tmp; + Bu::FString tmp = getEscape(); + value += tmp; } else if( chr == quot ) { @@ -158,7 +173,7 @@ void XmlReader::entity() } else { - throw XmlException( + throw Bu::XmlException( "Malformed ENTITY: unexpected '%c' found.", getChar() ); @@ -166,7 +181,7 @@ void XmlReader::entity() } else { - throw XmlException( + throw Bu::XmlException( "Unsupported header symbol: %s", buf.c_str() ); @@ -203,12 +218,12 @@ bool XmlReader::node() } else { - throw XmlException("Close node in singleNode malformed!"); + throw Bu::XmlException("Close node in singleNode malformed!"); } } else { - throw XmlException("Close node expected, but not found."); + throw Bu::XmlException("Close node expected, but not found."); return false; } @@ -224,7 +239,7 @@ bool XmlReader::startNode() if( getChar() == '/' ) { // Heh, it's actually a close node, go figure - FlexBuf fbName; + Bu::FString sName; usedChar(); gcall( ws() ); @@ -235,19 +250,19 @@ bool XmlReader::startNode() { // Here we actually compare the name we got to the name // we already set, they have to match exactly. - if( !strcasecmp( getCurrent()->getName(), fbName.getData() ) ) + if( getCurrent()->getName() == sName ) { closeNode(); break; } else { - throw XmlException("Got a mismatched node close tag."); + throw Bu::XmlException("Got a mismatched node close tag."); } } else { - fbName.appendData( chr ); + sName += chr; usedChar(); } } @@ -260,13 +275,13 @@ bool XmlReader::startNode() } else { - throw XmlException("Got extra junk data instead of node close tag."); + throw Bu::XmlException("Got extra junk data instead of node close tag."); } } else { // We're good, format is consistant - addNode(); + //addNode(); // Skip extra whitespace gcall( ws() ); @@ -278,7 +293,7 @@ bool XmlReader::startNode() } else { - throw XmlException("Expected to find node opening char, '<'."); + throw Bu::XmlException("Expected to find node opening char, '<'."); } return true; @@ -286,19 +301,19 @@ bool XmlReader::startNode() bool XmlReader::name() { - FlexBuf fbName; + Bu::FString sName; while( true ) { char chr = getChar(); if( isws( chr ) || chr == '>' || chr == '/' ) { - setName( fbName.getData() ); + addNode( sName ); return true; } else { - fbName.appendData( chr ); + sName += chr; usedChar(); } } @@ -325,7 +340,7 @@ bool XmlReader::paramlist() return true; } -StaticString *XmlReader::getEscape() +Bu::FString XmlReader::getEscape() { if( getChar( 1 ) == '#' ) { @@ -349,12 +364,12 @@ StaticString *XmlReader::getEscape() buf[0] = (char)strtol( buf, (char **)NULL, base ); buf[1] = '\0'; - return new StaticString( buf ); + return buf; } else { // ...otherwise replace with the appropriate string... - std::string buf; + Bu::FString buf; usedChar(); for(;;) { @@ -364,18 +379,14 @@ StaticString *XmlReader::getEscape() buf += cbuf; } - StaticString *tmp = (StaticString *)htEntity[buf.c_str()]; - if( tmp == NULL ) return NULL; - - StaticString *ret = new StaticString( *tmp ); - return ret; + return htEntity[buf]; } } bool XmlReader::param() { - FlexBuf fbName; - FlexBuf fbValue; + Bu::FString sName; + Bu::FString sValue; while( true ) { @@ -386,7 +397,7 @@ bool XmlReader::param() } else { - fbName.appendData( chr ); + sName.append( chr ); usedChar(); } } @@ -411,21 +422,18 @@ bool XmlReader::param() if( chr == '"' ) { usedChar(); - addProperty( fbName.getData(), fbValue.getData() ); + addProperty( sName.getStr(), sValue.getStr() ); return true; } else { if( chr == '&' ) { - StaticString *tmp = getEscape(); - if( tmp == NULL ) return false; - fbValue.appendData( tmp->getString() ); - delete tmp; + sValue += getEscape(); } else { - fbValue.appendData( chr ); + sValue += chr; usedChar(); } } @@ -439,21 +447,18 @@ bool XmlReader::param() chr = getChar(); if( isws( chr ) || chr == '/' || chr == '>' ) { - addProperty( fbName.getData(), fbValue.getData() ); + addProperty( sName.getStr(), sValue.getStr() ); return true; } else { if( chr == '&' ) { - StaticString *tmp = getEscape(); - if( tmp == NULL ) return false; - fbValue.appendData( tmp->getString() ); - delete tmp; + sValue += getEscape(); } else { - fbValue.appendData( chr ); + sValue += chr; usedChar(); } } @@ -462,7 +467,7 @@ bool XmlReader::param() } else { - throw XmlException("Expected an equals to seperate the params."); + throw Bu::XmlException("Expected an equals to seperate the params."); return false; } @@ -471,7 +476,7 @@ bool XmlReader::param() bool XmlReader::content() { - FlexBuf fbContent; + Bu::FString sContent; if( bStrip ) gcall( ws() ); @@ -482,37 +487,37 @@ bool XmlReader::content() { if( getChar(1) == '/' ) { - if( fbContent.getLength() > 0 ) + if( sContent.getSize() > 0 ) { if( bStrip ) { int j; - for( j = fbContent.getLength()-1; isws(fbContent.getData()[j]); j-- ); - ((char *)fbContent.getData())[j+1] = '\0'; + for( j = sContent.getSize()-1; isws(sContent[j]); j-- ); + sContent[j+1] = '\0'; } - setContent( fbContent.getData() ); + setContent( sContent.getStr() ); } usedChar( 2 ); gcall( ws() ); - FlexBuf fbName; + Bu::FString sName; while( true ) { chr = getChar(); if( isws( chr ) || chr == '>' ) { - if( !strcasecmp( getCurrent()->getName(), fbName.getData() ) ) + if( !strcasecmp( getCurrent()->getName().getStr(), sName.getStr() ) ) { closeNode(); break; } else { - throw XmlException("Mismatched close tag found: <%s> to <%s>.", getCurrent()->getName(), fbName.getData() ); + throw Bu::XmlException("Mismatched close tag found: <%s> to <%s>.", getCurrent()->getName().getStr(), sName.getStr() ); } } else { - fbName.appendData( chr ); + sName += chr; usedChar(); } } @@ -524,7 +529,7 @@ bool XmlReader::content() } else { - throw XmlException("Malformed close tag."); + throw Bu::XmlException("Malformed close tag."); } } else if( getChar(1) == '!' ) @@ -534,7 +539,7 @@ bool XmlReader::content() getChar(3) != '-' ) { // Not a valid XML comment - throw XmlException("Malformed comment start tag found."); + throw Bu::XmlException("Malformed comment start tag found."); } usedChar( 4 ); @@ -549,7 +554,7 @@ bool XmlReader::content() // The next one has to be a '>' now if( getChar( 2 ) != '>' ) { - throw XmlException("Malformed comment close tag found. You cannot have a '--' that isn't followed by a '>' in a comment."); + throw Bu::XmlException("Malformed comment close tag found. You cannot have a '--' that isn't followed by a '>' in a comment."); } usedChar( 3 ); break; @@ -569,16 +574,16 @@ bool XmlReader::content() } else { - if( fbContent.getLength() > 0 ) + if( sContent.getSize() > 0 ) { if( bStrip ) { int j; - for( j = fbContent.getLength()-1; isws(fbContent.getData()[j]); j-- ); - ((char *)fbContent.getData())[j+1] = '\0'; + for( j = sContent.getSize()-1; isws(sContent[j]); j-- ); + sContent[j+1] = '\0'; } - setContent( fbContent.getData() ); - fbContent.clearData(); + setContent( sContent.getStr() ); + sContent.clear(); } gcall( node() ); } @@ -587,14 +592,11 @@ bool XmlReader::content() } else if( chr == '&' ) { - StaticString *tmp = getEscape(); - if( tmp == NULL ) return false; - fbContent.appendData( tmp->getString() ); - delete tmp; + sContent += getEscape(); } else { - fbContent.appendData( chr ); + sContent += chr; usedChar(); } } diff --git a/src/xmlreader.h b/src/xmlreader.h index c8f7202..7c85ddb 100644 --- a/src/xmlreader.h +++ b/src/xmlreader.h @@ -2,10 +2,10 @@ #define XMLREADER #include -#include "xmldocument.h" -#include "flexbuf.h" -#include "hashtable.h" -#include "staticstring.h" +#include "bu/xmldocument.h" +#include "bu/hash.h" +#include "bu/fstring.h" +#include "bu/stream.h" /** * Takes care of reading in xml formatted data from a file. This could/should @@ -32,7 +32,7 @@ public: * in content, a-la html. *@param bStrip Strip out leading and trailing whitespace? */ - XmlReader( bool bStrip=false ); + XmlReader( Bu::Stream &sIn, bool bStrip=false ); /** * Destroy this XmlReader. @@ -54,12 +54,12 @@ private: *@returns A single character at the requested position, or 0 for end of * stream. */ - virtual char getChar( int nIndex = 0 ) = 0; + virtual char getChar( int nIndex = 0 ); /** * Called to increment the current stream position by a single character. */ - virtual void usedChar( int nAmnt = 1) = 0; + virtual void usedChar( int nAmnt = 1 ); /** * Automoton function: is whitespace. @@ -108,9 +108,9 @@ private: *@param name The name of the entity *@param value The value of the entity */ - void addEntity( const char *name, const char *value ); + void addEntity( const Bu::FString &name, const Bu::FString &value ); - StaticString *getEscape(); + Bu::FString getEscape(); /** * Automoton function: paramlist. Processes a list of node params. @@ -130,12 +130,15 @@ private: */ bool content(); - FlexBuf fbContent; /**< buffer for the current node's content. */ - FlexBuf fbParamName; /**< buffer for the current param's name. */ - FlexBuf fbParamValue; /**< buffer for the current param's value. */ - bool bStrip; /**< Are we stripping whitespace? */ + Bu::FString sContent; /**< buffer for the current node's content. */ + Bu::FString sParamName; /**< buffer for the current param's name. */ + Bu::FString sParamValue; /**< buffer for the current param's value. */ + Bu::Stream &sIn; + bool bStrip; /**< Are we stripping whitespace? */ - HashTable htEntity; /**< Entity type definitions. */ + Bu::Hash htEntity; /**< Entity type definitions. */ + + Bu::FString sBuf; }; #endif diff --git a/src/xmlwriter.cpp b/src/xmlwriter.cpp index 56880b6..7dc6ca9 100644 --- a/src/xmlwriter.cpp +++ b/src/xmlwriter.cpp @@ -2,17 +2,10 @@ #include #include "xmlwriter.h" -XmlWriter::XmlWriter( const char *sIndent, XmlNode *pRoot ) : - XmlDocument( pRoot ) +XmlWriter::XmlWriter( const Bu::FString &sIndent, XmlNode *pRoot ) : + XmlDocument( pRoot ), + sIndent( sIndent ) { - if( sIndent == NULL ) - { - this->sIndent = ""; - } - else - { - this->sIndent = sIndent; - } } XmlWriter::~XmlWriter() @@ -24,7 +17,7 @@ void XmlWriter::write() write( getRoot(), sIndent.c_str() ); } -void XmlWriter::write( XmlNode *pRoot, const char *sIndent ) +void XmlWriter::write( XmlNode *pRoot, const Bu::FString &sIndent ) { writeNode( pRoot, 0, sIndent ); } @@ -39,7 +32,7 @@ void XmlWriter::closeNode() } } -void XmlWriter::writeIndent( int nIndent, const char *sIndent ) +void XmlWriter::writeIndent( int nIndent, const Bu::FString &sIndent ) { if( sIndent == NULL ) return; for( int j = 0; j < nIndent; j++ ) @@ -48,26 +41,27 @@ void XmlWriter::writeIndent( int nIndent, const char *sIndent ) } } -std::string XmlWriter::escape( std::string sIn ) +Bu::FString XmlWriter::escape( const Bu::FString &sIn ) { - std::string sOut; + Bu::FString sOut; - std::string::const_iterator i; - for( i = sIn.begin(); i != sIn.end(); i++ ) + int nMax = sIn.getSize(); + for( int j = 0; j < nMax; j++ ) { - if( ((*i >= ' ' && *i <= '9') || - (*i >= 'a' && *i <= 'z') || - (*i >= 'A' && *i <= 'Z') ) && - (*i != '\"' && *i != '\'' && *i != '&') + char c = sIn[j]; + if( ((c >= ' ' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') ) && + (c != '\"' && c != '\'' && c != '&') ) { - sOut += *i; + sOut += c; } else { sOut += "&#"; char buf[4]; - sprintf( buf, "%u", (unsigned char)*i ); + sprintf( buf, "%u", (unsigned char)c ); sOut += buf; sOut += ';'; } @@ -76,19 +70,19 @@ std::string XmlWriter::escape( std::string sIn ) return sOut; } -void XmlWriter::writeNodeProps( XmlNode *pNode, int nIndent, const char *sIndent ) +void XmlWriter::writeNodeProps( XmlNode *pNode, int nIndent, const Bu::FString &sIndent ) { for( int j = 0; j < pNode->getNumProperties(); j++ ) { writeString(" "); - writeString( pNode->getPropertyName( j ) ); + //writeString( pNode->getPropertyName( j ) ); writeString("=\""); - writeString( escape( pNode->getProperty( j ) ).c_str() ); + //writeString( escape( pNode->getProperty( j ) ).c_str() ); writeString("\""); } } -void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) +void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const Bu::FString &sIndent ) { if( pNode->hasChildren() ) { @@ -96,15 +90,15 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) writeString("<"); writeString( pNode->getName() ); writeNodeProps( pNode, nIndent, sIndent ); - if( sIndent ) + if( sIndent != "" ) writeString(">\n"); else writeString(">"); - +/* if( pNode->getContent( 0 ) ) { writeIndent( nIndent+1, sIndent ); - if( sIndent ) + if( sIndent != "" ) { writeString( pNode->getContent( 0 ) ); writeString("\n"); @@ -129,9 +123,9 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) writeString( pNode->getContent( j+1 ) ); } } - +*/ writeIndent( nIndent, sIndent ); - if( sIndent ) + if( sIndent != "" ) { writeString("getName() ); @@ -143,7 +137,7 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) writeString( pNode->getName() ); writeString(">"); } - } + }/* else if( pNode->getContent() ) { writeIndent( nIndent, sIndent ); @@ -157,14 +151,14 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) writeString(">"); if( sIndent ) writeString("\n"); - } + }*/ else { writeIndent( nIndent, sIndent ); writeString("<"); writeString( pNode->getName() ); writeNodeProps( pNode, nIndent, sIndent ); - if( sIndent ) + if( sIndent != "" ) writeString("/>\n"); else writeString("/>"); diff --git a/src/xmlwriter.h b/src/xmlwriter.h index c48e810..7e3c876 100644 --- a/src/xmlwriter.h +++ b/src/xmlwriter.h @@ -31,7 +31,7 @@ public: * this to a tab or some spaces it will never effect the content of your * file. */ - XmlWriter( const char *sIndent=NULL, XmlNode *pRoot=NULL ); + XmlWriter( const Bu::FString &sIndent="", XmlNode *pRoot=NULL ); /** * Destroy the writer. @@ -49,16 +49,16 @@ public: void write(); private: - std::string sIndent; /**< The indent string */ + Bu::FString sIndent; /**< The indent string */ - std::string escape( std::string sIn ); + Bu::FString escape( const Bu::FString &sIn ); /** * Write the file. *@param pNode The root node *@param sIndent The indent text. */ - void write( XmlNode *pNode, const char *sIndent=NULL ); + void write( XmlNode *pNode, const Bu::FString &sIndent ); /** * Write a node in the file, including children. @@ -66,7 +66,7 @@ private: *@param nIndent The indent level (the number of times to include sIndent) *@param sIndent The indent text. */ - void writeNode( XmlNode *pNode, int nIndent, const char *sIndent ); + void writeNode( XmlNode *pNode, int nIndent, const Bu::FString &sIndent ); /** * Write the properties of a node. @@ -74,14 +74,14 @@ private: *@param nIndent The indent level of the containing node *@param sIndent The indent text. */ - void writeNodeProps( XmlNode *pNode, int nIndent, const char *sIndent ); + void writeNodeProps( XmlNode *pNode, int nIndent, const Bu::FString &sIndent ); /** * Called to write the actual indent. *@param nIndent The indent level. *@param sIndent The indent text. */ - void writeIndent( int nIndent, const char *sIndent ); + void writeIndent( int nIndent, const Bu::FString &sIndent ); /** * This is the function that must be overridden in order to use this class. @@ -90,7 +90,7 @@ private: * will break the XML formatting. *@param sString The string data to write to the output. */ - virtual void writeString( const char *sString ) = 0; + virtual void writeString( const Bu::FString &sString ) = 0; }; #endif -- cgit v1.2.3