extensions/regex/extension.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4188 89782d5fc91b
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Regular Expressions Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <sourcemod_version.h>
    33 #include "extension.h"
    34 #include <sh_string.h>
    35 #include "pcre.h"
    36 #include "CRegEx.h"
    37 using namespace SourceHook;
    38 
    39 /**
    40  * @file extension.cpp
    41  * @brief Implement Regex extension code here.
    42  */
    43 
    44 RegexExtension g_RegexExtension;		/**< Global singleton for extension's main interface */
    45 
    46 SMEXT_LINK(&g_RegexExtension);
    47 
    48 RegexHandler g_RegexHandler;
    49 HandleType_t g_RegexHandle=0;
    50 
    51 
    52 
    53 bool RegexExtension::SDK_OnLoad(char *error, size_t err_max, bool late)
    54 {
    55 	g_pShareSys->AddNatives(myself,regex_natives);
    56 	g_RegexHandle = g_pHandleSys->CreateType("Regex", &g_RegexHandler, 0, NULL, NULL, myself->GetIdentity(), NULL);
    57 	return true;
    58 }
    59 
    60 void RegexExtension::SDK_OnUnload()
    61 {
    62 	g_pHandleSys->RemoveType(g_RegexHandle, myself->GetIdentity());
    63 
    64 }
    65 
    66 const char *RegexExtension::GetExtensionVerString()
    67 {
    68 	return SOURCEMOD_VERSION;
    69 }
    70 
    71 const char *RegexExtension::GetExtensionDateString()
    72 {
    73 	return SOURCEMOD_BUILD_TIME;
    74 }
    75 
    76 static cell_t CompileRegex(IPluginContext *pCtx, const cell_t *params)
    77 {
    78 	char *regex;
    79 	pCtx->LocalToString(params[1], &regex);
    80 
    81 	RegEx *x = new RegEx();
    82 	
    83 	if (x->Compile(regex, params[2]) == 0)
    84 	{
    85 		cell_t *eOff;
    86 		pCtx->LocalToPhysAddr(params[5], &eOff);
    87 		const char *err = x->mError;
    88 		*eOff = x->mErrorOffset;
    89 		pCtx->StringToLocal(params[3], params[4], err ? err:"unknown");
    90 		return 0;
    91 	}
    92 
    93 	return g_pHandleSys->CreateHandle(g_RegexHandle, (void*)x, pCtx->GetIdentity(), myself->GetIdentity(), NULL);
    94 }
    95 
    96 
    97 static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
    98 {
    99 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   100 	HandleError err;
   101 	HandleSecurity sec;
   102 	sec.pOwner = NULL;
   103 	sec.pIdentity = myself->GetIdentity();
   104 
   105 	RegEx *x;
   106 
   107 	if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
   108 	{
   109 		return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
   110 	}
   111 
   112 	if (!x)
   113 	{
   114 		pCtx->ThrowNativeError("Regex data not found\n");
   115 
   116 		return 0;
   117 	}
   118 
   119 	char *str;
   120 	pCtx->LocalToString(params[2], &str);
   121 
   122 	int e = x->Match(str);
   123 
   124 	if (e == -1)
   125 	{
   126 		/* there was a match error.  move on. */
   127 		cell_t *res;
   128 		pCtx->LocalToPhysAddr(params[3], &res);
   129 		*res = x->mErrorOffset;
   130 		/* only clear the match results, since the regex object
   131 		   may still be referenced later */
   132 		x->ClearMatch();
   133 
   134 		return -1;
   135 	}
   136 	else if (e == 0) 
   137 	{
   138 		/* only clear the match results, since the regex object
   139 		   may still be referenced later */
   140 		x->ClearMatch();
   141 
   142 		return 0;
   143 	} 
   144 	else 
   145 	{
   146 		return x->mSubStrings;
   147 	}
   148 }
   149 
   150 static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
   151 {
   152 	Handle_t hndl=static_cast<Handle_t>(params[1]);
   153 	HandleError err;
   154 	HandleSecurity sec;
   155 	sec.pOwner=NULL;
   156 	sec.pIdentity=myself->GetIdentity();
   157 
   158 	RegEx *x;
   159 
   160 	if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
   161 	{
   162 		return pCtx->ThrowNativeError("Invalid file handle %x (error %d)", hndl, err);
   163 	}
   164 
   165 	if (!x)
   166 	{
   167 		pCtx->ThrowNativeError("Regex data not found\n");
   168 		return 0;
   169 	}
   170 
   171 	static char buffer[4096];
   172 	const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer));
   173 
   174 	if(!ret)
   175 	{
   176 		return 0;
   177 	}
   178 
   179 	pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL);
   180 
   181 	return 1;
   182 }
   183 
   184 void RegexHandler::OnHandleDestroy(HandleType_t type, void *object)
   185 {
   186 	RegEx *x = (RegEx *)object;
   187 
   188 	x->Clear();
   189 	delete x;
   190 }
   191 
   192 const sp_nativeinfo_t regex_natives[] = 
   193 {
   194 	{"GetRegexSubString",			GetRegexSubString},
   195 	{"MatchRegex",					MatchRegex},
   196 	{"CompileRegex",				CompileRegex},
   197 	{NULL,							NULL},
   198 };