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

embedded/2024/9/25 2:32:25/


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;
    }


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


http://www.ppmy.cn/embedded/41573.html

相关文章

Hadoop阶段性技能抽检题,无直接答案但有提示信息

项目名&#xff1a;Hadoop平台及组件的部署管理 考核内容&#xff1a; 考核以大数据技术为核心内容&#xff0c;重点考查同学们基于Hadoop平台环境下&#xff0c;利用Hadoop技术生态组件&#xff0c;综合软件开发相关技术&#xff0c;解决实际问题的能力&#xff0c;所有学生在…

【哈希】Leetcode 383. 赎金信【简单】

赎金信 给你两个字符串&#xff1a;ransomNote 和 magazine &#xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。 如果可以&#xff0c;返回 true &#xff1b;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。 解题思路 可以使用哈希…

Kubernetes(k8s)的认证(Authentication)策略解析

Kubernetes&#xff08;k8s&#xff09;的认证&#xff08;Authentication&#xff09;策略是确保只有经过验证的实体&#xff08;用户、服务账户等&#xff09;能够访问API服务器的基础安全措施。Kubernetes支持多种认证方法&#xff0c;以下是主要的认证策略&#xff1a; X50…

AI实景自动无人直播软件:引领直播新时代的智能化创作工具,实现一部手机24小时直播拓客

在科技飞速发展的今天&#xff0c;AI实景自动无人直播软件以其独特的功能和便捷性&#xff0c;正成为商家和内容创作者们的利器。这款先进的软件支持智能讲解、一键开播、智能回复以及手机拍摄真实场景等特性&#xff0c;为用户呈现了全新的直播体验&#xff0c;引领着直播行业…

怎么把图片上的字去掉

将图片上的字去掉通常需要使用图像编辑软件或在线工具。以下是一些常用的方法和步骤&#xff1a; 使用Adobe Photoshop&#xff1a; 打开Photoshop&#xff0c;导入需要编辑的图片。 选择“橡皮擦工具”或“克隆图章工具”。 如果使用“橡皮擦工具”&#xff0c;调整橡皮擦的…

微软推出的Microsoft Fabric 到底是什么?

近期&#xff0c;总有客户问小编&#xff0c;微软推出的 Microsoft Fabric 是什么&#xff1f;这个产品有什么特别之处呢&#xff1f;希望下面这篇文章能为大家解开一些疑惑。 微软Fabric是2023年5月推出的一个数据分析平台&#xff0c;它将关键数据管理和分析工作负载整合到一…

URL入参出参请求头可配置化

整体思路 通过spring的Spell表达式解析变量的参数值&#xff0c;参数名定义为${XXX},在解析参数值后&#xff0c;将${XXX}替换成#XXX以匹配Spell表达式。 核心实现类 package com.example.spring_boot_study.spring.spell;import cn.hutool.core.map.MapUtil; import cn.hut…

男士内裤哪个牌子质量好又舒服?五款不容错过的男士内裤

男士内裤&#xff0c;作为男士日常穿着的重要贴身衣物&#xff0c;其舒适度和透气性至关重要。尽管有些男士可能习惯长时间穿着同一条内裤&#xff0c;但为了确保健康和舒适&#xff0c;建议每3-6个月更换一次内裤。长时间不更换内裤会导致其舒适性和透气性下降&#xff0c;同时…