文章标题

OperationFile.h
// COperationFile.h : header file
//

pragma once

include

include

include

using namespace std;

class COperationFile
{
// Construction
public:
COperationFile();
~COperationFile();

public:
void OnFileFilling(In CString strFilePath , In BOOL bIsAddress=TRUE);
/* 函数功能:删除重复的字符串(最简单的删除方法,有时间修改为二分法排序删除)
* vctStrSaveFileData:输入输出参数
*/
void deleteRepeatString(Inout vector& vctStrSaveFileData);
void SetFileReadWrite( In CString cstrFileFullPath );
string CString2string(CString &strSrc);
/* 函数功能:处理输入字符串,去掉与命令无关的信息
* strSrc:输入源字符串
* strReqHeader:“问”字符串
* bIsAns:是否为“回”字符串
* 返回值:处理后的字符串,使用vector的原因是一些回答命令会超过7个字符,这个时候需要把超过7个字符的命令拆分为不能大于7个字符的命令串
*/
vector DealString(In CString strSrc, In CString strReqHeader, BOOL bIsAns=FALSE);
};

OperationFile.cpp
// COperationFile.cpp : implementation file
//

include “stdafx.h”

include “OperationFile.h”

define MAX_STRING_COUNT 400

define Ans _T(“Ans:”)

define REQ _T(“Req:”)

COperationFile::COperationFile( )
{
}

COperationFile::~COperationFile( )
{
}

void COperationFile::OnFileFilling(In CString strFilePath, In BOOL bIsAddress/=TRUE/)
{
// TODO: Add your control notification handler code here
int nRet=0;
CString strReq=_T(“”);
vector vctDealString;
vector vctStrSaveFileData;

CStdioFile stdioFile;
CFileException e;
nRet = stdioFile.Open(strFilePath, CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyNone, &e );
if (!nRet)
{
    AfxMessageBox(_T("文件打开失败!!!"));
    return;
}
stdioFile.SeekToBegin();
CString strTemp;
while (stdioFile.ReadString(strTemp))
{
    if (-1 != strTemp.Find(_T("Req:")) )
    {
        vctDealString = DealString(strTemp,_T(""));
        strReq=vctDealString[0];
        continue;
    }
    if (-1 == strTemp.Find(_T("Ans:")) )
    {
        continue;
    }
    if (strReq.GetLength() > 0 )
    {
        vctDealString = DealString(strTemp, strReq.Mid(0, 14), TRUE);
        vctStrSaveFileData.push_back(strReq);
        for (int k=0; k < vctDealString.size(); k++)
        {
            vctStrSaveFileData.push_back(vctDealString[k]);
        }           
    }
}
stdioFile.Flush();
stdioFile.Close();

deleteRepeatString(vctStrSaveFileData);

strFilePath+=_T(".txt");
CStdioFile newFile;
newFile.Open(strFilePath,CFile::modeCreate | CFile::modeWrite , &e );
if (!nRet)
{
    AfxMessageBox(_T("文件创建失败!!!"));
    return;
}
for (int k = 0; k < vctStrSaveFileData.size(); k++)
{
    if ( !vctStrSaveFileData[k].IsEmpty() )
    {
        newFile.WriteString(vctStrSaveFileData[k]);
        newFile.WriteString(_T("\r\n"));

        if ( vctStrSaveFileData[k+1].Find(_T("Req:")) != -1 )
        {
            newFile.WriteString(_T("\r\n"));
        }
    }       
}
newFile.Flush();
newFile.Close();
AfxMessageBox(_T("文件处理完成"));

}

vector COperationFile::DealString(In CString strSrc, In CString strReqHeader, BOOL bIsAns/=FALSE/)
{
int nValidCmd;
vector vctStrSaveCmd;
CString strTemp=strSrc, strAnsHeader=_T(“”), strAnsBody=_T(“”);
CString x20x09=_T(” Receive”);
if (!bIsAns)
strTemp=strTemp.Mid(0, strTemp.Find(x20x09));

ifdef _DEBUG0

if (strTemp.Find(_T("19 02 8F")) != -1 )
{
    AfxMessageBox(_T("发现要找的字符串!!!"));
}

endif

strTemp.Delete(5, 5);
strAnsHeader=strTemp.Mid(0, 12);
strAnsHeader.Insert(5, _T("  "));
strAnsBody=strTemp.Mid(12, strTemp.GetLength());
nValidCmd = strAnsBody.GetLength()/3;
if (nValidCmd <= 7)
{
    for (int k = 0; k < 7- nValidCmd; k++)
    {
        strAnsBody+= _T("00 ");
    }
    strAnsHeader.AppendFormat(_T("08 %02d "), nValidCmd);
    vctStrSaveCmd.push_back( strAnsHeader +strAnsBody);
}
else
{
    int nRemainder=0,  nCounter=21, nQuotient;
    nRemainder = (nValidCmd-6)%7;
    nQuotient=(nValidCmd-6)/7;
    if (nRemainder>0)
    {
        nRemainder=7-nRemainder;
        nQuotient+=1;
    }

    for (int k = 0; k < nRemainder; k++)
    {
        strAnsBody+=_T("00 ");
    }

    strTemp.Format(_T("08 1%01X %02X "), nValidCmd&0xF00, nValidCmd&0xFF);
    vctStrSaveCmd.push_back( strAnsHeader+strTemp+strAnsBody.Mid(0, 18));
    vctStrSaveCmd.push_back(strReqHeader+_T("08 30 00 00 00 00 00 00 00"));

    strAnsHeader.SetAt(4, '1');
    strAnsHeader.SetAt(5, 'N');
    strAnsBody=strAnsBody.Mid(18);
    //21 22 23 24...
    for (int k=0; k < nQuotient; k++)
    {
        if ( k == nQuotient-1)
        {
            strAnsHeader.SetAt(4, ' ');
            strAnsHeader.SetAt(5, ' ');
        }
        strTemp.Format(_T("08 %02d "), nCounter++);         
        vctStrSaveCmd.push_back(strAnsHeader+strTemp+strAnsBody.Mid(0, 21));
        strAnsBody=strAnsBody.Mid(21);
    }
}   
return vctStrSaveCmd;

}

void COperationFile::deleteRepeatString(vector& vctStrSaveFileData)
{
// map

ifdef _UNICODE

int nLength = strSrc.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP,0,strSrc,nLength,NULL,0,NULL,NULL);
char* VoicePath = new char[ nBytes + 1];
memset(VoicePath,0,nLength + 1);
WideCharToMultiByte(CP_ACP, 0, strSrc, nLength, VoicePath, nBytes, NULL, NULL); 
VoicePath[nBytes] = 0;
strTarge = VoicePath;

else

strTarge = strSrc.GetBuffer(0);

endif

return strTarge;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值