C++自定义脚本文件执行

15 篇文章 0 订阅
1 篇文章 0 订阅

GitHub - Ma-Yuyao/Lexical-Analyzer-Syntactic-Analyzer: Lexical-Analyzer-Syntactic-Analyzer By C++ 编译原理:C++实现的词法分析器和语法分析器
FunctionCall.h:
    
    #include <sstream>
    #include <string>
    #include <vector>
    // 函数调用
    class FunctionCall
    {
    public:
        FunctionCall();
        ~FunctionCall();
        std::string call(const std::string &line);

    private:
        void parse(const std::string &line);

    private:
        void call0();
        void call1();
        void call2();
        void call3();
        void call4();
        void call5();
    private:
        std::string              funcname;
        std::vector<std::string> args;
        std::string result;
    };

FunctionCall.cpp:
    #include "FunctionCall.h"
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <vector>

    FunctionCall::FunctionCall() {}
    FunctionCall::~FunctionCall() {}

    void  FunctionCall::parse(const std::string &line)
    {
        std::istringstream iss(line);
        std::string        tmp;
        while (std::getline(iss, tmp, ',')) {
            if (funcname.empty())
                funcname = tmp;
            else
                args.emplace_back(tmp);
        }
    }

    // 执行外部脚本
    std::string FunctionCall::call(const std::string &line)
    {
        parse(line);
        if(funcname.empty())
            return "invalid function";

        switch (args.size()) {
        case 0: {
            call0();
        } break;
        case 1: {
            call1();
        } break;
        case 2: {
            call2();
        } break;
        case 3: {
            call3();
        } break;
        case 4: {
            call4();
        } break;
        case 5: {
            call5();
        } break;
        default: {
        } break;
        }
        return result;
    }

    void FunctionCall::call0()
    {
        if("test" == funcname)
            std::cout << "run test" << std::endl;
    }

    void FunctionCall::call1() {
    }

    void FunctionCall::call2() {
    }

    void FunctionCall::call3() {
    }

    void FunctionCall::call4() {
    }

    void FunctionCall::call5() {
    }

LocalScriptFile.h:

    #include <string>

    class LocalScriptFile
    {
    public:
        bool doFile(const char *path);

    private:
        bool jumpComment(const std::string &line);
        void doScript(const std::string &line);

    private:
        bool m_isComment = false;
    };

LocalScriptFile.cpp:

    #include "LocalScriptFile.h"

    #include "FunctionCall.h"
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <vector>

    bool LocalScriptFile::jumpComment(const std::string &line) {
        if(m_isComment) {
            if (-1 != line.find("**/"))
                m_isComment = false;
            return true;
        } else{
            if (-1 != line.find("/**"))
                m_isComment = true;
            return m_isComment;
        }
    }

    bool LocalScriptFile::doFile(const char *path)
    {
        if (-1 != std::string(path).rfind(".lua")) {
            std::ifstream file(path);
            if (!file.is_open()) {
                std::cerr << "Failed to open the file." << std::endl;
                return false;
            }

            bool        isComment = false;
            std::string line;
            while (std::getline(file, line)) {
                //            std::cout << "Read line: " << line << std::endl;
                if(jumpComment(line))
                    continue;
                doScript(line);
            }
            file.close();
            return true;
        }
        return false;
    }

    // 执行外部脚本
    void LocalScriptFile::doScript(const std::string &line)
    {
        FunctionCall call;
        std::stringstream logStream;
        logStream << call.call(line) << " = " << line;
    }

test.lua
    /*
        测试脚本文件
    */
    test
    
使用示例:
    void main() {
        PxxLocalScriptFile scriptFile;
        if(scriptFile.doFile("test.lua"))
            std::cout << "success" << std::endl;
    }


创作不易,小小的支持一下吧!

  • 38
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现C++程序的Windows Installer自定义安装路径,你需要在创建安装程序的过程中进行以下步骤: 1. 打开Visual Studio,创建一个新的安装程序项目(InstallShield或WiX)。 2. 在项目中添加一个自定义安装路径的对话框。 3. 在对话框中添加一个文本框和一个“浏览”按钮。 4. 在安装程序脚本中添加代码,以便在用户选择自定义安装路径时将其保存到一个变量中。 5. 在安装程序脚本中使用该变量,以便在安装程序过程中将程序文件复制到用户选择的自定义安装路径。 下面是一些具体的步骤: 1. 在Visual Studio中创建一个新的安装程序项目,如InstallShield或WiX。 2. 在安装程序项目中,找到并打开“用户界面”或“UI”部分。 3. 在用户界面中,添加一个新的对话框,命名为“CustomPathDlg”(或其他您选择的名称)。 4. 在“CustomPathDlg”对话框中,添加一个文本框和一个“浏览”按钮。 5. 右键单击文本框,选择“属性”,并将其名称设置为“EDIT_INSTALLPATH”(或其他您选择的名称)。 6. 右键单击“浏览”按钮,选择“属性”,并将其名称设置为“PUSH_INSTALLPATHBROWSE”(或其他您选择的名称)。 7. 在“CustomPathDlg”对话框上右键单击,选择“属性”,并在“属性”窗口中选择“ControlCondition”属性。 8. 在“ControlCondition”属性中,添加以下代码: EDIT_INSTALLPATH <> "" 9. 在安装程序项目中找到“InstallScript”或“CustomAction”部分。 10. 在该部分中,添加以下代码: // 定义变量 STRING szCustomPath; // 获取自定义路径 MsiGetProperty(hInstall, "EDIT_INSTALLPATH", szCustomPath, 255, NULL); // 保存自定义路径 MsiSetProperty(hInstall, "CUSTOM_INSTALL_PATH", szCustomPath); 11. 在安装程序项目中找到“InstallExecuteSequence”部分。 12. 在该部分中,找到“InstallFiles”操作,并在该操作之前添加以下代码: // 获取自定义路径 MsiGetProperty(hInstall, "CUSTOM_INSTALL_PATH", szCustomPath, 255, NULL); // 设置安装路径 MsiSetTargetPath(hInstall, "INSTALLDIR", szCustomPath); 在执行安装程序时,用户将会看到一个新的对话框,其中包含一个文本框和一个“浏览”按钮,以便选择自定义安装路径。用户选择路径后,安装程序将会将路径保存到一个变量中,并在安装程序过程中将程序文件复制到该路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值