Guest User

file_helper

a guest
Mar 27th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- doesFileExist
  3. --
  4. -- Checks to see if a file exists in the path.
  5. --
  6. -- Enter:   name = file name
  7. --  path = path to file (directory)
  8. --  defaults to ResourceDirectory if "path" is missing.
  9. --
  10. -- Returns: true = file exists, false = file not found
  11. ----------------------------------------------------------------------------------
  12. --
  13. function doesFileExist( fname, path )
  14.  
  15.     local results = false
  16.  
  17.     local filePath = system.pathForFile( fname, path )
  18.  
  19.     -- filePath will be nil if file doesn't exist and the path is ResourceDirectory
  20.     --
  21.     if filePath then
  22.         filePath = io.open( filePath, "r" )
  23.     end
  24.  
  25.     if  filePath then
  26.         print( "File found -> " .. fname )
  27.         -- Clean up our file handles
  28.         filePath:close()
  29.         results = true
  30.     else
  31.         print( "File does not exist -> " .. fname )
  32.     end
  33.  
  34.     print()
  35.  
  36.     return results
  37. end
  38. ----------------------------------------------------------------------------------
  39. -- copyFile( src_name, src_path, dst_name, dst_path, overwrite )
  40. --
  41. -- Copies the source name/path to destination name/path
  42. --
  43. -- Enter:   src_name = source file name
  44. --      src_path = source path to file (directory), nil for ResourceDirectory
  45. --      dst_name = destination file name
  46. --      overwrite = true to overwrite file, false to not overwrite
  47. --
  48. -- Returns: false = error creating/copying file
  49. --      nil = source file not found
  50. --      1 = file already exists (not copied)
  51. --      2 = file copied successfully
  52. ----------------------------------------------------------------------------------
  53. --
  54. function copyFile( srcName, srcPath, dstName, dstPath, overwrite )
  55.     toast.show("copiando arquivo")
  56.     local results = false
  57.  
  58.     local srcPath = doesFileExist( srcName, srcPath )
  59.  
  60.     if srcPath == false then
  61.         -- Source file doesn't exist
  62.         toast.show("arquivo n encontrado")
  63.         return 3
  64.     end
  65.  
  66.     -- Check to see if destination file already exists
  67.     if not overwrite then
  68.         if fileLib.doesFileExist( dstName, dstPath ) then
  69.             -- Don't overwrite the file
  70.             return 1
  71.         end
  72.     end
  73.  
  74.     -- Copy the source file to the destination file
  75.     --
  76.     local rfilePath = system.pathForFile( srcName, srcPath )
  77.     local wfilePath = system.pathForFile( dstName, dstPath )
  78.  
  79.     local rfh = io.open( rfilePath, "rb" )
  80.  
  81.     local wfh = io.open( wfilePath, "wb" )
  82.  
  83.     if  not wfh then
  84.         print( "writeFileName open error!" )
  85.         return 4            -- error
  86.     else
  87.         -- Read the file from the Resource directory and write it to the destination directory
  88.         local data = rfh:read( "*a" )
  89.         if not data then
  90.             print( "read error!" )
  91.             return 5    -- error
  92.         else
  93.             if not wfh:write( data ) then
  94.                 print( "write error!" )
  95.                 return 6    -- error
  96.             end
  97.         end
  98.     end
  99.  
  100.     results = 2     -- file copied
  101.  
  102.     -- Clean up our file handles
  103.     rfh:close()
  104.     wfh:close()
  105.  
  106.     return results
  107. end
Advertisement
Add Comment
Please, Sign In to add comment