Advertisement
Fro

file.lua

Fro
Feb 6th, 2012
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. File = {
  3.     new = function( self, filePath )
  4.         if self.__index then
  5.             return setmetatable( { }, { __index = self } )
  6.         else
  7.             return setmetatable( { fPath = filePath, buffer = "" }, { __index = File } )
  8.         end
  9.     end,
  10.                
  11.     exist = function( self, filePath )
  12.         if self.__index then
  13.             if self.fPath then
  14.                 return fileExists( self.fPath )
  15.             end
  16.             return false
  17.         else
  18.             return fileExists( filePath )
  19.         end
  20.     end,
  21.    
  22.     rename = function( self, filePath, newFilePath )
  23.         if self.__index then
  24.             if self.fPath then
  25.                 local res  = fileRename( self.fPath, filePath )
  26.                 self.fPath = filePath
  27.                 return res
  28.             end
  29.             return false
  30.         else
  31.             return fileRename( filePath, newFilePath )
  32.         end    
  33.     end,
  34.    
  35.     delete = function( self, filePath )
  36.         if self.__index then
  37.             if self.fPath then
  38.                 return fileDelete( self.fPath )
  39.             end
  40.             return false
  41.         else
  42.             return fileDelete( filePath )
  43.         end
  44.     end,
  45.        
  46.     create = function( self, filePath )
  47.         if not self.file then
  48.             self.fPath = filePath or self.fPath
  49.             self.file  = fileCreate( self.fPath )
  50.             return self.file ~= false
  51.         end
  52.         return false
  53.     end,
  54.    
  55.     open = function( self, readOnly, filePath )
  56.         if not self.file then
  57.             self.fPath = filePath or self.fPath
  58.             self.file  = fileOpen( self.fPath, readOnly or false )
  59.             return self.file ~= false
  60.         end
  61.         return false
  62.     end,
  63.    
  64.     close = function( self )
  65.         if self.file then
  66.             local res = fileClose( self.file )
  67.             self.file   = nil
  68.             self.buffer = ""
  69.             return res
  70.         end
  71.         return false
  72.     end,
  73.    
  74.     pos = function( self, offset )
  75.         if self.file then
  76.             if offset then
  77.                 return fileSetPos( self.file, offset )
  78.             else
  79.                 return fileGetPos( self.file )
  80.             end
  81.         end
  82.         return false
  83.     end,
  84.    
  85.     size = function( self, filePath )
  86.         if filePath then
  87.             local hFile = fileOpen( filePath )
  88.             if hFile then
  89.                 local fSize = fileGetSize( hFile )
  90.                 fileClose( hFile )
  91.                 return fSize
  92.             end    
  93.         elseif self.file then
  94.             return fileGetPos( self.file )
  95.         end
  96.         return false
  97.     end,
  98.    
  99.     flush = function( self )
  100.         if self.file then
  101.             return fileFlush( self.file )
  102.         end
  103.         return false
  104.     end,
  105.    
  106.     write = function( self, string1, ... )
  107.         if self.file then
  108.             return fileWrite( self.file, string1, ... )
  109.         end
  110.         return false
  111.     end,
  112.    
  113.     read = function( self, count )
  114.         if self.file then
  115.             return fileRead( self.file, count )
  116.         end
  117.         return false
  118.     end,
  119.    
  120.     readLine = function( self, count )
  121.         if self.file then
  122.             while true do
  123.                 local endpos = string.find( self.buffer, "\n" )
  124.  
  125.                 if not endpos then
  126.                     if fileIsEOF( self.file ) then
  127.                         if self.buffer ~= "" then
  128.                             local line = self.buffer
  129.                             self.buffer = ""
  130.                             return line
  131.                         else
  132.                             return false
  133.                         end
  134.                     end
  135.                     self.buffer = self.buffer .. fileRead( self.file, count or 500 )
  136.                 else
  137.                     local line  = string.sub( self.buffer, 1, endpos - 1 )
  138.                     self.buffer = string.sub( self.buffer, endpos + 1 )
  139.                     return line
  140.                 end
  141.             end
  142.         end
  143.         return false
  144.     end,
  145.            
  146.     EOF = function( self )
  147.         if self.file then
  148.             return fileIsEOF( self.file )
  149.         end
  150.         return false
  151.     end
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement