diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-05-11 07:51:40 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-05-11 07:51:40 +0000 |
commit | 033c41ed57348abb3a418166b1fb39bfad3312de (patch) | |
tree | 72edbb0b7ff35ef35e4d533bca384b4f7c986942 /src/xmlnode.h | |
parent | ad92dc50b7cdf7cfe086f21d19442d03a90fd05d (diff) | |
download | libbu++-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/xmlnode.h')
-rw-r--r-- | src/xmlnode.h | 101 |
1 files changed, 36 insertions, 65 deletions
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 @@ | |||
2 | #define XMLNODE | 2 | #define XMLNODE |
3 | 3 | ||
4 | #include <iostream> | 4 | #include <iostream> |
5 | #include "linkedlist.h" | 5 | #include "bu/list.h" |
6 | #include "hashtable.h" | 6 | #include "bu/hash.h" |
7 | #include "bu/fstring.h" | ||
7 | 8 | ||
8 | /** | 9 | /** |
9 | * Maintains all data pertient to an XML node, including sub-nodes and content. | 10 | * Maintains all data pertient to an XML node, including sub-nodes and content. |
@@ -25,9 +26,8 @@ public: | |||
25 | *@param sContent The initial content string. | 26 | *@param sContent The initial content string. |
26 | */ | 27 | */ |
27 | XmlNode( | 28 | XmlNode( |
28 | const char *sName=NULL, | 29 | const Bu::FString &sName, |
29 | XmlNode *pParent = NULL, | 30 | XmlNode *pParent=NULL |
30 | const char *sContent=NULL | ||
31 | ); | 31 | ); |
32 | 32 | ||
33 | /** | 33 | /** |
@@ -39,7 +39,7 @@ public: | |||
39 | * Change the name of the node. | 39 | * Change the name of the node. |
40 | *@param sName The new name of the node. | 40 | *@param sName The new name of the node. |
41 | */ | 41 | */ |
42 | void setName( const char *sName ); | 42 | //void setName( const char *sName ); |
43 | 43 | ||
44 | /** | 44 | /** |
45 | * Construct a new node and add it as a child to this node, also return a | 45 | * Construct a new node and add it as a child to this node, also return a |
@@ -48,7 +48,7 @@ public: | |||
48 | *@param sContent The initial content of the new node. | 48 | *@param sContent The initial content of the new node. |
49 | *@returns A pointer to the newly created child node. | 49 | *@returns A pointer to the newly created child node. |
50 | */ | 50 | */ |
51 | XmlNode *addChild( const char *sName, const char *sContent=NULL ); | 51 | XmlNode *addChild( const Bu::FString &sName ); |
52 | 52 | ||
53 | /** | 53 | /** |
54 | * Add an already created XmlNode as a child to this node. The new child | 54 | * Add an already created XmlNode as a child to this node. The new child |
@@ -65,7 +65,7 @@ public: | |||
65 | * in use will overwrite that property. | 65 | * in use will overwrite that property. |
66 | *@param sValue The textual value of the property. | 66 | *@param sValue The textual value of the property. |
67 | */ | 67 | */ |
68 | void addProperty( const char *sName, const char *sValue ); | 68 | void addProperty( const Bu::FString &sName, const Bu::FString &sValue ); |
69 | 69 | ||
70 | /** | 70 | /** |
71 | * Get a pointer to the parent node, if any. | 71 | * Get a pointer to the parent node, if any. |
@@ -86,14 +86,6 @@ public: | |||
86 | int getNumChildren(); | 86 | int getNumChildren(); |
87 | 87 | ||
88 | /** | 88 | /** |
89 | * Get a child node at a specific index. | ||
90 | *@param nIndex The zero-based index of the child to retreive. | ||
91 | *@returns A pointer to the child, or NULL if you requested an invalid | ||
92 | * index. | ||
93 | */ | ||
94 | XmlNode *getChild( int nIndex ); | ||
95 | |||
96 | /** | ||
97 | * Get a child with the specified name, and possibly skip value. For an | 89 | * Get a child with the specified name, and possibly skip value. For an |
98 | * explination of skip values see the HashTable. | 90 | * explination of skip values see the HashTable. |
99 | *@param sName The name of the child to find. | 91 | *@param sName The name of the child to find. |
@@ -101,14 +93,14 @@ public: | |||
101 | *@returns A pointer to the child, or NULL if no child with that name was | 93 | *@returns A pointer to the child, or NULL if no child with that name was |
102 | * found. | 94 | * found. |
103 | */ | 95 | */ |
104 | XmlNode *getChild( const char *sName, int nSkip=0 ); | 96 | XmlNode *getChild( const Bu::FString &sName, int nSkip=0 ); |
105 | 97 | ||
106 | /** | 98 | /** |
107 | * Get a pointer to the name of this node. Do not change this, use setName | 99 | * Get a pointer to the name of this node. Do not change this, use setName |
108 | * instead. | 100 | * instead. |
109 | *@returns A pointer to the name of this node. | 101 | *@returns A pointer to the name of this node. |
110 | */ | 102 | */ |
111 | const char *getName(); | 103 | Bu::FString getName(); |
112 | 104 | ||
113 | /** | 105 | /** |
114 | * Set the content of this node, optionally at a specific index. Using the | 106 | * Set the content of this node, optionally at a specific index. Using the |
@@ -116,14 +108,7 @@ public: | |||
116 | *@param sContent The content string to use. | 108 | *@param sContent The content string to use. |
117 | *@param nIndex The index of the content. | 109 | *@param nIndex The index of the content. |
118 | */ | 110 | */ |
119 | void setContent( const char *sContent, int nIndex=-1 ); | 111 | //void setContent( const char *sContent, int nIndex=-1 ); |
120 | |||
121 | /** | ||
122 | * Get the content string at a given index, or zero for initial content. | ||
123 | *@param nIndex The index of the content. | ||
124 | *@returns A pointer to the content at that location. | ||
125 | */ | ||
126 | const char *getContent( int nIndex = 0 ); | ||
127 | 112 | ||
128 | /** | 113 | /** |
129 | * Get the number of properties in this node. | 114 | * Get the number of properties in this node. |
@@ -132,36 +117,12 @@ public: | |||
132 | int getNumProperties(); | 117 | int getNumProperties(); |
133 | 118 | ||
134 | /** | 119 | /** |
135 | * Get a property's name by index. | ||
136 | *@param nIndex The index of the property to examine. | ||
137 | *@returns A pointer to the name of the property specified, or NULL if none | ||
138 | * found. | ||
139 | */ | ||
140 | const char *getPropertyName( int nIndex ); | ||
141 | |||
142 | /** | ||
143 | * Get a proprty's value by index. | ||
144 | *@param nIndex The index of the property to examine. | ||
145 | *@returns A pointer to the value of the property specified, or NULL if none | ||
146 | * found. | ||
147 | */ | ||
148 | const char *getProperty( int nIndex ); | ||
149 | |||
150 | /** | ||
151 | * Get a propery's value by name. | 120 | * Get a propery's value by name. |
152 | *@param sName The name of the property to examine. | 121 | *@param sName The name of the property to examine. |
153 | *@returns A pointer to the value of the property specified, or NULL if none | 122 | *@returns A pointer to the value of the property specified, or NULL if none |
154 | * found. | 123 | * found. |
155 | */ | 124 | */ |
156 | const char *getProperty( const char *sName ); | 125 | Bu::FString getProperty( const Bu::FString &sName ); |
157 | |||
158 | /** | ||
159 | * Delete a property by index. | ||
160 | *@param nIndex The index of the property to delete. | ||
161 | *@returns True if the property was found and deleted, false if it wasn't | ||
162 | * found. | ||
163 | */ | ||
164 | void deleteProperty( int nIndex ); | ||
165 | 126 | ||
166 | /** | 127 | /** |
167 | * Delete a child node, possibly replacing it with some text. This actually | 128 | * Delete a child node, possibly replacing it with some text. This actually |
@@ -171,7 +132,7 @@ public: | |||
171 | *@returns True of the node was found, and deleted, false if it wasn't | 132 | *@returns True of the node was found, and deleted, false if it wasn't |
172 | * found. | 133 | * found. |
173 | */ | 134 | */ |
174 | void deleteNode( int nIndex, const char *sReplacementText = NULL ); | 135 | //void deleteNode( int nIndex, const char *sReplacementText = NULL ); |
175 | 136 | ||
176 | /** | 137 | /** |
177 | * Delete a given node, but move all of it's children and content up to | 138 | * Delete a given node, but move all of it's children and content up to |
@@ -180,7 +141,7 @@ public: | |||
180 | *@param nIndex The node to delete. | 141 | *@param nIndex The node to delete. |
181 | *@returns True if the node was found and deleted, false if it wasn't. | 142 | *@returns True if the node was found and deleted, false if it wasn't. |
182 | */ | 143 | */ |
183 | void deleteNodeKeepChildren( int nIndex ); | 144 | //void deleteNodeKeepChildren( int nIndex ); |
184 | 145 | ||
185 | /** | 146 | /** |
186 | * Detatch a given child node from this node. This effectively works just | 147 | * Detatch a given child node from this node. This effectively works just |
@@ -192,7 +153,7 @@ public: | |||
192 | *@returns A pointer to the newly detatched node, which then passes | 153 | *@returns A pointer to the newly detatched node, which then passes |
193 | * ownership to the caller. | 154 | * ownership to the caller. |
194 | */ | 155 | */ |
195 | XmlNode *detatchNode( int nIndex, const char *sReplacementText = NULL ); | 156 | //XmlNode *detatchNode( int nIndex, const char *sReplacementText = NULL ); |
196 | 157 | ||
197 | /** | 158 | /** |
198 | * Replace a given node with a different node that is not currently owned by | 159 | * Replace a given node with a different node that is not currently owned by |
@@ -201,7 +162,7 @@ public: | |||
201 | *@param pNewNode The new node to replace the old node with. | 162 | *@param pNewNode The new node to replace the old node with. |
202 | *@returns True if the node was found and replaced, false if it wasn't. | 163 | *@returns True if the node was found and replaced, false if it wasn't. |
203 | */ | 164 | */ |
204 | void replaceNode( int nIndex, XmlNode *pNewNode ); | 165 | //void replaceNode( int nIndex, XmlNode *pNewNode ); |
205 | 166 | ||
206 | /** | 167 | /** |
207 | * Replace a given node with the children and content of a given node. | 168 | * Replace a given node with the children and content of a given node. |
@@ -210,24 +171,34 @@ public: | |||
210 | * replace the node specified by nIndex. | 171 | * replace the node specified by nIndex. |
211 | *@returns True if the node was found and replaced, false if it wasn't. | 172 | *@returns True if the node was found and replaced, false if it wasn't. |
212 | */ | 173 | */ |
213 | void replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ); | 174 | //void replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ); |
214 | 175 | ||
215 | /** | 176 | /** |
216 | * Get a copy of this node and all children. getCopy is recursive, so | 177 | * Get a copy of this node and all children. getCopy is recursive, so |
217 | * beware copying large trees of xml. | 178 | * beware copying large trees of xml. |
218 | *@returns A newly created copy of this node and all of it's children. | 179 | *@returns A newly created copy of this node and all of it's children. |
219 | */ | 180 | */ |
220 | XmlNode *getCopy(); | 181 | //XmlNode *getCopy(); |
182 | |||
183 | enum ChildType | ||
184 | { | ||
185 | typeNode, | ||
186 | typeContent | ||
187 | }; | ||
221 | 188 | ||
222 | private: | 189 | private: |
223 | std::string sName; /**< The name of the node. */ | 190 | typedef struct |
224 | std::string *sPreContent; /**< The content that goes before any node. */ | 191 | { |
225 | LinkedList lChildren; /**< The children. */ | 192 | uint8_t nType; |
226 | LinkedList lPostContent; /**< The content that comes after children. */ | 193 | union { |
227 | HashTable hProperties; /**< Property hashtable. */ | 194 | XmlNode *pNode; |
228 | HashTable hChildren; /**< Children hashtable. */ | 195 | Bu::FString *pContent; |
229 | LinkedList lPropNames; /**< List of property names. */ | 196 | }; |
230 | LinkedList lPropValues; /**< List of property values. */ | 197 | } Child; |
198 | Bu::FString sName; /**< The name of the node. */ | ||
199 | Bu::List<Child> lChildren; /**< The children. */ | ||
200 | Bu::Hash<Bu::FString, Bu::FString> hProperties; /**< Property hashtable. */ | ||
201 | Bu::Hash<Bu::FString, Bu::List<XmlNode *> > hChildren; /**< Children hashtable. */ | ||
231 | XmlNode *pParent; /**< A pointer to the parent of this node. */ | 202 | XmlNode *pParent; /**< A pointer to the parent of this node. */ |
232 | int nCurContent; /**< The current content we're on, for using the -1 on | 203 | int nCurContent; /**< The current content we're on, for using the -1 on |
233 | setContent. */ | 204 | setContent. */ |