core/CTranslator.h
author David Anderson <dvander@alliedmods.net>
Sat Jan 27 09:25:07 2007 +0000 (2007-01-27)
changeset 401 18fc0da83775
parent 382 65a7ea52f5b0
child 515 643240c66787
permissions -rw-r--r--
fixed this using /MD in release mode
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    15 #define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    16 
    17 #include "sm_trie.h"
    18 #include <sh_string.h>
    19 #include <sh_vector.h>
    20 #include "sm_globals.h"
    21 #include "sm_memtable.h"
    22 #include "ITextParsers.h"
    23 
    24 #define MAX_TRANSLATE_PARAMS		32
    25 
    26 /* :TODO: write a templatized version of tries? */
    27 
    28 using namespace SourceHook;
    29 class CTranslator;
    30 
    31 enum PhraseParseState
    32 {
    33 	PPS_None = 0,
    34 	PPS_Phrases,
    35 	PPS_InPhrase,
    36 };
    37 
    38 struct Language
    39 {
    40 	char m_code2[3];
    41 	int m_FullName;
    42 };
    43 
    44 struct Translation
    45 {
    46 	const char *szPhrase;		/**< Translated phrase. */
    47 	unsigned int fmt_count;		/**< Number of format parameters. */
    48 	int *fmt_order;				/**< Format phrase order. */
    49 };
    50 
    51 enum TransError
    52 {
    53 	Trans_Okay = 0,
    54 	Trans_BadLanguage = 1,
    55 	Trans_BadPhrase = 2,
    56 	Trans_BadPhraseLanguage = 3
    57 };
    58 
    59 class CPhraseFile : public ITextListener_SMC
    60 {
    61 public:
    62 	CPhraseFile(CTranslator *pTranslator, const char *file);
    63 	~CPhraseFile();
    64 public:
    65 	void ReparseFile();
    66 	const char *GetFilename();
    67 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    68 public: //ITextListener_SMC
    69 	void ReadSMC_ParseStart();
    70 	void ReadSMC_ParseEnd(bool halted, bool failed);
    71 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    72 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    73 	SMCParseResult ReadSMC_LeavingSection();
    74 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    75 private:
    76 	void ParseError(const char *message, ...);
    77 	void ParseWarning(const char *message, ...);
    78 private:
    79 	Trie *m_pPhraseLookup;
    80 	String m_File;
    81 	CTranslator *m_pTranslator;
    82 	PhraseParseState m_ParseState;
    83 	int m_CurPhrase;
    84 	BaseMemTable *m_pMemory;
    85 	BaseStringTable *m_pStringTab;
    86 	unsigned int m_LangCount;
    87 	String m_ParseError;
    88 	String m_LastPhraseString;
    89 	unsigned int m_CurLine;
    90 	bool m_FileLogged;
    91 };
    92 
    93 class CTranslator : public ITextListener_SMC
    94 {
    95 public:
    96 	CTranslator();
    97 	~CTranslator();
    98 public: //ITextListener_SMC
    99 	void ReadSMC_ParseStart();
   100 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   101 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   102 	SMCParseResult ReadSMC_LeavingSection();
   103 public:
   104 	void RebuildLanguageDatabase(const char *lang_header_file);
   105 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   106 	BaseStringTable *GetStringTable();
   107 	unsigned int GetLanguageCount();
   108 	bool GetLanguageByCode(const char *code, unsigned int *index);
   109 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   110 	CPhraseFile *GetFileByIndex(unsigned int index);
   111 private:
   112 	CVector<Language *> m_Languages;
   113 	CVector<CPhraseFile *> m_Files;
   114 	BaseStringTable *m_pStringTab;
   115 	Trie *m_pLCodeLookup;
   116 	bool m_InLanguageSection;
   117 	String m_CustomError;
   118 };
   119 
   120 extern CTranslator g_Translator;
   121 
   122 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_