1 | /***************************************************************************
|
---|
2 | * Copyright (C) 2008 by Daniel Wendt *
|
---|
3 | * gentoo.murray@gmail.com *
|
---|
4 | * *
|
---|
5 | * This program is free software; you can redistribute it and/or modify *
|
---|
6 | * it under the terms of the GNU General Public License as published by *
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or *
|
---|
8 | * (at your option) any later version. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License *
|
---|
16 | * along with this program; if not, write to the *
|
---|
17 | * Free Software Foundation, Inc., *
|
---|
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
---|
19 | ***************************************************************************/
|
---|
20 |
|
---|
21 | #include "stdafx.h"
|
---|
22 | #include "XMLParser.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | namespace xml
|
---|
26 | {
|
---|
27 |
|
---|
28 | //------------------------------------- global Expat Callbacks:
|
---|
29 |
|
---|
30 | static void startElement(void *userData, const char *name, const char **atts)
|
---|
31 | {
|
---|
32 | XMLParserCallback* pCallback = (XMLParserCallback*) userData;
|
---|
33 | if( pCallback ) pCallback->StartElement( name, atts );
|
---|
34 | }
|
---|
35 |
|
---|
36 | static void endElement(void *userData, const char *name)
|
---|
37 | {
|
---|
38 | XMLParserCallback* pCallback = (XMLParserCallback*) userData;
|
---|
39 | if( pCallback ) pCallback->EndElement( name );
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | int XMLParser::Parse( XMLParserCallback& rCallback, const char* chFileName )
|
---|
46 | {
|
---|
47 | int ret = 1; // File not found
|
---|
48 |
|
---|
49 | FILE* fp = fopen(chFileName,"rb");
|
---|
50 | if( fp )
|
---|
51 | {
|
---|
52 | XML_Parser parser = XML_ParserCreate(NULL);
|
---|
53 |
|
---|
54 | XML_SetUserData( parser, (void*)&rCallback );
|
---|
55 |
|
---|
56 | // register Callbacks for start- and end-element events of the parser:
|
---|
57 | XML_SetElementHandler(parser, startElement, endElement);
|
---|
58 |
|
---|
59 | int done;
|
---|
60 | do // loop over whole file content
|
---|
61 | {
|
---|
62 | char buf[BUFSIZ];
|
---|
63 | size_t len = fread(buf, 1, sizeof(buf), fp); // read chunk of data
|
---|
64 | done = len < sizeof(buf); // end of file reached if buffer not completely filled
|
---|
65 | if (!XML_Parse(parser, buf, (int)len, done))
|
---|
66 | {
|
---|
67 | // a parse error occured:
|
---|
68 | fprintf(stderr,
|
---|
69 | "%s at line %d\n",
|
---|
70 | XML_ErrorString(XML_GetErrorCode(parser)),
|
---|
71 | XML_GetCurrentLineNumber(parser));
|
---|
72 | fclose(fp);
|
---|
73 | ret = 2; // quit, return = 2 indicating parsing error
|
---|
74 | done = 1;
|
---|
75 | }
|
---|
76 | } while (!done);
|
---|
77 |
|
---|
78 | XML_ParserFree(parser);
|
---|
79 | fclose(fp);
|
---|
80 | ret = 0;
|
---|
81 | }
|
---|
82 | return ret; // return = 0 indicating success
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | } // end namespace xml
|
---|
88 | //! \endcond
|
---|