一个C++调用lua文件操作的工具软件

8 篇文章 0 订阅

lua代码:


function WriteFengHaoFile(szDate,szSrc,szDst)

   local fileSrc = io.open(szSrc,"r")
   local fileDst = io.open(szDst,"w")

   if fileSrc ~= nil  then
      for line in fileSrc:lines() do

             local start,_ = string.find(line,szDate)
         if start ~= nil then
            fileDst:write(string.sub(line,1,10))
            fileDst:write("\r\n")
          end
       end
       fileSrc:close()
       fileDst:close()
   end

end



mfc代码:

#include "stdafx.h"
#include "ParseAccountFileByTime.h"
#include "ParseAccountFileByTimeDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

extern "C"
{
#include ".\\lua\\lua.h"
#include ".\\lua\\lauxlib.h"
#include ".\\lua\\lualib.h"
}



// CAboutDlg dialog used for App About

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

// Dialog Data
    enum { IDD = IDD_ABOUTBOX };

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

// Implementation
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()


// CParseAccountFileByTimeDlg dialog




CParseAccountFileByTimeDlg::CParseAccountFileByTimeDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CParseAccountFileByTimeDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_pSrc = _T("");
    m_pDst = _T("");
    m_pDate = _T("");
}

void CParseAccountFileByTimeDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_pSrc);
    DDV_MaxChars(pDX, m_pSrc, 200);
    DDX_Text(pDX, IDC_EDIT2, m_pDst);
    DDV_MaxChars(pDX, m_pDst, 200);
    DDX_Text(pDX, IDC_EDIT3, m_pDate);
    DDV_MaxChars(pDX, m_pDate, 50);
}

BEGIN_MESSAGE_MAP(CParseAccountFileByTimeDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, &CParseAccountFileByTimeDlg::OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, &CParseAccountFileByTimeDlg::OnBnClickedButton2)
    ON_BN_CLICKED(IDOK, &CParseAccountFileByTimeDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CParseAccountFileByTimeDlg message handlers

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

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    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);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CParseAccountFileByTimeDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

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

        // Center icon in client rectangle
        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;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CParseAccountFileByTimeDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


//获取源文件路径
void CParseAccountFileByTimeDlg::OnBnClickedButton1()
{

    char szCurrentFile[MAX_PATH];
    GetCurrentDirectory(MAX_PATH,szCurrentFile);

    CFileDialog dlg(TRUE,".txt",szCurrentFile, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR , "*.txt|*.*||");
    dlg.m_ofn.lpstrInitialDir = szCurrentFile;

    if(dlg.DoModal() == IDOK)
    {
        m_pSrc = dlg.GetPathName();
        UpdateData(FALSE);
    }

}

//设置生成文件路径
void CParseAccountFileByTimeDlg::OnBnClickedButton2()
{
    char szCurrentFile[MAX_PATH];
    GetCurrentDirectory(MAX_PATH,szCurrentFile);

    CFileDialog dlg(TRUE,".txt",szCurrentFile, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR , "*.txt|*.*||");
    dlg.m_ofn.lpstrInitialDir = szCurrentFile;

    if(dlg.DoModal() == IDOK)
    {
        m_pDst = dlg.GetPathName();
        UpdateData(FALSE);
    }
}




//调用lua函数,解析文件,生成新文件
void CParseAccountFileByTimeDlg::OnBnClickedOk()
{
    UpdateData(TRUE);
    char szErrMsg[MAX_PATH];
    
    lua_State *L =  luaL_newstate();
    luaL_openlibs(L);


    if(0 != luaL_dofile(L,"helloworld.lua"))
    {
        wsprintf(szErrMsg,"err:%s",lua_tostring(L,-1));
        AfxMessageBox(szErrMsg);
    }

 
    lua_getglobal(L,"WriteFengHaoFile");     //lua函数
    lua_pushstring(L,m_pDate.GetBuffer(1));  //参数1
    lua_pushstring(L,m_pSrc.GetBuffer(1));   //参数2
    lua_pushstring(L,m_pDst.GetBuffer(1));   //参数3

    if(lua_pcall(L,3,0,0) != 0)
    {
        wsprintf(szErrMsg,"err:%s",lua_tostring(L,-1));
        AfxMessageBox(szErrMsg);
    }


    AfxMessageBox("按时间抽取结束");

//    CDialogEx::OnOK();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值