📄 guest_code.cpp
字号:
#include "stdafx.h"
#include "guest_code.h"
#include "pctohandset.h"
#include "HD_DT_wl.h"
#include "Shlwapi.h"
unsigned char g_char_flag = '|';
/*************************************************************************
Function : SaveToFile
Description: Save the buff data to file
Input : opr_buff -- buff to be written to file
BufLen -- buffer length to be written
recsize -- the length of every record in byte
fsid -- table id
save_fn -- file name to save
Output : None
Return : FAILURE or SUCCESSFUL
*************************************************************************/
short SaveToFile(unsigned char *opr_buff,unsigned long BufLen ,int rec_size,int fsid,char *save_fn)
{
char strTmp[1000],strTmp1[100];
int count = BufLen/rec_size;
CFile dspFile;
unsigned char buf_tmp[100][100];
int i,j,m,pos_rec;
long iTmp,byte_power;
int num_fields;
unsigned char value_byte;
int type;
if(BufLen<=0) return FAILURE;
if(!dspFile.Open(save_fn,CFile::modeWrite|CFile::modeRead))
{
if(!dspFile.Open(save_fn,CFile::modeCreate|CFile::modeWrite|CFile::modeRead))
{
AfxMessageBox("不能打开文件!");
return FAILURE;
}
}
dspFile.SeekToEnd();
if(fsid<0 || fsid>MAX_NUM_OF_TABLE-1) return FALSE;
{
memset(strTmp,0,sizeof(strTmp));
for(i=0;i<g_array_table_struct[fsid].fields_num;i++)
{
strcat(strTmp,g_array_table_struct[fsid].str_fields_names[i]);
sprintf((char*)strTmp1,"%c",g_char_flag);
strcat(strTmp,(char*)strTmp1);
}
strcat(strTmp," \r\n");
dspFile.Write(strTmp,strlen(strTmp));
num_fields = g_array_table_struct[fsid].fields_num;
for(i=0;i<count;i++)
{
for(j=0;j<30;j++) //清楚buffer空间
{
memset(buf_tmp[j],0,100);
}
for(j=0,pos_rec=0,iTmp=0;j<num_fields;j++)
{
type = g_array_table_struct[fsid].fields_types[j];
if(type == 0) //字符类型
memcpy(buf_tmp[j],opr_buff+i*rec_size+pos_rec,g_array_table_struct[fsid].fields_lens[j]);
else if(type==1){ //十进制数字类型
for(m=0,byte_power=1,iTmp=0;m<g_array_table_struct[fsid].fields_lens[j];m++) //求数值的大小
{
value_byte = *(opr_buff+i*rec_size+pos_rec+g_array_table_struct[fsid].fields_lens[j]-1-m);
iTmp += value_byte*byte_power;
byte_power *= 256; //调整自己加权值
}
sprintf((char*)buf_tmp[j],"%d",iTmp); //转换成数字
}else if(type==2){ //十六进制类型
for(m=0;m<g_array_table_struct[fsid].fields_lens[j];m++) //求数值的大小
{
sprintf((char*)strTmp,"%02x",*(opr_buff+i*rec_size+pos_rec+m));
strcat((char*)buf_tmp[j],strTmp);
}
}else if(type==3){ //BCD码
for(m=0;m<g_array_table_struct[fsid].fields_lens[j];m++) //求数值的大小
{
sprintf((char*)strTmp,"%d%d",*(opr_buff+i*rec_size+pos_rec+m)>>4,*(opr_buff+i*rec_size+pos_rec+m)&0x0f);
strcat((char*)buf_tmp[j],strTmp);
}
}
pos_rec += g_array_table_struct[fsid].fields_lens[j]; //调整记录中的位置
}
memset(strTmp,0,sizeof(strTmp)); //写入文件
for(j=0;j<g_array_table_struct[fsid].fields_num;j++)
{
strcat(strTmp,(char*)buf_tmp[j]);
sprintf((char*)strTmp1,"%c",g_char_flag);
strcat(strTmp,(char*)strTmp1);
}
strcat(strTmp,"\r\n");
dspFile.Write(strTmp,strlen(strTmp));
}
}
dspFile.Close();
TRACE("File Save End.\r\n");
return SUCCESSFUL;
}
CString remove_space(CString str)
{
CString ret_str,ret_str1;
int i,len;
int pos;
len = str.GetLength();
for(i=0,pos=0;i<len;i++)
{
if(str.GetAt(i)!=' ')
{
ret_str +=str.GetAt(i) ;
}
}
//while(ret_str.Find("||",0)!=-1)
// ret_str.Replace("||","|0|");
return ret_str;
}
/*************************************************************************
Function : ReadFromFile
Description: read data from file
Input : recsize -- the length of every record in byte
fsid -- table id
filename -- file name to save
Output : opr_buff -- data buff pointer to write data readed
BufLen -- buffer length have written
Return : FAILURE or SUCCESSFUL
Note : this function pointer should be pay to ReadFromFile by set_file_funcs
*************************************************************************/
short ReadFromFile(unsigned char *opr_buff,unsigned long *BufLen,int rec_size,int fsid,char *filename)
{
CStdioFile file;
short ret = 0;
unsigned long records=0;
unsigned short chk_sum = 0;
unsigned char *pBuf = opr_buff;
int num_fields;
double iTmp;
int len;
int pos_rec,m,i,j;
int type;
CString strTmp,strRead;
char buffer1[100];
int len_num;
int nTmp;
if(fsid<0 || fsid>MAX_NUM_OF_TABLE-1)
{
strTmp.Format("文件号有错,文件号:%d!范围:0-%d",fsid,MAX_NUM_OF_TABLE-1);
AfxMessageBox(strTmp);
return FALSE;
}
num_fields = g_array_table_struct[fsid].fields_num;
if(FALSE==file.Open(filename, CFile::modeRead))
{
AfxMessageBox("文件不存在!");
return FAILURE;
}
records = 0;
while(file.ReadString(strRead))
{
//济南辰森特别代码
if(strcmp(strRead,"0|0|0|0")==0)
{
memset(opr_buff+records*rec_size,0,rec_size);
records++;
continue;
}
//济南辰森特别代码end
strRead.TrimRight(" ");
strRead.TrimLeft(" ");
if(strRead.GetLength() == 0) continue; //空行跳过
if(strRead.Left(2) == "//") continue; //注释行跳过
strRead = remove_space(strRead);
strRead.Replace(' ',' '); //将跳格键转换成空格
if(num_fields>1) {
len_num = strRead.Find(g_char_flag);
if(len_num<=0)
{
file.Close();
AfxMessageBox("数据中没有找到合法的分隔符,\r\n可能你的配置文档分隔符设置不对!");
return FAILURE;
}
}
for(j=0,pos_rec=0;j<num_fields;j++)
{
strRead.TrimRight(" "); //去头尾空格
strRead.TrimLeft(" ");
len_num = strRead.Find(g_char_flag);
if(len_num>0)
{
strTmp = strRead.Left(len_num);
strRead.Delete(0,len_num+1);
} else if(len_num==0){ //允许记录为空
strRead.Delete(0,1);
strTmp = "";
} else if(len_num==-1){
if(strRead.GetLength())
strTmp = strRead;
else
strTmp = "";
}
if(strTmp.GetLength())
{
type = g_array_table_struct[fsid].fields_types[j];
if(type == 0) //字符类型
{
memset(opr_buff+records*rec_size+pos_rec,0,g_array_table_struct[fsid].fields_lens[j]);//清除buffer
if(g_array_table_struct[fsid].fields_lens[j]>strlen(strTmp))
{
len = strlen(strTmp);
} else {
len = g_array_table_struct[fsid].fields_lens[j];
}
memcpy(opr_buff+records*rec_size+pos_rec,strTmp,len);
}else if(type==1){ //1:十进制数 目前只支持4个字节下的数字(0,1,2,3,4)
if(sscanf(strTmp,"%d",&iTmp)!=1) //读出数据
{
file.Close(); //数据格式有误退出,类型不对
strTmp.Format("十进制格式有错!-%d行",records+1);
AfxMessageBox(strTmp);
return FAILURE;
}
memset(buffer1,0,sizeof(buffer1));
memcpy((char*)buffer1,&iTmp,sizeof(iTmp)); //顺序
len_num = g_array_table_struct[fsid].fields_lens[j];
for(m=0;m<len_num;m++) *(opr_buff+records*rec_size+pos_rec+m) = buffer1[len_num-1-m]; //到序
}else if(type==2||type==3) {//2: 十六进制 长度没有限制
len_num = g_array_table_struct[fsid].fields_lens[j];
while(strTmp.GetLength()<len_num*2) strTmp+='0';
for(i=0;i<len_num;i++)
{
memcpy(buffer1,strTmp.Mid(i*2,2),2);
buffer1[3] = 0;
if(sscanf(buffer1,"%x",&nTmp)!=1)
{
file.Close(); //数据格式有误退出,类型不对
strTmp.Format("十六进制格式有误!-%d行",records+1);
AfxMessageBox(strTmp);
return FAILURE;
}
*(opr_buff+records*rec_size+pos_rec+i) = nTmp;
}
}
pos_rec += g_array_table_struct[fsid].fields_lens[j]; //调整记录中的位置
} else { //允许记录为空
memset(opr_buff+records*rec_size+pos_rec,0,g_array_table_struct[fsid].fields_lens[j]);//清除buffer
pos_rec += g_array_table_struct[fsid].fields_lens[j];
}
}
records++; //增加记录数
}
file.Close();
*BufLen = records*rec_size; //set param to thread
return SUCCESSFUL;
}
//读配置文件
int read_set_file()
{
CStdioFile file;
CString strRead,strTmp,strTmp1,strTmp2;
unsigned char filepath1[100],filepath2[100];
CFileException ex;
int tmp1,tmp2,tmp3,tmp4,i,j;
int m,len,step;
int iTmp[MAX_NUM_OF_TABLE];
int num_type,num_len;
if(!file.Open("filepath.txt",CFile::modeRead,&ex))
{
try{
file.Open("filepath.txt",CFile::modeCreate|CFile::modeWrite,&ex);
sprintf((char*)filepath1,"%s","0 1 .\r\n1 0 .\r\n1 0 .\r\n0 1 .\r\n0 1 .\r\n");
file.Write(filepath1,strlen((char*)filepath1));
}
catch(CFileException* e)
{
TCHAR szError[1024];
e->GetErrorMessage(szError, 1024);
AfxMessageBox(szError);
throw;
return FALSE;
}
}
for(i=0;i<MAX_NUM_OF_TABLE;i++)
{
memset(g_strUPLDFileName[i],0,sizeof(g_strUPLDFileName[i]));
memset(g_strDNLDFileName[i],0,sizeof(g_strDNLDFileName[i]));
}
//清楚设置buffer
for(i=0;i<MAX_NUM_OF_TABLE;i++) memset(&g_array_table_struct[i],0,sizeof(table_struct));
//读取数据库每一个表的名称,根据表名称的个数决定表的数量,最大个数为5
file.SeekToBegin();
while(1)
{
if(file.ReadString(strRead)==FALSE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -