⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 democommon.cpp

📁 数据库管理软件,具有很多功能,应用DBS数据库
💻 CPP
字号:
// demoCommon.cpp : implementation file
//

#include "stdafx.h"
#include "demoCommon.h"


// show a item text in CListCtrl
void ShowItemText(CListCtrl& listCtr,int nItem, int nSubItem, CString value)
{
	LVITEM item;

	item.mask = LVIF_TEXT;
//	item.state = LVIS_SELECTED;
//	item.stateMask = LVIS_SELECTED;
//	item.lParam = 1;
//	item.iIndent = 0;
	item.iItem = nItem;
	item.iSubItem = nSubItem;
	item.pszText = (char*)(LPCTSTR)(value);
	item.cchTextMax = strlen(item.pszText);

	if( nSubItem==0 )
		listCtr.InsertItem(&item);
	else
		listCtr.SetItem(&item);

}


//make sure the value you input is correct at lest in form
BOOL CheckValue(CString val, valType type, int len)
{
     BOOL rtn;
     BOOL hasdot=FALSE; // float中是否已出现小数点
     int days[2][13]= {{0,31,28,31,30,31,30,31,31,30,31,30,31},
                       {0,31,29,31,30,31,30,31,31,30,31,30,31} };
     int i,m,d,y;
     char s[32],msg[64];


     strcpy(s,(LPCTSTR)val);

     switch(type)
     {
         case _INTEGER: // integer
                rtn=TRUE;
                for(i=0; i<val.GetLength(); i++) // 判断格式是否错误
                   if(s[i]<'0' || s[i]>'9')
                   {
                      rtn=FALSE;
                      strcpy(msg,"Please input an Integer");
                      break;
                   }
                break;
         case _DOUBLE: // double
                rtn=TRUE;
                for(i=0; i<val.GetLength(); i++) // 判断格式是否错误
                {
                   if(s[i]=='.')
                   {
                      if(!hasdot)
                        hasdot=TRUE;
                      else
                      {
                         rtn=FALSE;
                         strcpy(msg,"Please input a Double");
                         break;
                      }
                   }
                   else if(s[i]<'0' || s[i]>'9')
                   {
                      rtn=FALSE;
                      strcpy(msg,"Please input a Double");
                      break;
                   }
                }
                 break;
         case _DATE: // date
                if( val.GetLength()!=len
					|| s[2]!='/' || s[5]!='/' )//判断格式是否正确
                {
                   rtn=FALSE;
                   strcpy(msg,"the format of Data-datatype is \"mm/dd/yyyy\"");
                }
                else
                {
                   for(i=0; i<val.GetLength(); i++) // 其余是否数字
                      if( i!=2 && i!=5 && (s[i]<'0' || s[i]>'9') )
                      {
                          rtn=FALSE;
                          strcpy(msg,
                               "the format of Data-datatype is \"mm/dd/yyyy\"");
                          break;
                       }

                   if(i==len)
                   {
                       m = atoi(s);   // month
                       d = atoi(s+3); // day
                       y = atoi(s+6); // year
                       i = ( (y%100==0 && y%40==0) || (y%4==0) ); // 是否闰年
                       if( m>=1&&m<=12 && d>=1&&d<=days[i][m] && y>=0 )
                          rtn=TRUE;
                       else
                       {
                          rtn=FALSE;
                          strcpy(msg,"It's an inexistent date.");
                       }
                   }
                }

               break;
         case _TIME: // time
                if( val.GetLength()!=len ||
					s[2]!=':' || s[5]!=':' ) // 判断格式是否正确
                {
                   rtn=FALSE;
                   strcpy(msg,"the format of Time-datatype is \"hh:mm:ss\"");
                }
                else
                {
                   for(i=0; i<val.GetLength(); i++) // 其余是否数字
                      if(i!=2 && i!=5 && (s[i]<'0' || s[i]>'9') )
                      {
                         rtn=FALSE;
                         strcpy(msg,"the format of Time-datatype is \"hh:mm:ss\"");
                         break;
                       }
                   if(i<len)
                       break;

                   rtn=FALSE;
                   strcpy(msg,"It's an inexistent time.");
                   i=atoi(s);
                   if( i>=0 && i<=23 ) // hour
                   {
                      i=atoi(s+3);
                      if(i>=0&&i<=59) // minute
                      {
                        i=atoi(s+6);
                         if(i>=0&&i<=59) // second
                            rtn=TRUE;
                      }
                   }
                }
                break;
        case _STRING:  // string
                 if( val.GetLength()<=len ) // 判断长度是否正确
                     rtn=TRUE;
                 else
                 {
                     rtn=FALSE;
                     strcpy(msg,"the string is too long.");
                 }
                 break;
     }
     
      if(!rtn)
        AfxMessageBox(msg, MB_OK|MB_ICONERROR );

     return rtn;
}


// if the record meet with the condition
BOOL FindRecord(byte* buf, queryCondition& qcond)
{
	BYTE *p;
	//  char s[20];
	int n,m;
	int intv,intvv;
	double fv,fvv;
	short opp;
	char sv[32],sc[16],st[8];
	BOOL orFalse, andTrue; // or_false, and_true
	inst * pinst;


	// process where clause
	pinst = qcond.queryInst;
	p = buf;

	// 没有where子句
	if( qcond.ornumth==0 )
		orFalse = false;
	else
		orFalse = true;

	// 判断是否符合条件
	n = 0; // or count
	while( orFalse && n<qcond.ornumth )
	{
		andTrue = true;
		m = qcond.orIndex[n]; // or index

		// if AND-clause be true?
		while( andTrue && m<qcond.orIndex[n+1] )
		{
			if	   (strcmp(pinst->op,">")==0)	opp=1;
			else if(strcmp(pinst->op,">=")==0)	opp=2;
			else if(strcmp(pinst->op,"==")==0)	opp=3;
			else if(strcmp(pinst->op,"!=")==0)	opp=4;
			else if(strcmp(pinst->op,"<")==0)	opp=5;
			else								opp=6; // "<="

			p += pinst->offset;

			switch(pinst->fType)
			{
			case _INTEGER: // int
				intv = *(int*)p;
				intvv = atoi(pinst->val);
				switch(opp)
				{
				case 1: if(intv<=intvv) andTrue=false; break;
				case 2: if(intv< intvv) andTrue=false; break;
				case 3: if(intv!=intvv) andTrue=false; break;
				case 4: if(intv==intvv) andTrue=false; break;
				case 5: if(intv>=intvv) andTrue=false; break;
				case 6: if(intv> intvv) andTrue=false; break;
				}
				break;
				
			case _DOUBLE: // double
				fv = *(double*)p;
				fvv = atof(pinst->val);
				switch(opp)
				{
				case 1: if(fv<=fvv) andTrue=false; break;
				case 2: if(fv< fvv) andTrue=false; break;
				case 3: if(fv!=fvv) andTrue=false; break;
				case 4: if(fv==fvv) andTrue=false; break;
				case 5: if(fv>=fvv) andTrue=false; break;
				case 6: if(fv> fvv) andTrue=false; break;
				}
				break;

			case _DATE: // date
				memset(sv,0,32);
				memset(st,0,8);
				strncpy(sv,(char*)p+6,4);
				strncpy(st,(char*)p,6);
				strcat(sv,st);
				memset(sc,0,16);
				memset(st,0,8);
				strncpy(sc,(char*)pinst->val+6,4);
				strncpy(st,(char*)pinst->val,6);
				strcat(sc,st);
				switch(opp)
				{
				case 1: if( strncmp(sv,sc,10)<=0 ) andTrue=false; break;
				case 2: if( strncmp(sv,sc,10)< 0 ) andTrue=false; break;
				case 3: if( strncmp(sv,sc,10)!=0 ) andTrue=false; break;
				case 4: if( strncmp(sv,sc,10)==0 ) andTrue=false; break;
				case 5: if( strncmp(sv,sc,10)>=0 ) andTrue=false; break;
				case 6: if( strncmp(sv,sc,10)> 0 ) andTrue=false; break;
				}
				break;

			case _TIME: // time
				memset(sv,0,32);
				strncpy(sv,(char*)p,8);
				 switch(opp)
				 {
				 case 1: if( strncmp(sv,pinst->val,8)<=0 ) andTrue=false; break;
				 case 2: if( strncmp(sv,pinst->val,8)< 0 ) andTrue=false; break;
				 case 3: if( strncmp(sv,pinst->val,8)!=0 ) andTrue=false; break;
				 case 4: if( strncmp(sv,pinst->val,8)==0 ) andTrue=false; break;
				 case 5: if( strncmp(sv,pinst->val,8)>=0 ) andTrue=false; break;
				 case 6: if( strncmp(sv,pinst->val,8)> 0 ) andTrue=false; break;
				}
				break;

			case _STRING: // string
				memset(sv,0,32);
				strncpy(sv,(char*)p,pinst->fLen);
				switch(opp)
				{
				case 1: if( strcmp(sv,pinst->val)<=0 ) andTrue=false; break;
				case 2: if( strcmp(sv,pinst->val)< 0 ) andTrue=false; break;
				case 3: if( strcmp(sv,pinst->val)!=0 ) andTrue=false; break;
				case 4: if( strcmp(sv,pinst->val)==0 ) andTrue=false; break;
				case 5: if( strcmp(sv,pinst->val)>=0 ) andTrue=false; break;
				case 6: if( strcmp(sv,pinst->val)> 0 ) andTrue=false; break;
				}
				break;
			} // FType switch end

			pinst++;
			m++; // next and-condition
			p=buf; 
		} // end of inner_while(at && m<pParent->orIndex[nn+1])

		if( andTrue && m==qcond.orIndex[n+1] ) // one 'and' clause is true
			orFalse=false;
		else // next or-clause
			pinst = qcond.queryInst + qcond.orIndex[++n] ;

	}// end of outer_while(orFalse && n>0)


	if( !orFalse ) // this record is selected
		return TRUE;
	else
		return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -