core/CTranslator.h
author David Anderson <dvander@alliedmods.net>
Thu Jan 25 22:36:38 2007 +0000 (2007-01-25)
changeset 370 d3a484f98f6d
parent 276 065d151c41b2
child 382 65a7ea52f5b0
permissions -rw-r--r--
added license headers
     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;
    47 	unsigned int lang_id;
    48 	unsigned int file_id;
    49 	void **params;
    50 };
    51 
    52 class CPhraseFile : public ITextListener_SMC
    53 {
    54 public:
    55 	CPhraseFile(CTranslator *pTranslator, const char *file);
    56 	~CPhraseFile();
    57 public:
    58 	void ReparseFile();
    59 	const char *GetFilename();
    60 	const char *GetTranslation(const Translation *pTrans, int **fmt_order, unsigned int *fmt_count);
    61 public: //ITextListener_SMC
    62 	void ReadSMC_ParseStart();
    63 	void ReadSMC_ParseEnd(bool halted, bool failed);
    64 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    65 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    66 	SMCParseResult ReadSMC_LeavingSection();
    67 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline);
    68 private:
    69 	void ParseError(const char *message, ...);
    70 	void ParseWarning(const char *message, ...);
    71 private:
    72 	Trie *m_pPhraseLookup;
    73 	String m_File;
    74 	CTranslator *m_pTranslator;
    75 	PhraseParseState m_ParseState;
    76 	int m_CurPhrase;
    77 	BaseMemTable *m_pMemory;
    78 	BaseStringTable *m_pStringTab;
    79 	unsigned int m_LangCount;
    80 	String m_ParseError;
    81 	String m_LastPhraseString;
    82 	unsigned int m_CurLine;
    83 	bool m_FileLogged;
    84 };
    85 
    86 class CTranslator : public ITextListener_SMC
    87 {
    88 public:
    89 	CTranslator();
    90 	~CTranslator();
    91 public: //ITextListener_SMC
    92 	void ReadSMC_ParseStart();
    93 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes);
    94 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes);
    95 	SMCParseResult ReadSMC_LeavingSection();
    96 public:
    97 	void RebuildLanguageDatabase(const char *lang_header_file);
    98 	unsigned int FindOrAddPhraseFile(const char *phrase_file);
    99 	BaseStringTable *GetStringTable();
   100 	unsigned int GetLanguageCount();
   101 	bool GetLanguageByCode(const char *code, unsigned int *index);
   102 	size_t Translate(char *buffer, size_t maxlength, const Translation *pTrans);
   103 private:
   104 	CVector<Language *> m_Languages;
   105 	CVector<CPhraseFile *> m_Files;
   106 	BaseStringTable *m_pStringTab;
   107 	Trie *m_pLCodeLookup;
   108 	bool m_InLanguageSection;
   109 	String m_CustomError;
   110 };
   111 
   112 extern CTranslator g_Translator;
   113 
   114 #endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_