1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/regex/extension.cpp Thu Jan 10 22:01:36 2008 +0000
1.3 @@ -0,0 +1,212 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod Sample Extension
1.8 + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id: extension.cpp 1336 2007-08-15 06:19:30Z damagedsoul $
1.33 + */
1.34 +
1.35 +#include "extension.h"
1.36 +
1.37 +#include <sh_string.h>
1.38 +#include <sh_vector.h>
1.39 +#include "pcre.h"
1.40 +#include "CRegEx.h"
1.41 +using namespace SourceHook;
1.42 +
1.43 +/**
1.44 + * @file extension.cpp
1.45 + * @brief Implement extension code here.
1.46 + */
1.47 +
1.48 +RegexExtension g_RegexExtension; /**< Global singleton for extension's main interface */
1.49 +
1.50 +SMEXT_LINK(&g_RegexExtension);
1.51 +
1.52 +RegexHandler g_RegexHandler;
1.53 +HandleType_t g_RegexHandle=0;
1.54 +
1.55 +
1.56 +SourceHook::CVector<RegEx *> PEL;
1.57 +
1.58 +int GetPEL()
1.59 +{
1.60 + for (int i=0; i<(int)PEL.size(); i++)
1.61 + {
1.62 + if (PEL[i]->isFree())
1.63 + return i;
1.64 + }
1.65 +
1.66 + RegEx *x = new RegEx();
1.67 + PEL.push_back(x);
1.68 +
1.69 + return (int)PEL.size() - 1;
1.70 +}
1.71 +
1.72 +bool RegexExtension::SDK_OnLoad(char *error, size_t err_max, bool late)
1.73 +{
1.74 + g_pShareSys->AddNatives(myself,regex_natives);
1.75 + g_RegexHandle = g_pHandleSys->CreateType("Regex", &g_RegexHandler, 0, NULL, NULL, myself->GetIdentity(), NULL);
1.76 + return true;
1.77 +}
1.78 +
1.79 +void RegexExtension::SDK_OnUnload()
1.80 +{
1.81 + g_pHandleSys->RemoveType(g_RegexHandle, myself->GetIdentity());
1.82 + for(int i=0; i<(int)PEL.size(); i++)
1.83 + {
1.84 + if(PEL[i])
1.85 + {
1.86 + delete PEL[i];
1.87 + PEL[i] = 0;
1.88 + }
1.89 + }
1.90 + PEL.clear();
1.91 +}
1.92 +
1.93 +static cell_t CompileRegex(IPluginContext *pCtx, const cell_t *params)
1.94 +{
1.95 + char *regex;
1.96 + pCtx->LocalToString(params[1], ®ex);
1.97 +
1.98 + int id = GetPEL();
1.99 + RegEx *x = PEL[id];
1.100 +
1.101 + if (x->Compile(regex, params[2]) == 0)
1.102 + {
1.103 + cell_t *eOff;
1.104 + pCtx->LocalToPhysAddr(params[5], &eOff);
1.105 + const char *err = x->mError;
1.106 + *eOff = x->mErrorOffset;
1.107 + pCtx->StringToLocal(params[3], params[4], err ? err:"unknown");
1.108 + return 0;
1.109 + }
1.110 +
1.111 + return g_pHandleSys->CreateHandle(g_RegexHandle, (void*)x, pCtx->GetIdentity(), myself->GetIdentity(), NULL);
1.112 +}
1.113 +
1.114 +
1.115 +static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
1.116 +{
1.117 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.118 + HandleError err;
1.119 + HandleSecurity sec;
1.120 + sec.pOwner = NULL;
1.121 + sec.pIdentity = myself->GetIdentity();
1.122 +
1.123 + RegEx *x;
1.124 +
1.125 + if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
1.126 + {
1.127 + return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
1.128 + }
1.129 +
1.130 + if (!x)
1.131 + {
1.132 + pCtx->ThrowNativeError("Regex data not found\n");
1.133 +
1.134 + return 0;
1.135 + }
1.136 +
1.137 + char *str;
1.138 + pCtx->LocalToString(params[2], &str);
1.139 +
1.140 + int e = x->Match(str);
1.141 +
1.142 + if (e == -1)
1.143 + {
1.144 + /* there was a match error. move on. */
1.145 + cell_t *res;
1.146 + pCtx->LocalToPhysAddr(params[3], &res);
1.147 + *res = x->mErrorOffset;
1.148 + /* only clear the match results, since the regex object
1.149 + may still be referenced later */
1.150 + x->ClearMatch();
1.151 +
1.152 + return -1;
1.153 + }
1.154 + else if (e == 0)
1.155 + {
1.156 + /* only clear the match results, since the regex object
1.157 + may still be referenced later */
1.158 + x->ClearMatch();
1.159 +
1.160 + return 0;
1.161 + }
1.162 + else
1.163 + {
1.164 + return x->mSubStrings;
1.165 + }
1.166 +}
1.167 +
1.168 +static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
1.169 +{
1.170 + Handle_t hndl=static_cast<Handle_t>(params[1]);
1.171 + HandleError err;
1.172 + HandleSecurity sec;
1.173 + sec.pOwner=NULL;
1.174 + sec.pIdentity=myself->GetIdentity();
1.175 +
1.176 + RegEx *x;
1.177 +
1.178 + if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
1.179 + {
1.180 + return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
1.181 + }
1.182 +
1.183 + if (!x)
1.184 + {
1.185 + pCtx->ThrowNativeError("Regex data not found\n");
1.186 + return 0;
1.187 + }
1.188 +
1.189 + static char buffer[4096];
1.190 + const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer));
1.191 +
1.192 + if(!ret)
1.193 + {
1.194 + return 0;
1.195 + }
1.196 +
1.197 + pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL);
1.198 +
1.199 + return 1;
1.200 +}
1.201 +
1.202 +void RegexHandler::OnHandleDestroy(HandleType_t type, void *object)
1.203 +{
1.204 + RegEx *x = (RegEx *)object;
1.205 +
1.206 + x->Clear();
1.207 +}
1.208 +
1.209 +const sp_nativeinfo_t regex_natives[] =
1.210 +{
1.211 + {"GetRegexSubString", GetRegexSubString},
1.212 + {"MatchRegex", MatchRegex},
1.213 + {"CompileRegex", CompileRegex},
1.214 + {NULL, NULL},
1.215 +};