// Token.cpp: implementation of the Token class.
//
//////////////////////////////////////////////////////////////////////
#include "Token.h"
#include <shlwapi.h>
#include "Helpers.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CToken::CToken(LPCTSTR mask, bool relaxed):
count(0),token(NULL), intval(0)
{
LPTSTR tmp;
int ln, i, last =0;
for (ln=0, i=0; mask[ln] !=0; ln++) if (mask[ln]=='*') i++;
token = new TOKEN[i+1];
for (i=0; i<= ln; i++) {
if (mask[i]=='*' || mask[i]==0) {
if ((i-last)>0) {
token[count].disp = (last>0)?( (mask[i]==0)? TOKEN::END: TOKEN::ENTIRE ): TOKEN::START;
token[count].len = i-last;
token[count].ptr = new TCHAR[i-last+1];
tmp = StrNCopy(token[count].ptr, mask+last, i-last-1);
*tmp=0;
count++;
}
last = i+1;
}
}
if (count==1 && relaxed) token[0].disp = TOKEN::ENTIRE;
if (count>0) intval = StrToInt(token[0].ptr);
}
CToken::~CToken()
{
while(count--) delete token[count].ptr;
delete token;
}
bool CToken::IsMatch(LPCTSTR string)
{
//if (this==NULL) return true;
unsigned ln = StrLen(string);
int i, res = 0;
LPCTSTR end=string+ln;
for (i = 0; i< count; i++) {
if (*string==0) return false;
if (token[i].disp==TOKEN::END) {
res = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
end-token[i].len, token[i].len,
token[i].ptr, token[i].len);
if (res!=CSTR_EQUAL) return false;
} else {
while (*string) {
res = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
string, token[i].len,
token[i].ptr, token[i].len);
if (res==CSTR_EQUAL) {
string+=token[i].len;
break;
}
if (token[i].disp==TOKEN::START) return false;
if ((--ln) < token[i].len) return false;
string++;
}
}
}
return (*string)!=0 || (res==CSTR_EQUAL);//;
}
int CToken::IntCompare(int number)
{
return (number - intval);
}
/***************************************************************************************
*
*
***************************************************************************************/
CTokenFile::CTokenFile(LPCTSTR mask, DWORD minSizeMB, DWORD maxSizeMB):
CToken(mask)
{
minmax.min = minSizeMB;
minmax.max = maxSizeMB;
}
CTokenFile::~CTokenFile()
{
}
bool CTokenFile::IsMatch(WIN32_FIND_DATA &wfd)
{
if (*(long*)wfd.cFileName == 0x2e || *(long*)wfd.cFileName == 0x002e002e) return false;
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0 && !ValidSize(wfd)) return false;
// if (StrCmp(wfd.cFileName,TEXT("CRUSH"))==0) {
// wfd.dwFileAttributes = wfd.dwFileAttributes;
// }
return CToken::IsMatch(wfd.cFileName);
}
bool CTokenFile::ValidSize(WIN32_FIND_DATA &wfd)
{
if ((minmax.min | minmax.max)==0) return true;
ULARGE_INTEGER ul;
ul.LowPart = wfd.nFileSizeLow;
ul.HighPart = wfd.nFileSizeHigh;
ul.QuadPart >>= 10;
if (minmax.min && minmax.min*1024 > ul.LowPart) return false;
if (minmax.max && minmax.max*1024 < ul.LowPart) return false;
//if ()) {
return true;
}