Introduce TextParser API.

This commit is contained in:
Arkshine
2014-07-21 21:12:35 +02:00
parent 5fbc5c074d
commit 9af7931a79
13 changed files with 2438 additions and 2 deletions

View File

@ -94,6 +94,8 @@ binary.sources = [
'CDataPack.cpp',
'datapacks.cpp',
'stackstructs.cpp',
'CTextParsers.cpp',
'textparse.cpp',
]
if builder.target_platform == 'windows':

1113
amxmodx/CTextParsers.cpp Normal file

File diff suppressed because it is too large Load Diff

90
amxmodx/CTextParsers.h Normal file
View 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_

View File

@ -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)

View File

@ -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];

View File

@ -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;

View File

@ -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" />

View File

@ -173,9 +173,15 @@
</ClCompile>
<ClCompile Include="..\datapacks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ClCompile>
<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>
@ -317,6 +323,18 @@
<ClInclude Include="..\CDataPack.h">
<Filter>Header Files</Filter>
</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>
<ResourceCompile Include="..\version.rc">

226
amxmodx/textparse.cpp Normal file
View 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
View 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_