+ import from github
This commit is contained in:
		
							
								
								
									
										232
									
								
								src/mo.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										232
									
								
								src/mo.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,232 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * All rights reserved.                           | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
|  * modification, are permitted provided that the following conditions | ||||
|  * are met: | ||||
|  * | ||||
|  *   1. Redistributions of source code must retain the above copyright | ||||
|  *      notice, this list of conditions and the following disclaimer. | ||||
|  * | ||||
|  *   2. Redistributions in binary form must reproduce the above copyright | ||||
|  *      notice, this list of conditions and the following disclaimer in the | ||||
|  *      documentation and/or other materials provided with the distribution. | ||||
|  * | ||||
|  *   3. The names of its contributors may not be used to endorse or promote  | ||||
|  *      products derived from this software without specific prior written  | ||||
|  *      permission. | ||||
|  * | ||||
|  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
|  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
|  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
|  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||||
|  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||||
|  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||||
|  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||||
|  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||||
|  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||||
|  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||
|  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  */ | ||||
| #include "../include/moFileReader.h" | ||||
| #include <iostream> | ||||
| #include <cstdlib> | ||||
|  | ||||
| #if defined(_MSC_VER) && defined(_DEBUG) | ||||
| #   include <crtdbg.h>    | ||||
| #endif /* _MSC_VER */ | ||||
|  | ||||
| using namespace moFileLib; | ||||
|  | ||||
| void Usage(const std::string appname) | ||||
| { | ||||
|     std::cout << "Usage: "                                                                  << std::endl; | ||||
|     std::cout << appname << " <option> <params>"                                           << std::endl; | ||||
|     std::cout << "Possible Options: "                                                       << std::endl; | ||||
|     std::cout << "--lookup <mofile> <msgid>        - Outputs the given ID from the file."   << std::endl;     | ||||
|     std::cout << "--export <mofile> [<exportfile>] - Exports the whole .mo-file as HTML."   << std::endl;         | ||||
|     std::cout << "--help,-h,-?  - Prints this screen"                                       << std::endl; | ||||
|     std::cout << "--license     - Prints the license of this program. "                     << std::endl; | ||||
|     std::cout                                                                               << std::endl; | ||||
|     std::cout << "Example: " << appname << " --export my18n.mo exportfile.html"            << std::endl; | ||||
|     std::cout << "Example: " << appname << " --lookup my18n.mo lookupstring"               << std::endl; | ||||
|     std::cout << "Please encapsualte strings or pathes with spaces in \". Thank you."       << std::endl; | ||||
|     std::cout << "Parameters in Brackets [] are optional."                                  << std::endl; | ||||
| } | ||||
|  | ||||
| void PrintLicense() | ||||
| { | ||||
|     std::cout << "\ | ||||
|     moFileReader - A simple .mo-File-Reader\n\ | ||||
|     Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)\n\ | ||||
|     All rights reserved.                          \n\ | ||||
|     \n\ | ||||
|     Redistribution and use in source and binary forms, with or without\n\ | ||||
|     modification, are permitted provided that the following conditions\n\ | ||||
|     are met:\n\ | ||||
|     \n\ | ||||
|     1. Redistributions of source code must retain the above copyright\n\ | ||||
|      notice, this list of conditions and the following disclaimer.\n\ | ||||
|     2. Redistributions in binary form must reproduce the above copyright\n\ | ||||
|      notice, this list of conditions and the following disclaimer in the\n\ | ||||
|      documentation and/or other materials provided with the distribution.\n\ | ||||
|     \n\ | ||||
|     3. The names of its contributors may not be used to endorse or promote \n\ | ||||
|      products derived from this software without specific prior written \n\ | ||||
|      permission.\n\ | ||||
|     \n\ | ||||
|     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\ | ||||
|     \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n\ | ||||
|     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n\ | ||||
|     A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n\ | ||||
|     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n\ | ||||
|     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n\ | ||||
|     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n\ | ||||
|     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n\ | ||||
|     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n\ | ||||
|     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n\ | ||||
|     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\ | ||||
|     " << std::endl; | ||||
| } | ||||
|  | ||||
| std::string GetAppName(const char* raw) | ||||
| { | ||||
|     std::string r(raw); | ||||
|     int first = r.find_last_of(moPATHSEP) + 1; | ||||
|     r = r.substr(first, r.length() - first); | ||||
|     return r; | ||||
| } | ||||
|  | ||||
| #if defined(_CONSOLE) | ||||
|  | ||||
| int main( int, char** argv ) | ||||
| { | ||||
| #if defined (_DEBUG) && defined(_MSC_VER) | ||||
|             long flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); | ||||
|             flag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF; | ||||
|             _CrtSetDbgFlag(flag); | ||||
| #endif /* _MSC_VER && _DEBUG */     | ||||
|  | ||||
|     std::string appname = GetAppName(argv[0]); | ||||
|  | ||||
|     if ( argv[1] == NULL ) | ||||
|     { | ||||
|         Usage(appname); | ||||
|         return EXIT_FAILURE; | ||||
|     } | ||||
|     if ( std::string(argv[1]) == "--help" || std::string(argv[1]) == "-?" || std::string(argv[1]) == "-h" ) | ||||
|     { | ||||
|         Usage(appname); | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
|     else if ( std::string(argv[1]) == "-v" || std::string(argv[1]) == "--version" ) | ||||
|     { | ||||
|         std::cout << "This program is part of the moReaderSDK written by Domenico Gentner."    << std::endl; | ||||
|         std::cout << "Released under the Terms of the MIT-License." << std::endl; | ||||
|         std::cout << "Type " << appname << " --license to view it." << std::endl; | ||||
|         std::cout << "Get all News and Updates from http://mofilereader.googlecode.com." << std::endl; | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
|     else if ( std::string(argv[1]) == "--license" ) | ||||
|     { | ||||
|         PrintLicense(); | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
|     else if ( std::string(argv[1]) == "--export" ) | ||||
|     { | ||||
|         std::string outfile; | ||||
|         if ( argv[2] == NULL ) | ||||
|         { | ||||
|             Usage(appname); | ||||
|             return EXIT_FAILURE; | ||||
|         } | ||||
|         if ( argv[3] ) | ||||
|         { | ||||
|             outfile = argv[3]; | ||||
|         } | ||||
|          | ||||
|         moFileReader::eErrorCode r = moFileReader::ExportAsHTML(argv[2], outfile); | ||||
|         if (  r == moFileReader::EC_SUCCESS ) | ||||
|         { | ||||
|             std::cout << "Dumped " << argv[2] << " successfully to " << outfile << std::endl; | ||||
|             return EXIT_SUCCESS; | ||||
|         } | ||||
|         else if ( r == moFileReader::EC_TABLEEMPTY ) | ||||
|         { | ||||
|             std::cout << "Could not dump " << argv[2] << " to " << outfile << " because the lookup-table is empty!" << std::endl; | ||||
|             return EXIT_FAILURE; | ||||
|         } | ||||
|         else if ( r == moFileReader::EC_FILENOTFOUND ) | ||||
|         { | ||||
|             std::cout << "Could not dump " << argv[2] << " to " << outfile << " because I could not open a file!" << std::endl; | ||||
|             return EXIT_FAILURE;             | ||||
|         } | ||||
|         else if ( r == moFileReader::EC_FILEINVALID ) | ||||
|         { | ||||
|             std::cout << "Could not dump " << argv[2] << " to " << outfile << " because the .mo-File was invalid!" << std::endl; | ||||
|             return EXIT_FAILURE; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             std::cout << "Could not dump " << argv[2] << " to " << outfile << ". An unknown error occured." << std::endl; | ||||
|             return EXIT_FAILURE; | ||||
|         } | ||||
|     } | ||||
|     else if ( std::string(argv[1]) == "--lookup") | ||||
|     { | ||||
|         if ( argv[2] == NULL || argv[3] == NULL ) | ||||
|         { | ||||
|             Usage(appname); | ||||
|             if (argv[3] == NULL) | ||||
|                 std::cout << "HINT: If you want to call an empty msgid, please use \"\" as parameter 3." << std::endl; | ||||
|             return EXIT_FAILURE; | ||||
|         } | ||||
|         if ( moReadMoFile(argv[2]) != moFileReader::EC_SUCCESS ) | ||||
|         { | ||||
|             std::cout << "Error while loading the file: " << moFileGetErrorDescription() << std::endl; | ||||
|             return -1; | ||||
|         } | ||||
|         std::cout << "-----------------------------------------" << std::endl; | ||||
|         std::cout << "Lookup: " <<   argv[3]                     << std::endl; | ||||
|         std::cout << "Result: " << _(argv[3])                    << std::endl; | ||||
|         std::cout << "-----------------------------------------" << std::endl; | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         Usage(appname); | ||||
|         std::cout << std::endl; | ||||
|         std::cout << "HINT: Missing --export or --lookup!" << std::endl; | ||||
|         return EXIT_FAILURE; | ||||
|     } | ||||
| } | ||||
|  | ||||
| #elif defined(_USRDLL) && defined(WIN32) | ||||
|  | ||||
| #include <windows.h> | ||||
| extern "C" | ||||
| int WINAPI DllMain( DWORD reason, LPVOID) | ||||
| { | ||||
|     switch (reason) | ||||
|     { | ||||
|         case DLL_THREAD_ATTACH: | ||||
|         case DLL_PROCESS_ATTACH: | ||||
|             { | ||||
| #if defined (_DEBUG) && defined(_MSC_VER) | ||||
|             long flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); | ||||
|             flag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF; | ||||
|             _CrtSetDbgFlag(flag); | ||||
| #endif /* _MSC_VER && _DEBUG */ | ||||
|             break; | ||||
|             } | ||||
|         default: | ||||
|             return FALSE; | ||||
|     }; | ||||
|  | ||||
|     return TRUE; | ||||
| } | ||||
|  | ||||
| #endif /* Compilation-Mode */ | ||||
|  | ||||
							
								
								
									
										509
									
								
								src/moFileReader.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										509
									
								
								src/moFileReader.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,509 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * All rights reserved.                           | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
|  * modification, are permitted provided that the following conditions | ||||
|  * are met: | ||||
|  * | ||||
|  *   1. Redistributions of source code must retain the above copyright | ||||
|  *      notice, this list of conditions and the following disclaimer. | ||||
|  * | ||||
|  *   2. Redistributions in binary form must reproduce the above copyright | ||||
|  *      notice, this list of conditions and the following disclaimer in the | ||||
|  *      documentation and/or other materials provided with the distribution. | ||||
|  * | ||||
|  *   3. The names of its contributors may not be used to endorse or promote  | ||||
|  *      products derived from this software without specific prior written  | ||||
|  *      permission. | ||||
|  * | ||||
|  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
|  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
|  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
|  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||||
|  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||||
|  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||||
|  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||||
|  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||||
|  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||||
|  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||
|  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  */ | ||||
| #include "../include/moFileReader.h" | ||||
| #include <iostream> | ||||
| #include <cstdlib> | ||||
|  | ||||
| MO_BEGIN_NAMESPACE | ||||
|  | ||||
| unsigned long moFileReader::SwapBytes(unsigned long in)  | ||||
| { | ||||
|     unsigned long b0 = (in >> 0) & 0xff; | ||||
|     unsigned long b1 = (in >> 8) & 0xff; | ||||
|     unsigned long b2 = (in >> 16) & 0xff; | ||||
|     unsigned long b3 = (in >> 24) & 0xff; | ||||
|  | ||||
|     return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; | ||||
| } | ||||
|  | ||||
| const std::string& moFileReader::GetErrorDescription() const | ||||
| { | ||||
|     return m_error; | ||||
| } | ||||
|  | ||||
| void moFileReader::ClearTable() | ||||
| { | ||||
|     m_lookup.clear(); | ||||
| } | ||||
|  | ||||
| unsigned int moFileReader::GetNumStrings() const | ||||
| { | ||||
|     return m_lookup.size(); | ||||
| } | ||||
|  | ||||
| std::string moFileReader::Lookup( const char* id ) const | ||||
| { | ||||
|     if ( m_lookup.size() <= 0) return id; | ||||
|     moLookupList::const_iterator iterator = m_lookup.find(id);     | ||||
|  | ||||
|     if ( iterator == m_lookup.end() ) | ||||
|     { | ||||
|         return id; | ||||
|     } | ||||
|     return iterator->second; | ||||
| } | ||||
|  | ||||
| moFileReader::eErrorCode moFileReader::ParseData(std::string data) | ||||
| { | ||||
|     // Creating a file-description. | ||||
|     moFileInfo moInfo; | ||||
|      | ||||
|     // Reference to the List inside moInfo. | ||||
|     moFileInfo::moTranslationPairList& TransPairInfo = moInfo.m_translationPairInformation; | ||||
|      | ||||
|     // Opening the file. | ||||
|     std::stringstream stream(data); | ||||
|      | ||||
|     // Read in all the 4 bytes of fire-magic, offsets and stuff... | ||||
|     stream.read((char*)&moInfo.m_magicNumber, 4); | ||||
|     stream.read((char*)&moInfo.m_fileVersion, 4); | ||||
|     stream.read((char*)&moInfo.m_numStrings, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetOriginal, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetTranslation, 4); | ||||
|     stream.read((char*)&moInfo.m_sizeHashtable, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetHashtable, 4); | ||||
|      | ||||
|     if ( stream.bad() ) | ||||
|     { | ||||
|         m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|         printf("%s", m_error.c_str()); | ||||
|         return moFileReader::EC_FILEINVALID; | ||||
|     } | ||||
|      | ||||
|     // Checking the Magic Number | ||||
|     if ( MagicNumber != moInfo.m_magicNumber ) | ||||
|     { | ||||
|         if ( MagicReversed != moInfo.m_magicNumber ) | ||||
|         { | ||||
|             m_error = "The Magic Number does not match in all cases!"; | ||||
|             printf("%s", m_error.c_str()); | ||||
|             return moFileReader::EC_MAGICNUMBER_NOMATCH; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             moInfo.m_reversed = true; | ||||
|             m_error = "Magic Number is reversed. We do not support this yet!"; | ||||
|             return moFileReader::EC_MAGICNUMBER_REVERSED; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     // Now we search all Length & Offsets of the original strings | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++ ) | ||||
|     { | ||||
|         moTranslationPairInformation _str; | ||||
|         stream.read((char*)&_str.m_orLength, 4); | ||||
|         stream.read((char*)&_str.m_orOffset, 4); | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             printf("%s", m_error.c_str()); | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|          | ||||
|         TransPairInfo.push_back(_str); | ||||
|     } | ||||
|      | ||||
|     // Get all Lengths & Offsets of the translated strings | ||||
|     // Be aware: The Descriptors already exist in our list, so we just mod. refs from the deque. | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++ ) | ||||
|     { | ||||
|         moTranslationPairInformation& _str = TransPairInfo[i]; | ||||
|         stream.read((char*)&_str.m_trLength, 4); | ||||
|         stream.read((char*)&_str.m_trOffset, 4); | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             printf("%s", m_error.c_str()); | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     // Normally you would read the hash-table here, but we don't use it. :) | ||||
|      | ||||
|     // Now to the interesting part, we read the strings-pairs now | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++) | ||||
|     { | ||||
|         // We need a length of +1 to catch the trailing \0. | ||||
|         int orLength = TransPairInfo[i].m_orLength+1; | ||||
|         int trLength = TransPairInfo[i].m_trLength+1; | ||||
|          | ||||
|         int orOffset = TransPairInfo[i].m_orOffset; | ||||
|         int trOffset = TransPairInfo[i].m_trOffset; | ||||
|          | ||||
|         // Original | ||||
|         char* original  = new char[orLength]; | ||||
|         memset(original, 0, sizeof(char)*orLength); | ||||
|          | ||||
|         stream.seekg(orOffset); | ||||
|         stream.read(original, orLength); | ||||
|          | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             printf("%s", m_error.c_str()); | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|          | ||||
|         // Translation | ||||
|         char* translation = new char[trLength]; | ||||
|         memset(translation, 0, sizeof(char)*trLength); | ||||
|          | ||||
|         stream.seekg(trOffset); | ||||
|         stream.read(translation, trLength); | ||||
|          | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             printf("%s", m_error.c_str()); | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|          | ||||
|         // Store it in the map. | ||||
|         m_lookup[std::string(original)] = std::string(translation); | ||||
|          | ||||
|         // Cleanup... | ||||
|         delete original; | ||||
|         delete translation; | ||||
|     } | ||||
|      | ||||
|     // Done :) | ||||
|     return moFileReader::EC_SUCCESS; | ||||
| } | ||||
|  | ||||
| moFileReader::eErrorCode moFileReader::ReadFile( const char* filename ) | ||||
| {     | ||||
|     // Creating a file-description. | ||||
|     moFileInfo moInfo; | ||||
|  | ||||
|     // Reference to the List inside moInfo. | ||||
|     moFileInfo::moTranslationPairList& TransPairInfo = moInfo.m_translationPairInformation; | ||||
|  | ||||
|     // Opening the file. | ||||
|     std::ifstream stream( filename, std::ios_base::binary | std::ios_base::in ); | ||||
|     if ( !stream.is_open() ) | ||||
|     { | ||||
|         m_error = std::string("Cannot open File ") + std::string(filename); | ||||
|         return moFileReader::EC_FILENOTFOUND; | ||||
|     } | ||||
|  | ||||
|     // Read in all the 4 bytes of fire-magic, offsets and stuff... | ||||
|     stream.read((char*)&moInfo.m_magicNumber, 4); | ||||
|     stream.read((char*)&moInfo.m_fileVersion, 4); | ||||
|     stream.read((char*)&moInfo.m_numStrings, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetOriginal, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetTranslation, 4); | ||||
|     stream.read((char*)&moInfo.m_sizeHashtable, 4); | ||||
|     stream.read((char*)&moInfo.m_offsetHashtable, 4); | ||||
|  | ||||
|     if ( stream.bad() ) | ||||
|     { | ||||
|         stream.close(); | ||||
|         m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|         return moFileReader::EC_FILEINVALID; | ||||
|     } | ||||
|  | ||||
|     // Checking the Magic Number | ||||
|     if ( MagicNumber != moInfo.m_magicNumber ) | ||||
|     { | ||||
|         if ( MagicReversed != moInfo.m_magicNumber ) | ||||
|         { | ||||
|             m_error = "The Magic Number does not match in all cases!"; | ||||
|             return moFileReader::EC_MAGICNUMBER_NOMATCH; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             moInfo.m_reversed = true; | ||||
|             m_error = "Magic Number is reversed. We do not support this yet!"; | ||||
|             return moFileReader::EC_MAGICNUMBER_REVERSED; | ||||
|         } | ||||
|     }   | ||||
|      | ||||
|     // Now we search all Length & Offsets of the original strings | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++ ) | ||||
|     { | ||||
|         moTranslationPairInformation _str;         | ||||
|         stream.read((char*)&_str.m_orLength, 4); | ||||
|         stream.read((char*)&_str.m_orOffset, 4); | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             stream.close(); | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|  | ||||
|         TransPairInfo.push_back(_str); | ||||
|     } | ||||
|  | ||||
|     // Get all Lengths & Offsets of the translated strings | ||||
|     // Be aware: The Descriptors already exist in our list, so we just mod. refs from the deque. | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++ ) | ||||
|     { | ||||
|         moTranslationPairInformation& _str = TransPairInfo[i]; | ||||
|         stream.read((char*)&_str.m_trLength, 4); | ||||
|         stream.read((char*)&_str.m_trOffset, 4); | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             stream.close(); | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // Normally you would read the hash-table here, but we don't use it. :) | ||||
|  | ||||
|     // Now to the interesting part, we read the strings-pairs now | ||||
|     for ( int i = 0; i < moInfo.m_numStrings; i++) | ||||
|     { | ||||
|         // We need a length of +1 to catch the trailing \0. | ||||
|         int orLength = TransPairInfo[i].m_orLength+1; | ||||
|         int trLength = TransPairInfo[i].m_trLength+1; | ||||
|  | ||||
|         int orOffset = TransPairInfo[i].m_orOffset; | ||||
|         int trOffset = TransPairInfo[i].m_trOffset; | ||||
|  | ||||
|         // Original | ||||
|         char* original  = new char[orLength]; | ||||
|         memset(original, 0, sizeof(char)*orLength); | ||||
|  | ||||
|         stream.seekg(orOffset); | ||||
|         stream.read(original, orLength); | ||||
|  | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|          | ||||
|         // Translation | ||||
|         char* translation = new char[trLength]; | ||||
|         memset(translation, 0, sizeof(char)*trLength); | ||||
|  | ||||
|         stream.seekg(trOffset); | ||||
|         stream.read(translation, trLength); | ||||
|  | ||||
|         if ( stream.bad() ) | ||||
|         { | ||||
|             m_error = "Stream bad during reading. The .mo-file seems to be invalid or has bad descriptions!"; | ||||
|             return moFileReader::EC_FILEINVALID; | ||||
|         } | ||||
|  | ||||
|         // Store it in the map.     | ||||
|         m_lookup[std::string(original)] = std::string(translation); | ||||
|  | ||||
|         // Cleanup... | ||||
|         delete original; | ||||
|         delete translation; | ||||
|     } | ||||
|  | ||||
|     // Done :) | ||||
|     stream.close(); | ||||
|     return moFileReader::EC_SUCCESS; | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::string filename, std::string css ) | ||||
| { | ||||
|     // Read the file | ||||
|     moFileReader reader; | ||||
|     moFileReader::eErrorCode r = reader.ReadFile(infile.c_str()) ; | ||||
|     if ( r != moFileReader::EC_SUCCESS ) | ||||
|     { | ||||
|         return r; | ||||
|     } | ||||
|     if ( reader.m_lookup.empty() ) | ||||
|     { | ||||
|         return moFileReader::EC_TABLEEMPTY; | ||||
|     }     | ||||
|  | ||||
|     // Beautify Output | ||||
|     std::string fname; | ||||
|     unsigned int pos = infile.find_last_of(moPATHSEP); | ||||
|     if ( pos != std::string::npos ) | ||||
|     { | ||||
|         fname = infile.substr( pos+1, infile.length() ); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         fname = infile; | ||||
|     } | ||||
|  | ||||
|     // if there is no filename given, we set it to the .mo + html, e.g. test.mo.html | ||||
|     std::string htmlfile(filename); | ||||
|     if (htmlfile.empty()) | ||||
|     { | ||||
|         htmlfile = infile + std::string(".html"); | ||||
|     }    | ||||
|  | ||||
|     // Ok, now prepare output. | ||||
|     std::ofstream stream(htmlfile.c_str()); | ||||
|     if ( stream.is_open() )  | ||||
|     { | ||||
|         stream << "<!DOCTYPE HTML PUBLIC \"- //W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" << std::endl; | ||||
|         stream << "<html><head><style type=\"text/css\">\n" << std::endl; | ||||
|         stream << css << std::endl; | ||||
|         stream << "</style>" << std::endl; | ||||
|         stream << "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" << std::endl; | ||||
|         stream << "<title>Dump of " << fname << "</title></head>" << std::endl; | ||||
|         stream << "<body>" << std::endl; | ||||
|         stream << "<center>" <<std::endl; | ||||
|         stream << "<h1>" << fname << "</h1>" << std::endl; | ||||
|         stream << "<table border=\"1\"><th colspan=\"2\">Project Info</th>" << std::endl; | ||||
|  | ||||
|         std::stringstream parsee; | ||||
|         parsee << reader.Lookup(""); | ||||
|       | ||||
|         while ( !parsee.eof() ) | ||||
|         { | ||||
|             char buffer[1024]; | ||||
|             parsee.getline(buffer, 1024); | ||||
|             std::string name; | ||||
|             std::string value; | ||||
|  | ||||
|             reader.GetPoEditorString( buffer, name, value ); | ||||
|             if ( !(name.empty() || value.empty()) ) | ||||
|             { | ||||
|                 stream << "<tr><td>" << name << "</td><td>" <<  value << "</td></tr>" << std::endl; | ||||
|             } | ||||
|         } | ||||
|         stream << "</table>" << std::endl; | ||||
|         stream << "<hr noshade/>" << std::endl; | ||||
|  | ||||
|         // Now output the content | ||||
|         stream << "<table border=\"1\"><th colspan=\"2\">Content</th>" << std::endl;         | ||||
|         for ( moLookupList::const_iterator it = reader.m_lookup.begin();  | ||||
|              it != reader.m_lookup.end(); it++) | ||||
|         { | ||||
|             if ( it->first != "" ) // Skip the empty msgid, its the table we handled above. | ||||
|             { | ||||
|                 stream << "<tr><td>" << it->first << "</td><td>" <<  it->second << "</td></tr>" << std::endl; | ||||
|             } | ||||
|         } | ||||
|         stream << "</table><br/>" << std::endl; | ||||
|  | ||||
|         stream << "</center>" << std::endl; | ||||
|         stream << "<div class=\"copyleft\">File generated by <a href=\"http://mofilereader.googlecode.com\" target=\"_blank\">moFileReaderSDK</a></div>" << std::endl;         | ||||
|         stream << "</body></html>" << std::endl; | ||||
|         stream.close(); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         return moFileReader::EC_FILENOTFOUND; | ||||
|     } | ||||
|     | ||||
|     return moFileReader::EC_SUCCESS; | ||||
| } | ||||
|  | ||||
|  | ||||
| // Removes spaces from front and end.  | ||||
| void moFileReader::Trim(std::string& in) | ||||
| { | ||||
|     while ( in[0] == ' ' ) | ||||
|     { | ||||
|         in = in.substr(1, in.length() ); | ||||
|     } | ||||
|     while( in[in.length()] == ' ' ) | ||||
|     { | ||||
|         in = in.substr(0, in.length() - 1 ); | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Extracts a value-pair from the po-edit-information | ||||
| bool moFileReader::GetPoEditorString(const char* buffer, std::string& name, std::string& value) | ||||
| { | ||||
|     std::string line(buffer); | ||||
|     size_t first = line.find_first_of(":"); | ||||
|  | ||||
|     if ( first != std::string::npos ) | ||||
|     { | ||||
|         name   = line.substr( 0, first ); | ||||
|         value  = line.substr( first + 1, line.length() ); | ||||
|  | ||||
|         // Replace <> with () for Html-Conformity. | ||||
|         MakeHtmlConform(value); | ||||
|         MakeHtmlConform(name); | ||||
|  | ||||
|         // Remove spaces from front and end. | ||||
|         Trim(value); | ||||
|         Trim(name); | ||||
|  | ||||
|         return true; | ||||
|     } | ||||
|     return false; | ||||
| } | ||||
|  | ||||
| // Replaces < with ( to satisfy html-rules. | ||||
| void moFileReader::MakeHtmlConform(std::string& inout) | ||||
| { | ||||
|     std::string temp = inout; | ||||
|     for ( unsigned int i = 0; i < temp.length(); i++) | ||||
|     { | ||||
|         if ( temp[i] == '>') | ||||
|         { | ||||
|             inout.replace(i, 1, ")"); | ||||
|         } | ||||
|         if ( temp[i] == '<' ) | ||||
|         { | ||||
|             inout.replace(i, 1, "("); | ||||
|         } | ||||
|     }     | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| moFileReaderSingleton& moFileReaderSingleton::GetInstance() | ||||
| { | ||||
|     static moFileReaderSingleton theoneandonly; | ||||
|     return theoneandonly; | ||||
| }  | ||||
|  | ||||
|  | ||||
| moFileReaderSingleton::moFileReaderSingleton(const moFileReaderSingleton& ) | ||||
| { | ||||
| } | ||||
|  | ||||
| moFileReaderSingleton::moFileReaderSingleton() | ||||
| { | ||||
| } | ||||
|  | ||||
| moFileReaderSingleton& moFileReaderSingleton::operator=(const moFileReaderSingleton&) | ||||
| { | ||||
|     return *this; | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| MO_END_NAMESPACE | ||||
		Reference in New Issue
	
	Block a user