From bb5ffac22d66806c55cffc302ccf07f78e1d233c Mon Sep 17 00:00:00 2001 From: numatrumpet Date: Sat, 6 Sep 2014 22:56:46 +0900 Subject: [PATCH] Using C++11, XMLUtil::ToErrorName(XMLError) returns the name of error type --- tinyxml2.cpp | 8 ++++++ tinyxml2.h | 72 ++++++++++++++++++++++++++++++++-------------------- xmltest.cpp | 3 +++ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 9e310de..fd9ef16 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -476,6 +476,14 @@ bool XMLUtil::ToDouble( const char* str, double* value ) return false; } +std::string XMLUtil::ToErrorName( const XMLError errorID ) +{ +#if __cplusplus > 199711LL + return ErrorNames[errorID]; +#else + return std::string("Use C++11 or higher to use this function"); +#endif +} char* XMLDocument::Identify( char* p, XMLNode** node ) { diff --git a/tinyxml2.h b/tinyxml2.h index f492a3c..c254d15 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -39,6 +39,8 @@ distribution. # include # include # include +# include +# include #endif /* @@ -480,6 +482,46 @@ public: } }; +#define FOR_EACH(F) \ + F(XML_SUCCESS) \ + F(XML_NO_ATTRIBUTE) \ + F(XML_WRONG_ATTRIBUTE_TYPE) \ + F(XML_ERROR_FILE_NOT_FOUND) \ + F(XML_ERROR_FILE_COULD_NOT_BE_OPENED) \ + F(XML_ERROR_FILE_READ_ERROR) \ + F(XML_ERROR_ELEMENT_MISMATCH) \ + F(XML_ERROR_PARSING_ELEMENT) \ + F(XML_ERROR_PARSING_ATTRIBUTE) \ + F(XML_ERROR_IDENTIFYING_TAG) \ + F(XML_ERROR_PARSING_TEXT) \ + F(XML_ERROR_PARSING_CDATA) \ + F(XML_ERROR_PARSING_COMMENT) \ + F(XML_ERROR_PARSING_DECLARATION) \ + F(XML_ERROR_PARSING_UNKNOWN) \ + F(XML_ERROR_EMPTY_DOCUMENT) \ + F(XML_ERROR_MISMATCHED_ELEMENT) \ + F(XML_ERROR_PARSING) \ + F(XML_CAN_NOT_CONVERT_TEXT) \ + F(XML_NO_TEXT_NODE) + +#define FF(X) X, +enum XMLError { + FOR_EACH(FF) + XML_ERROR_ID_NUM, + XML_NO_ERROR = 0 +}; +#undef FF + +#if __cplusplus > 199711LL +#define FF(X) #X, +const std::vector ErrorNames = { + FOR_EACH(FF) + "OUT_OF_RANGE" +}; +#undef FF +#endif +#undef FOR_EACH + /* Utility functionality. @@ -557,6 +599,9 @@ public: static bool ToBool( const char* str, bool* value ); static bool ToFloat( const char* str, float* value ); static bool ToDouble( const char* str, double* value ); + + // converts XMLError to strings + static std::string ToErrorName( const XMLError errorID ); }; @@ -997,33 +1042,6 @@ protected: }; -enum XMLError { - XML_NO_ERROR = 0, - XML_SUCCESS = 0, - - XML_NO_ATTRIBUTE, - XML_WRONG_ATTRIBUTE_TYPE, - - XML_ERROR_FILE_NOT_FOUND, - XML_ERROR_FILE_COULD_NOT_BE_OPENED, - XML_ERROR_FILE_READ_ERROR, - XML_ERROR_ELEMENT_MISMATCH, - XML_ERROR_PARSING_ELEMENT, - XML_ERROR_PARSING_ATTRIBUTE, - XML_ERROR_IDENTIFYING_TAG, - XML_ERROR_PARSING_TEXT, - XML_ERROR_PARSING_CDATA, - XML_ERROR_PARSING_COMMENT, - XML_ERROR_PARSING_DECLARATION, - XML_ERROR_PARSING_UNKNOWN, - XML_ERROR_EMPTY_DOCUMENT, - XML_ERROR_MISMATCHED_ELEMENT, - XML_ERROR_PARSING, - - XML_CAN_NOT_CONVERT_TEXT, - XML_NO_TEXT_NODE -}; - /** An attribute is a name-value pair. Elements have an arbitrary number of attributes, each with a unique name. diff --git a/xmltest.cpp b/xmltest.cpp index 55b597d..ef57f7e 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -1222,6 +1222,9 @@ int main( int argc, const char ** argv ) XMLDocument doc; XMLError error = doc.LoadFile( "resources/empty.xml" ); XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error ); +#if __cplusplus > 199711LL + XMLTest( "Loading an empty file and ErrorName as string", "XML_ERROR_EMPTY_DOCUMENT", XMLUtil::ToErrorName(error).c_str() ); +#endif } {