Guest User

io

a guest
Jun 22nd, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. -- Definition for the IO API
  2.  
  3. local g_defaultInput = {
  4.     bFileHandle = true,
  5.     bClosed = false,
  6.     close = function( self )
  7.     end,
  8.     read = function( self, _sFormat )
  9.         if _sFormat and _sFormat ~= "*l" then
  10.             error( "Unsupported format" )
  11.         end
  12.         return _G.read()
  13.     end,
  14.     lines = function( self )
  15.         return function()
  16.             return _G.read()
  17.         end
  18.     end,
  19. }
  20.  
  21. local g_defaultOutput = {
  22.     bFileHandle = true,
  23.     bClosed = false,
  24.     close = function( self )
  25.     end,
  26.     write = function( self, _sText )
  27.         _G.write( _sText )
  28.     end,
  29.     flush = function( self )
  30.     end,
  31. }
  32.  
  33. local g_currentInput = g_defaultInput
  34. local g_currentOutput = g_defaultOutput
  35.  
  36. function close( _file )
  37.     (_file or g_currentOutput):close()
  38. end
  39.  
  40. function flush()
  41.     g_currentOutput:flush()
  42. end
  43.  
  44. function input( _arg )
  45.     if _G.type( _arg ) == "string" then
  46.         g_currentInput = open( _arg, "r" )
  47.     elseif _G.type( _arg ) == "table" then
  48.         g_currentInput = _arg
  49.     elseif _G.type( _arg ) == "nil" then
  50.         return g_currentInput
  51.     else
  52.         error( "Expected file name or file handle" )
  53.     end
  54. end
  55.  
  56. function lines( _sFileName )
  57.     if _sFileName then
  58.         return open( _sFileName, "r" ):lines()
  59.     else
  60.         return g_currentInput:lines()
  61.     end
  62. end
  63.  
  64. function open( _sPath, _sMode )
  65.     local sMode = _sMode or "r"
  66.     local file = fs.open( _sPath, sMode )
  67.     if not file then
  68.         return nil
  69.     end
  70.    
  71.     if sMode == "r"then
  72.         return {
  73.             bFileHandle = true,
  74.             bClosed = false,               
  75.             close = function( self )
  76.                 file.close()
  77.                 self.bClosed = true
  78.             end,
  79.             read = function( self, _sFormat )
  80.                 local sFormat = _sFormat or "*l"
  81.                 if sFormat == "*l" then
  82.                     return file.readLine()
  83.                 elseif sFormat == "*a" then
  84.                     return file.readAll()
  85.                 else
  86.                     error( "Unsupported format" )
  87.                 end
  88.                 return nil
  89.             end,
  90.             lines = function( self )
  91.                 return function()
  92.                     local sLine = file.readLine()
  93.                     if sLine == nil then
  94.                         file.close()
  95.                         self.bClosed = true
  96.                     end
  97.                     return sLine
  98.                 end
  99.             end,
  100.         }
  101.     elseif sMode == "w" or sMode == "a" then
  102.         return {
  103.             bFileHandle = true,
  104.             bClosed = false,               
  105.             close = function( self )
  106.                 file.close()
  107.                 self.bClosed = true
  108.             end,
  109.             write = function( self, _sText )
  110.                 file.write( _sText )
  111.             end,
  112.             flush = function( self )
  113.                 file.flush()
  114.             end,
  115.         }
  116.    
  117.     elseif sMode == "rb" then
  118.         return {
  119.             bFileHandle = true,
  120.             bClosed = false,               
  121.             close = function( self )
  122.                 file.close()
  123.                 self.bClosed = true
  124.             end,
  125.             read = function( self )
  126.                 return file.read()
  127.             end,
  128.         }
  129.        
  130.     elseif sMode == "wb" or sMode == "ab" then
  131.         return {
  132.             bFileHandle = true,
  133.             bClosed = false,               
  134.             close = function( self )
  135.                 file.close()
  136.                 self.bClosed = true
  137.             end,
  138.             write = function( self, _number )
  139.                 file.write( _number )
  140.             end,
  141.             flush = function( self )
  142.                 file.flush()
  143.             end,
  144.         }
  145.    
  146.     else
  147.         file.close()
  148.         error( "Unsupported mode" )
  149.        
  150.     end
  151. end
  152.  
  153. function output( _arg )
  154.     if _G.type( _arg ) == "string" then
  155.         g_currentOutput = open( _arg, "w" )
  156.     elseif _G.type( _arg ) == "table" then
  157.         g_currentOutput = _arg
  158.     elseif _G.type( _arg ) == "nil" then
  159.         return g_currentOutput
  160.     else
  161.         error( "Expected file name or file handle" )
  162.     end
  163. end
  164.  
  165. function read( ... )
  166.     return input():read( ... )
  167. end
  168.  
  169. function type( _handle )
  170.     if _G.type( _handle ) == "table" and _handle.bFileHandle == true then
  171.         if _handle.bClosed then
  172.             return "closed file"
  173.         else
  174.             return "file"
  175.         end
  176.     end
  177.     return nil
  178. end
  179.  
  180. function write( ... )
  181.     return output():write( ... )
  182. end
Add Comment
Please, Sign In to add comment