Advertisement
Fro

file.lua

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