?? regularopt.cpp
字號:
// RegularOpt.cpp: implementation of the CRegularOpt class.
//
//////////////////////////////////////////////////////////////////////
/*
* Generated by MyEclipse Struts
*
* written by Yang Huaisheng
* Homepage: http://codefan.spaces.live.com
* version 0.01
* create at 2008-04-30
*
* Distribute freely, except: don't remove my name from the source or
* documentation (don't take credit for my work), mark your changes (don't
* get me blamed for your possible bugs), don't alter or remove this
* notice.
* No warrantee of any kind, express or implied, is included with this
* software; use at your own risk, responsibility for damages (if any) to
* anyone resulting from the use of this software rests entirely with the
* user.
*
* Send bug reports, bug fixes, enhancements, requests, flames, etc. to
* codefan@hotmial.com
*
*/
#include "stdafx.h"
#include "RegularOpt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CString CRegularOpt::RectToString(const CRect & rect,bool isRelRow,bool isRelCol)
{
CString str;//("");
if (isRelRow || isRelCol){
CString sLeft,sTop;
if (isRelCol) sLeft="-"; else sLeft.Format("%d",rect.left);
if (isRelRow) sTop="-"; else sTop.Format("%d",rect.top);
str.Format("%s,%s,%d,%d",sLeft,sTop,rect.Width(),rect.Height());
}else
str.Format("%d,%d,%d,%d",rect.left,rect.top,rect.Width(),rect.Height());
return str;
}
CRect CRegularOpt::RectFrom( LPCTSTR szRect)
{
if(szRect == NULL) return CRect(0,0,0,0);
CRect rect ;
int sl = strlen(szRect);
char * szCopy = new char[sl+2];
CopyMemory(szCopy , szRect,sl);
szCopy[sl] = szCopy[sl+1] = 0;
char * szP = szCopy;
char * szT = szCopy;
char * szE = szCopy + sl;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.left = atoi(szP);
szP = szT;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.top = atoi(szP);
szP = szT; szT++;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.right = rect.left + atoi(szP);
rect.bottom = rect.top + atoi(szT);
delete [] szCopy;
return rect;
}
CString CRegularOpt::XectToString(const CRect & rect)
{
CString str;//("");
str.Format("%d,%d,%d,%d",rect.left,rect.top,rect.right,rect.bottom);
return str;
}
CRect CRegularOpt::XectFrom(LPCTSTR szRect)
{
if(szRect == NULL) return CRect(0,0,0,0);
CRect rect ;
int sl = strlen(szRect);
char * szCopy = new char[sl+2];
CopyMemory(szCopy , szRect,sl);
szCopy[sl] = szCopy[sl+1] = 0;
char * szP = szCopy;
char * szT = szCopy;
char * szE = szCopy + sl;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.left = atoi(szP);
szP = szT;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.top = atoi(szP);
szP = szT; szT++;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
rect.right = atoi(szP);
rect.bottom = atoi(szT);
delete [] szCopy;
return rect;
}
CString CRegularOpt::SizeToString(const CSize & size)
{
CString str;
str.Format("%d,%d",size.cx,size.cy);
return str;
}
CSize CRegularOpt::SizeFrom( LPCTSTR szSize)
{
if(szSize == NULL) return CSize(0,0);
CSize size;
int sl = strlen(szSize);
char * szCopy = new char[sl+2];
CopyMemory(szCopy , szSize,sl);
szCopy[sl] = szCopy[sl+1] = 0;
char * szT = szCopy;
char * szE = szCopy + sl;
while(szT < szE && (*szT != ',') ) szT++;
(*szT) = 0; szT++;
size.cx = atoi(szCopy);
size.cy = atoi(szT);
delete []szCopy;
return size;
}
CString CRegularOpt::PointToString(const CPoint & point)
{
CString str;
str.Format("%d,%d",point.x,point.y);
return str;
}
CPoint CRegularOpt::PointFrom(LPCTSTR szPoint)
{
CSize size = SizeFrom(szPoint);
return CPoint(size);
}
CString CRegularOpt::ColorToString(COLORREF colorref)
{
CString str;
str.Format("#%x",colorref);
return str;
}
COLORREF CRegularOpt::ColorFrom(LPCTSTR szColor)
{
CString str(szColor);
DWORD colorref = 0;
str.MakeLower();
int n = str.Find('#')+1;
int sl = str.GetLength();
if(sl>7) sl = 7;
int d;
for(;n<sl;n++){
if(str[n] >= '0' && str[n] <='9') d = int(str[n] -'0');
else if(str[n] >= 'a' && str[n] <='f') d = int(str[n] -'a') + 10;
else d = 0;
colorref = colorref * 16 + d;
}
return colorref;
}
CString CRegularOpt::AlignToString(BYTE align)
{
CString str;
BYTE halign = align & 0x0F;
switch(halign){
case CT_ALIGN_RIGHT:
str = "right";
break;
case CT_ALIGN_CENTER:
str = "center";
break;
case CT_ALIGN_FILL:
str = "fill";
break;
default :
str = "left";
break;
}
BYTE valign = (align & 0xF0) >> 4;
switch(valign){
case CT_ALIGN_BOTTOM:
str += " bottom";
break;
case CT_ALIGN_CENTER:
str += " center";
break;
case CT_ALIGN_FILL:
str += " fill";
break;
default :
str += " top";
break;
}
return str;
}
BYTE CRegularOpt::AlignFrom(LPCTSTR szAlign)
{
CString str(szAlign);
str.MakeLower();
str.TrimLeft();
str.TrimRight();
int n = str.Find(' ');
if( n == -1){
if(str.Compare("right") == 0) return CT_ALIGN_RIGHT;
if(str.Compare("center") == 0) return CT_ALIGN_CENTER;
if(str.Compare("fill") == 0) return CT_ALIGN_FILL;
return CT_ALIGN_LEFT;
}
BYTE align = 0;
CString str1 = str.Mid(0,n);
if(str1.Compare("right") == 0) align = CT_ALIGN_RIGHT;
else if(str1.Compare("center") == 0) align = CT_ALIGN_CENTER;
else if(str1.Compare("fill") == 0) align = CT_ALIGN_FILL;
else if(str1.Compare("bottom") == 0) align = CT_ALIGN_BOTTOM << 4;
str1 = str.Mid(n);
str1.TrimLeft();
if(str1.Compare("right") == 0) align = (align & 0xF0) | CT_ALIGN_RIGHT;
else if(str1.Compare("center") == 0) align = (align & 0x0F) | (CT_ALIGN_CENTER << 4);
else if(str1.Compare("fill") == 0) align = (align & 0x0F) | (CT_ALIGN_FILL << 4);
else if(str1.Compare("bottom") == 0) align = (align & 0x0F) | (CT_ALIGN_BOTTOM << 4);
return align;
}
#if 0
CString CRegularOpt::VariantToString(_variant_t tvalue)
{
CString res;
switch(tvalue.vt){
case VT_I2:
res.Format("%d",tvalue.iVal);
break;
case VT_I4:
res.Format("%d",tvalue.intVal);
break;
case VT_I1:
case VT_UI1:
res.Format("%c",tvalue.cVal);
break;
case VT_R4:
res.Format("%f",tvalue.fltVal);
break;
case VT_DATE: // date
{
COleDateTime otime(tvalue);
res = otime.Format();
}
break;
case VT_R8:
res.Format("%f",tvalue.dblVal);
break;
case VT_UI2:
case VT_UI4:
res.Format("%d",tvalue.uiVal);
break;
case VT_INT:
res.Format("%d ",tvalue.intVal);
break;
case VT_DECIMAL:
case VT_I8:
case VT_CY:
res.Format("%I64 ",tvalue.intVal);
break;
case VT_UI8:
res.Format("%I64 ",tvalue.uintVal);
break;
case VT_UINT:
res.Format("%d ",tvalue.uintVal);
break;
case VT_LPWSTR:
case VT_LPSTR:
case VT_BSTR:
res = LPCTSTR(tvalue.bstrVal);
break;
case VT_BOOL:
res = (tvalue.boolVal)?"true":"false";
break;
default :
res = "";
}
return res;
}
CString CRegularOpt::VariantToString(_variant_t tvalue)
{
return CString((LPCTSTR)(_bstr_t)tvalue);
}
//#else
#endif
bool CRegularOpt::InnerMatch(LPCTSTR szValue , LPCTSTR szTempl)
{
int nLV = strlen(szValue);
int nLT = strlen(szTempl);
if(nLV == 0){
for(int i=0;i<nLT;i++)
if(szTempl[i]!='*')
return false;
return true;
}
int i(0),j(0);
while(i<nLV && j<nLT){
if( (szTempl[j] != '*' && szValue[i] == szTempl[j]) ||
szTempl[j] == '?'){
i++;j++;
}else if(szTempl[j] == '*'){
if(szTempl[j+1] == '*'){
if (szValue[i] == '*'){
i++; j+=2;
}else return false;
} else {
j++;
while(j<nLT && (szTempl[j]=='?' )){
j++;
i++;
}
int nStart = j;
if(nStart == nLT) return true;
while(j<nLT && szTempl[j]!='?' && szTempl[j]!='*') j++;
int nSubStr = j-nStart;
for(int k=i;k<=nLV-nSubStr;k++){
bool bMatchSub = true;
for(int l=nStart;l<j;l++)
if(szValue[k+l-nStart] != szTempl[l]){
bMatchSub = false;
break;
}
if(bMatchSub && InnerMatch(szValue+k+nSubStr ,szTempl+j)) return true;
}
return false;
}
}else
return false;
}
return (i==nLV && j==nLT);
}
bool CRegularOpt::IsMatch(LPCTSTR szValue , LPCTSTR szTempl)
{// ?_ *% 是通配符
if( szValue == szTempl) return true;
if( szValue == NULL || szTempl == NULL) return false;
int nLV = strlen(szValue);
int nLT = strlen(szTempl);
if(nLV == 0 && nLT == 0) return true;
char * lpszValue = new char[nLV+1];
char * lpszTempl = new char[nLT+1];
if((nLV>=2) && ( (szValue[0] == '"' && szValue[nLV-1] == '"' ) ||
(szValue[0] == '\'' && szValue[nLV-1] == '\'' ))
){
if(nLV>2)
CopyMemory(lpszValue,szValue+1,nLV-2);
lpszValue[nLV-2] = 0;
}else{
CopyMemory(lpszValue,szValue,nLV);
lpszValue[nLV] = 0;
}
if((nLT>=2) && ( (szTempl[0] == '"' && szTempl[nLT-1] == '"' ) ||
(szTempl[0] == '\'' && szTempl[nLT-1] == '\'' ) )
){
if(nLT>2)
CopyMemory(lpszTempl,szTempl+1,nLT-2);
lpszTempl[nLT-2] = 0;
}else{
CopyMemory(lpszTempl,szTempl,nLT);
lpszTempl[nLT] = 0;
}
bool bMatch = InnerMatch(lpszValue,lpszTempl);
delete [] lpszValue;
delete [] lpszTempl;
return bMatch;
}
bool CRegularOpt::IsNumber(LPCTSTR szNum , int &s,int &d)
{
s=0;d=0;
int sl = strlen(szNum);
if (sl<1) return false;
int sp=0;
while(sp<sl && (szNum[sp]==' ' || szNum[sp]== 9)) sp++;
if(szNum[sp]=='-' || szNum[sp]=='+') sp++;
if( sp==sl || (sp+1==sl && szNum[sp]=='.')) return false;//
while(sp<sl){
if(szNum[sp]>='0' && szNum[sp]<= '9'){
sp++;s++;
continue;
}
if(szNum[sp] == '.') {
sp++;
break;
}
return false;
}
if ((sp==sl) && (sl>1) && (szNum[sp]!='.') && (szNum[0]=='0')) return false;;
while(sp<sl){
if(szNum[sp]>='0' && szNum[sp]<= '9'){
sp++;d++;
}else
return false;
}
return true;
}
bool CRegularOpt::IsNumber(LPCTSTR szNum )
{
int s =0, d=0;
return IsNumber(szNum,s,d);
}
bool CRegularOpt::IsNumberArray(LPCTSTR szNumArray,char splitChar)
{
CStringList ssRes;
int nSL = SplitString(szNumArray, ssRes,splitChar);
if(nSL<1) return false;
POSITION pPos = ssRes.GetHeadPosition();
while(pPos != NULL) {
if(! IsNumber(ssRes.GetNext(pPos)) )
return false;
}
return true;
}
bool CRegularOpt::IsTrue(LPCTSTR szCondition )
{
if(_stricmp(szCondition,"true")==0) return true;
if(! IsNumber(szCondition)) return false;
int bRes = int( atof(szCondition));
return bRes!=0;
}
char CRegularOpt::GetNumbtye(LPCTSTR szNum , int nBit)
{
int sl = strlen(szNum);
int nPos = 0;
while(nPos<sl && szNum[nPos] != '.' ) nPos ++ ;
if(nBit<0)
nPos = nPos - nBit;
else
nPos = nPos - nBit - 1;
if( nPos < 0 || nPos >= sl) return '0';
return szNum[nPos];
}
bool CRegularOpt::IsIp(LPCTSTR szIp)
{
int sl = strlen(szIp);
int sp=0;
char c[4];
while(sp<sl && (szIp[sp]==' ' || szIp[sp]== 9)) sp++;
for(int i=0; i<4; i++){
int s=0;
while(sp<sl ){
if(szIp[sp]>='0' && szIp[sp]<= '9'){
if(s<3) c[s] = szIp[sp];
sp++;s++;
continue;
}
if(szIp[sp] == '.'){
sp++;
break;
}
else
return false;
}
if(s>3) return false;
c[s]=0;
int n = atoi(c);
if(n>255) return false;
}
return true;
}
bool CRegularOpt::IsDatetime(LPCTSTR szTime ,CTime & t_time)
{
int sl = strlen(szTime);
int sp=0;
int s=0;
char c[5];
int y,m,d,h,min,sec;
while(sp<sl && (szTime[sp]==' ' || szTime[sp]== 9)) sp++;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<4) c[s] = szTime[sp];
sp++;s++;
continue;
}
//if(szTime[sp] == '-')
sp++;
break;
}//check year
c[4]=0;
y = atoi(c);
if(y < 1970 || y>2038) return false;
s=0;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<2) c[s] = szTime[sp];
sp++;s++;
continue;
}
//if(szTime[sp] == '-')
sp++;
break;
}//check month
if(s > 2) return false;
c[s]=0;
m = atoi(c);
if(m<1 || m >12) return false;
s=0;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<2) c[s] = szTime[sp];
sp++;s++;
continue;
}
//if(szTime[sp] == '-')
sp++;
break;
}//check day
if(s > 2) return false;
c[s]=0;
d = atoi(c);
if(d<1 || d >31) return false;
while(sp<sl && (szTime[sp]==' ' || szTime[sp]== 9)) sp++;
s=0;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<2) c[s] = szTime[sp];
sp++;s++;
continue;
}
//if(szTime[sp] == ':')
sp++;
break;
}//check hour
if(s > 2) return false;
c[s]=0;
h = atoi(c);
if(h<0 || h >24) return false;
s=0;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<2) c[s] = szTime[sp];
sp++;s++;
continue;
}
//if(szTime[sp] == ':')
sp++;
break;
}//check minute
if(s > 2) return false;
c[s]=0;
min = atoi(c);
if(min<0 || min >60) return false;
s=0;
while(sp<sl ){
if(szTime[sp]>='0' && szTime[sp]<= '9'){
if(s<2) c[s] = szTime[sp];
sp++;s++;
continue;
}
break;
}//check second
if(s > 2) return false;
c[s]=0;
sec = atoi(c);
if(sec<0 || sec >60) return false;
t_time = CTime(y,m,d,h,min,sec,-1);
return true;
}
CString CRegularOpt::TrimDateString(LPCTSTR szDateStr)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -