如何转储所有_G表内容 [英] How to dump all _G table content

查看:35
本文介绍了如何转储所有_G表内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转储_G表._G表中的其他表也已转储(内联表).我想要一个好的格式.我有一个例子,但使用它转储_G表有一些问题

i want to dump _G table. in _G table has other table also dump (inline table). and i want have a good format. i have a example but use it dump _G table have some problem

function print_table(node)
-- to make output beautiful
local function tab(amt)
    local str = ""
    for i=1,amt do
        str = str .. "\t"
    end
    return str
end

local cache, stack, output = {},{},{}
local depth = 1
local output_str = "{\n"

while true do
    local size = 0
    for k,v in pairs(node) do
        size = size + 1
    end

    local cur_index = 1
    for k,v in pairs(node) do
        if (cache[node] == nil) or (cur_index >= cache[node]) then

            if (string.find(output_str,"}",output_str:len())) then
                output_str = output_str .. ",\n"
            elseif not (string.find(output_str,"\n",output_str:len())) then
                output_str = output_str .. "\n"
            end

            -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
            table.insert(output,output_str)
            output_str = ""

            local key
            if (type(k) == "number" or type(k) == "boolean") then
                key = "[" ..type(k).. ":"..tostring(k).."]"
            else
                key = "['"..type(k).. ":"..tostring(k).."']"
            end

            if (type(v) == "number" or type(v) == "boolean") then
                output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
            elseif (type(v) == "table") then
                output_str = output_str .. tab(depth) .. key .. " = {\n"
                table.insert(stack,node)
                table.insert(stack,v)
                cache[node] = cur_index+1
                break
            else
                output_str = output_str .. tab(depth) .. key ..  " = '"..tostring(v).."'"
            end

            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            else
                output_str = output_str .. ","
            end
        else
            -- close the table
            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            end
        end

        cur_index = cur_index + 1
    end

    if (size == 0) then
        output_str = output_str .. "\n" .. tab(depth-1) .. "}"
    end

    if (#stack > 0) then
        node = stack[#stack]
        stack[#stack] = nil
        depth = cache[node] == nil and depth + 1 or depth - 1
    else
        break
    end
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output,output_str)
output_str = table.concat(output)

print(output_str) end

这是结果日志

------start-------
['function:add_msg_define'] = 'function: 0x7c6baa98',
['number:BLEND_DST_RGB'] = 32968,
['function:_attachShader'] = 'function: 0x7c626cf0',
['number:INVALID_OPERATION'] = 1282,
['function:drawArrays'] = 'function: 0x7c627480',
['number:SRGB8_ALPHA8_EXT'] = 35907,
['number:CCW'] = 2
------end-------

我不为什么要修复此功能

i don't why how to fix this function

推荐答案

我使用这个:

-- deep dumps the contents of the table and it's contents' contents
function deepdump( tbl )
    local checklist = {}
    local function innerdump( tbl, indent )
        checklist[ tostring(tbl) ] = true
        for k,v in pairs(tbl) do
            print(indent..k,v,type(v),checklist[ tostring(tbl) ])
            if (type(v) == "table" and not checklist[ tostring(v) ]) then innerdump(v,indent.."    ") end
        end
    end
    print("=== DEEPDUMP -----")
    checklist[ tostring(tbl) ] = true
    innerdump( tbl, "" )
    print("------------------")
end

https://gist.github.com/HoraceBury/9321964

这篇关于如何转储所有_G表内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆