source: trunk/examples/objects/xmlparser/xmlparser.cc @ 2289

Last change on this file since 2289 was 2289, checked in by ldelgass, 9 years ago

Fix memory leak of file data

File size: 9.0 KB
Line 
1#include <iostream>
2#include <errno.h>
3#include <fstream>
4#include <sys/stat.h>
5#include "RpParserXML.h"
6
7size_t
8readFile (
9    const char *filePath,
10    const char **buf)
11{
12    if (buf == NULL) {
13        fprintf(stderr,"buf is NULL while opening file \"%s\"", filePath);
14        return 0;
15    }
16
17    FILE *f;
18    f = fopen(filePath, "rb");
19    if (f == NULL) {
20        fprintf(stderr,"can't open \"%s\": %s", filePath, strerror(errno));
21        return 0;
22    }
23    struct stat stat;
24    if (fstat(fileno(f), &stat) < 0) {
25        fprintf(stderr,"can't stat \"%s\": %s", filePath, strerror(errno));
26        return 0;
27    }
28    off_t size;
29    size = stat.st_size;
30    char* memblock;
31    memblock = new char [size+1];
32    if (memblock == NULL) {
33        fprintf(stderr,"can't allocate %zu bytes for file \"%s\": %s",
34            (size_t) size, filePath, strerror(errno));
35        fclose(f);
36        return 0;
37    }
38
39    size_t nRead;
40    nRead = fread(memblock, sizeof(char), size, f);
41    fclose(f);
42
43    if (nRead != (size_t)size) {
44        fprintf(stderr,"can't read %zu bytes from \"%s\": %s", (size_t) size, filePath,
45            strerror(errno));
46        return 0;
47    }
48
49    memblock[size] = '\0';
50    *buf = memblock;
51    return nRead;
52}
53
54int xmlparser_0_0 ()
55{
56    const char *desc = "test creating xml parser";
57    const char *testname = "xmlparser_0_0";
58    int retVal = 0;
59
60    const char *expected = "initialized ParserXml Object";
61    const char *received = NULL;
62
63    Rp_ParserXml *p = NULL;
64
65    p = Rp_ParserXmlCreate();
66
67    if (p == NULL) {
68        printf("Error: %s\n", testname);
69        printf("\t%s\n", desc);
70        printf("\texpected \"%s\"\n",expected);
71        printf("\treceived \"%s\"\n",received);
72        retVal = 1;
73    }
74
75    if (p) {
76        Rp_ParserXmlDestroy(&p);
77    }
78
79    return retVal;
80}
81
82int xmlparser_1_0 ()
83{
84    const char *desc = "test sending xml text to xml parser";
85    const char *testname = "xmlparser_1_0";
86    const char *infile = "xmlparser_1_0_in.xml";
87    const char *outfile = "xmlparser_1_0_out.xml";
88    int retVal = 0;
89
90    const char *xmltext = NULL;
91    const char *expected = NULL;
92
93    readFile(infile,&xmltext);
94    readFile(outfile,&expected);
95
96    const char *received = NULL;
97
98    Rp_ParserXml *p = NULL;
99
100    p = Rp_ParserXmlCreate();
101
102    Rp_ParserXmlParse(p, xmltext);
103
104    received = Rp_ParserXmlXml(p);
105
106
107    if (strcmp(expected,received) != 0) {
108        printf("Error: %s\n", testname);
109        printf("\t%s\n", desc);
110        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
111        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
112        retVal = 1;
113    }
114
115    if (p) {
116        Rp_ParserXmlDestroy(&p);
117    }
118
119    if (xmltext) {
120        delete[] xmltext;
121        xmltext = NULL;
122    }
123    if (expected) {
124        delete[] expected;
125        expected = NULL;
126    }
127
128    return retVal;
129}
130
131
132int xmlparser_1_1 ()
133{
134    const char *desc = "test sending xml text to xml parser";
135    const char *testname = "xmlparser_1_1";
136    const char *infile = "xmlparser_1_1_in.xml";
137    const char *outfile = "xmlparser_1_1_out.xml";
138    int retVal = 0;
139
140    const char *xmltext = NULL;
141    const char *expected = NULL;
142
143    readFile(infile,&xmltext);
144    readFile(outfile,&expected);
145
146    const char *received = NULL;
147
148    Rp_ParserXml *p = NULL;
149
150    p = Rp_ParserXmlCreate();
151
152    Rp_ParserXmlParse(p, xmltext);
153
154    received = Rp_ParserXmlXml(p);
155
156
157    if (strcmp(expected,received) != 0) {
158        printf("Error: %s\n", testname);
159        printf("\t%s\n", desc);
160        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
161        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
162        retVal = 1;
163    }
164
165    if (p) {
166        Rp_ParserXmlDestroy(&p);
167    }
168
169    if (xmltext) {
170        delete[] xmltext;
171        xmltext = NULL;
172    }
173    if (expected) {
174        delete[] expected;
175        expected = NULL;
176    }
177
178    return retVal;
179}
180
181
182int xmlparser_2_0 ()
183{
184    const char *desc = "test printing xml as path/val combos";
185    const char *testname = "xmlparser_2_0";
186    const char *infile = "xmlparser_2_0_in.xml";
187    int retVal = 0;
188
189    const char *xmltext = NULL;
190
191    readFile(infile,&xmltext);
192
193    const char *expected = "\
194run.tool.about Press Simulate to view results.\n\
195run.tool.command tclsh @tool/fermi.tcl @driver\n\
196run.input.number(Ef).about.label Fermi Level\n\
197run.input.number(Ef).about.description Energy at center of distribution.\n\
198run.input.number(Ef).units eV\n\
199run.input.number(Ef).min -10eV\n\
200run.input.number(Ef).max 10eV\n\
201run.input.number(Ef).default 0eV\n\
202run.input.number(Ef).current 0eV\n";
203
204    const char *received = NULL;
205
206    Rp_ParserXml *p = NULL;
207
208    p = Rp_ParserXmlCreate();
209
210    Rp_ParserXmlParse(p, xmltext);
211
212    received = Rp_ParserXmlPathVal(p);
213
214
215    if (strcmp(expected,received) != 0) {
216        printf("Error: %s\n", testname);
217        printf("\t%s\n", desc);
218        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
219        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
220        retVal = 1;
221    }
222
223    if (p) {
224        Rp_ParserXmlDestroy(&p);
225    }
226
227    if (xmltext) {
228        delete[] xmltext;
229        xmltext = NULL;
230    }
231
232    return retVal;
233}
234
235int xmlparser_3_0 ()
236{
237    const char *desc = "test getting xml value";
238    const char *testname = "xmlparser_3_0";
239    const char *infile = "xmlparser_2_0_in.xml";
240    int retVal = 0;
241
242    const char *xmltext = NULL;
243
244    readFile(infile,&xmltext);
245
246    const char *expected = "0eV";
247
248    const char *received = NULL;
249
250    Rp_ParserXml *p = NULL;
251
252    p = Rp_ParserXmlCreate();
253
254    Rp_ParserXmlParse(p, xmltext);
255
256    received = Rp_ParserXmlGet(p,"input.number(Ef).current");
257
258
259    if (strcmp(expected,received) != 0) {
260        printf("Error: %s\n", testname);
261        printf("\t%s\n", desc);
262        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
263        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
264        retVal = 1;
265    }
266
267    if (p) {
268        Rp_ParserXmlDestroy(&p);
269    }
270
271    if (xmltext) {
272        delete[] xmltext;
273        xmltext = NULL;
274    }
275
276    return retVal;
277}
278
279int xmlparser_4_0 ()
280{
281    const char *desc = "test putting xml value";
282    const char *testname = "xmlparser_4_0";
283    const char *infile = "xmlparser_2_0_in.xml";
284    int retVal = 0;
285
286    const char *xmltext = NULL;
287
288    readFile(infile,&xmltext);
289
290    const char *expected = "hi mom";
291
292    const char *received = NULL;
293
294    Rp_ParserXml *p = NULL;
295
296    p = Rp_ParserXmlCreate();
297
298    Rp_ParserXmlParse(p, xmltext);
299
300    Rp_ParserXmlPut(p,"input.number(gg).current","hi mom",0);
301    received = Rp_ParserXmlGet(p,"input.number(gg).current");
302
303    if (strcmp(expected,received) != 0) {
304        printf("Error: %s\n", testname);
305        printf("\t%s\n", desc);
306        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
307        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
308        retVal = 1;
309    }
310
311    if (p) {
312        Rp_ParserXmlDestroy(&p);
313    }
314
315    if (xmltext) {
316        delete[] xmltext;
317        xmltext = NULL;
318    }
319
320    return retVal;
321}
322
323int xmlparser_5_0 ()
324{
325    const char *desc = "test printing xml with degrees of presets";
326    const char *testname = "xmlparser_5_0";
327    const char *infile = "xmlparser_5_0_in.xml";
328    int retVal = 0;
329
330    const char *xmltext = NULL;
331
332    readFile(infile,&xmltext);
333
334    const char *expected = "\
335run.tool.about Press Simulate to view results.\n\
336run.tool.command tclsh @tool/fermi.tcl @driver\n\
337run.input.number(Ef).about.label Fermi Level\n\
338run.input.number(Ef).about.description Energy at center of distribution.\n\
339run.input.number(Ef).units eV\n\
340run.input.number(Ef).min -10eV\n\
341run.input.number(Ef).max 10eV\n\
342run.input.number(Ef).default 0eV\n\
343run.input.number(Ef).current 0eV\n\
344run.input.number(Ef).preset.value 300K\n\
345run.input.number(Ef).preset.label 300K (room temperature)\n\
346run.input.number(Ef).preset.value 77K\n\
347run.input.number(Ef).preset.label 77K (liquid nitrogen)\n\
348run.input.number(Ef).preset.value 4.2K\n\
349run.input.number(Ef).preset.label 4.2K (liquid helium)\n\
350";
351
352    const char *received = NULL;
353
354    Rp_ParserXml *p = NULL;
355
356    p = Rp_ParserXmlCreate();
357
358    Rp_ParserXmlParse(p, xmltext);
359
360    received = Rp_ParserXmlPathVal(p);
361
362
363    if (strcmp(expected,received) != 0) {
364        printf("Error: %s\n", testname);
365        printf("\t%s\n", desc);
366        printf("\texpected %zu bytes: \"%s\"\n",strlen(expected), expected);
367        printf("\treceived %zu bytes: \"%s\"\n",strlen(received), received);
368        retVal = 1;
369    }
370
371    if (p) {
372        Rp_ParserXmlDestroy(&p);
373    }
374
375    if (xmltext) {
376        delete[] xmltext;
377        xmltext = NULL;
378    }
379
380    return retVal;
381}
382
383// FIXME: test what happens when parser sees self closing tag <tag/>
384// FIXME: test get function
385// FIXME: test put function
386// FIXME: test putf function
387// FIXME: test appendf function
388// FIXME: look into why Rp_ParserXmlPathVal hits some nodes twice in gdb
389
390int main()
391{
392    xmlparser_0_0();
393    xmlparser_1_0();
394    xmlparser_1_1();
395    xmlparser_2_0();
396    xmlparser_3_0();
397    xmlparser_4_0();
398    xmlparser_5_0();
399
400    return 0;
401}
Note: See TracBrowser for help on using the repository browser.