Merge pull request #101 from Arkshine/textparsers
Introduce TextParser API
This commit is contained in:
@ -94,6 +94,8 @@ binary.sources = [
|
||||
'CDataPack.cpp',
|
||||
'datapacks.cpp',
|
||||
'stackstructs.cpp',
|
||||
'CTextParsers.cpp',
|
||||
'textparse.cpp',
|
||||
]
|
||||
|
||||
if builder.target_platform == 'windows':
|
||||
|
1130
amxmodx/CTextParsers.cpp
Normal file
1130
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_
|
||||
|
@ -21,7 +21,8 @@ OBJECTS = meta_api.cpp CFile.cpp CVault.cpp vault.cpp float.cpp file.cpp modules
|
||||
CMenu.cpp util.cpp amx.cpp amxdbg.cpp natives.cpp newmenus.cpp debugger.cpp \
|
||||
optimizer.cpp format.cpp messages.cpp libraries.cpp vector.cpp sorting.cpp \
|
||||
nongpl_matches.cpp CFlagManager.cpp datastructs.cpp \
|
||||
trie_natives.cpp CDataPack.cpp datapacks.cpp stackstructs.cpp
|
||||
trie_natives.cpp CDataPack.cpp datapacks.cpp stackstructs.cpp \
|
||||
CTextParsers.cpp textparse.cpp \
|
||||
|
||||
##############################################
|
||||
### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ###
|
||||
|
@ -64,6 +64,7 @@ extern AMX_NATIVE_INFO vector_Natives[];
|
||||
extern AMX_NATIVE_INFO g_SortNatives[];
|
||||
extern AMX_NATIVE_INFO g_DataStructNatives[];
|
||||
extern AMX_NATIVE_INFO g_StackNatives[];
|
||||
extern AMX_NATIVE_INFO g_TextParserNatives[];
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define DLLOAD(path) (DLHANDLE)LoadLibrary(path)
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include <amxmodx_version.h>
|
||||
#include "trie_natives.h"
|
||||
#include "CDataPack.h"
|
||||
#include "textparse.h"
|
||||
|
||||
plugin_info_t Plugin_info =
|
||||
{
|
||||
@ -395,6 +396,7 @@ int C_Spawn(edict_t *pent)
|
||||
g_TrieHandles.clear();
|
||||
g_TrieSnapshotHandles.clear();
|
||||
g_DataPackHandles.clear();
|
||||
g_TextParsersHandles.clear();
|
||||
|
||||
char map_pluginsfile_path[256];
|
||||
char prefixed_map_pluginsfile[256];
|
||||
|
@ -552,7 +552,8 @@ int set_amxnatives(AMX* amx, char error[128])
|
||||
amx_Register(amx, trie_Natives, -1);
|
||||
amx_Register(amx, g_DatapackNatives, -1);
|
||||
amx_Register(amx, g_StackNatives, -1);
|
||||
|
||||
amx_Register(amx, g_TextParserNatives, -1);
|
||||
|
||||
//we're not actually gonna check these here anymore
|
||||
amx->flags |= AMX_FLAG_PRENIT;
|
||||
|
||||
|
@ -306,6 +306,7 @@
|
||||
<ClCompile Include="..\CModule.cpp" />
|
||||
<ClCompile Include="..\CPlugin.cpp" />
|
||||
<ClCompile Include="..\CTask.cpp" />
|
||||
<ClCompile Include="..\CTextParsers.cpp" />
|
||||
<ClCompile Include="..\CVault.cpp" />
|
||||
<ClCompile Include="..\datapacks.cpp" />
|
||||
<ClCompile Include="..\debugger.cpp" />
|
||||
@ -335,6 +336,7 @@
|
||||
<AssemblerOutput Condition="'$(Configuration)|$(Platform)'=='JITRelease|Win32'">All</AssemblerOutput>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\strptime.cpp" />
|
||||
<ClCompile Include="..\textparse.cpp" />
|
||||
<ClCompile Include="..\trie_natives.cpp" />
|
||||
<ClCompile Include="..\util.cpp" />
|
||||
<ClCompile Include="..\vault.cpp" />
|
||||
@ -370,6 +372,7 @@
|
||||
<ClInclude Include="..\CQueue.h" />
|
||||
<ClInclude Include="..\CString.h" />
|
||||
<ClInclude Include="..\CTask.h" />
|
||||
<ClInclude Include="..\CTextParsers.h" />
|
||||
<ClInclude Include="..\CVault.h" />
|
||||
<ClInclude Include="..\CVector.h" />
|
||||
<ClInclude Include="..\datastructs.h" />
|
||||
@ -389,6 +392,7 @@
|
||||
<ClInclude Include="..\sh_stack.h" />
|
||||
<ClInclude Include="..\sh_tinyhash.h" />
|
||||
<ClInclude Include="..\svn_version.h" />
|
||||
<ClInclude Include="..\textparse.h" />
|
||||
<ClInclude Include="..\trie_natives.h" />
|
||||
<ClInclude Include="..\zlib\zconf.h" />
|
||||
<ClInclude Include="..\zlib\zlib.h" />
|
||||
@ -401,6 +405,8 @@
|
||||
<ItemGroup>
|
||||
<None Include="..\..\plugins\include\cellstack.inc" />
|
||||
<None Include="..\..\plugins\include\datapack.inc" />
|
||||
<None Include="..\..\plugins\include\textparse_ini.inc" />
|
||||
<None Include="..\..\plugins\include\textparse_smc.inc" />
|
||||
<None Include="..\amxdefn.asm" />
|
||||
<None Include="..\amxexecn.asm" />
|
||||
<None Include="..\amxjitsn.asm" />
|
||||
|
@ -177,6 +177,12 @@
|
||||
<ClCompile Include="..\stackstructs.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\CTextParsers.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\textparse.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\amx.h">
|
||||
@ -317,6 +323,12 @@
|
||||
<ClInclude Include="..\CDataPack.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\CTextParsers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\textparse.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="..\version.rc">
|
||||
@ -405,6 +417,12 @@
|
||||
<None Include="..\..\plugins\include\cellstack.inc">
|
||||
<Filter>Pawn Includes</Filter>
|
||||
</None>
|
||||
<None Include="..\..\plugins\include\textparse_ini.inc">
|
||||
<Filter>Pawn Includes</Filter>
|
||||
</None>
|
||||
<None Include="..\..\plugins\include\textparse_smc.inc">
|
||||
<Filter>Pawn Includes</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Object Include="..\Jit\helpers-x86.obj">
|
||||
|
394
amxmodx/textparse.cpp
Normal file
394
amxmodx/textparse.cpp
Normal file
@ -0,0 +1,394 @@
|
||||
/**
|
||||
* vim: set ts=4 sw=4 tw=99 noet:
|
||||
*
|
||||
* AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
* Copyright (C) The AMX Mod X Development Team.
|
||||
*
|
||||
* This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
* Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
* https://alliedmods.net/amxmodx-license
|
||||
*/
|
||||
|
||||
#include "amxmodx.h"
|
||||
#include <textparse.h>
|
||||
#include <am-vector.h>
|
||||
|
||||
TextParserHandles<ParseInfo> g_TextParsersHandles;
|
||||
|
||||
cell createParser()
|
||||
{
|
||||
return g_TextParsersHandles.create();
|
||||
}
|
||||
|
||||
cell destroyParser(cell *handle)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(*handle);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (g_TextParsersHandles.destroy(*handle))
|
||||
{
|
||||
*handle = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* SMC CONFIG.
|
||||
*/
|
||||
|
||||
// native SMCParser:SMC_CreateParser();
|
||||
static cell AMX_NATIVE_CALL SMC_CreateParser(AMX *amx, cell *params)
|
||||
{
|
||||
return createParser();
|
||||
}
|
||||
|
||||
// native SMC_SetParseStart(SMCParser:handle, const func[]);
|
||||
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 SMC parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
|
||||
{
|
||||
p->parse_start = registerSPForwardByName(amx, funcName, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->parse_start == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native SMC_SetParseEnd(SMCParser:handle, const func[]);
|
||||
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 SMC parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
|
||||
{
|
||||
p->parse_end = registerSPForwardByName(amx, funcName, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->parse_end == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native SMC_SetReaders(SMCParser:smc, const kvFunc[], const nsFunc[] = "", const esFunc[] = "");
|
||||
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 SMC parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kvLength = 0, nsLength = 0, esLength = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength)
|
||||
{
|
||||
p->key_value = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_STRING, FP_DONE);
|
||||
}
|
||||
|
||||
if (kvLength && (funcName = get_amxstring(amx, params[3], 1, nsLength)) && nsLength)
|
||||
{
|
||||
p->new_section = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_DONE);
|
||||
}
|
||||
|
||||
if (kvLength && (funcName = get_amxstring(amx, params[4], 2, esLength)) && esLength)
|
||||
{
|
||||
p->end_section = registerSPForwardByName(amx, funcName, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->key_value == -1 || (nsLength && p->new_section == -1) || (esLength && p->end_section == -1))
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native SMC_SetRawLine(SMCParser:handle, const func[]);
|
||||
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 SMC parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
|
||||
{
|
||||
p->raw_line = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->raw_line == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native SMCError:SMC_ParseFile(SMCParser:handle, const file[], &line = 0, &col = 0);
|
||||
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 SMC parse handle (%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 static_cast<cell>(p_err);
|
||||
}
|
||||
|
||||
// native bool:SMC_GetErrorString(SMCError:error, buffer[], buf_max);
|
||||
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]);
|
||||
}
|
||||
|
||||
// native SMC_DestroyParser(&SMCParser:handle);
|
||||
static cell AMX_NATIVE_CALL SMC_DestroyParser(AMX *amx, cell *params)
|
||||
{
|
||||
return destroyParser(get_amxaddr(amx, params[1]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* INI CONFIG
|
||||
*/
|
||||
|
||||
// native INIParser:INI_CreateParser();
|
||||
static cell AMX_NATIVE_CALL INI_CreateParser(AMX *amx, cell *params)
|
||||
{
|
||||
return createParser();
|
||||
}
|
||||
|
||||
// native bool:INI_ParseFile(INIParser:handle, const file[], &line = 0, &col = 0);
|
||||
static cell AMX_NATIVE_CALL INI_ParseFile(AMX *amx, cell *params)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length;
|
||||
const char *file = build_pathname("%s", get_amxstring(amx, params[2], 0, length));
|
||||
|
||||
unsigned int line, col;
|
||||
bool result = textparsers->ParseFile_INI(file, p, &line, &col);
|
||||
|
||||
*get_amxaddr(amx, params[3]) = line;
|
||||
*get_amxaddr(amx, params[4]) = col;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// native INI_SetParseStart(INIParser:handle, const func[]);
|
||||
static cell AMX_NATIVE_CALL INI_SetParseStart(AMX *amx, cell *params)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
|
||||
{
|
||||
p->parse_start = registerSPForwardByName(amx, funcName, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->parse_start == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native INI_SetParseEnd(INIParser:handle, const func[]);
|
||||
static cell AMX_NATIVE_CALL INI_SetParseEnd(AMX *amx, cell *params)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)))
|
||||
{
|
||||
p->parse_end = registerSPForwardByName(amx, funcName, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->parse_end == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native INI_SetReaders(INIParser:smc, const kvFunc[], const nsFunc[] = "" );
|
||||
static cell AMX_NATIVE_CALL INI_SetReaders(AMX *amx, cell *params)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kvLength = 0, nsLength = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength)
|
||||
{
|
||||
p->key_value = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_STRING, FP_CELL, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (kvLength && (funcName = get_amxstring(amx, params[3], 1, nsLength)) && nsLength)
|
||||
{
|
||||
p->new_section = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_CELL, FP_CELL, FP_CELL, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->key_value == -1 || (nsLength && p->new_section == -1))
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native INI_SetRawLine(INIParser:handle, const func[]);
|
||||
static cell AMX_NATIVE_CALL INI_SetRawLine(AMX *amx, cell *params)
|
||||
{
|
||||
ParseInfo *p = g_TextParsersHandles.lookup(params[1]);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
const char *funcName = NULL;
|
||||
|
||||
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
|
||||
{
|
||||
p->raw_line = registerSPForwardByName(amx, funcName, FP_CELL, FP_STRING, FP_CELL, FP_CELL, FP_DONE);
|
||||
}
|
||||
|
||||
if (p->raw_line == -1)
|
||||
{
|
||||
LogError(amx, AMX_ERR_NATIVE, "Function is not present (function \"%s\") (plugin \"%s\")", funcName, g_plugins.findPluginFast(amx)->getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// native INI_DestroyParser(&INIParser:handle);
|
||||
static cell AMX_NATIVE_CALL INI_DestroyParser(AMX *amx, cell *params)
|
||||
{
|
||||
return destroyParser(get_amxaddr(amx, params[1]));
|
||||
}
|
||||
|
||||
|
||||
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 },
|
||||
|
||||
{ "INI_CreateParser" , INI_CreateParser },
|
||||
{ "INI_ParseFile" , INI_ParseFile },
|
||||
{ "INI_SetParseStart" , INI_SetParseStart },
|
||||
{ "INI_SetParseEnd" , INI_SetParseEnd },
|
||||
{ "INI_SetReaders" , INI_SetReaders },
|
||||
{ "INI_SetRawLine" , INI_SetRawLine },
|
||||
{ "INI_DestroyParser" , INI_DestroyParser },
|
||||
|
||||
{ NULL, NULL },
|
||||
};
|
207
amxmodx/textparse.h
Normal file
207
amxmodx/textparse.h
Normal file
@ -0,0 +1,207 @@
|
||||
/**
|
||||
* vim: set ts=4 sw=4 tw=99 noet:
|
||||
*
|
||||
* AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
* Copyright (C) The AMX Mod X Development Team.
|
||||
*
|
||||
* This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
* Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
* https://alliedmods.net/amxmodx-license
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_TEXTPARSE_H_
|
||||
#define _INCLUDE_TEXTPARSE_H_
|
||||
|
||||
#include "amxmodx.h"
|
||||
#include "CTextParsers.h"
|
||||
|
||||
class ParseInfo :
|
||||
public ITextListener_SMC,
|
||||
public ITextListener_INI
|
||||
{
|
||||
public:
|
||||
ParseInfo()
|
||||
{
|
||||
parse_start = -1;
|
||||
parse_end = -1;
|
||||
new_section = -1;
|
||||
key_value = -1;
|
||||
end_section = -1;
|
||||
raw_line = -1;
|
||||
handle = -1;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* SMC CONFIG.
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* INI CONFIG.
|
||||
*/
|
||||
|
||||
void ReadINI_ParseStart()
|
||||
{
|
||||
if (parse_start != -1)
|
||||
executeForwards(parse_start, handle);
|
||||
}
|
||||
|
||||
void ReadINI_ParseEnd(bool halted)
|
||||
{
|
||||
if (parse_end != -1)
|
||||
executeForwards(parse_end, handle, halted ? 1 : 0);
|
||||
}
|
||||
|
||||
bool ReadINI_NewSection(const char *section, bool invalid_tokens, bool close_bracket, bool extra_tokens, unsigned int *curtok)
|
||||
{
|
||||
if (new_section != -1)
|
||||
return executeForwards(new_section, handle, section, invalid_tokens, close_bracket, extra_tokens, *curtok) > 0 ? true : false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadINI_KeyValue(const char *key, const char *value, bool invalid_tokens, bool equal_token, bool quotes, unsigned int *curtok)
|
||||
{
|
||||
if (key_value != -1)
|
||||
return executeForwards(key_value, handle, key, value, invalid_tokens, equal_token, quotes, *curtok) > 0 ? true : false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadINI_RawLine(const char *line, unsigned int *curtok)
|
||||
{
|
||||
if (raw_line != -1)
|
||||
return executeForwards(raw_line, handle, line, *curtok) > 0 ? true : false;
|
||||
|
||||
return true;
|
||||
}
|
||||
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_
|
Reference in New Issue
Block a user