Added LookupWithContext

This commit is contained in:
Edgar 2018-12-13 15:55:43 +01:00
parent 4e08b67503
commit 05fa218a21
15 changed files with 271 additions and 175 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ cmake-build-debug/
\.idea/
docs/node_modules/
docs/doc/
build/

View File

@ -1,10 +1,10 @@
env:
global:
- CONAN_REFERENCE: "MofileReader/0.1.2"
- CONAN_REFERENCE: "MofileReader/1.0.0"
- CONAN_USERNAME: "anotherfoxguy"
- CONAN_LOGIN_USERNAME: "anotherfoxguy"
- CONAN_CHANNEL: "MofileReader"
- CONAN_CHANNEL: "testing"
- CONAN_UPLOAD: "https://api.bintray.com/conan/anotherfoxguy/ror-dependencies"
linux: &linux

View File

@ -2,7 +2,7 @@
# moFileReader Main Build Script
#
# Defined Variables:
# - COMPILE_DLL
# - COMPILE_DLL
# - ON : Compiles the code as a shared Library
# - OFF : Compiles the code as a static Library
# - BUILD_DEBUG
@ -13,12 +13,9 @@
# possible settings.
#-------------------------------------------------------
cmake_minimum_required(VERSION 3.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
project(moFileReader)
# The main include directory
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/include)
# Let the user choose between static lib and dll
# To use it, call cmake -DCOMPILE_DLL=ON
option(COMPILE_DLL "Set this to ON if you want to compile the library as an DLL. When this is OFF, a static library is created (default)." OFF)
@ -30,6 +27,9 @@ endif ()
add_library(moFileReader STATIC ${CMAKE_SOURCE_DIR}/src/moFileReader.cpp ${CMAKE_SOURCE_DIR}/src/mo.cpp)
add_executable(moReader ${CMAKE_SOURCE_DIR}/src/mo.cpp)
target_include_directories(moFileReader PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(moReader PRIVATE ${CMAKE_SOURCE_DIR}/include)
if (COMPILE_DLL)
target_compile_definitions(moReader PRIVATE _CONSOLE MOFILE_IMPORT)
else ()
@ -42,10 +42,6 @@ target_link_libraries(moReader moFileReader)
option(BUILD_TEST "Set this to ON if you want to build the test" OFF)
if(BUILD_TEST)
enable_testing()
add_subdirectory(test)
endif()

View File

@ -6,10 +6,10 @@ environment:
PYTHON_VERSION: "2.7.8"
PYTHON_ARCH: "32"
CONAN_REFERENCE: "MofileReader/0.1.2"
CONAN_REFERENCE: "MofileReader/1.0.0"
CONAN_USERNAME: "anotherfoxguy"
CONAN_LOGIN_USERNAME: "anotherfoxguy"
CONAN_CHANNEL: "MofileReader"
CONAN_CHANNEL: "testing"
CONAN_UPLOAD: "https://api.bintray.com/conan/anotherfoxguy/ror-dependencies"
CONAN_VISUAL_VERSIONS: 15

View File

@ -3,7 +3,7 @@ import os
class MofilereaderConan(ConanFile):
name = "MofileReader"
version = "0.1.2"
version = "1.0.0"
license = "MIT"
url = "https://github.com/AnotherFoxGuy/conan-MofileReader/"
description = "This API lets you read .mo-Files and use their content just as you would do with GNUs gettext."

View File

@ -1,7 +1,8 @@
/*
* moFileReader - A simple .mo-File-Reader
* Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)
* All rights reserved.
* Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -14,8 +15,8 @@
* 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
* 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
@ -37,9 +38,9 @@
// Defines an export-macro when compiling as dll on woe32.
//-------------------------------------------------------------
#if defined(MOFILE_EXPORTS) && defined (WIN32)
# define MOEXPORT __declspec(dllexport)
#elif defined (MOFILE_IMPORT) && defined(WIN32)
# define MOEXPORT __declspec(dllimport)
# define MOEXPORT __declspec(dllexport)
#elif defined (MOFILE_IMPORT) && defined(WIN32)
# define MOEXPORT __declspec(dllimport)
#else
# define MOEXPORT
#endif
@ -66,5 +67,3 @@
#endif /* __MOFILECONFIG_H_INCLUDED__ */

View File

@ -1,7 +1,8 @@
/*
* moFileReader - A simple .mo-File-Reader
* Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)
* All rights reserved.
* Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -14,8 +15,8 @@
* 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
* 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
@ -45,33 +46,10 @@
#endif
/** \mainpage moFileReaderSDK
*
*
* <h2>Compilation with Visual C++ (Express and better)</h2>
*
* We provide a project for Visual C++ 2008. You can select 3 Types of Compilation:
*
* <h3>Executable (Release or Debug)</h3>
*
* This will compile the code as an executable which can lookup strings from every .mo-File you load with it.
* This can be handy if you want to have a peek into a file or test something etc. I recommend building the
* release-executable only if you just want to use it.
*
* <h3>Dynamic Loaded Library ( ReleaseDLL )</h3>
*
* This may be overkill, but perhaps you like it modular. This Configuration will create a dll and an import-library.
* Do not forget to link against the import-library and please define MOFILE_IMPORT in your preprocessor-settings,
* otherwise you will receive a bunch of linker-errors.
* You will find all files in the directory "lib" in the Solutions Directory.
*
* <h3>Static Library ( ReleaseLIB )</h3>
*
* This will compile the code as a static library with no Entry-Point. This is the recommended usage.
* But please do not forget to link against moFileReader.static.lib. Otherwise you will receive linker-errors.
*
* <h2>Compilation via cmake</h2>
*
* - Make sure you have cmake installed and in your path. If not, go to http://www.cmake.org and get it.
* - Make sure you have cmake installed and in your path. If not, go to http://www.cmake.org and get it.
* - Switch to a Shell or commandline
* - Run cmake in $INSTALLDIR\\build. See cmake --help for possible generators. Here are the available Options:
* - COMPILE_DLL Setting this to ON will compile the library as a shared module. By default, a static library is built.
@ -84,13 +62,13 @@
* \endcode
*
* cmake will compile the library and moReader[.exe]-binary, which can do lookups in moFiles and export moFiles as HTML.
* See moReader[.exe] --help for details.
* See moReader[.exe] --help for details.
* You will find the libraries in %%projectdir%%/lib and the binary in %%projectdir%%/bin
*
*
* <h2>None of those?</h2>
* <h2>Include in project</h2>
*
* The last option is to simply add moFileReader.cpp, moFileReader.h and moFileConfig.h to your project. Thats all you have to do.
* The last option is to simply add moFileReader.cpp, moFileReader.h and moFileConfig.h to your project. Thats all you have to do.
* You can safely exclude mo.cpp, since this file keeps the entry-points of the .exe and .dll only.
*
* <h2>Usage</h2>
@ -108,7 +86,7 @@
* }
*
* // Now, you can lookup the strings you stored in the .mo-File:
* std::cout << reader.Lookup("MyTranslationString") << std::endl;
* std::cout << reader.Lookup("MyTranslationString") << std::endl;
*
* \endcode
* Thats all! This small code has no dependencies, except the C/C++-runtime of your compiler,
@ -118,7 +96,13 @@
* a file to test it and I hate to release stuff I wasn't able to test.
*
* <h2>Changelog</h2>
*
*
* - Version 1.0.0
* - Added new function: LookupWithContext
* - Added unit-tests
* - Added support for packaging with Conan
* - Moved project to https://github.com/AnotherFoxGuy/MofileReader
*
* - Version 0.1.2
* - Generic improvements to the documentation.
* - Generic improvements to the code
@ -126,11 +110,11 @@
* message if only --export or --lookup where missing.
* - Added -h, --help and -? to moReader[.exe]. It will print the help-screen.
* - Added --version and -v to moReader[.exe]. It will print some informations about the program.
* - Added --license to moReader[.exe]. This will print its license.
* - --export gives now a feedback about success or failure.
* - The HTML-Dump-Method outputs now the whole table from the empty msgid in a nice html-table, not only a few hardcoded.
* - I had an issue-report that the Error-Constants can collide with foreign code under certain conditions,
* so I added a patch which renamed the error-constants to more compatible names.
* - Added --license to moReader[.exe]. This will print its license.
* - --export gives now a feedback about success or failure.
* - The HTML-Dump-Method outputs now the whole table from the empty msgid in a nice html-table, not only a few hardcoded.
* - I had an issue-report that the Error-Constants can collide with foreign code under certain conditions,
* so I added a patch which renamed the error-constants to more compatible names.
*
* - Version 0.1.1
* - Added the ability to export mo's as HTML.
@ -152,7 +136,7 @@
* \n
* Thanks for using this piece of OpenSource-Software.\n
* Submit patches and/or bugs on https://github.com/AnotherFoxGuy/MofileReader.
* Send your flames, dumb comments etc to /dev/null, thank you.
* Send your flames, dumb comments etc to /dev/null, thank you.
*/
@ -161,7 +145,7 @@
About Warning 4251:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;16.
I am aware of this warning and know how to deal with it.
I am aware of this warning and know how to deal with it.
To avoid that derived projects are influenced by this warning
I have deactivated it for your convinience.
Note: This warning only occurs, when using this code as a DLL.
@ -208,8 +192,8 @@ a:hover{color:blue;}\
struct moTranslationPairInformation
{
/// \brief Constructor
moTranslationPairInformation()
: m_orLength(0), m_orOffset(0),
moTranslationPairInformation()
: m_orLength(0), m_orOffset(0),
m_trLength(0), m_trOffset(0)
{}
@ -229,13 +213,13 @@ struct moTranslationPairInformation
/**
* \brief Describes the "Header" of a .mo-File.
*
*
* The File info keeps the header of a .mo-file and
*
* The File info keeps the header of a .mo-file and
* a list of the string-descriptions.
* The typedef is for the type of the string-list.
* The constructor ensures, that all members get a nice
* initial value.
*/
*/
struct moFileInfo
{
/// \brief Type for the list of all Translation-Pair-Descriptions.
@ -244,53 +228,53 @@ struct moFileInfo
/// \brief Constructor
moFileInfo()
: m_magicNumber(0), m_fileVersion(0), m_numStrings(0),
m_offsetOriginal(0), m_offsetTranslation(0), m_sizeHashtable(0),
m_offsetOriginal(0), m_offsetTranslation(0), m_sizeHashtable(0),
m_offsetHashtable(0), m_reversed(false)
{}
/// \brief The Magic Number, compare it to g_MagicNumber.
int m_magicNumber;
int m_magicNumber;
/// \brief The File Version, 0 atm according to the manpage.
int m_fileVersion;
int m_fileVersion;
/// \brief Number of Strings in the .mo-file.
int m_numStrings;
int m_numStrings;
/// \brief Offset of the Table of the Original Strings
int m_offsetOriginal;
int m_offsetOriginal;
/// \brief Offset of the Table of the Translated Strings
int m_offsetTranslation;
int m_offsetTranslation;
/// \brief Size of 1 Entry in the Hashtable.
int m_sizeHashtable;
int m_sizeHashtable;
/// \brief The Offset of the Hashtable.
int m_offsetHashtable;
int m_offsetHashtable;
/** \brief Tells you if the bytes are reversed
* \note When this is true, the bytes are reversed and the Magic number is like g_MagicReversed
*/
bool m_reversed;
bool m_reversed;
/// \brief A list containing offset and length of the strings in the file.
moTranslationPairList m_translationPairInformation;
moTranslationPairList m_translationPairInformation;
};
/**
/**
* \brief This class is a gettext-replacement.
*
*
* The usage is quite simple:\n
* Tell the class which .mo-file it shall load via
* Tell the class which .mo-file it shall load via
* moFileReader::ReadFile(). The method will attempt to load
* the file, all translations will be stored in memory.
* Afterwards you can lookup the strings with moFileReader::Lookup() just
* like you would do with gettext.
* Additionally, you can call moFileReader::ReadFile() for as much files as you
* Additionally, you can call moFileReader::ReadFile() for as much files as you
* like. But please be aware, that if there are duplicated keys (original strings),
* that they will replace each other in the lookup-table. There is no check done, if a
* that they will replace each other in the lookup-table. There is no check done, if a
* key already exists.
*
* \note If you add "Lookup" to the keywords of the gettext-parser (like poEdit),
@ -306,10 +290,10 @@ protected:
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.
static const long MagicNumber = 0x950412DE;
/// \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 long MagicReversed = 0xDE120495;
/// \brief The possible errorcodes for methods of this class
@ -330,14 +314,14 @@ public:
/// \brief Empty Lookup-Table (returned by ExportAsHTML())
EC_TABLEEMPTY,
/// \brief The magic number did not match
EC_MAGICNUMBER_NOMATCH,
/// \brief The magic number did not match
EC_MAGICNUMBER_NOMATCH,
/**
* \brief The magic number is reversed.
* \note This is an error until the class supports it.
*/
EC_MAGICNUMBER_REVERSED,
EC_MAGICNUMBER_REVERSED,
};
/** \brief Reads a .mo-file
@ -361,17 +345,26 @@ public:
virtual eErrorCode ReadFile(const char* filename);
/** \brief Returns the searched translation or returns the input.
* \param[in,out] id The id of the translation to search for.
* \param[in] id The id of the translation to search for.
* \return The value you passed in via _id or the translated string.
*/
virtual std::string Lookup( const char* id ) const;
/** \brief Returns the searched translation or returns the input, restricted to the context given by context.
* See https://www.gnu.org/software/gettext/manual/html_node/Contexts.html for more info.
* \param[in] context The id of the translation to search for.
* \param[in] id The id of the translation to search for.
* \return The value you passed in via _id or the translated string.
*/
virtual std::string LookupWithContext (const char* context, const char* id) const;
/// \brief Returns the Error Description.
virtual const std::string& GetErrorDescription() const;
/// \brief Empties the Lookup-Table.
virtual void ClearTable();
/** \brief Returns the Number of Entries in our Lookup-Table.
* \note The mo-File-table always contains an empty msgid, which contains informations
* about the tranlsation-project. So the real number of strings is always minus 1.
@ -379,9 +372,9 @@ public:
virtual unsigned int GetNumStrings() const;
/** \brief Exports the whole content of the .mo-File as .html
* \param[in] infile The .mo-File to export.
* \param[in] filename Where to store the .html-file. If empty, the path and filename of the _infile with .html appended.
* \param[in,out] css The css-script for the visual style of the
* \param[in] infile The .mo-File to export.
* \param[in] filename Where to store the .html-file. If empty, the path and filename of the _infile with .html appended.
* \param[in,out] css The css-script for the visual style of the
* file, in case you don't like mine ;).
* \see g_css for the possible and used css-values.
*/
@ -395,7 +388,7 @@ protected:
* \param[in] in The value to swap.
* \return The swapped value.
*/
unsigned long SwapBytes(unsigned long in);
unsigned long SwapBytes(unsigned long in);
private:
// Holds the lookup-table
@ -410,29 +403,29 @@ private:
*
*
* This class derives from moFileReader and builds a singleton to access its methods
* in a global manner.
* in a global manner.
* \note This class is a Singleton. Please access it via moFileReaderSingleton::GetInstance()
* or use the provided wrappers:\n
* - moReadMoFile()
* - _()
* - moFileClearTable()
* - moFileClearTable()
* - moFileGetErrorDescription()
* - moFileGetNumStrings();
*/
class MOEXPORT moFileReaderSingleton : public moFileReader
{
private:
// Private Contructor and Copy-Constructor to avoid
// Private Contructor and Copy-Constructor to avoid
// that this class is instanced.
moFileReaderSingleton();
moFileReaderSingleton(const moFileReaderSingleton&);
moFileReaderSingleton& operator=(const moFileReaderSingleton&);
public:
/** \brief Singleton-Accessor.
/** \brief Singleton-Accessor.
* \return A static instance of moFileReaderSingleton.
*/
static moFileReaderSingleton& GetInstance();
static moFileReaderSingleton& GetInstance();
};
/** \brief Reads the .mo-File.
@ -455,6 +448,16 @@ inline std::string _(const char* id)
return r;
}
/** \brief Looks for the spec. string to translate.
* \param[in] id The string-id to search.
* \return The translation if found, otherwise it returns id.
*/
inline std::string _L(const char* id)
{
std::string r = moFileReaderSingleton::GetInstance().Lookup(id);
return r;
}
/// \brief Resets the Lookup-Table.
inline void moFileClearTable()
{
@ -462,7 +465,7 @@ inline void moFileClearTable()
}
/// \brief Returns the last known error as string or an empty class.
inline std::string moFileGetErrorDescription()
inline std::string moFileGetErrorDescription()
{
std::string r = moFileReaderSingleton::GetInstance().GetErrorDescription();
return r;

View File

@ -1,7 +1,8 @@
/*
* moFileReader - A simple .mo-File-Reader
* Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)
* All rights reserved.
* Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -14,8 +15,8 @@
* 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
* 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
@ -30,12 +31,12 @@
* 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 "moFileReader.h"
#include <iostream>
#include <cstdlib>
#if defined(_MSC_VER) && defined(_DEBUG)
# include <crtdbg.h>
# include <crtdbg.h>
#endif /* _MSC_VER */
using namespace moFileLib;
@ -45,8 +46,8 @@ 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 << "--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;
@ -107,7 +108,7 @@ int main( int, char** argv )
long flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
_CrtSetDbgFlag(flag);
#endif /* _MSC_VER && _DEBUG */
#endif /* _MSC_VER && _DEBUG */
std::string appname = GetAppName(argv[0]);
@ -146,7 +147,7 @@ int main( int, char** argv )
{
outfile = argv[3];
}
moFileReader::eErrorCode r = moFileReader::ExportAsHTML(argv[2], outfile);
if ( r == moFileReader::EC_SUCCESS )
{
@ -161,7 +162,7 @@ int main( int, char** argv )
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;
return EXIT_FAILURE;
}
else if ( r == moFileReader::EC_FILEINVALID )
{
@ -229,4 +230,3 @@ int WINAPI DllMain( DWORD reason, LPVOID)
}
#endif /* Compilation-Mode */

View File

@ -1,7 +1,8 @@
/*
* moFileReader - A simple .mo-File-Reader
* Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)
* All rights reserved.
* Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -14,8 +15,8 @@
* 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
* 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
@ -30,13 +31,13 @@
* 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 "moFileReader.h"
#include <iostream>
#include <cstdlib>
MO_BEGIN_NAMESPACE
unsigned long moFileReader::SwapBytes(unsigned long in)
unsigned long moFileReader::SwapBytes(unsigned long in)
{
unsigned long b0 = (in >> 0) & 0xff;
unsigned long b1 = (in >> 8) & 0xff;
@ -64,7 +65,7 @@ unsigned int moFileReader::GetNumStrings() const
std::string moFileReader::Lookup( const char* id ) const
{
if ( m_lookup.size() <= 0) return id;
moLookupList::const_iterator iterator = m_lookup.find(id);
moLookupList::const_iterator iterator = m_lookup.find(id);
if ( iterator == m_lookup.end() )
{
@ -73,17 +74,33 @@ std::string moFileReader::Lookup( const char* id ) const
return iterator->second;
}
std::string moFileReader::LookupWithContext (const char* context, const char* id) const
{
std::string idName = context;
idName += '\x04';
idName += id;
if (m_lookup.size () <= 0) return id;
moLookupList::const_iterator iterator = m_lookup.find (idName);
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);
@ -92,14 +109,14 @@ moFileReader::eErrorCode moFileReader::ParseData(std::string data)
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 )
{
@ -116,7 +133,7 @@ moFileReader::eErrorCode moFileReader::ParseData(std::string data)
return moFileReader::EC_MAGICNUMBER_REVERSED;
}
}
// Now we search all Length & Offsets of the original strings
for ( int i = 0; i < moInfo.m_numStrings; i++ )
{
@ -129,10 +146,10 @@ moFileReader::eErrorCode moFileReader::ParseData(std::string data)
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++ )
@ -147,61 +164,61 @@ moFileReader::eErrorCode moFileReader::ParseData(std::string data)
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;
@ -247,11 +264,11 @@ moFileReader::eErrorCode moFileReader::ReadFile( const char* filename )
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;
moTranslationPairInformation _str;
stream.read((char*)&_str.m_orLength, 4);
stream.read((char*)&_str.m_orOffset, 4);
if ( stream.bad() )
@ -303,7 +320,7 @@ moFileReader::eErrorCode moFileReader::ReadFile( const char* filename )
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);
@ -317,7 +334,7 @@ moFileReader::eErrorCode moFileReader::ReadFile( const char* filename )
return moFileReader::EC_FILEINVALID;
}
// Store it in the map.
// Store it in the map.
m_lookup[std::string(original)] = std::string(translation);
// Cleanup...
@ -344,7 +361,7 @@ moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::str
if ( reader.m_lookup.empty() )
{
return moFileReader::EC_TABLEEMPTY;
}
}
// Beautify Output
std::string fname;
@ -363,11 +380,11 @@ moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::str
if (htmlfile.empty())
{
htmlfile = infile + std::string(".html");
}
}
// Ok, now prepare output.
std::ofstream stream(htmlfile.c_str());
if ( stream.is_open() )
if ( stream.is_open() )
{
stream << R"(<!DOCTYPE HTML PUBLIC "- //W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">)"
<< std::endl;
@ -383,7 +400,7 @@ moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::str
std::stringstream parsee;
parsee << reader.Lookup("");
while ( !parsee.eof() )
{
char buffer[1024];
@ -402,7 +419,7 @@ moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::str
// Now output the content
stream << R"(<table border="1"><th colspan="2">Content</th>)" << std::endl;
for ( moLookupList::const_iterator it = reader.m_lookup.begin();
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.
@ -421,12 +438,12 @@ moFileReader::eErrorCode moFileReader::ExportAsHTML(std::string infile, std::str
{
return moFileReader::EC_FILENOTFOUND;
}
return moFileReader::EC_SUCCESS;
}
// Removes spaces from front and end.
// Removes spaces from front and end.
void moFileReader::Trim(std::string& in)
{
while ( in[0] == ' ' )
@ -477,7 +494,7 @@ void moFileReader::MakeHtmlConform(std::string& inout)
{
inout.replace(i, 1, "(");
}
}
}
}
@ -489,7 +506,7 @@ moFileReaderSingleton& moFileReaderSingleton::GetInstance()
{
static moFileReaderSingleton theoneandonly;
return theoneandonly;
}
}
moFileReaderSingleton::moFileReaderSingleton(const moFileReaderSingleton& )

View File

@ -8,8 +8,13 @@ include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(REQUIRES gtest/1.8.1@bincrafters/stable BASIC_SETUP CMAKE_TARGETS BUILD missing)
add_executable(test test.cpp)
target_link_libraries(test CONAN_PKG::gtest moFileReader)
add_test(NAME mo_test COMMAND test)
add_executable(moFileReaderTest test.cpp)
target_include_directories(moFileReaderTest PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(moFileReaderTest CONAN_PKG::gtest moFileReader)
add_test(NAME mo_test COMMAND moFileReaderTest)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test.mo DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin/)
add_custom_command(
TARGET moFileReaderTest POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/test.mo $<TARGET_FILE_DIR:moFileReaderTest>/test.mo
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/test.mo ${CMAKE_CURRENT_BINARY_DIR}/test.mo
)

View File

@ -1,2 +1,2 @@
#!/bin/sh
xgettext -o test.pot -k_L -s -j test.cpp
xgettext -o test.pot -k_ -k"_LC:1c,2" -s -c --package-name=moFileReader test.cpp

View File

@ -2,19 +2,35 @@
#include "moFileReader.h"
#include "gtest/gtest.h"
#define _L(str) moFileLib::moFileReaderSingleton::GetInstance().Lookup(str)
using namespace moFileLib;
#define _LC(ctx,str) moFileReaderSingleton::GetInstance().LookupWithContext(ctx,str)
auto testMo = "test.mo";
TEST(moFileReader, setup)
{
EXPECT_EQ(moFileLib::moFileReaderSingleton::GetInstance().ReadFile(testMo), moFileLib::moFileReader::EC_SUCCESS);
EXPECT_EQ(moFileReaderSingleton::GetInstance().ReadFile(testMo), moFileLib::moFileReader::EC_SUCCESS);
}
TEST(moFileReader, Lookup)
{
moFileLib::moFileReaderSingleton::GetInstance().ReadFile(testMo);
EXPECT_EQ("Text Nederlands Een", _L("String English One"));
EXPECT_EQ("Text Nederlands Twee", _L("String English Two"));
EXPECT_EQ("Text Nederlands Drie", _L("String English Three"));
moFileReaderSingleton::GetInstance ().ReadFile (testMo);
/* This is the first comment. */
EXPECT_EQ ("Text Nederlands Een", _ ("String English One"));
/* This is the second comment. */
EXPECT_EQ ("Text Nederlands Twee", _ ("String English Two"));
/* This is the third comment. */
EXPECT_EQ ("Text Nederlands Drie", _ ("String English Three"));
}
TEST (moFileReader, LookupWithContext)
{
moFileReaderSingleton::GetInstance ().ReadFile (testMo);
/* This is the first comment. */
EXPECT_EQ ("Text Nederlands Een", _LC ("TEST|String|1", "String English"));
/* This is the second comment. */
EXPECT_EQ ("Text Nederlands Twee", _LC ("TEST|String|2", "String English"));
/* This is the third comment. */
EXPECT_EQ ("Text Nederlands Drie", _LC ("TEST|String|3", "String English"));
}

Binary file not shown.

View File

@ -5,27 +5,64 @@
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Project-Id-Version: moFileReader\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-12-13 10:49+0100\n"
"PO-Revision-Date: 2018-12-13 11:00+0100\n"
"POT-Creation-Date: 2018-12-13 14:39+0100\n"
"PO-Revision-Date: 2018-12-13 14:40+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language-Team: Test\n"
"Language: nl\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.6\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 2.2\n"
#: test.cpp:21
#. This is the first comment.
#: test.cpp:31
msgctxt "TEST|String|1"
msgid "String English"
msgstr "Text Nederlands Een"
#. This is the second comment.
#: test.cpp:33
msgctxt "TEST|String|2"
msgid "String English"
msgstr "Text Nederlands Twee"
#. This is the third comment.
#: test.cpp:35
msgctxt "TEST|String|3"
msgid "String English"
msgstr "Text Nederlands Drie"
#. This is the first comment.
#: test.cpp:19
msgid "String English One"
msgstr "Text Nederlands Een"
#. This is the third comment.
#: test.cpp:23
msgid "String English Three"
msgstr "Text Nederlands Drie"
#: test.cpp:22
#. This is the second comment.
#: test.cpp:21
msgid "String English Two"
msgstr "Text Nederlands Twee"
#~ msgctxt "TEST|String|1"
#~ msgid "String English One"
#~ msgstr "Text Nederlands Een"
#~ msgctxt "TEST|String|3"
#~ msgid "String English Three"
#~ msgstr "Text Nederlands Drie"
#~ msgctxt "TEST|String|2"
#~ msgid "String English Two"
#~ msgstr "Text Nederlands Twee"
#~ msgctxt "Menu|"
#~ msgid "File"
#~ msgstr "Bestand"

View File

@ -1,30 +1,51 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# This file is distributed under the same license as the moFileReader package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Project-Id-Version: moFileReader\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-12-13 10:49+0100\n"
"POT-Creation-Date: 2018-12-13 14:39+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: test.cpp:21
#. This is the first comment.
#: test.cpp:31
msgctxt "TEST|String|1"
msgid "String English"
msgstr ""
#. This is the second comment.
#: test.cpp:33
msgctxt "TEST|String|2"
msgid "String English"
msgstr ""
#. This is the third comment.
#: test.cpp:35
msgctxt "TEST|String|3"
msgid "String English"
msgstr ""
#. This is the first comment.
#: test.cpp:19
msgid "String English One"
msgstr ""
#. This is the third comment.
#: test.cpp:23
msgid "String English Three"
msgstr ""
#: test.cpp:22
#. This is the second comment.
#: test.cpp:21
msgid "String English Two"
msgstr ""