Advertisement
Rakoonic

Some file IO routines

Dec 2nd, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1.  
  2. --------------------------------------------------------------
  3. -- PROTOTYPES AND LOCAL VARIABLES ----------------------------
  4.  
  5. local JSON = require( "json" )
  6. local LFS  = require( "lfs" )
  7.  
  8. local class = {}
  9.  
  10. --------------------------------------------------------------
  11. -- FILE FUNCTIONS --------------------------------------------
  12.  
  13. -- Encode table to JSON string
  14. function class.JSONEncode( data )
  15.  
  16.     return JSON.encode(data)
  17.  
  18. end
  19.  
  20. -- Decode JSON string to table
  21. function class.JSONDecode( data )
  22.  
  23.     return JSON.decode(data)
  24.  
  25. end
  26.  
  27. -- Save data to a file
  28. function class.saveData( filePath, data )
  29.  
  30.     -- Save file
  31.     local file = io.open( system.pathForFile( filePath, system.DocumentsDirectory ), "w" )
  32.     file:write(data)
  33.     io.close( file )
  34.    
  35. end
  36.  
  37. -- Load data from a file, returning a table
  38. function class.loadData( filePath, directory ) 
  39.  
  40.     -- Get file path and check it exists
  41.     local fullPath = system.pathForFile( filePath, class.getDirectory( directory ) )
  42.     if fullPath == nil then return false ; end
  43.  
  44.     -- Load file
  45.     local file = io.open( fullPath, "r" )      
  46.     if file then
  47.         local data = file:read( "*a" )
  48.         io.close( file )
  49.         return data
  50.     end
  51.  
  52.     -- Some sort of error, so return false
  53.     return false
  54.    
  55. end
  56.  
  57. -- Get correct path
  58. function class.getDirectory( directory )
  59.  
  60.     -- Allow for missed parameter
  61.     if directory == nil then directory = "docs" ; end
  62.  
  63.     -- Return the correct path
  64.     if directory == "docs" or directory == "documents" then return system.DocumentsDirectory
  65.     else                                                    return system.ResourceDirectory ; end
  66.  
  67. end
  68.  
  69. -- Find if a file exists
  70. function class.fileExists( filePath, directory )
  71.  
  72.     -- Does file exist?
  73.     local results  = false
  74.     local filePath = system.pathForFile( filePath, class.getDirectory( directory ) )
  75.     if filePath then
  76.         local file = io.open( filePath, "r" )
  77.         if file then
  78.             file:close()
  79.             results = true
  80.         end
  81.     end
  82.    
  83.     return results
  84.  
  85. end
  86.  
  87. -- Remove a file ( also checks if it exists)
  88. function class.deleteFile( filePath, directory )
  89.  
  90.     -- Allow for missed parameter
  91.     directory = directory or "docs"
  92.  
  93.     -- Get file path and check it exists
  94.     local file, fullPath
  95.     if directory == "docs" or directory == "documents" then fullPath = system.pathForFile( filePath, system.DocumentsDirectory )
  96.     else                                                    fullPath = system.pathForFile( filePath, system.ResourceDirectory ) ; end
  97.  
  98.     -- Attempt to remove the file
  99.     return os.remove( fullPath )
  100.    
  101. end
  102.  
  103. --------------------------------------------------------------
  104. -- RETURN CLASS DEFINITION -----------------------------------
  105.  
  106. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement