🔧 Proper handling of contexts strings
This commit is contained in:
		| @@ -41,9 +41,9 @@ | |||||||
| #include <cstring> // this is for memset when compiling with gcc. | #include <cstring> // this is for memset when compiling with gcc. | ||||||
| #include <deque> | #include <deque> | ||||||
| #include <fstream> | #include <fstream> | ||||||
| #include <map> |  | ||||||
| #include <sstream> | #include <sstream> | ||||||
| #include <string> | #include <string> | ||||||
|  | #include <unordered_map> | ||||||
|  |  | ||||||
| //------------------------------------------------------------- | //------------------------------------------------------------- | ||||||
| // Path-Seperators are different on other OS. | // Path-Seperators are different on other OS. | ||||||
| @@ -289,7 +289,10 @@ class moFileReader | |||||||
| { | { | ||||||
|   protected: |   protected: | ||||||
|     /// \brief Type for the map which holds the translation-pairs later. |     /// \brief Type for the map which holds the translation-pairs later. | ||||||
|     typedef std::map<std::string, std::string> moLookupList; |     typedef std::unordered_map<std::string, std::string> moLookupList; | ||||||
|  |  | ||||||
|  |     /// \brief Type for the 2D map which holds the translation-pairs later. | ||||||
|  |     typedef std::unordered_map<std::string, moLookupList> moContextLookupList; | ||||||
|  |  | ||||||
|   public: |   public: | ||||||
|     /// \brief The Magic Number describes the endianess of bytes on the system. |     /// \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. |     /// \brief If the Magic Number is Reversed, we need to swap the bytes. | ||||||
|     static const unsigned int MagicReversed = 0xDE120495; |     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 |     /// \brief The possible errorcodes for methods of this class | ||||||
|     enum eErrorCode |     enum eErrorCode | ||||||
|     { |     { | ||||||
| @@ -586,8 +592,24 @@ class moFileReader | |||||||
|                 return moFileReader::EC_FILEINVALID; |                 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. |             // 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... |             // Cleanup... | ||||||
|             delete[] original; |             delete[] original; | ||||||
| @@ -608,8 +630,7 @@ class moFileReader | |||||||
|         if (m_lookup.empty()) return id; |         if (m_lookup.empty()) return id; | ||||||
|         auto iterator = m_lookup.find(id); |         auto iterator = m_lookup.find(id); | ||||||
|  |  | ||||||
|         if (iterator == m_lookup.end()) { return id; } |         return iterator == m_lookup.end() ? id : iterator->second; | ||||||
|         return iterator->second; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** \brief Returns the searched translation or returns the input, restricted to the context given by context. |     /** \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 LookupWithContext(const char *context, const char *id) const | ||||||
|     { |     { | ||||||
|         std::string idName = context; |         if (m_lookup_context.empty()) return id; | ||||||
|         idName += '\x04'; |         auto iterator = m_lookup_context.find(context); | ||||||
|         idName += id; |  | ||||||
|  |  | ||||||
|         if (m_lookup.empty()) return id; |         if(iterator == m_lookup_context.end()) return id; | ||||||
|         auto iterator = m_lookup.find(idName); |         auto iterator2 = iterator->second.find(id); | ||||||
|  |  | ||||||
|         if (iterator == m_lookup.end()) { return id; } |         return iterator2 == iterator->second.end() ? id : iterator2->second; | ||||||
|         return iterator->second; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// \brief Returns the Error Description. |     /// \brief Returns the Error Description. | ||||||
| @@ -762,6 +781,7 @@ class moFileReader | |||||||
|   private: |   private: | ||||||
|     // Holds the lookup-table |     // Holds the lookup-table | ||||||
|     moLookupList m_lookup; |     moLookupList m_lookup; | ||||||
|  |     moContextLookupList m_lookup_context; | ||||||
|  |  | ||||||
|     // Replaces < with ( to satisfy html-rules. |     // Replaces < with ( to satisfy html-rules. | ||||||
|     static void MakeHtmlConform(std::string &_inout) |     static void MakeHtmlConform(std::string &_inout) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user