Advertisement
Guest User

test

a guest
Jul 1st, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. -- ABC
  2. -- DEF
  3. local f = io.open ( require('shell').resolve ('test'),'r')
  4.  
  5. -- Works fine
  6. f:seek('set',7)
  7. print ( f:read('*line') )
  8. f:seek('set',0)
  9. print ( f:read('*line') )
  10. f:close ()
  11.  
  12. local b = {
  13.   ['resource']=nil,
  14.   ['seek'] = {[1]=0},
  15.   ['open'] = function ( self )
  16.     self ['resource'] = io.open ( 'test', 'r' )
  17.   end,
  18. }
  19. setmetatable ( b, {
  20.   ['__index'] = function ( self, index )
  21.     if self ['seek'][index] == nil then
  22.       local tmpIndex = 1
  23.       for i = index, 1, -1 do
  24.         if self ['seek'][i] ~= nil then
  25.           tmpIndex = i i = 0
  26.         end
  27.       end
  28.  
  29.       for i = tmpIndex, index, 1 do
  30.         self ['resource']:seek ( 'set', self ['seek'][i] )
  31.         local line = self ['resource']:read ('*line')
  32.         self ['seek'][(i+1)] = self ['seek'][i] + #line + 1
  33.       end
  34.     end
  35.    
  36.     print ( 'seek', self['seek'][index] )
  37.     self['resource']:seek ('set', self['seek'][index])
  38.     return self['resource']:read('*line')
  39.   end,
  40. } )
  41.  
  42. b:open()
  43. print ( b[2] )
  44. print ( b[1] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement