extensions/regex/CRegEx.cpp
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1990 de135d1f05b1
child 3833 7563b32966af
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
ds@1990
     1
/**
ds@1990
     2
 * vim: set ts=4 :
ds@1990
     3
 * =============================================================================
ds@1990
     4
 * SourceMod Regular Expressions Extension
ds@1990
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1990
     6
 * =============================================================================
ds@1990
     7
 *
ds@1990
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1990
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1990
    10
 * Free Software Foundation.
ds@1990
    11
 * 
ds@1990
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1990
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1990
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1990
    15
 * details.
ds@1990
    16
 *
ds@1990
    17
 * You should have received a copy of the GNU General Public License along with
ds@1990
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1990
    19
 *
ds@1990
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1990
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1990
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1990
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1990
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1990
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1990
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1990
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1990
    28
 *
ds@1990
    29
 * Version: $Id$
ds@1990
    30
 */
ds@1990
    31
ds@1990
    32
#include "pcre.h"
ds@1990
    33
#include "CRegEx.h"
ds@1990
    34
#include <sh_string.h>
ds@1990
    35
#include "extension.h"
ds@1990
    36
ds@1990
    37
RegEx::RegEx()
ds@1990
    38
{
ds@1990
    39
	mErrorOffset = 0;
ds@1990
    40
	mError = NULL;
ds@1990
    41
	re = NULL;
ds@1990
    42
	mFree = true;
ds@1990
    43
	subject = NULL;
ds@1990
    44
	mSubStrings = 0;
ds@1990
    45
}
ds@1990
    46
ds@1990
    47
void RegEx::Clear ()
ds@1990
    48
{
ds@1990
    49
	mErrorOffset = 0;
ds@1990
    50
	mError = NULL;
ds@1990
    51
	if (re)
ds@1990
    52
		pcre_free(re);
ds@1990
    53
	re = NULL;
ds@1990
    54
	mFree = true;
ds@1990
    55
	if (subject)
ds@1990
    56
		delete [] subject;
ds@1990
    57
	subject = NULL;
ds@1990
    58
	mSubStrings = 0;
ds@1990
    59
}
ds@1990
    60
ds@1990
    61
RegEx::~RegEx()
ds@1990
    62
{
ds@1990
    63
	Clear();
ds@1990
    64
}
ds@1990
    65
ds@1990
    66
bool RegEx::isFree(bool set, bool val)
ds@1990
    67
{
ds@1990
    68
	if (set)
ds@1990
    69
	{
ds@1990
    70
		mFree = val;
ds@1990
    71
		return true;
ds@1990
    72
	} else {
ds@1990
    73
		return mFree;
ds@1990
    74
	}
ds@1990
    75
}
ds@1990
    76
ds@1990
    77
int RegEx::Compile(const char *pattern, int iFlags)
ds@1990
    78
{
ds@1990
    79
	if (!mFree)
ds@1990
    80
		Clear();
ds@1990
    81
		
ds@1990
    82
	re = pcre_compile(pattern, iFlags, &mError, &mErrorOffset, NULL);
ds@1990
    83
ds@1990
    84
	if (re == NULL)
ds@1990
    85
	{
ds@1990
    86
		return 0;
ds@1990
    87
	}
ds@1990
    88
ds@1990
    89
	mFree = false;
ds@1990
    90
ds@1990
    91
	return 1;
ds@1990
    92
}
ds@1990
    93
ds@1990
    94
int RegEx::Match(const char *str)
ds@1990
    95
{
ds@1990
    96
	int rc = 0;
ds@1990
    97
ds@1990
    98
	if (mFree || re == NULL)
ds@1990
    99
		return -1;
ds@1990
   100
		
ds@1990
   101
	this->ClearMatch();
ds@1990
   102
ds@1990
   103
	//save str
ds@1990
   104
	subject = new char[strlen(str)+1];
ds@1990
   105
	strcpy(subject, str);
ds@1990
   106
ds@1990
   107
	rc = pcre_exec(re, NULL, subject, (int)strlen(subject), 0, 0, ovector, 30);
ds@1990
   108
ds@1990
   109
	if (rc < 0)
ds@1990
   110
	{
ds@1990
   111
		if (rc == PCRE_ERROR_NOMATCH)
ds@1990
   112
		{
ds@1990
   113
			return 0;
ds@1990
   114
		} else {
ds@1990
   115
			mErrorOffset = rc;
ds@1990
   116
			return -1;
ds@1990
   117
		}
ds@1990
   118
	}
ds@1990
   119
ds@1990
   120
	mSubStrings = rc;
ds@1990
   121
ds@1990
   122
	return 1;
ds@1990
   123
}
ds@1990
   124
void RegEx::ClearMatch()
ds@1990
   125
{
ds@1990
   126
	// Clears match results
ds@1990
   127
	mErrorOffset = 0;
ds@1990
   128
	mError = NULL;
ds@1990
   129
	if (subject)
ds@1990
   130
		delete [] subject;
ds@1990
   131
	subject = NULL;
ds@1990
   132
	mSubStrings = 0;
ds@1990
   133
}
ds@1990
   134
ds@1990
   135
const char *RegEx::GetSubstring(int s, char buffer[], int max)
ds@1990
   136
{
ds@1990
   137
	int i = 0;
ds@1990
   138
	if (s >= mSubStrings || s < 0)
ds@1990
   139
		return NULL;
ds@1990
   140
ds@1990
   141
	char *substr_a = subject + ovector[2*s];
ds@1990
   142
	int substr_l = ovector[2*s+1] - ovector[2*s];
ds@1990
   143
ds@1990
   144
	for (i = 0; i<substr_l; i++)
ds@1990
   145
	{
ds@1990
   146
		if (i >= max)
ds@1990
   147
			break;
ds@1990
   148
		buffer[i] = substr_a[i];
ds@1990
   149
	}
ds@1990
   150
ds@1990
   151
	buffer[i] = '\0';
ds@1990
   152
ds@1990
   153
	return buffer;
ds@1990
   154
}
ds@1990
   155