core/Translator.h
author Scott Ehlert <ds@alliedmods.net>
Thu Apr 05 03:02:00 2007 +0000 (2007-04-05)
changeset 661 5c0988b9bf0c
parent 653 ef60870067ff
child 663 b526b82e256e
permissions -rw-r--r--
Ugh, initial import of core config file stuff
Warning: This is positively terrible code.
It works, but needs A LOT of work still :\
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    16 #define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
    17 
    18 #include "sm_trie.h"
    19 #include <sh_string.h>
    20 #include <sh_vector.h>
    21 #include "sm_globals.h"
    22 #include "sm_memtable.h"
    23 #include "ITextParsers.h"
    24 
    25 #define MAX_TRANSLATE_PARAMS		32
    26 
    27 /* :TODO: write a templatized version of tries? */
    28 
    29 using namespace SourceHook;
    30 class Translator;
    31 
    32 enum PhraseParseState
    33 {
    34 	PPS_None = 0,
    35 	PPS_Phrases,
    36 	PPS_InPhrase,
    37 };
    38 
    39 struct Language
    40 {
    41 	char m_code2[3];
    42 	int m_FullName;
    43 };
    44 
    45 struct Translation
    46 {
    47 	const char *szPhrase;		/**< Translated phrase. */
    48 	unsigned int fmt_count;		/**< Number of format parameters. */
    49 	int *fmt_order;				/**< Format phrase order. */
    50 };
    51 
    52 enum TransError
    53 {
    54 	Trans_Okay = 0,
    55 	Trans_BadLanguage = 1,
    56 	Trans_BadPhrase = 2,
    57 	Trans_BadPhraseLanguage = 3,
    58 	Trans_BadPhraseFile = 4,
    59 };
    60 
    61 class CPhraseFile : public ITextListener_SMC
    62 {
    63 public:
    64 	CPhraseFile(Translator *pTranslator, const char *file);
    65 	~CPhraseFile();
    66 public:
    67 	void ReparseFile();
    68 	const char *GetFilename();
    69 	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
    70 public: //ITextListener_SMC
    71 	void ReadSMC_ParseStart();
    72 	void ReadSMC_ParseEnd(bool halted, bool failed);
    73 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    74 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    75 	SMCParseResult ReadSMC_LeavingSection();
    76 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    77 private:
    78 	void ParseError(const char *message, ...);
    79 	void ParseWarning(const char *message, ...);
    80 private:
    81 	Trie *m_pPhraseLookup;
    82 	String m_File;
    83 	Translator *m_pTranslator;
    84 	PhraseParseState m_ParseState;
    85 	int m_CurPhrase;
    86 	BaseMemTable *m_pMemory;
    87 	BaseStringTable *m_pStringTab;
    88 	unsigned int m_LangCount;
    89 	String m_ParseError;
    90 	String m_LastPhraseString;
    91 	unsigned int m_CurLine;
    92 	bool m_FileLogged;
    93 };
    94 
    95 class Translator : 
    96 	public ITextListener_SMC,
    97 	public SMGlobalClass
    98 {
    99 public:
   100 	Translator();
   101 	~Translator();
   102 public: // SMGlobalClass
   103 	CoreConfigErr OnSourceModConfigChanged(const char *option, const char *value);
   104 	void OnSourceModAllInitialized();
   105 public: // ITextListener_SMC
   106 	void ReadSMC_ParseStart();
   107 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
   108 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
   109 	SMCParseResult ReadSMC_LeavingSection();
   110 public:
   111 	void RebuildLanguageDatabase(const char *lang_header_file);
   112 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
   113 	BaseStringTable *GetStringTable();
   114 	unsigned int GetLanguageCount();
   115 	bool GetLanguageByCode(const char *code, unsigned int *index);
   116 	size_t Translate(char *buffer, size_t maxlength, void **params, const Translation *pTrans);
   117 	CPhraseFile *GetFileByIndex(unsigned int index);
   118 	TransError CoreTrans(int client, 
   119 						 char *buffer, 
   120 						 size_t maxlength, 
   121 						 const char *phrase, 
   122 						 void **params, 
   123 						 size_t *outlen=NULL);
   124 	const char *GetServerLanguageCode() const;
   125 private:
   126 	bool AddLanguage(const char *langcode, const char *description);
   127 private:
   128 	CVector<Language *> m_Languages;
   129 	CVector<CPhraseFile *> m_Files;
   130 	BaseStringTable *m_pStringTab;
   131 	Trie *m_pLCodeLookup;
   132 	bool m_InLanguageSection;
   133 	String m_CustomError;
   134 	char m_ServerLangCode[3];
   135 };
   136 
   137 extern CPhraseFile *g_pCorePhrases;
   138 extern Translator g_Translator;
   139 
   140 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_