Introduce TextParser API.
This commit is contained in:
parent
5fbc5c074d
commit
9af7931a79
|
@ -94,6 +94,8 @@ binary.sources = [
|
||||||
'CDataPack.cpp',
|
'CDataPack.cpp',
|
||||||
'datapacks.cpp',
|
'datapacks.cpp',
|
||||||
'stackstructs.cpp',
|
'stackstructs.cpp',
|
||||||
|
'CTextParsers.cpp',
|
||||||
|
'textparse.cpp',
|
||||||
]
|
]
|
||||||
|
|
||||||
if builder.target_platform == 'windows':
|
if builder.target_platform == 'windows':
|
||||||
|
|
1113
amxmodx/CTextParsers.cpp
Normal file
1113
amxmodx/CTextParsers.cpp
Normal file
File diff suppressed because it is too large
Load Diff
90
amxmodx/CTextParsers.h
Normal file
90
amxmodx/CTextParsers.h
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
/**
|
||||||
|
* vim: set ts=4 :
|
||||||
|
* =============================================================================
|
||||||
|
* SourceMod
|
||||||
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||||
|
* =============================================================================
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||||
|
* Free Software Foundation.
|
||||||
|
*
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||||
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||||
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||||
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||||
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||||
|
* this exception to all derivative works. AlliedModders LLC defines further
|
||||||
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||||
|
* or <http://www.sourcemod.net/license.php>.
|
||||||
|
*
|
||||||
|
* Version: $Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _INCLUDE_SOURCEMOD_TEXTPARSERS_H_
|
||||||
|
#define _INCLUDE_SOURCEMOD_TEXTPARSERS_H_
|
||||||
|
|
||||||
|
#include <ITextParsers.h>
|
||||||
|
#include <am-vector.h>
|
||||||
|
|
||||||
|
using namespace SourceMod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param void * IN: Stream pointer
|
||||||
|
* @param char * IN/OUT: Stream buffer
|
||||||
|
* @param size_t IN: Maximum size of buffer
|
||||||
|
* @param unsigned int * OUT: Number of bytes read (0 = end of stream)
|
||||||
|
* @return True on success, false on failure
|
||||||
|
*/
|
||||||
|
typedef bool(*STREAMREADER)(void *, char *, size_t, unsigned int *);
|
||||||
|
|
||||||
|
class TextParsers : public ITextParsers
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TextParsers();
|
||||||
|
public:
|
||||||
|
bool ParseFile_INI(const char *file,
|
||||||
|
ITextListener_INI *ini_listener,
|
||||||
|
unsigned int *line,
|
||||||
|
unsigned int *col);
|
||||||
|
|
||||||
|
SMCError ParseFile_SMC(const char *file,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states);
|
||||||
|
|
||||||
|
SMCError ParseSMCFile(const char *file,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states,
|
||||||
|
char *buffer,
|
||||||
|
size_t maxsize);
|
||||||
|
|
||||||
|
SMCError ParseSMCStream(const char *stream,
|
||||||
|
size_t length,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states,
|
||||||
|
char *buffer,
|
||||||
|
size_t maxsize);
|
||||||
|
|
||||||
|
unsigned int GetUTF8CharBytes(const char *stream);
|
||||||
|
|
||||||
|
const char *GetSMCErrorString(SMCError err);
|
||||||
|
bool IsWhitespace(const char *stream);
|
||||||
|
private:
|
||||||
|
SMCError ParseStream_SMC(void *stream,
|
||||||
|
STREAMREADER srdr,
|
||||||
|
ITextListener_SMC *smc,
|
||||||
|
SMCStates *states);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern TextParsers g_TextParser;
|
||||||
|
|
||||||
|
#endif //_INCLUDE_SOURCEMOD_TEXTPARSERS_H_
|
||||||
|
|
|
@ -64,6 +64,7 @@ extern AMX_NATIVE_INFO vector_Natives[];
|
||||||
extern AMX_NATIVE_INFO g_SortNatives[];
|
extern AMX_NATIVE_INFO g_SortNatives[];
|
||||||
extern AMX_NATIVE_INFO g_DataStructNatives[];
|
extern AMX_NATIVE_INFO g_DataStructNatives[];
|
||||||
extern AMX_NATIVE_INFO g_StackNatives[];
|
extern AMX_NATIVE_INFO g_StackNatives[];
|
||||||
|
extern AMX_NATIVE_INFO g_TextParserNatives[];
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#define DLLOAD(path) (DLHANDLE)LoadLibrary(path)
|
#define DLLOAD(path) (DLHANDLE)LoadLibrary(path)
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include <amxmodx_version.h>
|
#include <amxmodx_version.h>
|
||||||
#include "trie_natives.h"
|
#include "trie_natives.h"
|
||||||
#include "CDataPack.h"
|
#include "CDataPack.h"
|
||||||
|
#include "textparse.h"
|
||||||
|
|
||||||
plugin_info_t Plugin_info =
|
plugin_info_t Plugin_info =
|
||||||
{
|
{
|
||||||
|
@ -395,6 +396,7 @@ int C_Spawn(edict_t *pent)
|
||||||
g_TrieHandles.clear();
|
g_TrieHandles.clear();
|
||||||
g_TrieSnapshotHandles.clear();
|
g_TrieSnapshotHandles.clear();
|
||||||
g_DataPackHandles.clear();
|
g_DataPackHandles.clear();
|
||||||
|
g_TextParsersHandles.clear();
|
||||||
|
|
||||||
char map_pluginsfile_path[256];
|
char map_pluginsfile_path[256];
|
||||||
char prefixed_map_pluginsfile[256];
|
char prefixed_map_pluginsfile[256];
|
||||||
|
|
|
@ -552,6 +552,7 @@ int set_amxnatives(AMX* amx, char error[128])
|
||||||
amx_Register(amx, trie_Natives, -1);
|
amx_Register(amx, trie_Natives, -1);
|
||||||
amx_Register(amx, g_DatapackNatives, -1);
|
amx_Register(amx, g_DatapackNatives, -1);
|
||||||
amx_Register(amx, g_StackNatives, -1);
|
amx_Register(amx, g_StackNatives, -1);
|
||||||
|
amx_Register(amx, g_TextParserNatives, -1);
|
||||||
|
|
||||||
//we're not actually gonna check these here anymore
|
//we're not actually gonna check these here anymore
|
||||||
amx->flags |= AMX_FLAG_PRENIT;
|
amx->flags |= AMX_FLAG_PRENIT;
|
||||||
|
|
|
@ -306,6 +306,7 @@
|
||||||
<ClCompile Include="..\CModule.cpp" />
|
<ClCompile Include="..\CModule.cpp" />
|
||||||
<ClCompile Include="..\CPlugin.cpp" />
|
<ClCompile Include="..\CPlugin.cpp" />
|
||||||
<ClCompile Include="..\CTask.cpp" />
|
<ClCompile Include="..\CTask.cpp" />
|
||||||
|
<ClCompile Include="..\CTextParsers.cpp" />
|
||||||
<ClCompile Include="..\CVault.cpp" />
|
<ClCompile Include="..\CVault.cpp" />
|
||||||
<ClCompile Include="..\datapacks.cpp" />
|
<ClCompile Include="..\datapacks.cpp" />
|
||||||
<ClCompile Include="..\debugger.cpp" />
|
<ClCompile Include="..\debugger.cpp" />
|
||||||
|
@ -335,6 +336,7 @@
|
||||||
<AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='JITRelease|Win32'">All</AssemblerOutput>
|
<AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='JITRelease|Win32'">All</AssemblerOutput>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\strptime.cpp" />
|
<ClCompile Include="..\strptime.cpp" />
|
||||||
|
<ClCompile Include="..\textparse.cpp" />
|
||||||
<ClCompile Include="..\trie_natives.cpp" />
|
<ClCompile Include="..\trie_natives.cpp" />
|
||||||
<ClCompile Include="..\util.cpp" />
|
<ClCompile Include="..\util.cpp" />
|
||||||
<ClCompile Include="..\vault.cpp" />
|
<ClCompile Include="..\vault.cpp" />
|
||||||
|
@ -370,6 +372,7 @@
|
||||||
<ClInclude Include="..\CQueue.h" />
|
<ClInclude Include="..\CQueue.h" />
|
||||||
<ClInclude Include="..\CString.h" />
|
<ClInclude Include="..\CString.h" />
|
||||||
<ClInclude Include="..\CTask.h" />
|
<ClInclude Include="..\CTask.h" />
|
||||||
|
<ClInclude Include="..\CTextParsers.h" />
|
||||||
<ClInclude Include="..\CVault.h" />
|
<ClInclude Include="..\CVault.h" />
|
||||||
<ClInclude Include="..\CVector.h" />
|
<ClInclude Include="..\CVector.h" />
|
||||||
<ClInclude Include="..\datastructs.h" />
|
<ClInclude Include="..\datastructs.h" />
|
||||||
|
|
|
@ -177,6 +177,12 @@
|
||||||
<ClCompile Include="..\stackstructs.cpp">
|
<ClCompile Include="..\stackstructs.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\CTextParsers.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\textparse.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\amx.h">
|
<ClInclude Include="..\amx.h">
|
||||||
|
@ -317,6 +323,18 @@
|
||||||
<ClInclude Include="..\CDataPack.h">
|
<ClInclude Include="..\CDataPack.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<<<<<<< HEAD
|
||||||
|
=======
|
||||||
|
<ClInclude Include="..\sm_stringhashmap.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\sm_memtable.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\CTextParsers.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
>>>>>>> Introduce TextParser API.
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ResourceCompile Include="..\version.rc">
|
<ResourceCompile Include="..\version.rc">
|
||||||
|
|
226
amxmodx/textparse.cpp
Normal file
226
amxmodx/textparse.cpp
Normal file
|
@ -0,0 +1,226 @@
|
||||||
|
/**
|
||||||
|
* vim: set ts=4 :
|
||||||
|
* =============================================================================
|
||||||
|
* SourceMod
|
||||||
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||||
|
* =============================================================================
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||||
|
* Free Software Foundation.
|
||||||
|
*
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||||
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||||
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||||
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||||
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||||
|
* this exception to all derivative works. AlliedModders LLC defines further
|
||||||
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||||
|
* or <http://www.sourcemod.net/license.php>.
|
||||||
|
*
|
||||||
|
* Version: $Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "amxmodx.h"
|
||||||
|
#include <textparse.h>
|
||||||
|
#include <am-vector.h>
|
||||||
|
|
||||||
|
TextParserHandles<ParseInfo> g_TextParsersHandles;
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_CreateParser(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
return static_cast<cell>(g_TextParsersHandles.create());
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_SetParseStart(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
const char* functionName = get_amxstring(amx, params[2], 0, length);
|
||||||
|
int function = registerSPForwardByName(amx, functionName, FP_CELL, FP_DONE);
|
||||||
|
|
||||||
|
if (function == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", functionName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
p->parse_start = function;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_SetParseEnd(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
const char* funcName = get_amxstring(amx, params[2], 0, length);
|
||||||
|
int func = registerSPForwardByName(amx, funcName, FP_CELL, FP_DONE);
|
||||||
|
|
||||||
|
if (func == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
p->parse_end = func;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_SetReaders(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
const char* NewSectionFuncName = get_amxstring(amx, params[2], 0, length);
|
||||||
|
const char* KeyValueFuncName = get_amxstring(amx, params[3], 1, length);
|
||||||
|
const char* EndSectionFuncName = get_amxstring(amx, params[4], 2, length);
|
||||||
|
|
||||||
|
int NewSectionFunc = registerSPForwardByName(amx, NewSectionFuncName, FP_CELL, FP_STRING, FP_DONE);
|
||||||
|
if (NewSectionFunc == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", NewSectionFuncName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int KeyValueFunc = registerSPForwardByName(amx, KeyValueFuncName, FP_CELL, FP_STRING, FP_STRING, FP_DONE);
|
||||||
|
if (KeyValueFunc == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", KeyValueFuncName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int EndSectionFunc = registerSPForwardByName(amx, EndSectionFuncName, FP_CELL, FP_DONE);
|
||||||
|
if (EndSectionFunc == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", EndSectionFuncName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
p->new_section = NewSectionFunc;
|
||||||
|
p->key_value = KeyValueFunc;
|
||||||
|
p->end_section = EndSectionFunc;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_SetRawLine(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
const char* funcName = get_amxstring(amx, params[2], 0, length);
|
||||||
|
|
||||||
|
int func = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_CELL, FP_DONE);
|
||||||
|
if (func == -1)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
p->raw_line = func;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_ParseFile(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
const char *file = build_pathname("%s", get_amxstring(amx, params[2], 0, length));
|
||||||
|
|
||||||
|
SMCStates states;
|
||||||
|
SMCError p_err = textparsers->ParseFile_SMC(file, p, &states);
|
||||||
|
|
||||||
|
*get_amxaddr(amx, params[3]) = states.line;
|
||||||
|
*get_amxaddr(amx, params[4]) = states.col;
|
||||||
|
|
||||||
|
return (cell)p_err;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_GetErrorString(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
const char *str = textparsers->GetSMCErrorString((SMCError)params[1]);
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return set_amxstring(amx, params[2], str, params[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static cell AMX_NATIVE_CALL SMC_DestroyParser(AMX *amx, cell *params)
|
||||||
|
{
|
||||||
|
cell *ptr = get_amxaddr(amx, params[1]);
|
||||||
|
|
||||||
|
ParseInfo *p = g_TextParsersHandles.lookup(*ptr);
|
||||||
|
|
||||||
|
if (p == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_TextParsersHandles.destroy(*ptr))
|
||||||
|
{
|
||||||
|
*ptr = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
AMX_NATIVE_INFO g_TextParserNatives[] =
|
||||||
|
{
|
||||||
|
{ "SMC_CreateParser" , SMC_CreateParser },
|
||||||
|
{ "SMC_ParseFile" , SMC_ParseFile },
|
||||||
|
{ "SMC_GetErrorString", SMC_GetErrorString },
|
||||||
|
{ "SMC_SetParseStart" , SMC_SetParseStart },
|
||||||
|
{ "SMC_SetParseEnd" , SMC_SetParseEnd },
|
||||||
|
{ "SMC_SetReaders" , SMC_SetReaders },
|
||||||
|
{ "SMC_SetRawLine" , SMC_SetRawLine },
|
||||||
|
{ "SMC_DestroyParser" , SMC_DestroyParser },
|
||||||
|
{ NULL, NULL },
|
||||||
|
};
|
191
amxmodx/textparse.h
Normal file
191
amxmodx/textparse.h
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
/**
|
||||||
|
* vim: set ts=4 :
|
||||||
|
* =============================================================================
|
||||||
|
* SourceMod
|
||||||
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||||
|
* =============================================================================
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||||
|
* Free Software Foundation.
|
||||||
|
*
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||||
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||||
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||||
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||||
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||||
|
* this exception to all derivative works. AlliedModders LLC defines further
|
||||||
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||||
|
* or <http://www.sourcemod.net/license.php>.
|
||||||
|
*
|
||||||
|
* Version: $Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _INCLUDE_TEXTPARSE_H_
|
||||||
|
#define _INCLUDE_TEXTPARSE_H_
|
||||||
|
|
||||||
|
#include "amxmodx.h"
|
||||||
|
#include "CTextParsers.h"
|
||||||
|
|
||||||
|
class ParseInfo : public ITextListener_SMC
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ParseInfo()
|
||||||
|
{
|
||||||
|
parse_start = -1;
|
||||||
|
parse_end = -1;
|
||||||
|
new_section = -1;
|
||||||
|
key_value = -1;
|
||||||
|
end_section = -1;
|
||||||
|
raw_line = -1;
|
||||||
|
handle = -1;
|
||||||
|
}
|
||||||
|
~ParseInfo() {}
|
||||||
|
public:
|
||||||
|
void ReadSMC_ParseStart()
|
||||||
|
{
|
||||||
|
if (parse_start != -1)
|
||||||
|
{
|
||||||
|
executeForwards(parse_start, handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadSMC_ParseEnd(bool halted, bool failed)
|
||||||
|
{
|
||||||
|
if (parse_end != -1)
|
||||||
|
{
|
||||||
|
executeForwards(parse_end, handle, halted ? 1 : 0, failed ? 1 : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
|
||||||
|
{
|
||||||
|
if (new_section != -1)
|
||||||
|
{
|
||||||
|
return (SMCResult)executeForwards(new_section, handle, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
|
||||||
|
{
|
||||||
|
if (key_value != -1)
|
||||||
|
{
|
||||||
|
return (SMCResult)executeForwards(key_value, handle, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SMCResult ReadSMC_LeavingSection(const SMCStates *states)
|
||||||
|
{
|
||||||
|
if (end_section != -1)
|
||||||
|
{
|
||||||
|
return (SMCResult)executeForwards(end_section, handle, handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line)
|
||||||
|
{
|
||||||
|
if (raw_line != -1)
|
||||||
|
{
|
||||||
|
return (SMCResult)executeForwards(raw_line, handle, line, states->line);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
int parse_start;
|
||||||
|
int parse_end;
|
||||||
|
int new_section;
|
||||||
|
int key_value;
|
||||||
|
int end_section;
|
||||||
|
int raw_line;
|
||||||
|
int handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class TextParserHandles
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ke::Vector<T *> m_textparsers;
|
||||||
|
|
||||||
|
public:
|
||||||
|
TextParserHandles() { }
|
||||||
|
~TextParserHandles()
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < m_textparsers.length(); i++)
|
||||||
|
{
|
||||||
|
if (m_textparsers[i] != NULL)
|
||||||
|
{
|
||||||
|
delete m_textparsers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_textparsers.clear();
|
||||||
|
}
|
||||||
|
T *lookup(int handle)
|
||||||
|
{
|
||||||
|
handle--;
|
||||||
|
|
||||||
|
if (handle < 0 || handle >= static_cast<int>(m_textparsers.length()))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_textparsers[handle];
|
||||||
|
}
|
||||||
|
int create()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < m_textparsers.length(); i++)
|
||||||
|
{
|
||||||
|
if (m_textparsers[i] == NULL)
|
||||||
|
{
|
||||||
|
// reuse handle
|
||||||
|
m_textparsers[i] = new T;
|
||||||
|
|
||||||
|
return static_cast<int>(i)+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_textparsers.append(new T);
|
||||||
|
return m_textparsers.length();
|
||||||
|
}
|
||||||
|
bool destroy(int handle)
|
||||||
|
{
|
||||||
|
handle--;
|
||||||
|
|
||||||
|
if (handle < 0 || handle >= static_cast<int>(m_textparsers.length()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_textparsers[handle] == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
delete m_textparsers[handle];
|
||||||
|
m_textparsers[handle] = NULL;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern TextParserHandles<ParseInfo> g_TextParsersHandles;
|
||||||
|
|
||||||
|
#endif // _INCLUDE_TEXTPARSE_H_
|
223
plugins/include/textparse.inc
Normal file
223
plugins/include/textparse.inc
Normal file
|
@ -0,0 +1,223 @@
|
||||||
|
/**
|
||||||
|
* vim: set ts=4 :
|
||||||
|
* =============================================================================
|
||||||
|
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
|
||||||
|
* =============================================================================
|
||||||
|
*
|
||||||
|
* This file is part of the SourceMod/SourcePawn SDK.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||||
|
* Free Software Foundation.
|
||||||
|
*
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||||
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||||
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||||
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||||
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||||
|
* this exception to all derivative works. AlliedModders LLC defines further
|
||||||
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||||
|
* or <http://www.sourcemod.net/license.php>.
|
||||||
|
*
|
||||||
|
* Version: $Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined _textparse_included
|
||||||
|
#endinput
|
||||||
|
#endif
|
||||||
|
#define _textparse_included
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Everything below describes the SMC Parse, or "SourceMod Configuration" format.
|
||||||
|
* This parser is entirely event based. You must hook events to receive data.
|
||||||
|
* The file format itself is nearly identical to Valve's KeyValues format.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse result directive.
|
||||||
|
*/
|
||||||
|
enum SMCResult
|
||||||
|
{
|
||||||
|
SMCParse_Continue, /**< Continue parsing */
|
||||||
|
SMCParse_Halt, /**< Stop parsing here */
|
||||||
|
SMCParse_HaltFail /**< Stop parsing and return failure */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse error codes.
|
||||||
|
*/
|
||||||
|
enum SMCError
|
||||||
|
{
|
||||||
|
SMCError_Okay = 0, /**< No error */
|
||||||
|
SMCError_StreamOpen, /**< Stream failed to open */
|
||||||
|
SMCError_StreamError, /**< The stream died... somehow */
|
||||||
|
SMCError_Custom, /**< A custom handler threw an error */
|
||||||
|
SMCError_InvalidSection1, /**< A section was declared without quotes, and had extra tokens */
|
||||||
|
SMCError_InvalidSection2, /**< A section was declared without any header */
|
||||||
|
SMCError_InvalidSection3, /**< A section ending was declared with too many unknown tokens */
|
||||||
|
SMCError_InvalidSection4, /**< A section ending has no matching beginning */
|
||||||
|
SMCError_InvalidSection5, /**< A section beginning has no matching ending */
|
||||||
|
SMCError_InvalidTokens, /**< There were too many unidentifiable strings on one line */
|
||||||
|
SMCError_TokenOverflow, /**< The token buffer overflowed */
|
||||||
|
SMCError_InvalidProperty1, /**< A property was declared outside of any section */
|
||||||
|
};
|
||||||
|
|
||||||
|
enum TextParser
|
||||||
|
{
|
||||||
|
Invalid_TextPaser = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new SMC file format parser.
|
||||||
|
* This is used to set parse hooks.
|
||||||
|
*
|
||||||
|
* @return A new Handle to an SMC Parse structure.
|
||||||
|
*/
|
||||||
|
native TextParser:SMC_CreateParser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses an SMC file.
|
||||||
|
*
|
||||||
|
* @param smc A Handle to an SMC Parse structure.
|
||||||
|
* @param file A string containing the file path.
|
||||||
|
* @param line An optional by reference cell to store the last line number read.
|
||||||
|
* @param col An optional by reference cell to store the last column number read.
|
||||||
|
|
||||||
|
* @return An SMCParseError result.
|
||||||
|
* @error Invalid or corrupt Handle.
|
||||||
|
*/
|
||||||
|
native SMCError:SMC_ParseFile(TextParser:smc, const file[], &line = 0, &col = 0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an error string for an SMCError code.
|
||||||
|
*
|
||||||
|
* @note SMCError_Okay returns false.
|
||||||
|
* @note SMCError_Custom (which is thrown on SMCParse_HaltFail) returns false.
|
||||||
|
*
|
||||||
|
* @param error The SMCParseError code.
|
||||||
|
* @param buffer A string buffer for the error (contents undefined on failure).
|
||||||
|
* @param buf_max The maximum size of the buffer.
|
||||||
|
*
|
||||||
|
* @return True on success, false otherwise.
|
||||||
|
*/
|
||||||
|
native bool:SMC_GetErrorString(SMCError:error, buffer[], buf_max);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the SMC_ParseStart function of a parse Handle.
|
||||||
|
* Below is the prototype of callback.:
|
||||||
|
*
|
||||||
|
* - ParseStart:
|
||||||
|
* Called when parsing is started.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
* @noreturn
|
||||||
|
*
|
||||||
|
* public SMC_ParseStart(TextParser:smc)
|
||||||
|
*
|
||||||
|
* @param smc Handle to an SMC Parse.
|
||||||
|
* @param func A ParseStart callback.
|
||||||
|
*
|
||||||
|
* @noreturn
|
||||||
|
* @error Invalid or corrupt Handle.
|
||||||
|
*/
|
||||||
|
native SMC_SetParseStart(TextParser:smc, const func[]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the SMC_ParseEnd of a parse handle.
|
||||||
|
* Below is the prototype of callback.:
|
||||||
|
*
|
||||||
|
* - ParseEnd:
|
||||||
|
* Called when parsing is halted.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
* @param halted True if abnormally halted, false otherwise.
|
||||||
|
* @param failed True if parsing failed, false otherwise.
|
||||||
|
* @noreturn
|
||||||
|
*
|
||||||
|
* public SMC_ParseEnd(TextParser:smc, bool:halted, bool:failed)
|
||||||
|
*
|
||||||
|
* @param smc Handle to an SMC Parse.
|
||||||
|
* @param func A ParseEnd callback..
|
||||||
|
*
|
||||||
|
* @noreturn
|
||||||
|
* @error Invalid or corrupt Handle.
|
||||||
|
*/
|
||||||
|
native SMC_SetParseEnd(TextParser:smc, const func[]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the three main reader functions.
|
||||||
|
* Below are the different prototypes of callbacks:
|
||||||
|
*
|
||||||
|
* - NewSection:
|
||||||
|
* Called when the parser finds the end of the current section.
|
||||||
|
* @note Enclosing quotes are always stripped.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
* @param name String containing section name.
|
||||||
|
* @return An SMCResult action to take.
|
||||||
|
*
|
||||||
|
* public SMCResult:SMC_NewSection(TextParser:smc, const name[])
|
||||||
|
*
|
||||||
|
* - KeyValue:
|
||||||
|
* Called when the parser finds a new key/value pair.
|
||||||
|
* @note Enclosing quotes are always stripped.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
* @param key String containing key name.
|
||||||
|
* @param value String containing value name.
|
||||||
|
* @return An SMCResult action to take.
|
||||||
|
*
|
||||||
|
* public SMCResult:SMC_KeyValue(TextParser:smc, const key[], const value[])
|
||||||
|
*
|
||||||
|
* - EndSection:
|
||||||
|
* Called when the parser finds the end of the current section.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
*
|
||||||
|
* public SMCResult:SMC_EndSection(TextParser:smc)
|
||||||
|
* -
|
||||||
|
* @param smc An SMC parse Handle.
|
||||||
|
* @param ns A NewSection callback.
|
||||||
|
* @param kv A KeyValue callback.
|
||||||
|
* @param es An EndSection callback.
|
||||||
|
*
|
||||||
|
* @noreturn
|
||||||
|
*/
|
||||||
|
native SMC_SetReaders(TextParser:smc, const nsFunc[], const kvFunc[], const esFunc[]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a raw line reader on an SMC parser Handle.
|
||||||
|
* Below is the prototype of callback.:
|
||||||
|
*
|
||||||
|
* - RawLine:
|
||||||
|
* Called whenever a raw line is read.
|
||||||
|
*
|
||||||
|
* @param smc The SMC Parse Handle.
|
||||||
|
* @param line A string containing the raw line from the file.
|
||||||
|
* @param lineno The line number it occurs on.
|
||||||
|
* @return An SMCResult action to take.
|
||||||
|
*
|
||||||
|
* public SMCResult:SMC_RawLine(TextParser:smc, const line[], lineno)
|
||||||
|
*
|
||||||
|
* @param smc Handle to an SMC Parse.
|
||||||
|
* @param func A RawLine callback..
|
||||||
|
* @noreturn
|
||||||
|
*/
|
||||||
|
native SMC_SetRawLine(TextParser:smc, const func[]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of a text parser.
|
||||||
|
*
|
||||||
|
* @param smc Handle to an SMC Parse.
|
||||||
|
* @return True if disposed, false otherwise.
|
||||||
|
*/
|
||||||
|
native SMC_DestroyParser(&TextParser:smc);
|
116
plugins/testsuite/textparse_test.sma
Normal file
116
plugins/testsuite/textparse_test.sma
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
#include <amxmodx>
|
||||||
|
#include <textparse>
|
||||||
|
|
||||||
|
new SuccessCount;
|
||||||
|
new Trie:ExpectedKVData;
|
||||||
|
|
||||||
|
public plugin_init()
|
||||||
|
{
|
||||||
|
register_concmd("textparse_vdf", "ConsoleCommand_TextParseVDF");
|
||||||
|
register_clcmd("textparse_ini", "ServerCommand_TextParseINI");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VDF Config format
|
||||||
|
*/
|
||||||
|
|
||||||
|
public ConsoleCommand_TextParseVDF()
|
||||||
|
{
|
||||||
|
server_print("Testing text parser with VDF config file format...");
|
||||||
|
|
||||||
|
new const configFile[] = "addons/amxmodx/scripting/testsuite/textparse_test.cfg";
|
||||||
|
|
||||||
|
ExpectedKVData = TrieCreate();
|
||||||
|
TrieSetString(ExpectedKVData, "Logging", "on");
|
||||||
|
TrieSetString(ExpectedKVData, "LogMode", "daily");
|
||||||
|
TrieSetString(ExpectedKVData, "ServerLang", "en");
|
||||||
|
TrieSetString(ExpectedKVData, "PublicChatTrigger", "!");
|
||||||
|
TrieSetString(ExpectedKVData, "SilentChatTrigger", "/");
|
||||||
|
TrieSetString(ExpectedKVData, "SilentFailSuppress", "no");
|
||||||
|
TrieSetString(ExpectedKVData, "PassInfoVar", "_password");
|
||||||
|
TrieSetString(ExpectedKVData, "MenuItemSound", "buttons/button14.wav");
|
||||||
|
TrieSetString(ExpectedKVData, "MenuExitSound", "buttons/combine_button7.wav");
|
||||||
|
TrieSetString(ExpectedKVData, "MenuExitBackSound", "buttons/combine_button7.wav");
|
||||||
|
TrieSetString(ExpectedKVData, "AllowClLanguageVar", "On");
|
||||||
|
TrieSetString(ExpectedKVData, "DisableAutoUpdate", "no");
|
||||||
|
TrieSetString(ExpectedKVData, "ForceRestartAfterUpdate", "no");
|
||||||
|
TrieSetString(ExpectedKVData, "AutoUpdateURL", "http://update.sourcemod.net/update/");
|
||||||
|
TrieSetString(ExpectedKVData, "DebugSpew", "no");
|
||||||
|
TrieSetString(ExpectedKVData, "SteamAuthstringValidation", "yes");
|
||||||
|
TrieSetString(ExpectedKVData, "BlockBadPlugins", "yes");
|
||||||
|
TrieSetString(ExpectedKVData, "SlowScriptTimeout", "8");
|
||||||
|
|
||||||
|
new const expectedSectionCount = 2; // Include start and end.
|
||||||
|
new const expectedStartEndCount = 2;
|
||||||
|
new const expectedKeyValueCount = TrieGetSize(ExpectedKVData);
|
||||||
|
new const expectedLineCount = file_size(configFile, .flag = 1) - 1;
|
||||||
|
|
||||||
|
new TextParser:parser = SMC_CreateParser();
|
||||||
|
|
||||||
|
SMC_SetReaders(parser, "ReadCore_NewSection", "ReadCore_KeyValue", "ReadCore_EndSection");
|
||||||
|
SMC_SetParseStart(parser, "ReadCore_ParseStart");
|
||||||
|
SMC_SetRawLine(parser, "ReadCore_CurrentLine");
|
||||||
|
SMC_SetParseEnd(parser, "ReadCore_ParseEnd");
|
||||||
|
|
||||||
|
new line, col;
|
||||||
|
new SMCError:err = SMC_ParseFile_VDF(parser, configFile, line, col);
|
||||||
|
|
||||||
|
if (err != SMCError_Okay)
|
||||||
|
{
|
||||||
|
new buffer[64];
|
||||||
|
server_print("%s", SMC_GetErrorString(err, buffer, charsmax(buffer)) ? buffer : "Fatal parse error");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line == expectedLineCount + 1 && col == 2)
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
SMC_DestroyParser(parser);
|
||||||
|
|
||||||
|
server_print("^tTests successful: %d/%d", SuccessCount, expectedStartEndCount + expectedSectionCount + expectedKeyValueCount + expectedLineCount + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_ParseStart(TextParser:smc)
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_NewSection(TextParser:smc, const name[])
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_KeyValue(TextParser:smc, const key[], const value[])
|
||||||
|
{
|
||||||
|
new buffer[128];
|
||||||
|
if (TrieGetString(ExpectedKVData, key, buffer, charsmax(buffer)) && equal(value, buffer))
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_EndSection(TextParser:smc)
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_CurrentLine(TextParser:smc, const line[], lineno)
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadCore_ParseEnd(TextParser:smc)
|
||||||
|
{
|
||||||
|
++SuccessCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* INI Config format
|
||||||
|
*/
|
||||||
|
public ServerCommand_TextParseINI()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
450
public/ITextParsers.h
Normal file
450
public/ITextParsers.h
Normal file
|
@ -0,0 +1,450 @@
|
||||||
|
/**
|
||||||
|
* vim: set ts=4 :
|
||||||
|
* =============================================================================
|
||||||
|
* SourceMod
|
||||||
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||||
|
* =============================================================================
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||||
|
* Free Software Foundation.
|
||||||
|
*
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||||
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||||
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||||
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||||
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||||
|
* this exception to all derivative works. AlliedModders LLC defines further
|
||||||
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||||
|
* or <http://www.sourcemod.net/license.php>.
|
||||||
|
*
|
||||||
|
* Version: $Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
|
||||||
|
#define _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file ITextParsers.h
|
||||||
|
* @brief Defines various text/file parsing functions, as well as UTF-8 support code.
|
||||||
|
*/
|
||||||
|
namespace SourceMod
|
||||||
|
{
|
||||||
|
|
||||||
|
#define SMINTERFACE_TEXTPARSERS_NAME "ITextParsers"
|
||||||
|
#define SMINTERFACE_TEXTPARSERS_VERSION 4
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The INI file format is defined as:
|
||||||
|
* WHITESPACE: 0x20, \n, \t, \r
|
||||||
|
* IDENTIFIER: A-Z a-z 0-9 _ - , + . $ ? /
|
||||||
|
* STRING: Any set of symbols
|
||||||
|
*
|
||||||
|
* Basic syntax is comprised of SECTIONs.
|
||||||
|
* A SECTION is defined as:
|
||||||
|
* [SECTIONNAME]
|
||||||
|
* OPTION
|
||||||
|
* OPTION
|
||||||
|
* OPTION...
|
||||||
|
*
|
||||||
|
* SECTIONNAME is an IDENTIFIER.
|
||||||
|
* OPTION can be repeated any number of times, once per line.
|
||||||
|
* OPTION is defined as one of:
|
||||||
|
* KEY = "VALUE"
|
||||||
|
* KEY = VALUE
|
||||||
|
* KEY
|
||||||
|
* Where KEY is an IDENTIFIER and VALUE is a STRING.
|
||||||
|
*
|
||||||
|
* WHITESPACE should always be omitted.
|
||||||
|
* COMMENTS should be stripped, and are defined as text occurring in:
|
||||||
|
* ;<TEXT>
|
||||||
|
*
|
||||||
|
* Example file below. Note that
|
||||||
|
* The second line is technically invalid. The event handler
|
||||||
|
* must decide whether this should be allowed.
|
||||||
|
* --FILE BELOW--
|
||||||
|
* [gaben]
|
||||||
|
* hi = clams
|
||||||
|
* bye = "NO CLAMS"
|
||||||
|
*
|
||||||
|
* [valve]
|
||||||
|
* cannot
|
||||||
|
* maintain
|
||||||
|
* products
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Contains parse events for INI files.
|
||||||
|
*/
|
||||||
|
class ITextListener_INI
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns version number.
|
||||||
|
*/
|
||||||
|
virtual unsigned int GetTextParserVersion1()
|
||||||
|
{
|
||||||
|
return SMINTERFACE_TEXTPARSERS_VERSION;
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Called when a new section is encountered in an INI file.
|
||||||
|
*
|
||||||
|
* @param section Name of section in between the [ and ] characters.
|
||||||
|
* @param invalid_tokens True if invalid tokens were detected in the name.
|
||||||
|
* @param close_bracket True if a closing bracket was detected, false otherwise.
|
||||||
|
* @param extra_tokens True if extra tokens were detected on the line.
|
||||||
|
* @param curtok Contains current token in the line where the section name starts.
|
||||||
|
* You can add to this offset when failing to point to a token.
|
||||||
|
* @return True to keep parsing, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool ReadINI_NewSection(const char *section,
|
||||||
|
bool invalid_tokens,
|
||||||
|
bool close_bracket,
|
||||||
|
bool extra_tokens,
|
||||||
|
unsigned int *curtok)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called when encountering a key/value pair in an INI file.
|
||||||
|
*
|
||||||
|
* @param key Name of key.
|
||||||
|
* @param value String containing value (with quotes stripped, if any).
|
||||||
|
* @param invalid_tokens Whether or not the key contained invalid tokens.
|
||||||
|
* @param equal_token There was an '=' sign present (in case the value is missing).
|
||||||
|
* @param quotes Whether value was enclosed in quotes.
|
||||||
|
* @param curtok Contains the token index of the start of the value string.
|
||||||
|
* This can be changed when returning false.
|
||||||
|
* @return True to keep parsing, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool ReadINI_KeyValue(const char *key,
|
||||||
|
const char *value,
|
||||||
|
bool invalid_tokens,
|
||||||
|
bool equal_token,
|
||||||
|
bool quotes,
|
||||||
|
unsigned int *curtok)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called after a line has been preprocessed, if it has text.
|
||||||
|
*
|
||||||
|
* @param line Contents of line.
|
||||||
|
* @param curtok Pointer to optionally store failed position in string.
|
||||||
|
* @return True to keep parsing, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool ReadINI_RawLine(const char *line, unsigned int *curtok)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* :TODO: write this in CFG (context free grammar) format so it makes sense
|
||||||
|
*
|
||||||
|
* The SMC file format is defined as:
|
||||||
|
* WHITESPACE: 0x20, \n, \t, \r
|
||||||
|
* IDENTIFIER: Any ASCII character EXCLUDING ", {, }, ;, //, / *, or WHITESPACE.
|
||||||
|
* STRING: Any set of symbols enclosed in quotes.
|
||||||
|
* Note: if a STRING does not have quotes, it is parsed as an IDENTIFIER.
|
||||||
|
*
|
||||||
|
* Basic syntax is comprised of SECTIONBLOCKs.
|
||||||
|
* A SECTIONBLOCK defined as:
|
||||||
|
*
|
||||||
|
* SECTIONNAME
|
||||||
|
* {
|
||||||
|
* OPTION
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* OPTION can be repeated any number of times inside a SECTIONBLOCK.
|
||||||
|
* A new line will terminate an OPTION, but there can be more than one OPTION per line.
|
||||||
|
* OPTION is defined any of:
|
||||||
|
* "KEY" "VALUE"
|
||||||
|
* SECTIONBLOCK
|
||||||
|
*
|
||||||
|
* SECTIONNAME, KEY, VALUE, and SINGLEKEY are strings
|
||||||
|
* SECTIONNAME cannot have trailing characters if quoted, but the quotes can be optionally removed.
|
||||||
|
* If SECTIONNAME is not enclosed in quotes, the entire sectionname string is used (minus surrounding whitespace).
|
||||||
|
* If KEY is not enclosed in quotes, the key is terminated at first whitespace.
|
||||||
|
* If VALUE is not properly enclosed in quotes, the entire value string is used (minus surrounding whitespace).
|
||||||
|
* The VALUE may have inner quotes, but the key string may not.
|
||||||
|
*
|
||||||
|
* For an example, see configs/permissions.cfg
|
||||||
|
*
|
||||||
|
* WHITESPACE should be ignored.
|
||||||
|
* Comments are text occurring inside the following tokens, and should be stripped
|
||||||
|
* unless they are inside literal strings:
|
||||||
|
* ;<TEXT>
|
||||||
|
* //<TEXT>
|
||||||
|
* / *<TEXT> */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Lists actions to take when an SMC parse hook is done.
|
||||||
|
*/
|
||||||
|
enum SMCResult
|
||||||
|
{
|
||||||
|
SMCResult_Continue, /**< Continue parsing */
|
||||||
|
SMCResult_Halt, /**< Stop parsing here */
|
||||||
|
SMCResult_HaltFail /**< Stop parsing and return SMCError_Custom */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Lists error codes possible from parsing an SMC file.
|
||||||
|
*/
|
||||||
|
enum SMCError
|
||||||
|
{
|
||||||
|
SMCError_Okay = 0, /**< No error */
|
||||||
|
SMCError_StreamOpen, /**< Stream failed to open */
|
||||||
|
SMCError_StreamError, /**< The stream died... somehow */
|
||||||
|
SMCError_Custom, /**< A custom handler threw an error */
|
||||||
|
SMCError_InvalidSection1, /**< A section was declared without quotes, and had extra tokens */
|
||||||
|
SMCError_InvalidSection2, /**< A section was declared without any header */
|
||||||
|
SMCError_InvalidSection3, /**< A section ending was declared with too many unknown tokens */
|
||||||
|
SMCError_InvalidSection4, /**< A section ending has no matching beginning */
|
||||||
|
SMCError_InvalidSection5, /**< A section beginning has no matching ending */
|
||||||
|
SMCError_InvalidTokens, /**< There were too many unidentifiable strings on one line */
|
||||||
|
SMCError_TokenOverflow, /**< The token buffer overflowed */
|
||||||
|
SMCError_InvalidProperty1, /**< A property was declared outside of any section */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief States for line/column
|
||||||
|
*/
|
||||||
|
struct SMCStates
|
||||||
|
{
|
||||||
|
unsigned int line; /**< Current line */
|
||||||
|
unsigned int col; /**< Current col */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Describes the events available for reading an SMC stream.
|
||||||
|
*/
|
||||||
|
class ITextListener_SMC
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns version number.
|
||||||
|
*/
|
||||||
|
virtual unsigned int GetTextParserVersion2()
|
||||||
|
{
|
||||||
|
return SMINTERFACE_TEXTPARSERS_VERSION;
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Called when starting parsing.
|
||||||
|
*/
|
||||||
|
virtual void ReadSMC_ParseStart()
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called when ending parsing.
|
||||||
|
*
|
||||||
|
* @param halted True if abnormally halted, false otherwise.
|
||||||
|
* @param failed True if parsing failed, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual void ReadSMC_ParseEnd(bool halted, bool failed)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called when entering a new section
|
||||||
|
*
|
||||||
|
* @param states Parsing states.
|
||||||
|
* @param name Name of section, with the colon omitted.
|
||||||
|
* @return SMCResult directive.
|
||||||
|
*/
|
||||||
|
virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
|
||||||
|
{
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called when encountering a key/value pair in a section.
|
||||||
|
*
|
||||||
|
* @param states Parsing states.
|
||||||
|
* @param key Key string.
|
||||||
|
* @param value Value string. If no quotes were specified, this will be NULL,
|
||||||
|
* and key will contain the entire string.
|
||||||
|
* @return SMCResult directive.
|
||||||
|
*/
|
||||||
|
virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
|
||||||
|
{
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called when leaving the current section.
|
||||||
|
*
|
||||||
|
* @param states Parsing states.
|
||||||
|
* @return SMCResult directive.
|
||||||
|
*/
|
||||||
|
virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
|
||||||
|
{
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Called after an input line has been preprocessed.
|
||||||
|
*
|
||||||
|
* @param states Parsing states.
|
||||||
|
* @param line Contents of the line, null terminated at the position
|
||||||
|
* of the newline character (thus, no newline will exist).
|
||||||
|
* @return SMCResult directive.
|
||||||
|
*/
|
||||||
|
virtual SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line)
|
||||||
|
{
|
||||||
|
return SMCResult_Continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Contains various text stream parsing functions.
|
||||||
|
*/
|
||||||
|
class ITextParsers /*: public SMInterface*/
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual const char *GetInterfaceName()
|
||||||
|
{
|
||||||
|
return SMINTERFACE_TEXTPARSERS_NAME;
|
||||||
|
}
|
||||||
|
virtual unsigned int GetInterfaceVersion()
|
||||||
|
{
|
||||||
|
return SMINTERFACE_TEXTPARSERS_VERSION;
|
||||||
|
}
|
||||||
|
virtual bool IsVersionCompatible(unsigned int version)
|
||||||
|
{
|
||||||
|
if (version < 2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
/*return SMInterface::IsVersionCompatible(version);*/
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Parses an INI-format file.
|
||||||
|
*
|
||||||
|
* @param file Path to file.
|
||||||
|
* @param ini_listener Event handler for reading file.
|
||||||
|
* @param line If non-NULL, will contain last line parsed (0 if file could not be opened).
|
||||||
|
* @param col If non-NULL, will contain last column parsed (undefined if file could not be opened).
|
||||||
|
* @return True if parsing succeeded, false if file couldn't be opened or there was a syntax error.
|
||||||
|
*/
|
||||||
|
virtual bool ParseFile_INI(const char *file,
|
||||||
|
ITextListener_INI *ini_listener,
|
||||||
|
unsigned int *line,
|
||||||
|
unsigned int *col) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Parses an SMC-format text file.
|
||||||
|
* Note that the parser makes every effort to obey broken syntax.
|
||||||
|
* For example, if an open brace is missing, but the section name has a colon,
|
||||||
|
* it will let you know. It is up to the event handlers to decide whether to be strict or not.
|
||||||
|
*
|
||||||
|
* @param file Path to file.
|
||||||
|
* @param smc_listener Event handler for reading file.
|
||||||
|
* @param states Optional pointer to store last known states.
|
||||||
|
* @return An SMCError result code.
|
||||||
|
*/
|
||||||
|
virtual SMCError ParseFile_SMC(const char *file,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Converts an SMCError to a string.
|
||||||
|
*
|
||||||
|
* @param err SMCError.
|
||||||
|
* @return String error message, or NULL if none.
|
||||||
|
*/
|
||||||
|
virtual const char *GetSMCErrorString(SMCError err) =0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns the number of bytes that a multi-byte character contains in a UTF-8 stream.
|
||||||
|
* If the current character is not multi-byte, the function returns 1.
|
||||||
|
*
|
||||||
|
* @param stream Pointer to multi-byte ANSI character string.
|
||||||
|
* @return Number of bytes in current character.
|
||||||
|
*/
|
||||||
|
virtual unsigned int GetUTF8CharBytes(const char *stream) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns whether the first multi-byte character in the given stream
|
||||||
|
* is a whitespace character.
|
||||||
|
*
|
||||||
|
* @param stream Pointer to multi-byte character string.
|
||||||
|
* @return True if first character is whitespace, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool IsWhitespace(const char *stream) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Same as ParseFile_SMC, but with an extended error buffer.
|
||||||
|
*
|
||||||
|
* @param file Path to file.
|
||||||
|
* @param smc_listener Event handler for reading file.
|
||||||
|
* @param states Optional pointer to store last known states.
|
||||||
|
* @param buffer Error message buffer.
|
||||||
|
* @param maxsize Maximum size of the error buffer.
|
||||||
|
* @return Error code.
|
||||||
|
*/
|
||||||
|
virtual SMCError ParseSMCFile(const char *file,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states,
|
||||||
|
char *buffer,
|
||||||
|
size_t maxsize) =0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Parses a raw UTF8 stream as an SMC file.
|
||||||
|
*
|
||||||
|
* @param stream Memory containing data.
|
||||||
|
* @param length Number of bytes in the stream.
|
||||||
|
* @param smc_listener Event handler for reading file.
|
||||||
|
* @param states Optional pointer to store last known states.
|
||||||
|
* @param buffer Error message buffer.
|
||||||
|
* @param maxsize Maximum size of the error buffer.
|
||||||
|
* @return Error code.
|
||||||
|
*/
|
||||||
|
virtual SMCError ParseSMCStream(const char *stream,
|
||||||
|
size_t length,
|
||||||
|
ITextListener_SMC *smc_listener,
|
||||||
|
SMCStates *states,
|
||||||
|
char *buffer,
|
||||||
|
size_t maxsize) =0;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline unsigned int _GetUTF8CharBytes(const char *stream)
|
||||||
|
{
|
||||||
|
unsigned char c = *(unsigned char *)stream;
|
||||||
|
if (c & (1<<7))
|
||||||
|
{
|
||||||
|
if (c & (1<<5))
|
||||||
|
{
|
||||||
|
if (c & (1<<4))
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern SourceMod::ITextParsers *textparsers;
|
||||||
|
|
||||||
|
#endif //_INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
|
||||||
|
|
Loading…
Reference in New Issue
Block a user