Main Page   Namespaces   Classes ( Hierarchy )   Functions & Members  

moFileReader.h

Go to the documentation of this file.
00001 /*
00002  * moFileReader - A simple .mo-File-Reader
00003  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com)
00004  * All rights reserved.                          
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  *   1. Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *   2. Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in the
00015  *      documentation and/or other materials provided with the distribution.
00016  *
00017  *   3. The names of its contributors may not be used to endorse or promote 
00018  *      products derived from this software without specific prior written 
00019  *      permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00025  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 #ifndef __MOFILEREADER_H_INCLUDED__
00034 #define __MOFILEREADER_H_INCLUDED__
00035 
00036 #include <deque>
00037 #include <map>
00038 #include <fstream>
00039 #include <cstring> // this is for memset when compiling with gcc.
00040 #include <string>
00041 #include <sstream>
00042 
00043 #ifndef __MOFILECONFIG_H_INCLUDED__
00044 #   include "moFileConfig.h"
00045 #endif
00046 
00169 /*
00170     About Warning 4251:
00171     http://support.microsoft.com/default.aspx?scid=KB;EN-US;16.
00172 
00173     I am aware of this warning and know how to deal with it. 
00174     To avoid that derived projects are influenced by this warning
00175     I have deactivated it for your convinience.
00176     Note: This warning only occurs, when using this code as a DLL.
00177 */
00178 #if defined(_MSC_VER) && ( defined(_EXPORT) || defined(MOFILE_IMPORT) )
00179 #   pragma warning (disable:4251)
00180 #endif /* _MSC_VER */
00181 
00182 
00186 MO_BEGIN_NAMESPACE
00187 
00188 const std::string g_css = \
00189 "\
00190 body {\
00191     background-color: black;\
00192     color: silver;\
00193 }\
00194 table {\
00195 width: 80%;}\
00196 th {\
00197 background-color: orange;\
00198 color: black;\
00199 }\
00200 hr { color: red;width: 80%; size: 5px; }\
00201 a:link{color: gold;}\
00202 a:visited{color: grey;}\
00203 a:hover{color:blue;}\
00204 .copyleft{\
00205     font-size: 12px; \
00206     text-align: center;\
00207 }\
00208 ";
00209 
00217 struct moTranslationPairInformation
00218 {
00220     moTranslationPairInformation() 
00221         : m_orLength(0), m_orOffset(0), 
00222           m_trLength(0), m_trOffset(0)
00223     {}
00224 
00226     int m_orLength;
00227 
00229     int m_orOffset;
00230 
00232     int m_trLength;
00233 
00235     int m_trOffset;
00236 };
00237 
00248 struct moFileInfo
00249 {
00251     typedef std::deque<moTranslationPairInformation> moTranslationPairList;
00252 
00254     moFileInfo()
00255         : m_magicNumber(0), m_fileVersion(0), m_numStrings(0),
00256           m_offsetOriginal(0), m_offsetTranslation(0), m_sizeHashtable(0), 
00257           m_offsetHashtable(0), m_reversed(false)
00258     {}
00259 
00261     int m_magicNumber;          
00262     
00264     int m_fileVersion;          
00265 
00267     int m_numStrings;           
00268 
00270     int m_offsetOriginal;       
00271 
00273     int m_offsetTranslation;    
00274 
00276     int m_sizeHashtable;       
00277 
00279     int m_offsetHashtable;      
00280 
00284     bool m_reversed;            
00285 
00287     moTranslationPairList m_translationPairInformation;   
00288 };
00289 
00310 class MOEXPORT moFileReader
00311 {
00312 protected:
00314     typedef std::map<std::string, std::string> moLookupList;
00315 
00316 public:
00317 
00319     static const long _MagicNumber   = 0x950412DE;
00320 
00322     static const long _MagicReversed = 0xDE120495;
00323 
00325     enum eErrorCode
00326     {
00328         EC_SUCCESS = 0,
00329 
00331         EC_ERROR,
00332 
00334         EC_FILENOTFOUND,
00335 
00337         EC_FILEINVALID,
00338 
00340         EC_TABLEEMPTY,
00341 
00343         EC_MAGICNUMBER_NOMATCH, 
00344 
00349         EC_MAGICNUMBER_REVERSED,           
00350     };
00351 
00360     virtual eErrorCode ReadFile(const char* _filename);
00361 
00366     virtual std::string Lookup( const char* _id ) const;
00367 
00369     virtual const std::string& GetErrorDescription() const;
00370 
00372     virtual void ClearTable();
00373     
00378     virtual unsigned int GetNumStrings() const;
00379 
00387     static eErrorCode ExportAsHTML(const std::string _infile, const std::string _filename = "", const std::string _css = g_css );
00388 
00389 protected:
00391     std::string m_error;
00392 
00397     unsigned long SwapBytes(unsigned long _in);    
00398 
00399 private:
00400     // Holds the lookup-table
00401     moLookupList m_lookup;
00402 
00403     void MakeHtmlConform(std::string& _inout);
00404     bool GetPoEditorString(const char* _buffer, std::string& _name, std::string& _value);
00405     void Trim(std::string& _in);
00406 };
00407 
00421 class MOEXPORT moFileReaderSingleton : public moFileReader
00422 {
00423 private:
00424     // Private Contructor and Copy-Constructor to avoid 
00425     // that this class is instanced.
00426     moFileReaderSingleton();
00427     moFileReaderSingleton(const moFileReaderSingleton&);
00428     moFileReaderSingleton& operator=(const moFileReaderSingleton&);
00429     
00430 public:
00434     static moFileReaderSingleton& GetInstance();      
00435 };
00436 
00441 inline moFileReader::eErrorCode moReadMoFile(const char* _filename)
00442 {
00443     moFileReader::eErrorCode r = moFileReaderSingleton::GetInstance().ReadFile(_filename);
00444     return r;
00445 }
00446 
00451 inline std::string _(const char* id)
00452 {
00453     std::string r = moFileReaderSingleton::GetInstance().Lookup(id);
00454     return r;
00455 }
00456 
00458 inline void moFileClearTable()
00459 {
00460     moFileReaderSingleton::GetInstance().ClearTable();
00461 }
00462 
00464 inline std::string moFileGetErrorDescription() 
00465 {
00466     std::string r = moFileReaderSingleton::GetInstance().GetErrorDescription();
00467     return r;
00468 }
00469 
00471 inline int moFileGetNumStrings()
00472 {
00473     int r = moFileReaderSingleton::GetInstance().GetNumStrings();
00474     return r;
00475 }
00476 
00477 #if defined(_MSC_VER)
00478 #   pragma warning (default:4251)
00479 #endif /* _MSC_VER */
00480 
00481 MO_END_NAMESPACE
00482 
00483 #endif /* __MOFILEREADER_H_INCLUDED__ */

Generated on 17 Jan 2009 for moFileReader by Doxygen1.5.7.1