1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/regex/CRegEx.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,155 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod Regular Expressions Extension
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "pcre.h"
1.36 +#include "CRegEx.h"
1.37 +#include <sh_string.h>
1.38 +#include "extension.h"
1.39 +
1.40 +RegEx::RegEx()
1.41 +{
1.42 + mErrorOffset = 0;
1.43 + mError = NULL;
1.44 + re = NULL;
1.45 + mFree = true;
1.46 + subject = NULL;
1.47 + mSubStrings = 0;
1.48 +}
1.49 +
1.50 +void RegEx::Clear ()
1.51 +{
1.52 + mErrorOffset = 0;
1.53 + mError = NULL;
1.54 + if (re)
1.55 + pcre_free(re);
1.56 + re = NULL;
1.57 + mFree = true;
1.58 + if (subject)
1.59 + delete [] subject;
1.60 + subject = NULL;
1.61 + mSubStrings = 0;
1.62 +}
1.63 +
1.64 +RegEx::~RegEx()
1.65 +{
1.66 + Clear();
1.67 +}
1.68 +
1.69 +bool RegEx::isFree(bool set, bool val)
1.70 +{
1.71 + if (set)
1.72 + {
1.73 + mFree = val;
1.74 + return true;
1.75 + } else {
1.76 + return mFree;
1.77 + }
1.78 +}
1.79 +
1.80 +int RegEx::Compile(const char *pattern, int iFlags)
1.81 +{
1.82 + if (!mFree)
1.83 + Clear();
1.84 +
1.85 + re = pcre_compile(pattern, iFlags, &mError, &mErrorOffset, NULL);
1.86 +
1.87 + if (re == NULL)
1.88 + {
1.89 + return 0;
1.90 + }
1.91 +
1.92 + mFree = false;
1.93 +
1.94 + return 1;
1.95 +}
1.96 +
1.97 +int RegEx::Match(const char *str)
1.98 +{
1.99 + int rc = 0;
1.100 +
1.101 + if (mFree || re == NULL)
1.102 + return -1;
1.103 +
1.104 + this->ClearMatch();
1.105 +
1.106 + //save str
1.107 + subject = new char[strlen(str)+1];
1.108 + strcpy(subject, str);
1.109 +
1.110 + rc = pcre_exec(re, NULL, subject, (int)strlen(subject), 0, 0, ovector, 30);
1.111 +
1.112 + if (rc < 0)
1.113 + {
1.114 + if (rc == PCRE_ERROR_NOMATCH)
1.115 + {
1.116 + return 0;
1.117 + } else {
1.118 + mErrorOffset = rc;
1.119 + return -1;
1.120 + }
1.121 + }
1.122 +
1.123 + mSubStrings = rc;
1.124 +
1.125 + return 1;
1.126 +}
1.127 +void RegEx::ClearMatch()
1.128 +{
1.129 + // Clears match results
1.130 + mErrorOffset = 0;
1.131 + mError = NULL;
1.132 + if (subject)
1.133 + delete [] subject;
1.134 + subject = NULL;
1.135 + mSubStrings = 0;
1.136 +}
1.137 +
1.138 +const char *RegEx::GetSubstring(int s, char buffer[], int max)
1.139 +{
1.140 + int i = 0;
1.141 + if (s >= mSubStrings || s < 0)
1.142 + return NULL;
1.143 +
1.144 + char *substr_a = subject + ovector[2*s];
1.145 + int substr_l = ovector[2*s+1] - ovector[2*s];
1.146 +
1.147 + for (i = 0; i<substr_l; i++)
1.148 + {
1.149 + if (i >= max)
1.150 + break;
1.151 + buffer[i] = substr_a[i];
1.152 + }
1.153 +
1.154 + buffer[i] = '\0';
1.155 +
1.156 + return buffer;
1.157 +}
1.158 +