diff --git a/contrib/openddlparser/code/OpenDDLParser.cpp b/contrib/openddlparser/code/OpenDDLParser.cpp index 6a9f802ec..0c9e0bd98 100644 --- a/contrib/openddlparser/code/OpenDDLParser.cpp +++ b/contrib/openddlparser/code/OpenDDLParser.cpp @@ -132,6 +132,24 @@ OpenDDLParser::~OpenDDLParser() { clear(); } +void OpenDDLParser::logToStream(FILE *f, LogSeverity severity, const std::string &message) { + if (f) { + const char *tag = "none"; + switch (severity) { + case ddl_debug_msg: tag = "debug"; break; + case ddl_info_msg: tag = "info"; break; + case ddl_warn_msg: tag = "warn"; break; + case ddl_error_msg: tag = "error"; break; + } + fprintf(f, "OpenDDLParser: (%5s) %s\n", tag, message.c_str()); + } +} + +OpenDDLParser::logCallback OpenDDLParser::StdLogCallback (FILE *destination) { + using namespace std::placeholders; + return std::bind(logToStream, destination ? destination : stderr, _1, _2); +} + void OpenDDLParser::setLogCallback(logCallback callback) { // install user-specific log callback; null = no log callback m_logCallback = callback; @@ -171,12 +189,8 @@ size_t OpenDDLParser::getBufferSize() const { void OpenDDLParser::clear() { m_buffer.resize(0); - if (nullptr != m_context) { - delete m_context; - m_context = nullptr; - } - - // DDLNode::releaseNodes(); + delete m_context; + m_context = nullptr; } bool OpenDDLParser::validate() { @@ -506,7 +520,9 @@ char *OpenDDLParser::parseName(char *in, char *end, Name **name) { in = parseIdentifier(in, end, &id); if (id) { currentName = new Name(ntype, id); - *name = currentName; + if (currentName) { + *name = currentName; + } } return in; diff --git a/contrib/openddlparser/code/Value.cpp b/contrib/openddlparser/code/Value.cpp index 708a6878f..5a8aa39be 100644 --- a/contrib/openddlparser/code/Value.cpp +++ b/contrib/openddlparser/code/Value.cpp @@ -113,13 +113,14 @@ Value::~Value() { if (m_data != nullptr) { if (m_type == ValueType::ddl_ref) { Reference *tmp = (Reference *)m_data; - if (tmp != nullptr) + if (tmp != nullptr) { delete tmp; - } else + } + } else { delete[] m_data; + } } - if (m_next != nullptr) - delete m_next; + delete m_next; } void Value::setBool(bool value) { diff --git a/contrib/openddlparser/include/openddlparser/OpenDDLParser.h b/contrib/openddlparser/include/openddlparser/OpenDDLParser.h index 5794add90..3fbb4b6af 100644 --- a/contrib/openddlparser/include/openddlparser/OpenDDLParser.h +++ b/contrib/openddlparser/include/openddlparser/OpenDDLParser.h @@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#include BEGIN_ODDLPARSER_NS @@ -97,8 +98,8 @@ DLL_ODDLPARSER_EXPORT const char *getTypeToken(Value::ValueType type); //------------------------------------------------------------------------------------------------- class DLL_ODDLPARSER_EXPORT OpenDDLParser { public: - /// @brief The log callback function pointer. - typedef void (*logCallback)(LogSeverity severity, const std::string &msg); + /// @brief The log callback function. + typedef std::function logCallback; public: /// @brief The default class constructor. @@ -120,6 +121,11 @@ public: /// @return The current log callback. logCallback getLogCallback() const; + /// @brief A default log callback that writes to a FILE. + /// @param destination [in] Output stream. NULL will use stderr. + /// @return A callback that you can pass to setLogCallback. + static logCallback StdLogCallback(FILE *destination = nullptr); + /// @brief Assigns a new buffer to parse. /// @param buffer [in] The buffer /// @param len [in] Size of the buffer @@ -192,6 +198,9 @@ private: typedef std::vector DDLNodeStack; DDLNodeStack m_stack; Context *m_context; + + /// @brief Callback for StdLogCallback(). Not meant to be called directly. + static void logToStream (FILE *, LogSeverity, const std::string &); }; END_ODDLPARSER_NS