core/CTranslator.h
author David Anderson <dvander@alliedmods.net>
Mon Feb 26 09:21:08 2007 +0000 (2007-02-26)
changeset 539 fa7e8a232a18
parent 520 7676d61cde2d
permissions -rw-r--r--
fixed stupid export
     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 	Trans_BadPhraseFile = 4,
    58 };
    59 
    60 class CPhraseFile : public ITextListener_SMC
    61 {
    62 public:
    63 	CPhraseFile(CTranslator *pTranslator, const char *file);
    64 	~CPhraseFile();
    65 public:
    66 	void ReparseFile();
    67 	const char *GetFilename();
    68 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    69 public: //ITextListener_SMC
    70 	void ReadSMC_ParseStart();
    71 	void ReadSMC_ParseEnd(bool halted, bool failed);
    72 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    73 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    74 	SMCParseResult ReadSMC_LeavingSection();
    75 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    76 private:
    77 	void ParseError(const char *message, ...);
    78 	void ParseWarning(const char *message, ...);
    79 private:
    80 	Trie *m_pPhraseLookup;
    81 	String m_File;
    82 	CTranslator *m_pTranslator;
    83 	PhraseParseState m_ParseState;
    84 	int m_CurPhrase;
    85 	BaseMemTable *m_pMemory;
    86 	BaseStringTable *m_pStringTab;
    87 	unsigned int m_LangCount;
    88 	String m_ParseError;
    89 	String m_LastPhraseString;
    90 	unsigned int m_CurLine;
    91 	bool m_FileLogged;
    92 };
    93 
    94 class CTranslator : 
    95 	public ITextListener_SMC,
    96 	public SMGlobalClass
    97 {
    98 public:
    99 	CTranslator();
   100 	~CTranslator();
   101 public: //SMGlobalClass
   102 	void OnSourceModAllInitialized();
   103 public: //ITextListener_SMC
   104 	void ReadSMC_ParseStart();
   105 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   106 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   107 	SMCParseResult ReadSMC_LeavingSection();
   108 public:
   109 	void RebuildLanguageDatabase(const char *lang_header_file);
   110 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   111 	BaseStringTable *GetStringTable();
   112 	unsigned int GetLanguageCount();
   113 	bool GetLanguageByCode(const char *code, unsigned int *index);
   114 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   115 	CPhraseFile *GetFileByIndex(unsigned int index);
   116 	TransError CoreTrans(int client, 
   117 						 char *buffer, 
   118 						 size_t maxlength, 
   119 						 const char *phrase, 
   120 						 void **params, 
   121 						 size_t *outlen=NULL);
   122 private:
   123 	bool AddLanguage(const char *langcode, const char *description);
   124 private:
   125 	CVector<Language *> m_Languages;
   126 	CVector<CPhraseFile *> m_Files;
   127 	BaseStringTable *m_pStringTab;
   128 	Trie *m_pLCodeLookup;
   129 	bool m_InLanguageSection;
   130 	String m_CustomError;
   131 };
   132 
   133 extern CPhraseFile *g_pCorePhrases;
   134 extern CTranslator g_Translator;
   135 
   136 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_