mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-29 16:31:11 +02:00
our current experimental Neutrino branch
git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@27 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
178
lib/xmltree/xmlinterface.cpp
Normal file
178
lib/xmltree/xmlinterface.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* $Header: /cvs/tuxbox/apps/dvb/zapit/src/xmlinterface.cpp,v 1.25 2004/04/07 19:33:21 thegoodguy Exp $
|
||||
*
|
||||
* xmlinterface for zapit - d-box2 linux project
|
||||
*
|
||||
* (C) 2002 by thegoodguy <thegoodguy@berlios.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <xmlinterface.h>
|
||||
|
||||
#ifdef USE_LIBXML
|
||||
#include <libxml/xmlmemory.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/parserInternals.h>
|
||||
#else /* USE_LIBXML */
|
||||
#include <xmltok.h>
|
||||
#endif /* USE_LIBXML */
|
||||
|
||||
|
||||
unsigned long xmlGetNumericAttribute(const xmlNodePtr node, char *name, const int base)
|
||||
{
|
||||
char *ptr = xmlGetAttribute(node, name);
|
||||
|
||||
if (!ptr)
|
||||
return 0;
|
||||
|
||||
return strtoul(ptr, 0, base);
|
||||
}
|
||||
|
||||
long xmlGetSignedNumericAttribute(const xmlNodePtr node, char *name, const int base)
|
||||
{
|
||||
char *ptr = xmlGetAttribute(node, name);
|
||||
|
||||
if (!ptr)
|
||||
return 0;
|
||||
|
||||
return strtol(ptr, 0, base);
|
||||
}
|
||||
|
||||
xmlNodePtr xmlGetNextOccurence(xmlNodePtr cur, const char * s)
|
||||
{
|
||||
while ((cur != NULL) && (strcmp(xmlGetName(cur), s) != 0))
|
||||
cur = cur->xmlNextNode;
|
||||
return cur;
|
||||
}
|
||||
|
||||
|
||||
std::string Unicode_Character_to_UTF8(const int character)
|
||||
{
|
||||
#ifdef USE_LIBXML
|
||||
xmlChar buf[5];
|
||||
int length = xmlCopyChar(4, buf, character);
|
||||
return std::string((char*)buf, length);
|
||||
#else /* USE_LIBXML */
|
||||
char buf[XML_UTF8_ENCODE_MAX];
|
||||
int length = XmlUtf8Encode(character, buf);
|
||||
return std::string(buf, length);
|
||||
#endif /* USE_LIBXML */
|
||||
}
|
||||
|
||||
#ifdef USE_LIBXML
|
||||
xmlDocPtr parseXmlFile(const char * filename)
|
||||
{
|
||||
xmlDocPtr doc;
|
||||
xmlNodePtr cur;
|
||||
|
||||
doc = xmlParseFile(filename);
|
||||
|
||||
if (doc == NULL)
|
||||
{
|
||||
WARN("Error parsing \"%s\"", filename);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur = xmlDocGetRootElement(doc);
|
||||
if (cur == NULL)
|
||||
{
|
||||
WARN("Empty document\n");
|
||||
xmlFreeDoc(doc);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
#else /* USE_LIBXML */
|
||||
xmlDocPtr parseXml(const char * data)
|
||||
{
|
||||
XMLTreeParser* tree_parser;
|
||||
|
||||
tree_parser = new XMLTreeParser(NULL);
|
||||
|
||||
if (!tree_parser->Parse(data, strlen(data), true))
|
||||
{
|
||||
printf("Error parsing XML Data: %s at line %d\n",
|
||||
tree_parser->ErrorString(tree_parser->GetErrorCode()),
|
||||
tree_parser->GetCurrentLineNumber());
|
||||
|
||||
delete tree_parser;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!tree_parser->RootNode())
|
||||
{
|
||||
printf("Error: No Root Node\n");
|
||||
delete tree_parser;
|
||||
return NULL;
|
||||
}
|
||||
return tree_parser;
|
||||
}
|
||||
|
||||
xmlDocPtr parseXmlFile(const char * filename)
|
||||
{
|
||||
char buffer[2048];
|
||||
XMLTreeParser* tree_parser;
|
||||
size_t done;
|
||||
size_t length;
|
||||
FILE* xml_file;
|
||||
|
||||
xml_file = fopen(filename, "r");
|
||||
|
||||
if (xml_file == NULL)
|
||||
{
|
||||
perror(filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tree_parser = new XMLTreeParser(NULL);
|
||||
|
||||
do
|
||||
{
|
||||
length = fread(buffer, 1, sizeof(buffer), xml_file);
|
||||
done = length < sizeof(buffer);
|
||||
|
||||
if (!tree_parser->Parse(buffer, length, done))
|
||||
{
|
||||
printf("Error parsing \"%s\": %s at line %d\n",
|
||||
filename,
|
||||
tree_parser->ErrorString(tree_parser->GetErrorCode()),
|
||||
tree_parser->GetCurrentLineNumber());
|
||||
|
||||
fclose(xml_file);
|
||||
delete tree_parser;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
while (!done);
|
||||
|
||||
fclose(xml_file);
|
||||
|
||||
if (!tree_parser->RootNode())
|
||||
{
|
||||
delete tree_parser;
|
||||
return NULL;
|
||||
}
|
||||
return tree_parser;
|
||||
}
|
||||
#endif /* USE_LIBXML */
|
Reference in New Issue
Block a user