core/Translator.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 17 18:27:51 2008 +0000 (2008-08-17)
changeset 2230 e650c36ac67b
parent 2042 4a2faafca477
child 2289 44045ead61cd
permissions -rw-r--r--
Ew! No spaces in filenames.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SOURCEMOD_TRANSLATOR_H_
ds@1952
    33
#define _INCLUDE_SOURCEMOD_TRANSLATOR_H_
ds@1952
    34
ds@1952
    35
#include "sm_trie.h"
ds@1952
    36
#include <sh_string.h>
ds@1952
    37
#include <sh_vector.h>
ds@1952
    38
#include "sm_globals.h"
ds@1952
    39
#include "sm_memtable.h"
ds@1952
    40
#include "ITextParsers.h"
dvander@2042
    41
#include <ITranslator.h>
ds@1952
    42
ds@1952
    43
#define MAX_TRANSLATE_PARAMS		32
ds@1952
    44
#define CORELANG_ENGLISH			0
ds@1952
    45
ds@1952
    46
/* :TODO: write a templatized version of tries? */
ds@1952
    47
ds@1952
    48
using namespace SourceHook;
ds@1952
    49
class Translator;
ds@1952
    50
ds@1952
    51
enum PhraseParseState
ds@1952
    52
{
ds@1952
    53
	PPS_None = 0,
ds@1952
    54
	PPS_Phrases,
ds@1952
    55
	PPS_InPhrase,
ds@1952
    56
};
ds@1952
    57
ds@1952
    58
struct Language
ds@1952
    59
{
ds@1952
    60
	char m_code2[3];
ds@1952
    61
	int m_FullName;
ds@1952
    62
};
ds@1952
    63
dvander@2042
    64
class CPhraseFile : 
dvander@2042
    65
	public ITextListener_SMC,
dvander@2042
    66
	public IPhraseFile
ds@1952
    67
{
ds@1952
    68
public:
ds@1952
    69
	CPhraseFile(Translator *pTranslator, const char *file);
ds@1952
    70
	~CPhraseFile();
ds@1952
    71
public:
ds@1952
    72
	void ReparseFile();
ds@1952
    73
	const char *GetFilename();
ds@1952
    74
	TransError GetTranslation(const char *szPhrase, unsigned int lang_id, Translation *pTrans);
ds@1952
    75
public: //ITextListener_SMC
ds@1952
    76
	void ReadSMC_ParseStart();
ds@1952
    77
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1952
    78
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1952
    79
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1952
    80
	void ReadSMC_ParseEnd(bool halted, bool failed);
ds@1952
    81
private:
ds@1952
    82
	void ParseError(const char *message, ...);
ds@1952
    83
	void ParseWarning(const char *message, ...);
ds@1952
    84
private:
ds@1952
    85
	Trie *m_pPhraseLookup;
ds@1952
    86
	String m_File;
ds@1952
    87
	Translator *m_pTranslator;
ds@1952
    88
	PhraseParseState m_ParseState;
ds@1952
    89
	int m_CurPhrase;
ds@1952
    90
	BaseMemTable *m_pMemory;
ds@1952
    91
	BaseStringTable *m_pStringTab;
ds@1952
    92
	unsigned int m_LangCount;
ds@1952
    93
	String m_ParseError;
ds@1952
    94
	String m_LastPhraseString;
ds@1952
    95
	bool m_FileLogged;
ds@1952
    96
};
ds@1952
    97
ds@1952
    98
class Translator : 
ds@1952
    99
	public ITextListener_SMC,
dvander@2042
   100
	public SMGlobalClass,
dvander@2042
   101
	public ITranslator
ds@1952
   102
{
ds@1952
   103
public:
ds@1952
   104
	Translator();
ds@1952
   105
	~Translator();
ds@1952
   106
public: // SMGlobalClass
ds@1952
   107
	ConfigResult OnSourceModConfigChanged(const char *key, 
ds@1952
   108
		const char *value, 
ds@1952
   109
		ConfigSource source, 
ds@1952
   110
		char *error, 
ds@1952
   111
		size_t maxlength);
ds@1952
   112
	void OnSourceModAllInitialized();
ds@1952
   113
	void OnSourceModLevelChange(const char *mapName);
dvander@2042
   114
	void OnSourceModShutdown();
ds@1952
   115
public: // ITextListener_SMC
ds@1952
   116
	void ReadSMC_ParseStart();
ds@1952
   117
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1952
   118
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1952
   119
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1952
   120
public:
ds@1952
   121
	void RebuildLanguageDatabase(const char *lang_header_file);
ds@1952
   122
	unsigned int FindOrAddPhraseFile(const char *phrase_file);
ds@1952
   123
	BaseStringTable *GetStringTable();
ds@1952
   124
	unsigned int GetLanguageCount();
ds@1952
   125
	bool GetLanguageInfo(unsigned int number, const char **code, const char **name);
ds@1952
   126
	bool GetLanguageByCode(const char *code, unsigned int *index);
ds@1952
   127
	bool GetLanguageByName(const char *name, unsigned int *index);
ds@1952
   128
	CPhraseFile *GetFileByIndex(unsigned int index);
dvander@2042
   129
public: //ITranslator
dvander@2042
   130
	unsigned int GetServerLanguage();
dvander@2042
   131
	unsigned int GetClientLanguage(int client);
dvander@2042
   132
	const char *GetInterfaceName();
dvander@2042
   133
	unsigned int GetInterfaceVersion();
dvander@2042
   134
	IPhraseCollection *CreatePhraseCollection();
dvander@2042
   135
	int SetGlobalTarget(int index);
dvander@2042
   136
	int GetGlobalTarget() const;
dvander@2042
   137
	size_t FormatString(
ds@1952
   138
		char *buffer, 
ds@1952
   139
		size_t maxlength, 
dvander@2042
   140
		SourcePawn::IPluginContext *pContext,
dvander@2042
   141
		const cell_t *params,
dvander@2042
   142
		unsigned int param);
dvander@2042
   143
	bool FormatString(
dvander@2042
   144
		char *buffer,
dvander@2042
   145
		size_t maxlength,
dvander@2042
   146
		const char *format,
dvander@2042
   147
		IPhraseCollection *pPhrases,
dvander@2042
   148
		void **params,
dvander@2042
   149
		unsigned int numparams,
dvander@2042
   150
		size_t *pOutLength,
dvander@2042
   151
		const char **pFailPhrase);
ds@1952
   152
private:
ds@1952
   153
	bool AddLanguage(const char *langcode, const char *description);
ds@1952
   154
private:
ds@1952
   155
	CVector<Language *> m_Languages;
ds@1952
   156
	CVector<CPhraseFile *> m_Files;
ds@1952
   157
	BaseStringTable *m_pStringTab;
ds@1952
   158
	Trie *m_pLCodeLookup;
ds@1952
   159
	bool m_InLanguageSection;
ds@1952
   160
	String m_CustomError;
ds@1952
   161
	unsigned int m_ServerLang;
ds@1952
   162
	char m_InitialLang[3];
ds@1952
   163
};
ds@1952
   164
dvander@2042
   165
/* Nice little wrapper to handle error logging and whatnot */
dvander@2042
   166
bool CoreTranslate(char *buffer, 
dvander@2042
   167
				   size_t maxlength,
dvander@2042
   168
				   const char *format,
dvander@2042
   169
				   unsigned int numparams,
dvander@2042
   170
				   size_t *pOutLength,
dvander@2042
   171
				   ...);
dvander@2042
   172
dvander@2042
   173
extern IPhraseCollection *g_pCorePhrases;
ds@1952
   174
extern unsigned int g_pCorePhraseID;
ds@1952
   175
extern Translator g_Translator;
ds@1952
   176
ds@1952
   177
#endif //_INCLUDE_SOURCEMOD_TRANSLATOR_H_