| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// BRegexp.dllのラッパークラス |
| 3 |
//--------------------------------------------------------------------------- |
| 4 |
|
| 5 |
#pragma hdrstop |
| 6 |
#include "KRegexp.h" |
| 7 |
|
| 8 |
//--------------------------------------------------------------------------- |
| 9 |
#pragma package(smart_init) |
| 10 |
|
| 11 |
//--------------------------------------------------------------------------- |
| 12 |
// Staticメンバの宣言 |
| 13 |
//--------------------------------------------------------------------------- |
| 14 |
|
| 15 |
TDll *KRegexp::hBRegexp = NULL; |
| 16 |
BREGEXPVERSION KRegexp::GetVersion = NULL; |
| 17 |
BREGFREE KRegexp::BRegFree = NULL; |
| 18 |
BMATCH KRegexp::BMatch = NULL; |
| 19 |
|
| 20 |
//AnsiString KRegexp::Pattern[MAX_PATTERN]; |
| 21 |
AnsiString *KRegexp::Pattern = NULL; |
| 22 |
|
| 23 |
//--------------------------------------------------------------------------- |
| 24 |
/** |
| 25 |
* bregexp.dll の 初期化 |
| 26 |
* 最初に呼ぶ |
| 27 |
*/ |
| 28 |
void |
| 29 |
KRegexp::Init(void) { |
| 30 |
hBRegexp = new TDll("bregexp.dll"); |
| 31 |
if(hBRegexp == NULL) { |
| 32 |
ShowMessage("Failed to load BREGEXP.DLL"); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
GetVersion = (BREGEXPVERSION)hBRegexp->GetProcAddress("BRegexpVersion"); |
| 37 |
if(GetVersion == NULL) { |
| 38 |
ShowMessage("Failed to load BREGEXP.DLL"); |
| 39 |
Free(); |
| 40 |
return; |
| 41 |
} |
| 42 |
BMatch = (BMATCH)hBRegexp->GetProcAddress("BMatch"); |
| 43 |
if(BMatch == NULL) { |
| 44 |
ShowMessage("Failed to load proc."); |
| 45 |
Free(); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
BRegFree = (BREGFREE)hBRegexp->GetProcAddress("BRegfree"); |
| 50 |
if(BRegFree == NULL) { |
| 51 |
ShowMessage("Fail to load proc."); |
| 52 |
Free(); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
Pattern = new AnsiString[MAX_PATTERN]; |
| 57 |
} |
| 58 |
//--------------------------------------------------------------------------- |
| 59 |
void |
| 60 |
KRegexp::Free(void) { |
| 61 |
if(hBRegexp) { |
| 62 |
delete hBRegexp; |
| 63 |
} |
| 64 |
hBRegexp = NULL; |
| 65 |
if(Pattern) { |
| 66 |
delete Pattern; |
| 67 |
} |
| 68 |
Pattern = NULL; |
| 69 |
} |
| 70 |
//--------------------------------------------------------------------------- |
| 71 |
/** |
| 72 |
* BMatchのフロントエンド |
| 73 |
* マッチした文字列を知りたい場合に呼ぶ |
| 74 |
* 返り値:マッチしたら真 マッチしなければ偽 |
| 75 |
*/ |
| 76 |
//--------------------------------------------------------------------------- |
| 77 |
bool |
| 78 |
KRegexp::GetMatchStr(AnsiString asTarget, AnsiString asRegexp) { |
| 79 |
|
| 80 |
if(BMatch==NULL) |
| 81 |
return false; |
| 82 |
|
| 83 |
for(int i=0;i<MAX_PATTERN;i++) { |
| 84 |
Pattern[i] = ""; |
| 85 |
} |
| 86 |
|
| 87 |
char msg[256]; |
| 88 |
BREGEXP *rxp = NULL; |
| 89 |
asRegexp = "m/"+ asRegexp + "/k"; |
| 90 |
char *szTarget = asTarget.c_str(); |
| 91 |
int matched = BMatch(asRegexp.c_str(),szTarget, szTarget+strlen(szTarget),&rxp,msg); |
| 92 |
if(matched) { |
| 93 |
for(int i=0;(i<rxp->nparens+1 && i <MAX_PATTERN);i++) { |
| 94 |
char str[256]; |
| 95 |
*(rxp->endp[i]) = NULL; |
| 96 |
strcpy(str,rxp->startp[i]); |
| 97 |
Pattern[i] = str; |
| 98 |
} |
| 99 |
} |
| 100 |
if(rxp!=NULL) |
| 101 |
BRegFree(rxp); |
| 102 |
return matched; |
| 103 |
} |
| 104 |
//--------------------------------------------------------------------------- |
| 105 |
|