Files Classes Functions Hierarchy
00001 #include <iostream> 00002 using namespace std; 00003 00004 #include <stringconvert.h> 00005 #include <stringtagparser.h> 00006 #include <stringtagparsertest.h> 00007 #include <tokenizer.h> 00008 00009 string stringtagparsertest::doc[] = 00010 { 00011 "", 00012 "Extract data between unique tags.", 00013 "Read a taged array", 00014 "Unit test extracting data between tags.", 00015 "Extracting non-unique taged elements.", 00016 "Extract C++ code commands and arguments." 00017 }; 00018 00019 void stringtagparsertest::test01() 00020 { 00021 string str= 00022 "<gameD2>\ 00023 <startcolor>.324,.1,.8928</startcolor>\ 00024 <matrixD2>\ 00025 <m>25</m>\ 00026 <n>23</n>\ 00027 </matrixD2>\ 00028 <settings>\ 00029 <walls>1</walls>\ 00030 <pipes>0</pipes>\ 00031 </settings>\ 00032 </gameD2>\ 00033 "; 00034 00035 stringtagparser sp((stringc)str); 00036 00037 //sp.data("matrixD2"); 00038 00039 cout << "Inside walls tag." << endl; 00040 cout << sp.data("walls") << endl; 00041 cout << "Inside startcolor tag." << endl; 00042 cout << sp.data("startcolor") << endl; 00043 cout << "Inside settings tag." << endl; 00044 cout << sp.data("settings") << endl; 00045 00046 cout << endl; 00047 cout << "Including the tags matrixD2 with * delimeters" << endl; 00048 cout << "*" << sp.data_with_tags("matrixD2") << "*" << endl; 00049 00050 } 00051 00052 void stringtagparsertest::test02() 00053 { 00054 string str= 00055 "<data>\ 00056 <parts>\ 00057 <n>3</n>\ 00058 <1>\ 00059 <item>29960-2</item>\ 00060 <quantity>2</quantity>\ 00061 <desc>2GB PC5300 DDR2 667 MHz SO-Dim</desc>\ 00062 <sn>zsd283884</sn>\ 00063 </1>\ 00064 <2>\ 00065 <item>si393-02</item>\ 00066 <quantity>1</quantity>\ 00067 <desc>Logitech headset</desc>\ 00068 <sn></sn>\ 00069 </2>\ 00070 <3>\ 00071 <item>jsie-38291-13</item>\ 00072 <quantity>1</quantity>\ 00073 <desc>Netgear 3G Broadband Wireless Router MBR624GU</desc>\ 00074 <sn></sn>\ 00075 </3>\ 00076 </parts>\ 00077 </data>"; 00078 00079 // stringtagparser sp(str); 00080 // cout << sp.data("data","parts","n") << endl; 00081 00082 string parts = stringtagparser(str).data("data","parts"); 00083 uint n; 00084 stringtagparser sp(parts); 00085 stringfrom(n,sp.data("n")); 00086 00087 cout << n << endl; 00088 for (uint i=1; i<=n; ++i) 00089 { 00090 cout << "item: " << sp.data(stringto(i),"item") << endl; 00091 cout << "description: " << sp.data(stringto(i),"desc") << endl; 00092 } 00093 } 00094 00095 00096 int stringtagparsertest::unittest01() 00097 { 00098 string str= 00099 "<gameD2>\ 00100 <startcolor>.324,.1,.8928</startcolor>\ 00101 <matrixD2>\ 00102 <m>25</m>\ 00103 <n>23</n>\ 00104 </matrixD2>\ 00105 <settings>\ 00106 <walls>1</walls>\ 00107 <pipes>0</pipes>\ 00108 </settings>\ 00109 </gameD2>\ 00110 "; 00111 00112 string ans1="1"; 00113 string ans2=".324,.1,.8928"; 00114 string ans3="\ 00115 <walls>1</walls>\ 00116 <pipes>0</pipes>\ 00117 "; 00118 string ans4="<matrixD2>\ 00119 <m>25</m>\ 00120 <n>23</n>\ 00121 </matrixD2>"; 00122 00123 stringtagparser sp(str); 00124 00125 assertreturnT(sp.data("walls")==ans1,1); 00126 assertreturnT(sp.data("startcolor")==ans2,1); 00127 assertreturnT(sp.data("settings")==ans3,1); 00128 00129 //cout << "*" << sp.data_with_tags("matrixD2") << "*" << endl; 00130 //cout << "*" << ans4 << "*" << endl; 00131 00132 assertreturnT(sp.data_with_tags("matrixD2")==ans4,1); 00133 00134 return 0; 00135 } 00136 00137 void stringtagparsertest::test03() 00138 { 00139 cout << stringtagparsertest::doc[4] << endl; 00140 00141 string str= 00142 "<data>\ 00143 <funkyobj>\ 00144 <obj>3 4 2</obj>\ 00145 <obj>1 13 2</obj>\ 00146 <obj>9 0 2</obj>\ 00147 <obj>7 20 -2</obj>\ 00148 </funkyobj>\ 00149 </data>"; 00150 00151 cout << "str=" << str << endl; 00152 cout << "stringtagiter" << endl; 00153 00154 stringtagiter i(str,"obj"); 00155 00156 for (i.reset(); !i; ++i) 00157 { 00158 //cout << "#" << endl; 00159 cout << i() << endl; 00160 } 00161 00162 } 00163 00164 void stringtagparsertest::test04() 00165 { 00166 cout << "Example of using parser and tokenizer." << endl; 00167 cout << "Extracting C++ source code funtion calls" << endl; 00168 00169 string str="<gobj> glPushAttrib(GL_CURRENT_BIT); glPushAttrib(GL_LIGHTING_BIT); glDisable(GL_LIGHTING); glBegin(GL_LINES); glColor3f(1.0,0.0,0.0); glVertex3f(0.0,0.0,0.0); glVertex3f(1.0,0.0,0.0); glColor3f(0.0,1.0,0.0); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.0,0.0); glColor3f(0.0,0.0,1.0); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,0.0,1.0); glEnd(); glPopAttrib(); glPopAttrib(); </gobj>"; 00170 00171 cout << "str=" << str; 00172 stringtagparser p1(str); 00173 string s2(p1.data("gobj")); 00174 //cout << s2 << endl; 00175 00176 tokenizer ss(s2); 00177 ss.subtract(";"); // split string into substrings 00178 ss.trim(); 00179 ss.remove_if(); // delete empty strings 00180 // At this point all strings should be 00181 // of the form <name>(arg with commas) 00182 cout << "***" << endl; 00183 for ( ss.reset(); !ss; ++ss) 00184 { 00185 string& s(*ss); 00186 replace(s.begin(),s.end(),',',' '); 00187 replace(s.begin(),s.end(),'(',' '); 00188 replace(s.begin(),s.end(),')',' '); 00189 } 00190 for ( ss.reset(); !ss; ++ss) 00191 { cout << ss() << endl; } 00192 00193 } 00194 00195 int stringtagparsertest::unittest02() 00196 { 00197 cout << "" << endl; 00198 string str= 00199 "<mkerrors>\ 00200 <abc> </abc>\ 00201 <abcd></abcd>\ 00202 <currentsettings>\ 00203 <state> </state>\ 00204 <libraries></libraries>\ 00205 <projectdirectory></projectdirectory>\ 00206 </currentsettings>\ 00207 <configuration>\ 00208 <linux> <libraries>-lGL -lGLU -lglut</libraries></linux>\ 00209 <config001><libraries>/usr/local/lib/libntl.a<libaries>\ 00210 </config001>\ 00211 </configuration>\ 00212 </mkerrors>"; 00213 00214 stringtagparser sp(str); 00215 00216 string s; 00217 string tag; 00218 00219 cout << "Test with one space in the tag" << endln; 00220 tag="abc"; 00221 cout << SHOW(tag) << endln; 00222 s = sp.data(tag); 00223 cout << "*" << SHOW(s) << "*" << endl; 00224 00225 spacerdelete<>()[s]; 00226 assertreturnOS( (bool)spacerdelete<>()[s] ); 00227 cout << SHOW3(s) << endln; 00228 00229 assertreturnOS(sp.tagfound); 00230 cout << SHOW(sp.tagfound) << endln; 00231 00232 cout << "Test with no space in the tag" << endln; 00233 tag="abcd"; 00234 cout << SHOW(tag) << endl; 00235 s = sp.data(tag); 00236 cout << "*" << SHOW(s) << "*" << endl; 00237 00238 assertreturnOS( (bool)spacerdelete<>()[s] ); 00239 00240 cout << SHOW3(s) << endln; 00241 assertreturnOS(sp.tagfound); 00242 00243 tag="nonexistant_tag"; 00244 s = sp.data(tag); 00245 00246 assertreturnOS(!sp.tagfound); 00247 00248 return 0; 00249 } 00250 00251 00252 00253 00254
1.5.8