加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

自动生成类包含头文件

发布时间:2020-12-14 22:55:38 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env lua--------------------------------------------------------------------------------- this LUA script is aim to generate inclu

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/env lua

-------------------------------------------------------------------------------
-- this LUA script is aim to generate include shutcut file in ./inc/
-- First,it will find all *.h and *.hpp files in current path. Then,find out 
-- all class which defined in head file. Next,build a file in ./inc/ which be 
-- named with class name. Last,write "#include "./<headfile>"" into that file.
-- auth : Chunjun Li <[email?protected]>
-- date : 2013-10-02
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- get_class_list_in_file
-- berif find all class which is defined in head file,and return class name 
--        table.
-------------------------------------------------------------------------------
function get_class_list_in_file(file_name)
    local class_list = {}
    local headfile = assert(io.open(file_name,"r"))
    while true do
        local line = headfile:read("*line") 
        if line == nil then break end
        if string.find(line,"^class%s+") then
            if not string.find(line,"^class%s+%a[%w_]*%s*;$") then
                local _,_,class_name = string.find(line,"^class%s+(%a[%w_]*)")
                if class_name then
                    table.insert(class_list,class_name)
                end
            end
        end
    end
    headfile:close()
    return class_list
end

-------------------------------------------------------------------------------
-- build_class_include_file
-- berif build a file in ./inc/ and named as class name. then write C include 
--        statent inside.
-------------------------------------------------------------------------------
function build_class_include_file(class_name,file_name)
    local context = "#include "../" .. file_name .. ""n"
    local outfile = assert(io.open("inc/" .. class_name,"a"))
    outfile:write(context)
    outfile:close()
end

-------------------------------------------------------------------------------
-- do_file
-- berif find classes and build class include files in ./inc/
-------------------------------------------------------------------------------
function do_file(file_name)
    class_list = get_class_list_in_file(file_name)
    for _,class_name in pairs(class_list) do
        build_class_include_file(class_name,file_name)
        print(class_name .. " --> " .. file_name)
    end
end

-------------------------------------------------------------------------------
-- scan_head_files
-- berif find all head file and run do_file() each
-------------------------------------------------------------------------------
function scan_head_files(path)
    local tmp_file_name = "head_file_list.tmp"
    local find_cmd = "find " .. path .. " -type f -name "*.h" -or -name "*.hpp" > " .. tmp_file_name

    os.execute "rm -rf ./inc"
    os.execute "mkdir ./inc"

    os.execute(find_cmd)
    local file = assert(io.open(tmp_file_name,"r"))
    while true do 
        local line = file:read("*line")
        if line == nil then break end
        do_file(line)
    end
    file:close()
    os.remove(tmp_file_name)
end

scan_head_files("./")

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读