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