Advertisement
PaymentOption

Persistance API

Sep 9th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. -- Persistance API written by PaymentOption for lieudusty.
  2.  
  3. function NewVariable( sPath, value )
  4.     if type( value ) == "table" then
  5.         fs.delete( sPath )
  6.         local fFile = fs.open( sPath, "w" )
  7.        
  8.         for index,valueInTable in pairs( value ) do
  9.             fFile.writeLine( valueInTable )
  10.         end
  11.         fFile.close()
  12.         return
  13.     end
  14.    
  15.     fs.delete( sPath )
  16.     local fFile = fs.open( sPath, "w" )
  17.     fFile.write( value )
  18.     fFile.close()
  19. end
  20.  
  21. function AppendVarFile( sPath, value )
  22.     if fs.exists( sPath ) then
  23.         if type( value ) == "table" then
  24.             local fFile = fs.open( sPath, "r" )
  25.             local sFileContents = fFile.readAll()
  26.             fFile.close()
  27.            
  28.             local sTableInString = ""
  29.             for index,val in pairs( value ) do
  30.                 sTableInString = sTableInString .. val .. '\n'
  31.             end
  32.            
  33.             fs.delete( sPath )
  34.             fFile = fs.open( sPath, "w" )
  35.             local sAppendedFile = sFileContents .. '\n' .. sTableInString
  36.             fFile.write( sAppendedFile )
  37.             fFile.close()
  38.             return
  39.         end
  40.        
  41.         local fFile = fs.open( sPath, "r" )
  42.         local sFileContents = fFile.readAll()
  43.         fFile.close()
  44.        
  45.         fs.delete( sPath )
  46.         local sAppendedFile = sFileContents .. '\n' .. value
  47.         fFile = fs.open( sPath, "w" )
  48.         fFile.write( sAppendedFile )
  49.         fFile.close()
  50.     end
  51. end
  52.  
  53. function GetVariable( sPath )
  54.     if fs.exists( sPath ) then
  55.         local function GetFileLength_InLines( sPath )
  56.             local fFile = io.open( sPath, "r" )
  57.             local nLength = 0
  58.            
  59.             for line in fFile:lines() do
  60.                 nLength = nLength+1
  61.             end
  62.             fFile:close()
  63.            
  64.             return nLength
  65.         end
  66.        
  67.         local nLines = GetFileLength_InLines( sPath )
  68.         local fFile = fs.open( sPath, "r" )
  69.         local tVars = {}
  70.        
  71.         for nLine=1, nLines do
  72.             table.insert( tVars, fFile.readLine() )
  73.         end
  74.         fFile.close()
  75.        
  76.         if #tVars == 1 then
  77.             return tVars[1]
  78.         else
  79.             return tVars
  80.         end
  81.     end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement