amxmodx/modules/regex/module.cpp

337 lines
7.1 KiB
C++
Raw Normal View History

// 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
//
// Regular Expressions Module
//
2004-10-05 07:17:49 +00:00
#include <string.h>
#include "pcre.h"
2004-10-05 08:13:55 +00:00
#include "amxxmodule.h"
2014-07-06 10:13:01 +00:00
#include <am-vector.h>
#include <am-utility.h>
2004-10-05 07:17:49 +00:00
#include "CRegEx.h"
#include "utils.h"
2004-10-05 07:17:49 +00:00
2014-07-06 10:13:01 +00:00
ke::Vector<RegEx *> PEL;
2004-10-05 07:17:49 +00:00
int GetPEL()
{
2014-07-06 10:13:01 +00:00
for (int i=0; i<(int)PEL.length(); i++)
2004-10-05 07:17:49 +00:00
{
if (PEL[i]->isFree())
return i;
}
RegEx *x = new RegEx();
2014-07-06 10:13:01 +00:00
PEL.append(x);
2004-10-05 07:17:49 +00:00
2014-07-06 10:13:01 +00:00
return (int)PEL.length() - 1;
2004-10-05 07:17:49 +00:00
}
// native Regex:regex_compile(const pattern[], &ret, error[], maxLen, const flags[]="");
static cell AMX_NATIVE_CALL regex_compile(AMX *amx, cell *params)
{
int len;
const char *regex = MF_GetAmxString(amx, params[1], 0, &len);
const char *flags = MF_GetAmxString(amx, params[5], 1, &len);
2004-10-05 07:17:49 +00:00
int id = GetPEL();
RegEx *x = PEL[id];
if (x->Compile(regex, flags) == 0)
{
const char *err = x->mError;
*MF_GetAmxAddr(amx, params[2]) = x->mErrorOffset;
MF_SetAmxString(amx, params[3], err?err:"unknown", params[4]);
return -1;
}
return id+1;
}
// native Regex:regex_compile_ex(const pattern[], flags = 0, error[] = "", maxLen = 0, &errcode = 0);
static cell AMX_NATIVE_CALL regex_compile_ex(AMX *amx, cell *params)
{
int len;
const char *regex = MF_GetAmxString(amx, params[1], 0, &len);
int id = GetPEL();
RegEx *x = PEL[id];
if (x->Compile(regex, params[2]) == 0)
{
const char *err = x->mError;
*MF_GetAmxAddr(amx, params[5]) = x->mErrorOffset;
MF_SetAmxString(amx, params[3], err ? err : "unknown", params[4]);
return -1;
}
return id + 1;
}
cell match(AMX *amx, cell *params, bool all)
2004-10-05 07:17:49 +00:00
{
int len;
const char *str = MF_GetAmxString(amx, params[1], 0, &len);
const char *regex = MF_GetAmxString(amx, params[2], 1, &len);
int id = GetPEL();
RegEx *x = PEL[id];
char *flags = NULL;
cell *errorCode;
int result = 0;
if (!all)
{
if (*params / sizeof(cell) >= 6) // compiled with 1.8's extra parameter
{
flags = MF_GetAmxString(amx, params[6], 2, &len);
}
result = x->Compile(regex, flags);
errorCode = MF_GetAmxAddr(amx, params[3]);
}
else
{
result = x->Compile(regex, params[3]);
errorCode = MF_GetAmxAddr(amx, params[6]);
}
if (!result)
2004-10-05 07:17:49 +00:00
{
const char *err = x->mError;
*errorCode = x->mErrorOffset;
MF_SetAmxString(amx, params[4], err ? err : "unknown", params[5]);
2004-10-05 08:32:23 +00:00
return -1;
2004-10-05 07:17:49 +00:00
}
int e;
if (all)
e = x->MatchAll(str);
else
e = x->Match(str);
2004-10-05 07:17:49 +00:00
if (e == -1)
{
/* there was a match error. destroy this and move on. */
*errorCode = x->mErrorOffset;
2004-10-05 07:17:49 +00:00
x->Clear();
2004-10-05 08:32:23 +00:00
return -2;
}
else if (e == 0)
{
*errorCode = 0;
2004-10-05 08:32:23 +00:00
x->Clear();
return 0;
}
else
{
*errorCode = x->Count();
2014-07-17 09:20:52 +00:00
if (all)
return x->Count();
2004-10-05 07:17:49 +00:00
}
return id + 1;
2004-10-05 07:17:49 +00:00
}
// native Regex:regex_match(const string[], const pattern[], &ret, error[], maxLen, const flags[] = "");
static cell AMX_NATIVE_CALL regex_match(AMX *amx, cell *params)
{
return match(amx, params, false);
}
// native Regex:regex_match_all(const string[], const pattern[], flags = 0, error[] = "", maxLen = 0, &errcode = 0);
static cell AMX_NATIVE_CALL regex_match_all(AMX *amx, cell *params)
{
return match(amx, params, true);
}
cell match_c(AMX *amx, cell *params, bool all)
{
int id = params[2] - 1;
2014-07-06 10:13:01 +00:00
if (id >= (int)PEL.length() || id < 0 || PEL[id]->isFree())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid regex handle %d", id);
return 0;
}
int len;
const char *str = MF_GetAmxString(amx, params[1], 0, &len);
cell *errorCode = MF_GetAmxAddr(amx, params[3]);
RegEx *x = PEL[id];
int e;
if (all)
e = x->MatchAll(str);
else
e = x->Match(str);
if (e == -1)
{
/* there was a match error. move on. */
*errorCode = x->mErrorOffset;
/* only clear the match results, since the regex object
may still be referenced later */
x->ClearMatch();
return -2;
}
else if (e == 0)
{
*errorCode = 0;
/* only clear the match results, since the regex object
may still be referenced later */
x->ClearMatch();
return 0;
}
else
{
*errorCode = x->Count();
return x->Count();
}
}
2004-10-05 07:17:49 +00:00
// native regex_match_c(const string[], Regex:id, &ret);
static cell AMX_NATIVE_CALL regex_match_c(AMX *amx, cell *params)
{
return match_c(amx, params, false);
}
// native regex_match_all_c(const string[], Regex:id, &ret);
static cell AMX_NATIVE_CALL regex_match_all_c(AMX *amx, cell *params)
{
return match_c(amx, params, true);
}
// native regex_substr(Regex:id, str_id, buffer[], maxLen);
2004-10-05 07:17:49 +00:00
static cell AMX_NATIVE_CALL regex_substr(AMX *amx, cell *params)
{
2004-10-05 08:51:57 +00:00
int id = params[1]-1;
2014-07-06 10:13:01 +00:00
if (id >= (int)PEL.length() || id < 0 || PEL[id]->isFree())
2004-10-05 07:17:49 +00:00
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid regex handle %d", id);
return 0;
}
RegEx *x = PEL[id];
static char buffer[16384]; // Same as AMXX buffer.
2004-10-05 07:17:49 +00:00
size_t length;
size_t maxLength = ke::Min<size_t>(params[4], sizeof(buffer) - 1);
const char *ret = x->GetSubstring(params[2], buffer, maxLength, &length);
2004-10-05 07:17:49 +00:00
if (ret == NULL)
{
2004-10-05 07:17:49 +00:00
return 0;
}
if (length >= maxLength && ret[length - 1] & 1 << 7)
{
maxLength -= UTIL_CheckValidChar((char *)ret + length - 1);
}
2004-10-05 07:17:49 +00:00
MF_SetAmxString(amx, params[3], ret, maxLength);
2004-10-05 07:17:49 +00:00
return 1;
}
2004-10-05 08:32:23 +00:00
static cell AMX_NATIVE_CALL regex_free(AMX *amx, cell *params)
{
cell *c = MF_GetAmxAddr(amx, params[1]);
int id = *c;
*c = 0;
2004-10-05 08:51:57 +00:00
id -= 1;
2014-07-06 10:13:01 +00:00
if (id >= (int)PEL.length() || id < 0 || PEL[id]->isFree())
2004-10-05 08:32:23 +00:00
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid regex handle %d", id);
return 0;
}
RegEx *x = PEL[id];
x->Clear();
return 1;
}
2014-07-17 09:20:52 +00:00
//native regex_replace(Regex:pattern, string[], maxLen, const replace[], flags = REGEX_FORMAT_DEFAULT, &errcode = 0);
static cell AMX_NATIVE_CALL regex_replace(AMX *amx, cell *params)
{
int id = params[1] - 1;
if (id >= (int)PEL.length() || id < 0 || PEL[id]->isFree())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid regex handle %d", id);
return 0;
}
int textLen, replaceLen;
char *text = MF_GetAmxString(amx, params[2], 0, &textLen);
const char *replace = MF_GetAmxString(amx, params[4], 1, &replaceLen);
cell *erroCode = MF_GetAmxAddr(amx, params[6]);
RegEx *x = PEL[id];
int e = x->Replace(text, params[3] + 1, replace, replaceLen, params[5]);
if (e == -1)
{
*erroCode = x->mErrorOffset;
x->ClearMatch();
return -2;
}
else if (e == 0)
{
*erroCode = 0;
x->ClearMatch();
return 0;
}
MF_SetAmxString(amx, params[2], text, params[3]);
return e;
}
2004-10-05 07:17:49 +00:00
AMX_NATIVE_INFO regex_Natives[] = {
{"regex_compile", regex_compile},
{"regex_compile_ex", regex_compile_ex},
2004-10-05 07:17:49 +00:00
{"regex_match", regex_match},
{"regex_match_c", regex_match_c},
{"regex_match_all", regex_match_all},
{"regex_match_all_c", regex_match_all_c},
2004-10-05 07:17:49 +00:00
{"regex_substr", regex_substr},
2014-07-17 09:20:52 +00:00
{"regex_replace", regex_replace},
2004-10-05 08:32:23 +00:00
{"regex_free", regex_free},
2004-10-05 07:17:49 +00:00
{NULL, NULL},
};
void OnAmxxAttach()
{
MF_AddNatives(regex_Natives);
}
void OnAmxxDetach()
{
2014-07-06 10:13:01 +00:00
for (int i = 0; i<(int)PEL.length(); i++)
2004-10-05 07:17:49 +00:00
{
if (PEL[i])
{
delete PEL[i];
PEL[i] = 0;
}
}
PEL.clear();
2004-10-05 08:13:55 +00:00
}