Advertisement
vx13

lib/table_io.lua

Feb 12th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. --[[
  2.   Save Table to File
  3.   Load Table from File
  4.   v 1.0
  5.  
  6.   Lua 5.2 compatible
  7.  
  8.   Only Saves Tables, Numbers and Strings
  9.   Insides Table References are saved
  10.   Does not save Userdata, Metatables, Functions and indices of these
  11.   ----------------------------------------------------
  12.   table.save( table , filename )
  13.  
  14.   on failure: returns an error msg
  15.  
  16.   ----------------------------------------------------
  17.   table.load( filename or stringtable )
  18.  
  19.   Loads a table that has been saved via the table.save function
  20.  
  21.   on success: returns a previously saved table
  22.   on failure: returns as second argument an error msg
  23.   ----------------------------------------------------
  24.  
  25.   Licensed under the same terms as Lua itself.
  26.    
  27.   http://lua-users.org/wiki/SaveTableToFile
  28. ]]--
  29. do
  30.   -- declare local variables
  31.   --// exportstring( string )
  32.   --// returns a "Lua" portable version of the string
  33.   local function exportstring( s )
  34.     return string.format("%q", s)
  35.   end
  36.  
  37.   --// The Save Function
  38.   function table.save(  tbl,filename )
  39.     local charS,charE = "   ","\n"
  40.     local file,err = io.open( filename, "wb" )
  41.     if err then return err end
  42.  
  43.     -- initiate variables for save procedure
  44.     local tables,lookup = { tbl },{ [tbl] = 1 }
  45.     file:write( "return {"..charE )
  46.  
  47.     for idx,t in ipairs( tables ) do
  48.       file:write( "-- Table: {"..idx.."}"..charE )
  49.       file:write( "{"..charE )
  50.       local thandled = {}
  51.  
  52.       for i,v in ipairs( t ) do
  53.         thandled[i] = true
  54.         local stype = type( v )
  55.         -- only handle value
  56.         if stype == "table" then
  57.           if not lookup[v] then
  58.             table.insert( tables, v )
  59.             lookup[v] = #tables
  60.           end
  61.           file:write( charS.."{"..lookup[v].."},"..charE )
  62.         elseif stype == "string" then
  63.           file:write(  charS..exportstring( v )..","..charE )
  64.         elseif stype == "number" then
  65.           file:write(  charS..tostring( v )..","..charE )
  66.         end
  67.       end
  68.  
  69.       for i,v in pairs( t ) do
  70.         -- escape handled values
  71.         if (not thandled[i]) then
  72.        
  73.           local str = ""
  74.           local stype = type( i )
  75.           -- handle index
  76.           if stype == "table" then
  77.             if not lookup[i] then
  78.               table.insert( tables,i )
  79.               lookup[i] = #tables
  80.             end
  81.             str = charS.."[{"..lookup[i].."}]="
  82.           elseif stype == "string" then
  83.             str = charS.."["..exportstring( i ).."]="
  84.           elseif stype == "number" then
  85.             str = charS.."["..tostring( i ).."]="
  86.           end
  87.        
  88.           if str ~= "" then
  89.             stype = type( v )
  90.             -- handle value
  91.             if stype == "table" then
  92.               if not lookup[v] then
  93.                 table.insert( tables,v )
  94.                 lookup[v] = #tables
  95.               end
  96.               file:write( str.."{"..lookup[v].."},"..charE )
  97.             elseif stype == "string" then
  98.               file:write( str..exportstring( v )..","..charE )
  99.             elseif stype == "number" then
  100.               file:write( str..tostring( v )..","..charE )
  101.             end
  102.           end
  103.         end
  104.       end
  105.       file:write( "},"..charE )
  106.     end
  107.     file:write( "}" )
  108.     file:close()
  109.   end
  110.  
  111.   --// The Load Function
  112.   function table.load( sfile )
  113.     local ftables,err = loadfile( sfile )
  114.     if err then return _,err end
  115.     local tables = ftables()
  116.     for idx = 1,#tables do
  117.       local tolinki = {}
  118.       for i,v in pairs( tables[idx] ) do
  119.         if type( v ) == "table" then
  120.           tables[idx][i] = tables[v[1]]
  121.         end
  122.         if type( i ) == "table" and tables[i[1]] then
  123.           table.insert( tolinki,{ i,tables[i[1]] } )
  124.         end
  125.       end
  126.       -- link indices
  127.       for _,v in ipairs( tolinki ) do
  128.         tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  129.       end
  130.     end
  131.     return tables[1]
  132.   end
  133. -- close do
  134. end
  135.  
  136. -- ChillCode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement