amxmodx/amxmodx/CLang.h

196 lines
5.1 KiB
C
Raw Normal View History

/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
#ifndef _INCLUDE_CLANG_H
#define _INCLUDE_CLANG_H
#include "sh_tinyhash.h"
#define LANG_SERVER 0
#define LANG_PLAYER -1
2004-07-28 22:31:30 +04:00
2005-11-20 06:47:47 +03:00
#define LANG_STATUS_OK 0 // OK
#define LANG_STATUS_KLNOTFOUND 1 // Lang key not found in the lang.txt
#define LANG_STATUS_LNOTFOUND 3 // Invalid lang
struct md5Pair
{
String file;
String val;
};
struct sKeyDef
{
String *definition;
int key;
};
class defentry
{
public:
defentry() : definition(NULL)
{
};
defentry(const defentry &src)
{
definition = src.definition;
}
~defentry()
{
}
String *definition;
};
struct keytbl_val
{
keytbl_val() : index(-1)
{
};
int index;
};
class CLangMngr
{
class CLang
{
public:
2004-08-19 20:37:32 +04:00
// Construct an empty CLang object
CLang();
2004-08-19 20:37:32 +04:00
// Construct a CLang object initialized with a language name
CLang(const char *lang);
2004-08-19 20:37:32 +04:00
// Destructor
~CLang();
2004-08-19 20:37:32 +04:00
// Get the definition
const char *GetDef(int key, int &status);
2004-08-19 20:37:32 +04:00
// Add definitions to this language
void MergeDefinitions(CQueue <sKeyDef> & vec);
2004-08-19 20:37:32 +04:00
// Reset this language
void Clear();
2004-08-19 20:37:32 +04:00
// compare this language to a language name
friend bool operator == (const CLang &left, const char *right)
{
2005-09-17 03:48:51 +04:00
return strcmp(left.m_LanguageName, right) == 0 ? true : false;
}
2005-09-10 04:38:42 +04:00
2004-08-19 20:37:32 +04:00
// Get language name
const char *GetName() { return m_LanguageName; }
2004-08-19 20:37:32 +04:00
// Save to file
bool Save(FILE *fp, int &defOffset, uint32_t &curOffset);
bool SaveDefinitions(FILE *fp, uint32_t &curOffset);
2004-08-19 20:37:32 +04:00
// Load
bool Load(FILE *fp);
2004-08-19 20:37:32 +04:00
void SetMngr(CLangMngr *l) { m_LMan = l; }
// Get number of entries
int Entries() { return m_LookUpTable.size(); }
protected:
typedef THash<int, defentry> LookUpVec;
typedef LookUpVec::iterator LookUpVecIter;
char m_LanguageName[3];
2004-08-19 20:37:32 +04:00
// our lookup table
LookUpVec m_LookUpTable;
2004-08-19 20:37:32 +04:00
CLangMngr *m_LMan;
public:
void AddEntry(int key, const char *definition);
};
2004-08-19 20:37:32 +04:00
// Merge definitions into a language
void MergeDefinitions(const char *lang, CQueue <sKeyDef> &tmpVec);
2004-08-19 20:37:32 +04:00
// strip lowercase; make lower if needed
2005-09-10 04:38:42 +04:00
static size_t strip(char *str, char *newstr, bool makelower = false);
typedef CVector<CLang*> LangVec;
typedef CVector<CLang*>::iterator LangVecIter;
LangVec m_Languages;
2004-08-19 20:37:32 +04:00
CVector<md5Pair *> FileList;
CVector<String *> KeyList;
THash<String, keytbl_val> KeyTable;
2004-08-19 20:37:32 +04:00
// Get a lang object (construct if needed)
CLang * GetLang(const char *name);
CLang * GetLangR(const char *name);
2004-08-19 20:37:32 +04:00
// Current global client-id for functions like client_print with first parameter 0
2004-07-28 22:31:30 +04:00
int m_CurGlobId;
public:
2004-08-19 20:37:32 +04:00
// Merge a definitions file
int MergeDefinitionFile(const char *file);
2005-11-20 06:47:47 +03:00
// Get a definition from a lang name and a key
const char *GetDef(const char *langName, const char *key, int &status);
2004-08-19 20:37:32 +04:00
// Format a string
2004-07-28 22:31:30 +04:00
const char *Format(const char *src, ...);
2004-08-19 20:37:32 +04:00
// Format a string for an AMX plugin
2004-07-28 22:31:30 +04:00
char *FormatAmxString(AMX *amx, cell *params, int parm, int &len);
char *FormatString(const char *fmt, va_list &ap);
2004-08-19 20:37:32 +04:00
// Save
bool Save(const char *filename);
2004-08-19 20:37:32 +04:00
// Load
bool Load(const char *filename);
2004-08-19 20:37:32 +04:00
// Cache
bool LoadCache(const char *filename);
bool SaveCache(const char *filename);
void InvalidateCache();
2004-08-19 20:37:32 +04:00
// Get index
int GetKeyEntry(String &key);
int GetKeyEntry(const char *key);
int GetKeyIndex(const char *key);
2004-08-19 20:37:32 +04:00
// Get key from index
const char *GetKey(int key);
2004-08-19 20:37:32 +04:00
// Add key
int AddKeyEntry(String &key);
2004-07-28 22:31:30 +04:00
2004-08-19 20:37:32 +04:00
// Get the number of languages
2004-07-28 22:31:30 +04:00
int GetLangsNum();
2004-08-19 20:37:32 +04:00
// Get the name of a language
2004-07-28 22:31:30 +04:00
const char *GetLangName(int langId);
2004-08-19 20:37:32 +04:00
// Check if a language exists
2004-07-28 22:31:30 +04:00
bool LangExists(const char *langName);
2004-08-19 20:37:32 +04:00
// When a language id in a format string in FormatAmxString is LANG_PLAYER, the glob id decides which language to take.
2004-07-28 22:31:30 +04:00
void SetDefLang(int id);
2004-08-19 20:37:32 +04:00
inline int GetDefLang() const { return m_CurGlobId; }
2004-08-19 20:37:32 +04:00
// Reset
2004-07-28 22:31:30 +04:00
void Clear();
CLangMngr();
~CLangMngr();
};
#endif //_INCLUDE_CLANG_H