3 * =============================================================================
4 * SourceMod Regular Expressions Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "extension.h"
34 #include <sh_string.h>
37 using namespace SourceHook;
41 * @brief Implement Regex extension code here.
44 RegexExtension g_RegexExtension; /**< Global singleton for extension's main interface */
46 SMEXT_LINK(&g_RegexExtension);
48 RegexHandler g_RegexHandler;
49 HandleType_t g_RegexHandle=0;
53 bool RegexExtension::SDK_OnLoad(char *error, size_t err_max, bool late)
55 g_pShareSys->AddNatives(myself,regex_natives);
56 g_RegexHandle = g_pHandleSys->CreateType("Regex", &g_RegexHandler, 0, NULL, NULL, myself->GetIdentity(), NULL);
60 void RegexExtension::SDK_OnUnload()
62 g_pHandleSys->RemoveType(g_RegexHandle, myself->GetIdentity());
66 static cell_t CompileRegex(IPluginContext *pCtx, const cell_t *params)
69 pCtx->LocalToString(params[1], ®ex);
71 RegEx *x = new RegEx();
73 if (x->Compile(regex, params[2]) == 0)
76 pCtx->LocalToPhysAddr(params[5], &eOff);
77 const char *err = x->mError;
78 *eOff = x->mErrorOffset;
79 pCtx->StringToLocal(params[3], params[4], err ? err:"unknown");
83 return g_pHandleSys->CreateHandle(g_RegexHandle, (void*)x, pCtx->GetIdentity(), myself->GetIdentity(), NULL);
87 static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
89 Handle_t hndl = static_cast<Handle_t>(params[1]);
93 sec.pIdentity = myself->GetIdentity();
97 if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
99 return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
104 pCtx->ThrowNativeError("Regex data not found\n");
110 pCtx->LocalToString(params[2], &str);
112 int e = x->Match(str);
116 /* there was a match error. move on. */
118 pCtx->LocalToPhysAddr(params[3], &res);
119 *res = x->mErrorOffset;
120 /* only clear the match results, since the regex object
121 may still be referenced later */
128 /* only clear the match results, since the regex object
129 may still be referenced later */
136 return x->mSubStrings;
140 static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
142 Handle_t hndl=static_cast<Handle_t>(params[1]);
146 sec.pIdentity=myself->GetIdentity();
150 if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
152 return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
157 pCtx->ThrowNativeError("Regex data not found\n");
161 static char buffer[4096];
162 const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer));
169 pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL);
174 void RegexHandler::OnHandleDestroy(HandleType_t type, void *object)
176 RegEx *x = (RegEx *)object;
182 const sp_nativeinfo_t regex_natives[] =
184 {"GetRegexSubString", GetRegexSubString},
185 {"MatchRegex", MatchRegex},
186 {"CompileRegex", CompileRegex},