Advertisement
Rafael_Sol_Maker

RAFAEL_SOL_MAKER's VX FILE CLASS ENTENSION v.1.0

Nov 17th, 2011
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.67 KB | None | 0 0
  1. #===============================================================================
  2. #             RAFAEL_SOL_MAKER's VX FILE CLASS ENTENSION v.1.0
  3. #  (Based on 'ftools.rb', an old default (now deprecated) module from Ruby)
  4. #-------------------------------------------------------------------------------
  5. # Description:  Extensão para o módulo File, para aumentar o número de funções e
  6. #               consequentemente melhorar a tarefa da manipulação de arquivos.
  7. #-------------------------------------------------------------------------------
  8. # How to Use: -
  9. #-------------------------------------------------------------------------------
  10. # Special Thanks: Hirofumi Watanabe (author), Zachary Landau
  11. #-------------------------------------------------------------------------------
  12. #===============================================================================
  13.  
  14. class << File
  15.   BUFFER_SIZE = 8 * 1024
  16.  
  17. #===============================================================================
  18. # If <to> is a valid directory, <from> will be combined (appended) with <to>,
  19. # putting the backslashes as necessary. Otherwise, <to> will be returned.
  20. # Useful to compose a directory with <from> and <to> if only the filename was
  21. # specified in <to>.
  22. #===============================================================================
  23.   def append_dir(from, to)
  24.     if directory? to
  25.       join to.sub(%r([/\\]$), ''), basename(from)
  26.     else
  27.       return to
  28.     end
  29.   end
  30.  
  31. #===============================================================================
  32. # Copies a file from <from> to <to>. If <to> is a directory, copies <from> to
  33. # <to/from>.
  34. #===============================================================================
  35.   def copy(from, to)
  36.     to = append_dir(from, to)
  37.     fmode = stat(from).mode
  38.     tpath = to
  39.     not_exist = !exist?(tpath)
  40.  
  41.     from = open(from, "rb")
  42.     to = open(to, "wb")
  43.     begin
  44.       while true
  45.         to.syswrite from.sysread(BUFFER_SIZE)
  46.       end
  47.     rescue EOFError
  48.       ret = true
  49.     rescue
  50.       ret = false
  51.     ensure
  52.       to.close
  53.       from.close
  54.     end
  55.     chmod(fmode, tpath) if not_exist
  56.     return ret
  57.   end
  58.  
  59. #===============================================================================
  60. # Moves a file from <from> to <to> using the 'copy' command. If <to> is a
  61. # directory, movies from <from> to <to/from>.
  62. #
  63. #===============================================================================
  64.   def move(from, to)
  65.     to = append_dir(from, to)
  66.    
  67.     if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
  68.       unlink to
  69.     end
  70.     fstat = stat(from)
  71.     begin
  72.       rename from, to
  73.     rescue
  74.       from_stat = stat(from)
  75.       copy from, to and unlink from
  76.       utime(from_stat.atime, from_stat.mtime, to)
  77.       begin
  78.         chown(fstat.uid, fstat.gid, to)
  79.       rescue
  80.       end
  81.     end
  82.   end
  83.  
  84. #===============================================================================
  85. # Returns <true> if the contents of the files <from> and <to> are identical.
  86. #===============================================================================
  87.   def compare(from, to)
  88.     return false if stat(from).size != stat(to).size
  89.     from = open(from, "rb")
  90.     to = open(to, "rb")
  91.     ret = false
  92.     fr = tr = ''
  93.  
  94.     begin
  95.       while fr == tr
  96.         fr = from.read(BUFFER_SIZE)
  97.         if fr
  98.           tr = to.read(fr.size)
  99.         else
  100.           ret = to.read(BUFFER_SIZE)
  101.           ret = !ret || ret.length == 0
  102.           break
  103.         end
  104.       end
  105.     rescue
  106.       ret = false
  107.     ensure
  108.       to.close
  109.       from.close
  110.     end
  111.     ret
  112.   end
  113.  
  114. #===============================================================================
  115. # Remove a listof files. Each parameter must be a filename that will be deleted.
  116. # Returns the number of erased files.
  117. #===============================================================================
  118.   def remove_files(*files)    
  119.     files.each do |file|
  120.       begin
  121.         unlink file        
  122.       rescue Errno::EACCES # for Windows
  123.         continue if symlink? file
  124.         begin
  125.           mode = stat(file).mode
  126.           o_chmod mode | 0200, file
  127.           unlink file          
  128.         rescue
  129.           o_chmod mode, file rescue nil
  130.         end
  131.       rescue
  132.       end
  133.     end
  134.   end
  135.  
  136. #===============================================================================
  137. # Create a directory and all the directory tree, if needed. Example given:
  138. # File.make_dirs 'C:/Users/Admin/RPGVX'
  139. # Will make it to create all the following directories, if they doesn't exists.
  140. #  * C:/Users
  141. #  * C:/Users/Admin
  142. #  * C:/Users/Admin/RPGVX
  143. #===============================================================================
  144.   def make_dirs(*dirs)    
  145.     mode = 0755
  146.     for dir in dirs
  147.       parent = dirname(dir)
  148.       next if parent == dir or directory? dir
  149.       make_dirs parent unless directory? parent      
  150.       if basename(dir) != ""
  151.         begin
  152.           Dir.mkdir dir, mode
  153.         rescue SystemCallError
  154.           raise unless directory? dir
  155.         end
  156.       end
  157.     end
  158.   end
  159.  
  160. #===============================================================================
  161. # If <from> and  <to> aren't the same file, it will copy <to> e set the
  162. # permission mode to <mode>. If <to> is a directory, copies from <from> to
  163. # <to/from>. IF <mode> is not specified, the default will be used.
  164. #===============================================================================
  165.   def install(from, to, mode = nil)
  166.     to = append_dir(from, to)
  167.     unless exist? to and compare from, to
  168.       remove_files to if exist? to
  169.       copy from, to
  170.       chmod mode, to
  171.     end
  172.   end
  173.  
  174. end
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement