| 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 <cstdio>
|
|---|
| 22 | #include "stdafx.h"
|
|---|
| 23 | #include "XMLParser.h"
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | namespace xml
|
|---|
| 27 | {
|
|---|
| 28 |
|
|---|
| 29 | //------------------------------------- global Expat Callbacks:
|
|---|
| 30 |
|
|---|
| 31 | static void startElement(void *userData, const char *name, const char **atts)
|
|---|
| 32 | {
|
|---|
| 33 | XMLParserCallback* pCallback = (XMLParserCallback*) userData;
|
|---|
| 34 | if( pCallback ) pCallback->StartElement( name, atts );
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | static void endElement(void *userData, const char *name)
|
|---|
| 38 | {
|
|---|
| 39 | XMLParserCallback* pCallback = (XMLParserCallback*) userData;
|
|---|
| 40 | if( pCallback ) pCallback->EndElement( name );
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | int XMLParser::Parse( XMLParserCallback& rCallback, const char* chFileName )
|
|---|
| 47 | {
|
|---|
| 48 | int ret = 1; // File not found
|
|---|
| 49 |
|
|---|
| 50 | FILE* fp = fopen(chFileName,"rb");
|
|---|
| 51 | if( fp )
|
|---|
| 52 | {
|
|---|
| 53 | XML_Parser parser = XML_ParserCreate(NULL);
|
|---|
| 54 |
|
|---|
| 55 | XML_SetUserData( parser, (void*)&rCallback );
|
|---|
| 56 |
|
|---|
| 57 | // register Callbacks for start- and end-element events of the parser:
|
|---|
| 58 | XML_SetElementHandler(parser, startElement, endElement);
|
|---|
| 59 |
|
|---|
| 60 | int done;
|
|---|
| 61 | do // loop over whole file content
|
|---|
| 62 | {
|
|---|
| 63 | char buf[BUFSIZ];
|
|---|
| 64 | size_t len = fread(buf, 1, sizeof(buf), fp); // read chunk of data
|
|---|
| 65 | done = len < sizeof(buf); // end of file reached if buffer not completely filled
|
|---|
| 66 | if (!XML_Parse(parser, buf, (int)len, done))
|
|---|
| 67 | {
|
|---|
| 68 | // a parse error occured:
|
|---|
| 69 | fprintf(stderr,
|
|---|
| 70 | "%s at line %d\n",
|
|---|
| 71 | XML_ErrorString(XML_GetErrorCode(parser)),
|
|---|
| 72 | XML_GetCurrentLineNumber(parser));
|
|---|
| 73 | fclose(fp);
|
|---|
| 74 | ret = 2; // quit, return = 2 indicating parsing error
|
|---|
| 75 | done = 1;
|
|---|
| 76 | }
|
|---|
| 77 | } while (!done);
|
|---|
| 78 |
|
|---|
| 79 | XML_ParserFree(parser);
|
|---|
| 80 | fclose(fp);
|
|---|
| 81 | ret = 0;
|
|---|
| 82 | }
|
|---|
| 83 | return ret; // return = 0 indicating success
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | } // end namespace xml
|
|---|
| 89 | //! \endcond
|
|---|