From 0deade3177abd84313b2cd22477021bf492d9d2d Mon Sep 17 00:00:00 2001 From: Edgar Date: Fri, 15 Jan 2021 09:58:41 +0100 Subject: [PATCH] :wrench: Proper handling of contexts strings --- include/moFileReader.hpp | 44 +++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/include/moFileReader.hpp b/include/moFileReader.hpp index 90beec1..d59dbc0 100644 --- a/include/moFileReader.hpp +++ b/include/moFileReader.hpp @@ -41,9 +41,9 @@ #include // this is for memset when compiling with gcc. #include #include -#include #include #include +#include //------------------------------------------------------------- // Path-Seperators are different on other OS. @@ -289,7 +289,10 @@ class moFileReader { protected: /// \brief Type for the map which holds the translation-pairs later. - typedef std::map moLookupList; + typedef std::unordered_map moLookupList; + + /// \brief Type for the 2D map which holds the translation-pairs later. + typedef std::unordered_map moContextLookupList; public: /// \brief The Magic Number describes the endianess of bytes on the system. @@ -298,6 +301,9 @@ class moFileReader /// \brief If the Magic Number is Reversed, we need to swap the bytes. static const unsigned int MagicReversed = 0xDE120495; + /// \brief The character that is used to separate context strings + static const char ContextSeparator = '\x04'; + /// \brief The possible errorcodes for methods of this class enum eErrorCode { @@ -586,8 +592,24 @@ class moFileReader return moFileReader::EC_FILEINVALID; } + std::string original_str = original; + std::string translation_str = translation; + auto x = original_str.find(ContextSeparator); + // Store it in the map. - m_lookup[std::string(original)] = std::string(translation); + if(x == std::string::npos){ + m_lookup[original_str] = translation_str; + } + else{ + // try-catch for handling out_of_range exceptions + try { + m_lookup_context[original_str.substr(0, x)][original_str.substr(x + 1, original_str.length())] = translation_str; + } + catch (const std::exception& e) { + m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; + return moFileReader::EC_ERROR; + } + } // Cleanup... delete[] original; @@ -608,8 +630,7 @@ class moFileReader if (m_lookup.empty()) return id; auto iterator = m_lookup.find(id); - if (iterator == m_lookup.end()) { return id; } - return iterator->second; + return iterator == m_lookup.end() ? id : iterator->second; } /** \brief Returns the searched translation or returns the input, restricted to the context given by context. @@ -620,15 +641,13 @@ class moFileReader */ std::string LookupWithContext(const char *context, const char *id) const { - std::string idName = context; - idName += '\x04'; - idName += id; + if (m_lookup_context.empty()) return id; + auto iterator = m_lookup_context.find(context); - if (m_lookup.empty()) return id; - auto iterator = m_lookup.find(idName); + if(iterator == m_lookup_context.end()) return id; + auto iterator2 = iterator->second.find(id); - if (iterator == m_lookup.end()) { return id; } - return iterator->second; + return iterator2 == iterator->second.end() ? id : iterator2->second; } /// \brief Returns the Error Description. @@ -762,6 +781,7 @@ class moFileReader private: // Holds the lookup-table moLookupList m_lookup; + moContextLookupList m_lookup_context; // Replaces < with ( to satisfy html-rules. static void MakeHtmlConform(std::string &_inout)