extensions/regex/CRegEx.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jun 14 09:32:50 2010 -0400 (2010-06-14)
changeset 3041 a0587dee87ae
parent 1990 de135d1f05b1
child 3833 7563b32966af
permissions -rw-r--r--
Fixed outputname null pointer crash in sdktools (bug 4422, r=fyren)
     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 "pcre.h"
    33 #include "CRegEx.h"
    34 #include <sh_string.h>
    35 #include "extension.h"
    36 
    37 RegEx::RegEx()
    38 {
    39 	mErrorOffset = 0;
    40 	mError = NULL;
    41 	re = NULL;
    42 	mFree = true;
    43 	subject = NULL;
    44 	mSubStrings = 0;
    45 }
    46 
    47 void RegEx::Clear ()
    48 {
    49 	mErrorOffset = 0;
    50 	mError = NULL;
    51 	if (re)
    52 		pcre_free(re);
    53 	re = NULL;
    54 	mFree = true;
    55 	if (subject)
    56 		delete [] subject;
    57 	subject = NULL;
    58 	mSubStrings = 0;
    59 }
    60 
    61 RegEx::~RegEx()
    62 {
    63 	Clear();
    64 }
    65 
    66 bool RegEx::isFree(bool set, bool val)
    67 {
    68 	if (set)
    69 	{
    70 		mFree = val;
    71 		return true;
    72 	} else {
    73 		return mFree;
    74 	}
    75 }
    76 
    77 int RegEx::Compile(const char *pattern, int iFlags)
    78 {
    79 	if (!mFree)
    80 		Clear();
    81 		
    82 	re = pcre_compile(pattern, iFlags, &mError, &mErrorOffset, NULL);
    83 
    84 	if (re == NULL)
    85 	{
    86 		return 0;
    87 	}
    88 
    89 	mFree = false;
    90 
    91 	return 1;
    92 }
    93 
    94 int RegEx::Match(const char *str)
    95 {
    96 	int rc = 0;
    97 
    98 	if (mFree || re == NULL)
    99 		return -1;
   100 		
   101 	this->ClearMatch();
   102 
   103 	//save str
   104 	subject = new char[strlen(str)+1];
   105 	strcpy(subject, str);
   106 
   107 	rc = pcre_exec(re, NULL, subject, (int)strlen(subject), 0, 0, ovector, 30);
   108 
   109 	if (rc < 0)
   110 	{
   111 		if (rc == PCRE_ERROR_NOMATCH)
   112 		{
   113 			return 0;
   114 		} else {
   115 			mErrorOffset = rc;
   116 			return -1;
   117 		}
   118 	}
   119 
   120 	mSubStrings = rc;
   121 
   122 	return 1;
   123 }
   124 void RegEx::ClearMatch()
   125 {
   126 	// Clears match results
   127 	mErrorOffset = 0;
   128 	mError = NULL;
   129 	if (subject)
   130 		delete [] subject;
   131 	subject = NULL;
   132 	mSubStrings = 0;
   133 }
   134 
   135 const char *RegEx::GetSubstring(int s, char buffer[], int max)
   136 {
   137 	int i = 0;
   138 	if (s >= mSubStrings || s < 0)
   139 		return NULL;
   140 
   141 	char *substr_a = subject + ovector[2*s];
   142 	int substr_l = ovector[2*s+1] - ovector[2*s];
   143 
   144 	for (i = 0; i<substr_l; i++)
   145 	{
   146 		if (i >= max)
   147 			break;
   148 		buffer[i] = substr_a[i];
   149 	}
   150 
   151 	buffer[i] = '\0';
   152 
   153 	return buffer;
   154 }
   155