Merge with develop

--HG--
branch : compatibility-develop
This commit is contained in:
kervala 2017-04-22 18:19:33 +02:00
commit f739ed9e62
4 changed files with 93 additions and 47 deletions

View file

@ -110,6 +110,11 @@ public:
*/ */
bool init (IStream &stream); bool init (IStream &stream);
/** Return the error string.
* if not empty, something wrong appends
*/
static std::string getErrorString();
/** Release the resources used by the stream. /** Release the resources used by the stream.
*/ */
void release (); void release ();
@ -172,7 +177,12 @@ public:
static bool getContentString (std::string &result, xmlNodePtr node); static bool getContentString (std::string &result, xmlNodePtr node);
/** /**
* Release meory used by libxml2, to only call before exit. * Init all structures used by libxml2, to only call once.
*/
static void initLibXml();
/**
* Release memory used by libxml2, to only call before exit.
*/ */
static void releaseLibXml(); static void releaseLibXml();
@ -234,13 +244,16 @@ private:
uint _ContentStringIndex; uint _ContentStringIndex;
// Error message // Error message
std::string _ErrorString; static std::string _ErrorString;
// Try binary mode // Try binary mode
bool _TryBinaryMode; bool _TryBinaryMode;
// If not NULL, binary mode detected, use this stream in serials // If not NULL, binary mode detected, use this stream in serials
IStream *_BinaryStream; IStream *_BinaryStream;
// LibXml has been initialized
static bool _LibXmlIntialized;
}; };

View file

@ -80,7 +80,6 @@ class COXml : public IStream
{ {
friend int xmlOutputWriteCallbackForNeL ( void *context, const char *buffer, int len ); friend int xmlOutputWriteCallbackForNeL ( void *context, const char *buffer, int len );
friend int xmlOutputCloseCallbackForNeL ( void *context ); friend int xmlOutputCloseCallbackForNeL ( void *context );
friend void xmlGenericErrorFuncWrite (void *ctx, const char *msg, ...);
public: public:
/** Stream ctor /** Stream ctor
@ -100,7 +99,7 @@ public:
/** Return the error string. /** Return the error string.
* if not empty, something wrong appends * if not empty, something wrong appends
*/ */
const char *getErrorString () const; static std::string getErrorString ();
/** Default dstor /** Default dstor
* *
@ -178,9 +177,6 @@ private:
// Current content string // Current content string
std::string _ContentString; std::string _ContentString;
// Error message
std::string _ErrorString;
}; };

View file

@ -17,7 +17,6 @@
#include "stdmisc.h" #include "stdmisc.h"
#include "nel/misc/i_xml.h" #include "nel/misc/i_xml.h"
#include "nel/misc/sstring.h"
#ifndef NL_DONT_USE_EXTERNAL_CODE #ifndef NL_DONT_USE_EXTERNAL_CODE
@ -39,6 +38,10 @@ namespace NLMISC
const char SEPARATOR = ' '; const char SEPARATOR = ' ';
std::string CIXml::_ErrorString;
bool CIXml::_LibXmlIntialized = false;
// *************************************************************************** // ***************************************************************************
#define readnumber(dest,digits) \ #define readnumber(dest,digits) \
@ -124,7 +127,7 @@ void xmlGenericErrorFuncRead (void *ctx, const char *msg, ...)
// Get the error string // Get the error string
string str; string str;
NLMISC_CONVERT_VARGS (str, msg, NLMISC::MaxCStringSize); NLMISC_CONVERT_VARGS (str, msg, NLMISC::MaxCStringSize);
((CIXml*)ctx)->_ErrorString += str; CIXml::_ErrorString += str;
} }
// *************************************************************************** // ***************************************************************************
@ -134,7 +137,7 @@ bool CIXml::init (IStream &stream)
// Release // Release
release (); release ();
xmlInitParser(); initLibXml();
// Default : XML mode // Default : XML mode
_BinaryStream = NULL; _BinaryStream = NULL;
@ -190,12 +193,7 @@ bool CIXml::init (IStream &stream)
} }
} }
// Set error handler
_ErrorString.clear(); _ErrorString.clear();
xmlSetGenericErrorFunc (this, xmlGenericErrorFuncRead);
// Ask to get debug info
xmlLineNumbersDefault(1);
// The parser context // The parser context
_Parser = xmlCreatePushParserCtxt(NULL, NULL, buffer, 4, NULL); _Parser = xmlCreatePushParserCtxt(NULL, NULL, buffer, 4, NULL);
@ -1068,6 +1066,7 @@ bool CIXml::getPropertyString (std::string &result, xmlNodePtr node, const std::
// Found // Found
return true; return true;
} }
return false; return false;
} }
@ -1075,18 +1074,21 @@ bool CIXml::getPropertyString (std::string &result, xmlNodePtr node, const std::
int CIXml::getIntProperty(xmlNodePtr node, const std::string &property, int defaultValue) int CIXml::getIntProperty(xmlNodePtr node, const std::string &property, int defaultValue)
{ {
CSString s; std::string s;
bool b;
b=getPropertyString(s,node,property); bool b = getPropertyString(s, node, property);
if (b==false)
if (!b)
return defaultValue; return defaultValue;
s=s.strip(); // remove leading and trailing spaces
sint val=s.atoi(); s = trim(s);
if (val==0 && s!="0")
sint val;
if (!fromString(s, val) || (val == 0 && s != "0"))
{ {
nlwarning("bad integer value: %s",s.c_str()); nlwarning("Bad integer value: %s",s.c_str());
return defaultValue; return defaultValue;
} }
@ -1097,14 +1099,25 @@ int CIXml::getIntProperty(xmlNodePtr node, const std::string &property, int defa
double CIXml::getFloatProperty(xmlNodePtr node, const std::string &property, float defaultValue) double CIXml::getFloatProperty(xmlNodePtr node, const std::string &property, float defaultValue)
{ {
CSString s; std::string s;
bool b;
b=getPropertyString(s,node,property); bool b = getPropertyString(s, node, property);
if (b==false)
if (!b)
return defaultValue; return defaultValue;
return s.strip().atof(); // remove leading and trailing spaces
s = trim(s);
float val;
if (!fromString(s, val))
{
nlwarning("Bad float value: %s", s.c_str());
return defaultValue;
}
return val;
} }
// *************************************************************************** // ***************************************************************************
@ -1112,10 +1125,10 @@ double CIXml::getFloatProperty(xmlNodePtr node, const std::string &property, flo
std::string CIXml::getStringProperty(xmlNodePtr node, const std::string &property, const std::string& defaultValue) std::string CIXml::getStringProperty(xmlNodePtr node, const std::string &property, const std::string& defaultValue)
{ {
std::string s; std::string s;
bool b;
b=getPropertyString(s,node,property); bool b = getPropertyString(s, node, property);
if (b==false)
if (!b)
return defaultValue; return defaultValue;
return s; return s;
@ -1141,9 +1154,43 @@ bool CIXml::getContentString (std::string &result, xmlNodePtr node)
// *************************************************************************** // ***************************************************************************
void CIXml::initLibXml()
{
if (_LibXmlIntialized) return;
_ErrorString.clear();
// Set error handler
xmlSetGenericErrorFunc (NULL, xmlGenericErrorFuncRead);
LIBXML_TEST_VERSION
// an error occured during initialization
if (!_ErrorString.empty())
{
throw EXmlParsingError (_ErrorString);
}
// Ask to get debug info
xmlLineNumbersDefault(1);
_LibXmlIntialized = true;
}
// ***************************************************************************
void CIXml::releaseLibXml() void CIXml::releaseLibXml()
{ {
if (!_LibXmlIntialized) return;
xmlCleanupParser(); xmlCleanupParser();
_LibXmlIntialized = false;
}
std::string CIXml::getErrorString()
{
return _ErrorString;
} }
} // NLMISC } // NLMISC

View file

@ -17,6 +17,7 @@
#include "stdmisc.h" #include "stdmisc.h"
#include "nel/misc/o_xml.h" #include "nel/misc/o_xml.h"
#include "nel/misc/i_xml.h"
#ifndef NL_DONT_USE_EXTERNAL_CODE #ifndef NL_DONT_USE_EXTERNAL_CODE
@ -142,27 +143,15 @@ COXml::COXml () : IStream (false /* Output mode */)
// *************************************************************************** // ***************************************************************************
void xmlGenericErrorFuncWrite (void *ctx, const char *msg, ...)
{
// Get the error string
string str;
NLMISC_CONVERT_VARGS (str, msg, NLMISC::MaxCStringSize);
((COXml*)ctx)->_ErrorString += str;
}
// ***************************************************************************
bool COXml::init (IStream *stream, const std::string &version) bool COXml::init (IStream *stream, const std::string &version)
{ {
resetPtrTable(); resetPtrTable();
CIXml::initLibXml();
// Output stream ? // Output stream ?
if (!stream->isReading()) if (!stream->isReading())
{ {
// Set error handler
_ErrorString.clear();
xmlSetGenericErrorFunc (this, xmlGenericErrorFuncWrite);
// Set XML mode // Set XML mode
setXMLMode (true); setXMLMode (true);
@ -673,9 +662,10 @@ bool COXml::isStringValidForProperties (const std::string &str)
// *************************************************************************** // ***************************************************************************
const char *COXml::getErrorString () const std::string COXml::getErrorString()
{ {
return _ErrorString.c_str (); // error string is managed by CIXml
return CIXml::getErrorString();
} }
} // NLMISC } // NLMISC