MFC编写的小工具



// toolDlg.cpp : 实现文件
//


#include "stdafx.h"
#include "tool.h"
#include "toolDlg.h"
#include "afxdialogex.h"
#include <vector>
#include <string>


#include "Xml.h"
#include "XmlNode.h"
#include "XmlNodes.h"
#include "Xsl.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


using namespace std;
using namespace JWXml;


//using string::std;




// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();


// 对话框数据
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()




// CtoolDlg 对话框






CtoolDlg::CtoolDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CtoolDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CtoolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CtoolDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CtoolDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CtoolDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CtoolDlg::OnBnClickedButton3)
END_MESSAGE_MAP()




// CtoolDlg 消息处理程序


BOOL CtoolDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();


// 将“关于...”菜单项添加到系统菜单中。


// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标


// TODO: 在此添加额外的初始化代码


return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}


void CtoolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}


// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。


void CtoolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);


// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}


//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CtoolDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}






void CtoolDlg::sortXml(vector<CString> &vctDate)
{


CString strDate;


CString strSite= "";




int iSite = 8;
int iCtorl = 0;
GetDlgItemText(IDC_EDIT2,strSite);


if (strSite.GetLength()!=NULL)
{
iSite = atoi(strSite);
}


GetDlgItemText(IDC_EDIT3,strSite);


if (strSite.GetLength()!=NULL)
{
iCtorl = atoi(strSite);
}




CString strCtorl = "";


CXml xml;


xml.Create("AUTO");
CXmlNode nodea = xml.GetRoot();


CXmlNode commNode;


CXmlNode nodeTow;








CXmlNode cstNode = *(nodea.NewChild("CUSTOMUR"));


CXmlNode cstCmdNode = *(cstNode.NewChild("CMD"));


CString str = "a5 a5 00 2d 10 01 07 A1 20 02 81 03 01 04 00 00 07 e8 05 00 00 07 e0 06 FF FF FF FF 07 00 00 FF FF 08 00 00 00 00 09 00 00 0a 00 00 0b 00 00 0c E6 ??";


str.MakeUpper();


CString str2 = "a5 a5 00 2d 10 01 07 a1 20 02 81 03 01 04 00 00 00 00 05 00 00 07 e0 06 00 00 ff ff 07 00 00 07 e1 08 00 00 00 00 09 00 00 0a 00 00 0b 00 00 0c 83 ??";
str2.MakeUpper();
cstCmdNode.SetValue(str);


CXmlNode initNode = *(nodea.NewChild("INIT_IO"));
CXmlNode autosendNode = *(initNode.NewChild("AUTOSEND"));
autosendNode.SetValue("0");
CXmlNode cmdNode = *(initNode.NewChild("CMD"));
cmdNode.SetValue(str2);




commNode = *(nodea.NewChild("COMM"));


vector<CString>::iterator itLine = vctDate.begin();
for (unsigned int i =0;i<vctDate.size();i++)
{






strDate = itLine[i];




CString strCC = "";
strCC.Format(_T("%d"),iCtorl);


CString strItrm = "ITRM" + strCC;


nodeTow = *(commNode.NewChild(strItrm));


CXmlNode ENABLE = *(nodeTow.NewChild("ENABLE"));
ENABLE.SetValue("1");


//添加发命令;
CXmlNode ReceNode = *(nodeTow.NewChild("RECE"));


strDate.MakeUpper();
ReceNode.SetValue(strDate);




//添加收命令;


ReceNode = *(nodeTow.NewChild("SEND"));

CString strLeft = strDate.Mid(24,2);


int iiSite = strtol(strLeft,NULL,16);


iiSite = iiSite + iSite;


CString strSite = 0;


strSite.Format(_T("%x"),iiSite);




CString strLLeft = strDate.Left(24);
strLLeft = strLLeft + strSite;


CString strRight = strDate.Mid(30,2);
int i40 = strtol(strRight,NULL,16);


i40 = i40 + 0x40;
CString str40 = 0;
str40.Format(_T("%x"),i40);


str40 = "10 1b " + str40;


CString strRRight = strDate.Mid(32,6);


strRRight = str40 +strRRight+" 31 32 33 ??";


CString strNewDate = strLLeft + " " + strRRight;


strNewDate.MakeUpper();


ReceNode.SetValue(strNewDate);


ReceNode = *(nodeTow.NewChild("ACTIVE"));


iCtorl++;
CString strii = "";
strii.Format(_T("%d"),(iCtorl));


CString strACTIVE = "E" + strii;
ReceNode.SetValue(strACTIVE);







//添加流控帧;
strCC.Format(_T("%d"),iCtorl);


strItrm = "ITRM" + strCC;


nodeTow = *(commNode.NewChild(strItrm));


ENABLE = *(nodeTow.NewChild("ENABLE"));
ENABLE.SetValue("0");


ReceNode = *(nodeTow.NewChild("RECE"));




CString strReceLeft = strDate.Left(26);
strReceLeft = strReceLeft + " 30 00 ?? 00 00 00 00 00 ??";


strReceLeft.MakeUpper();
ReceNode.SetValue(strReceLeft);


//添加SEND多帧;
//21;
ReceNode = *(nodeTow.NewChild("SEND"));


//strLLeft = strLLeft + strSite;


strNewDate = strLLeft + " 21 31 32 33 34 35 36 37 ??";


strNewDate.MakeUpper();
ReceNode.SetValue(strNewDate);


//22;
ReceNode = *(nodeTow.NewChild("SEND"));


//strLLeft = strLLeft + strSite;


strNewDate = strLLeft + " 22 38 39 40 41 35 36 37 ??";


strNewDate.MakeUpper();
ReceNode.SetValue(strNewDate);


//23;
ReceNode = *(nodeTow.NewChild("SEND"));


//strLLeft = strLLeft + strSite;


strNewDate = strLLeft + " 23 31 32 33 34 35 36 37 ??";


strNewDate.MakeUpper();


ReceNode.SetValue(strNewDate);


ReceNode = *(nodeTow.NewChild("ACTIVE"));
strACTIVE = "D" + strCC;
ReceNode.SetValue(strACTIVE);


iCtorl++;







}


xml.SaveWithFormatted("rough.xml");




}


void CtoolDlg::sortOneXml(vector<CString> &vctDate)
{


CString strDate;


CString strSite= "";




int iSite = 8;
int iCtorl = 0;
GetDlgItemText(IDC_EDIT2,strSite);


if (strSite.GetLength()!=NULL)
{
iSite = atoi(strSite);
}


GetDlgItemText(IDC_EDIT3,strSite);


if (strSite.GetLength()!=NULL)
{
iCtorl = atoi(strSite);
}




CString strCtorl = "";


CXml xml;


xml.Create("AUTO");
CXmlNode nodea = xml.GetRoot();


CXmlNode commNode;


CXmlNode nodeTow;








CXmlNode cstNode = *(nodea.NewChild("CUSTOMUR"));


CXmlNode cstCmdNode = *(cstNode.NewChild("CMD"));


CString str = "a5 a5 00 2d 10 01 07 A1 20 02 81 03 10 04 00 00 07 e8 05 00 00 07 e0 06 ff ff ff ff 07 00 00 00 00 08 00 00 00 00 09 00 00 0a 00 00 0b 00 00 0c E6 ??";


str.MakeUpper();
CString str2 = "a5 a5 00 2d 10 01 07 a1 20 02 81 03 01 04 00 00 00 00 05 00 00 07 e0 06 00 00 ff ff 07 00 00 07 e1 08 00 00 00 00 09 00 00 0a 00 00 0b 00 00 0c 83 ??";
str2.MakeUpper();


cstCmdNode.SetValue(str);


CXmlNode initNode = *(nodea.NewChild("INIT_IO"));
CXmlNode autosendNode = *(initNode.NewChild("AUTOSEND"));
autosendNode.SetValue("0");
CXmlNode cmdNode = *(initNode.NewChild("CMD"));
cmdNode.SetValue(str2);


commNode = *(nodea.NewChild("COMM"));


vector<CString>::iterator itLine = vctDate.begin();
for (unsigned int i =0;i<vctDate.size();i++)
{






strDate = itLine[i];




CString strCC = "";
strCC.Format(_T("%d"),iCtorl);


CString strItrm = "ITRM" + strCC;


nodeTow = *(commNode.NewChild(strItrm));


CXmlNode ENABLE = *(nodeTow.NewChild("ENABLE"));
ENABLE.SetValue("1");


//添加发命令;
CXmlNode ReceNode = *(nodeTow.NewChild("RECE"));


strDate.MakeUpper();
ReceNode.SetValue(strDate);




//添加收命令;


ReceNode = *(nodeTow.NewChild("SEND"));






CString strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 21 8F";


CString strSonLeft = strDate.Mid(30,2);


strLeft = strLeft + " " +strSonLeft;


//叫上0x40;
CString strRight = strDate.Mid(33,2);


int i40 = strtol(strRight,NULL,16);


i40 = i40 + 0x40;
CString str40 = 0;
str40.Format(_T("%x"),i40);


CString strRRight = strDate.Mid(35,15);


CString strNewDate = strLeft +" " +str40 + strRRight + " ??";


strNewDate.MakeUpper();


ReceNode.SetValue(strNewDate);




//加一帧;


ReceNode = *(nodeTow.NewChild("SEND"));


strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 22";
strNewDate = strLeft + " 09 00 20 20 20 30 67 03 ??";
ReceNode.SetValue(strNewDate);


//在加一帧;


ReceNode = *(nodeTow.NewChild("SEND"));


strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 23";
strNewDate = strLeft + " 4C 55 20 20 41 00 00 00 ??";
ReceNode.SetValue(strNewDate);


iCtorl++;


}




xml.SaveWithFormatted("rough.xml");
}


void CtoolDlg::sortOneXmlTow(vector<CString> &vctDate)
{
CString strDate;


CString strSite= "";




int iSite = 8;
int iCtorl = 0;
GetDlgItemText(IDC_EDIT2,strSite);


if (strSite.GetLength()!=NULL)
{
iSite = atoi(strSite);
}


GetDlgItemText(IDC_EDIT3,strSite);


if (strSite.GetLength()!=NULL)
{
iCtorl = atoi(strSite);
}




CString strCtorl = "";


CXml xml;


xml.Create("AUTO");
CXmlNode nodea = xml.GetRoot();


CXmlNode commNode;


CXmlNode nodeTow;








CXmlNode cstNode = *(nodea.NewChild("CUSTOMUR"));


CXmlNode cstCmdNode = *(cstNode.NewChild("CMD"));


CString str = "a5 a5 00 2d 10 01 07 A1 20 02 81 03 10 04 00 00 07 e8 05 00 00 07 e0 06 ff ff ff ff 07 00 00 00 00 08 00 00 00 00 09 00 00 0a 00 00 0b 00 00 0c E6 ??";


str.MakeUpper();


cstCmdNode.SetValue(str);


CXmlNode initNode = *(nodea.NewChild("INIT_IO"));
CXmlNode autosendNode = *(initNode.NewChild("AUTOSEND"));
autosendNode.SetValue("0");
CXmlNode cmdNode = *(initNode.NewChild("CMD"));
cmdNode.SetValue(str);


commNode = *(nodea.NewChild("COMM"));


vector<CString>::iterator itLine = vctDate.begin();
for (unsigned int i =0;i<vctDate.size();i++)
{






strDate = itLine[i];




CString strCC = "";
strCC.Format(_T("%d"),iCtorl);


CString strItrm = "ITRM" + strCC;


nodeTow = *(commNode.NewChild(strItrm));


CXmlNode ENABLE = *(nodeTow.NewChild("ENABLE"));
ENABLE.SetValue("1");


//添加发命令;
CXmlNode ReceNode = *(nodeTow.NewChild("RECE"));


strDate.MakeUpper();
ReceNode.SetValue(strDate);




//添加收命令;


ReceNode = *(nodeTow.NewChild("SEND"));






CString strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 21 8F";


CString strSonLeft = strDate.Mid(30,2);


strLeft = strLeft + " " +strSonLeft;


//叫上0x40;
CString strRight = strDate.Mid(33,2);


int i40 = strtol(strRight,NULL,16);


i40 = i40 + 0x40;
CString str40 = 0;
str40.Format(_T("%x"),i40);


CString strRRight = strDate.Mid(35,15);


CString strNewDate = strLeft +" " +str40 + strRRight + " ??";


strNewDate.MakeUpper();


ReceNode.SetValue(strNewDate);




//加一帧;


ReceNode = *(nodeTow.NewChild("SEND"));


strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 22";
strNewDate = strLeft + " 09 00 20 20 20 30 67 03 ??";
ReceNode.SetValue(strNewDate);


//在加一帧;


ReceNode = *(nodeTow.NewChild("SEND"));


strLeft = strDate.Left(18);
strLeft = strLeft + "40 00 23";
strNewDate = strLeft + " 4C 55 20 20 41 00 00 00 ??";
ReceNode.SetValue(strNewDate);


iCtorl++;


}




xml.SaveWithFormatted("rough.xml");
}


bool IsEixst(vector<CString> &vct, CString str)
{
for (unsigned int i=0; i<vct.size(); i++)
{
if (str == vct[i])
{
return true;
}
}
return false;
}


void sortSCFile(CString strLine,vector<CString>&vctDate)
{




CString strRece ="rece";




if (strLine.Find(strRece) != -1)
{

if (strLine.Find("00 00 00 00 00 00 00") != -1)
{
return;
}
else
{
strLine = strLine.Left(strLine.Find("\t\t"));
strLine = strLine.Mid(strLine.Find("\t")+1);
if (IsEixst(vctDate,strLine))
{
return;
}
else
{
vctDate.push_back(strLine);
}
}


}



}


void TraverseDir(/*const*/ CString& strDir, vector<CString>& vecFiles)//strDir为传入的文件夹路径,vecFiles为扫描到的文件集
{
CFileFind ff;
//在路径后面添加\*.*后缀
if (strDir.Right(1) != "\\")
strDir += "\\";
strDir += "*.log";
BOOL ret = ff.FindFile(strDir);
while (ret)
{
ret = ff.FindNextFile();
if (ff.IsDirectory() && !ff.IsDots())
{
CString path = ff.GetFilePath();
TraverseDir(path, vecFiles);
}
else if (!ff.IsDirectory() && !ff.IsDots())
{
//CString name = ff.GetFileName();//获取带后缀的文件名
CString name = ff.GetFilePath();//获取文件路径
//CString name = ff.GetFileTitle();//获取不带后缀的文件名
vecFiles.push_back(name);
}


}
}


CString GetWorkDir()   
{    
char pFileName[MAX_PATH];   
int nPos = GetCurrentDirectory( MAX_PATH, pFileName);   


CString csFullPath(pFileName);    
if( nPos < 0 )   
return CString("");   
else   
return csFullPath;   
}  




void CtoolDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码


CString str = "";
GetDlgItemText(IDC_EDIT1,str);


CEdit *pE = (CEdit*)GetDlgItem(IDC_EDIT1);
int iLineCount = pE->GetLineCount();


//CString strLine = "";


TCHAR strLine[200] = {0};


vector<CString>vctDate;
vctDate.clear();


for (int i=0; i<iLineCount; i++)
{

pE->GetLine(i,strLine,200);




sortSCFile(strLine,vctDate); //把一些无用的数据清理掉;


}


sortXml(vctDate);






AfxMessageBox("Succeed",MB_OK);

}




void CtoolDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码




CString str = "";
GetDlgItemText(IDC_EDIT1,str);


CEdit *pE = (CEdit*)GetDlgItem(IDC_EDIT1);
int iLineCount = pE->GetLineCount();


//CString strLine = "";


TCHAR strLine[200] = {0};


vector<CString>vctDate;
vctDate.clear();


for (int i=0; i<iLineCount; i++)
{


pE->GetLine(i,strLine,200);




sortSCFile(strLine,vctDate); //把一些无用的数据清理掉;


}


sortOneXmlTow(vctDate);






AfxMessageBox("Succeed",MB_OK);


}




void CtoolDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码


CString str = "";
GetDlgItemText(IDC_EDIT1,str);


CEdit *pE = (CEdit*)GetDlgItem(IDC_EDIT1);
int iLineCount = pE->GetLineCount();


//CString strLine = "";


TCHAR strLine[200] = {0};


vector<CString>vctDate;
vctDate.clear();


for (int i=0; i<iLineCount; i++)
{


pE->GetLine(i,strLine,200);




sortSCFile(strLine,vctDate); //把一些无用的数据清理掉;


}
CStdioFile Wfile;
if (!Wfile.Open("rough.xml",CFile::modeCreate|CFile::modeWrite))
{
return;
}


vector<CString>::iterator itLine = vctDate.begin();
for (unsigned int i =0;i<vctDate.size();i++)
{
CString strDate = itLine[i];
strDate = strDate.Mid(30,8);


Wfile.WriteString(strDate);
Wfile.WriteString("\n");


}


Wfile.Close();


AfxMessageBox("Succeed",MB_OK);
}

猜你喜欢

转载自blog.csdn.net/liuyudong_/article/details/80741396
今日推荐