Stekeblad

Lua tableDump API

Dec 1st, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. --TableDump for lua, takes a table and a filename, writes the table to the file in a easy-to-read format. Not designed to be reversed
  2.  
  3. --[[
  4. Copyright 2017 Stekeblad
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7.  
  8. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  9.  
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11. ]]
  12.  
  13. -----------------------------------
  14.  
  15. -- Possible future features: maxDepth, don't allow tables nested deeper then x levels
  16. --                           protection against recursion, a={b={c=a}} --> a==a.b.c==a.b.c.b.c
  17.  
  18. -- For customization, the functions indent and niceColumns can be changed for different intentation length and column width
  19.  
  20. local depth
  21. local handle
  22.  
  23. local function indent()
  24.   for i=0, depth*3, 1 do
  25.     handle:write(" ")
  26.   end
  27. end
  28.  
  29. local function niceColumns(key)
  30.   handle:write(key)
  31.   for i = 20 - string.len(key), 0, -1 do
  32.     handle:write(" ")
  33.   end
  34. end
  35.  
  36. local function tableBreakdown(tab)
  37.   handle:write("\n")
  38.   indent()
  39.   handle:write("{")
  40.   depth = depth + 1
  41.   local firstPair = true
  42.   for key, val in pairs(tab) do
  43.     if not firstPair then
  44.       handle:write(",\n")
  45.     else
  46.       firstPair = false
  47.       handle:write("\n")
  48.     end
  49.     if (type(val) == "table") then
  50.       indent()
  51.       handle:write(key)
  52.       tableBreakdown(val)
  53.     else
  54.       indent()
  55.       niceColumns(key)
  56.      
  57.       if (type(val) == "boolean") then
  58.         if val then
  59.           handle:write("true")
  60.         else
  61.           handle:write("false")
  62.         end
  63.       elseif (type(val) == "number") then
  64.         handle:write(val)
  65.       elseif (type(val) == "string") then
  66.         handle:write("\"" .. val .. "\"")
  67.       else
  68.         handle:write("[" .. string.upper(type(val)) .. "]")
  69.       end
  70.     end
  71.   end
  72.   handle:write("\n")
  73.   depth = depth - 1
  74.   indent()
  75.   handle:write("}")
  76. end
  77.  
  78. function tableDump(tab, fileName)
  79.   handle = io.open(fileName, "w")
  80.   depth = 0
  81.   if not handle then error("tableDump: Failed to open file for writing", 2) end
  82.   tableBreakdown(tab)
  83.   handle:close()
  84.   return true
  85. end
Add Comment
Please, Sign In to add comment