aboutsummaryrefslogtreecommitdiff
path: root/src/xmlwriter.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-05-11 07:51:40 +0000
committerMike Buland <eichlan@xagasoft.com>2007-05-11 07:51:40 +0000
commit033c41ed57348abb3a418166b1fb39bfad3312de (patch)
tree72edbb0b7ff35ef35e4d533bca384b4f7c986942 /src/xmlwriter.cpp
parentad92dc50b7cdf7cfe086f21d19442d03a90fd05d (diff)
downloadlibbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.gz
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.bz2
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.xz
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.zip
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.
Diffstat (limited to 'src/xmlwriter.cpp')
-rw-r--r--src/xmlwriter.cpp62
1 files changed, 28 insertions, 34 deletions
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 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include "xmlwriter.h" 3#include "xmlwriter.h"
4 4
5XmlWriter::XmlWriter( const char *sIndent, XmlNode *pRoot ) : 5XmlWriter::XmlWriter( const Bu::FString &sIndent, XmlNode *pRoot ) :
6 XmlDocument( pRoot ) 6 XmlDocument( pRoot ),
7 sIndent( sIndent )
7{ 8{
8 if( sIndent == NULL )
9 {
10 this->sIndent = "";
11 }
12 else
13 {
14 this->sIndent = sIndent;
15 }
16} 9}
17 10
18XmlWriter::~XmlWriter() 11XmlWriter::~XmlWriter()
@@ -24,7 +17,7 @@ void XmlWriter::write()
24 write( getRoot(), sIndent.c_str() ); 17 write( getRoot(), sIndent.c_str() );
25} 18}
26 19
27void XmlWriter::write( XmlNode *pRoot, const char *sIndent ) 20void XmlWriter::write( XmlNode *pRoot, const Bu::FString &sIndent )
28{ 21{
29 writeNode( pRoot, 0, sIndent ); 22 writeNode( pRoot, 0, sIndent );
30} 23}
@@ -39,7 +32,7 @@ void XmlWriter::closeNode()
39 } 32 }
40} 33}
41 34
42void XmlWriter::writeIndent( int nIndent, const char *sIndent ) 35void XmlWriter::writeIndent( int nIndent, const Bu::FString &sIndent )
43{ 36{
44 if( sIndent == NULL ) return; 37 if( sIndent == NULL ) return;
45 for( int j = 0; j < nIndent; j++ ) 38 for( int j = 0; j < nIndent; j++ )
@@ -48,26 +41,27 @@ void XmlWriter::writeIndent( int nIndent, const char *sIndent )
48 } 41 }
49} 42}
50 43
51std::string XmlWriter::escape( std::string sIn ) 44Bu::FString XmlWriter::escape( const Bu::FString &sIn )
52{ 45{
53 std::string sOut; 46 Bu::FString sOut;
54 47
55 std::string::const_iterator i; 48 int nMax = sIn.getSize();
56 for( i = sIn.begin(); i != sIn.end(); i++ ) 49 for( int j = 0; j < nMax; j++ )
57 { 50 {
58 if( ((*i >= ' ' && *i <= '9') || 51 char c = sIn[j];
59 (*i >= 'a' && *i <= 'z') || 52 if( ((c >= ' ' && c <= '9') ||
60 (*i >= 'A' && *i <= 'Z') ) && 53 (c >= 'a' && c <= 'z') ||
61 (*i != '\"' && *i != '\'' && *i != '&') 54 (c >= 'A' && c <= 'Z') ) &&
55 (c != '\"' && c != '\'' && c != '&')
62 ) 56 )
63 { 57 {
64 sOut += *i; 58 sOut += c;
65 } 59 }
66 else 60 else
67 { 61 {
68 sOut += "&#"; 62 sOut += "&#";
69 char buf[4]; 63 char buf[4];
70 sprintf( buf, "%u", (unsigned char)*i ); 64 sprintf( buf, "%u", (unsigned char)c );
71 sOut += buf; 65 sOut += buf;
72 sOut += ';'; 66 sOut += ';';
73 } 67 }
@@ -76,19 +70,19 @@ std::string XmlWriter::escape( std::string sIn )
76 return sOut; 70 return sOut;
77} 71}
78 72
79void XmlWriter::writeNodeProps( XmlNode *pNode, int nIndent, const char *sIndent ) 73void XmlWriter::writeNodeProps( XmlNode *pNode, int nIndent, const Bu::FString &sIndent )
80{ 74{
81 for( int j = 0; j < pNode->getNumProperties(); j++ ) 75 for( int j = 0; j < pNode->getNumProperties(); j++ )
82 { 76 {
83 writeString(" "); 77 writeString(" ");
84 writeString( pNode->getPropertyName( j ) ); 78 //writeString( pNode->getPropertyName( j ) );
85 writeString("=\""); 79 writeString("=\"");
86 writeString( escape( pNode->getProperty( j ) ).c_str() ); 80 //writeString( escape( pNode->getProperty( j ) ).c_str() );
87 writeString("\""); 81 writeString("\"");
88 } 82 }
89} 83}
90 84
91void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent ) 85void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const Bu::FString &sIndent )
92{ 86{
93 if( pNode->hasChildren() ) 87 if( pNode->hasChildren() )
94 { 88 {
@@ -96,15 +90,15 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent )
96 writeString("<"); 90 writeString("<");
97 writeString( pNode->getName() ); 91 writeString( pNode->getName() );
98 writeNodeProps( pNode, nIndent, sIndent ); 92 writeNodeProps( pNode, nIndent, sIndent );
99 if( sIndent ) 93 if( sIndent != "" )
100 writeString(">\n"); 94 writeString(">\n");
101 else 95 else
102 writeString(">"); 96 writeString(">");
103 97/*
104 if( pNode->getContent( 0 ) ) 98 if( pNode->getContent( 0 ) )
105 { 99 {
106 writeIndent( nIndent+1, sIndent ); 100 writeIndent( nIndent+1, sIndent );
107 if( sIndent ) 101 if( sIndent != "" )
108 { 102 {
109 writeString( pNode->getContent( 0 ) ); 103 writeString( pNode->getContent( 0 ) );
110 writeString("\n"); 104 writeString("\n");
@@ -129,9 +123,9 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent )
129 writeString( pNode->getContent( j+1 ) ); 123 writeString( pNode->getContent( j+1 ) );
130 } 124 }
131 } 125 }
132 126*/
133 writeIndent( nIndent, sIndent ); 127 writeIndent( nIndent, sIndent );
134 if( sIndent ) 128 if( sIndent != "" )
135 { 129 {
136 writeString("</"); 130 writeString("</");
137 writeString( pNode->getName() ); 131 writeString( pNode->getName() );
@@ -143,7 +137,7 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent )
143 writeString( pNode->getName() ); 137 writeString( pNode->getName() );
144 writeString(">"); 138 writeString(">");
145 } 139 }
146 } 140 }/*
147 else if( pNode->getContent() ) 141 else if( pNode->getContent() )
148 { 142 {
149 writeIndent( nIndent, sIndent ); 143 writeIndent( nIndent, sIndent );
@@ -157,14 +151,14 @@ void XmlWriter::writeNode( XmlNode *pNode, int nIndent, const char *sIndent )
157 writeString(">"); 151 writeString(">");
158 if( sIndent ) 152 if( sIndent )
159 writeString("\n"); 153 writeString("\n");
160 } 154 }*/
161 else 155 else
162 { 156 {
163 writeIndent( nIndent, sIndent ); 157 writeIndent( nIndent, sIndent );
164 writeString("<"); 158 writeString("<");
165 writeString( pNode->getName() ); 159 writeString( pNode->getName() );
166 writeNodeProps( pNode, nIndent, sIndent ); 160 writeNodeProps( pNode, nIndent, sIndent );
167 if( sIndent ) 161 if( sIndent != "" )
168 writeString("/>\n"); 162 writeString("/>\n");
169 else 163 else
170 writeString("/>"); 164 writeString("/>");