dumptable.lua
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 19:50:43 -0400
changeset 57 4974e5368a29
parent 13 7b82873f8f94
permissions -rw-r--r--
Minor type cleanup ("unsigned char" -> "uint8_t").
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
function dumptable(tabname, tab, depth)
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
    if depth == nil then  -- first call, before any recursion?
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
        depth = 1
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
    end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
    if tabname ~= nil then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
        if tab == nil then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
            print(tabname .. " = nil")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
            return
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
        else
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
            print(tabname .. " = {")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
        end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    local depthstr = ""
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    for i=1,(depth*4) do
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
        depthstr = depthstr .. " "
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    if tab.DUMPTABLE_ITERATED then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
        print(depthstr .. "(...circular reference...)")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    else
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        tab.DUMPTABLE_ITERATED = true
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
        for k,v in pairs(tab) do
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
            if type(v) == "table" then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
                print(depthstr .. tostring(k) .. " = {")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
                dumptable(nil, v, depth + 1)
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
                print(depthstr .. "}")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
            else
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
                if k ~= "DUMPTABLE_ITERATED" then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
                    print(depthstr .. tostring(k) .. " = " .. tostring(v))
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
                end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
            end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
        tab.DUMPTABLE_ITERATED = nil
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    if tabname ~= nil then
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
        print("}")
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
end
7b82873f8f94 Added dumptable.lua
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42