pixellover2

luatest

Sep 12th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.77 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. do
  28.    -- declare local variables
  29.    --// exportstring( string )
  30.    --// returns a "Lua" portable version of the string
  31.    local function exportstring( s )
  32.       return string.format("%q", s)
  33.    end
  34.  
  35.    --// The Save Function
  36.    function table.save(  tbl,filename )
  37.       local charS,charE = "   ","\n"
  38.       local file,err = io.open( filename, "wb" )
  39.       if err then return err end
  40.  
  41.       -- initiate variables for save procedure
  42.       local tables,lookup = { tbl },{ [tbl] = 1 }
  43.       file:write( "return {"..charE )
  44.  
  45.       for idx,t in ipairs( tables ) do
  46.          file:write( "-- Table: {"..idx.."}"..charE )
  47.          file:write( "{"..charE )
  48.          local thandled = {}
  49.  
  50.          for i,v in ipairs( t ) do
  51.             thandled[i] = true
  52.             local stype = type( v )
  53.             -- only handle value
  54.             if stype == "table" then
  55.                if not lookup[v] then
  56.                   table.insert( tables, v )
  57.                   lookup[v] = #tables
  58.                end
  59.                file:write( charS.."{"..lookup[v].."},"..charE )
  60.             elseif stype == "string" then
  61.                file:write(  charS..exportstring( v )..","..charE )
  62.             elseif stype == "number" then
  63.                file:write(  charS..tostring( v )..","..charE )
  64.             end
  65.          end
  66.  
  67.          for i,v in pairs( t ) do
  68.             -- escape handled values
  69.             if (not thandled[i]) then
  70.            
  71.                local str = ""
  72.                local stype = type( i )
  73.                -- handle index
  74.                if stype == "table" then
  75.                   if not lookup[i] then
  76.                      table.insert( tables,i )
  77.                      lookup[i] = #tables
  78.                   end
  79.                   str = charS.."[{"..lookup[i].."}]="
  80.                elseif stype == "string" then
  81.                   str = charS.."["..exportstring( i ).."]="
  82.                elseif stype == "number" then
  83.                   str = charS.."["..tostring( i ).."]="
  84.                end
  85.            
  86.                if str ~= "" then
  87.                   stype = type( v )
  88.                   -- handle value
  89.                   if stype == "table" then
  90.                      if not lookup[v] then
  91.                         table.insert( tables,v )
  92.                         lookup[v] = #tables
  93.                      end
  94.                      file:write( str.."{"..lookup[v].."},"..charE )
  95.                   elseif stype == "string" then
  96.                      file:write( str..exportstring( v )..","..charE )
  97.                   elseif stype == "number" then
  98.                      file:write( str..tostring( v )..","..charE )
  99.                   end
  100.                end
  101.             end
  102.          end
  103.          file:write( "},"..charE )
  104.       end
  105.       file:write( "}" )
  106.       file:close()
  107.    end
  108.    
  109.    --// The Load Function
  110.    function table.load( sfile )
  111.       local ftables,err = loadfile( sfile )
  112.       if err then return _,err end
  113.       local tables = ftables()
  114.       for idx = 1,#tables do
  115.          local tolinki = {}
  116.          for i,v in pairs( tables[idx] ) do
  117.             if type( v ) == "table" then
  118.                tables[idx][i] = tables[v[1]]
  119.             end
  120.             if type( i ) == "table" and tables[i[1]] then
  121.                table.insert( tolinki,{ i,tables[i[1]] } )
  122.             end
  123.          end
  124.          -- link indices
  125.          for _,v in ipairs( tolinki ) do
  126.             tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  127.          end
  128.       end
  129.       return tables[1]
  130.    end
  131. -- close do
  132. end
  133.  
  134. -- ChillCode
  135. Testcode
  136. dofile( "table.save-1.0.lua" )
  137.  
  138. function Main()
  139.      
  140. print( "Serialise Test ..." )
  141.    
  142. local t = {}
  143. t.a = 1
  144. t.b = 2
  145. t.c = {}
  146. -- self reference
  147. t.c.a = t
  148. t.inass = { 1,2,3,4,5,6,7,8,9,10 }
  149. t.inasst = { {1},{2},{3},{4},{5},{6},{7},{8},{9},{10} }
  150. -- random
  151. t.f = { [{ a = 5, b = 7, }] = "helloooooooo", [{ 1,2,3, m = 5, 5,6,7 }] = "A Table", }
  152.  
  153. t.func = function(x,y)
  154.    print( "Hello\nWorld" )
  155.    local sum = x+y
  156.    return sum
  157. end
  158.  
  159. -- get test string, not string.char(26)
  160. local str = ""
  161. for i = 0, 255 do
  162.    str = str..string.char( i )
  163. end
  164. t.lstri = { [str] = 1 }
  165. t.lstrv = str
  166.  
  167. local function test() print("Hello") end
  168.  
  169. t[test] = 1
  170.  
  171.    
  172. print( "\n## BEFORE SAVE ##" )
  173.  
  174. printtbl( t )
  175.  
  176. --// test save to file
  177. assert( table.save( t, "test_tbl.lua" ) == nil )
  178.    
  179. -- load table from file
  180. local t2,err = table.load( "test_tbl.lua" )
  181.  
  182. assert( err == nil )
  183.  
  184.  
  185. print( "\n## AFTER SAVE ##" )
  186.  
  187. print( "\n## LOAD FROM FILE ##" )
  188.  
  189. printtbl( t2 )
  190.  
  191. print( "\n//Test References" )
  192.    
  193. assert( t2.c.a == t2 )
  194.    
  195. print( "\n//Test Long string" )
  196.  
  197. assert( t.lstrv == t2.lstrv )
  198.  
  199. print( "\n//Test Function\n\n" )
  200.  
  201. assert( t2.func == nil )
  202.  
  203. print( "\n*** Test SUCCESSFUL ***" )
  204.  
  205. end
  206.  
  207. function printtbl( t,tab,lookup )
  208.    local lookup = lookup or { [t] = 1 }
  209.    local tab = tab or ""
  210.    for i,v in pairs( t ) do
  211.       print( tab..tostring(i), v )
  212.       if type(i) == "table" and not lookup[i] then
  213.          lookup[i] = 1
  214.          print( tab.."Table: i" )
  215.          printtbl( i,tab.."\t",lookup )
  216.       end
  217.       if type(v) == "table" and not lookup[v] then
  218.          lookup[v] = 1
  219.          print( tab.."Table: v" )
  220.          printtbl( v,tab.."\t",lookup )
  221.       end
  222.    end
  223. end
  224.  
  225. function SaveHugeTable()
  226.  
  227.    local _t = {}
  228.  
  229.    for i=1,1000000 do
  230.  
  231.       local __t = {}
  232.       table.insert(_t, __t)
  233.       table.insert(__t, "THIS IS A STRING WITH A FEW CHARS")
  234.       for i=1,30 do
  235.          table.insert(__t, i)
  236.       end  
  237.    end
  238.    
  239.    print("Built Hunge Table!")
  240.  
  241.    t1 = os.clock()
  242.    assert( table.save( _t, "test_huge_tbl.lua" ) == nil )
  243.    print("Done Saving: "..os.difftime(os.clock(), t1))
  244.    
  245.    _t = nil
  246.    collectgarbage()
  247.    
  248.    t1 = os.clock()
  249.    local t2,err = table.load( "test_huge_tbl.lua" )
  250.    print("Done Loading: "..os.difftime(os.clock(), t1))
  251.    
  252.    print("Num: "..#t2)
  253.  
  254.    assert( err == nil )
  255.    
  256.    print( "\n*** Test SUCCESSFUL ***" )
  257.    
  258.    io.read()
  259. end
  260.  
  261.  
  262. Main()
  263. --SaveHugeTable()
  264.  
  265. io.read()
Add Comment
Please, Sign In to add comment