Initial import
[ruby-io-mixins.git] / test / io_loop.rb
1 require 'io/mixins'
2 class IOLoop
3
4   include IO::Editable
5
6   attr_reader :string
7
8   def initialize(string = "")
9     @string = string
10     @pos = 0
11   end
12
13   def sysseek(offset, whence = IO::SEEK_SET)
14     @pos = case whence
15            when IO::SEEK_SET then offset
16            when IO::SEEK_CUR then @pos + offset
17            when IO::SEEK_END then @string.length + offset
18            end % @string.size
19   end
20
21   # def eof
22     # @string.nil? || @string.empty?
23   # end
24
25   def putchar(char)
26     @string[@pos,1] = char.respond_to?(:chr) ? char.chr : char[0,0]
27     @pos += 1
28     @pos %= @string.length
29     char
30   end
31   private :putchar
32
33   def syswrite(string)
34     string.each_byte do |byte|
35       putchar(byte)
36     end
37   end
38
39   def sysread(length)
40     raise IOError if @string.length < length
41     if length <= @string.length - @pos
42       out = @string[@pos,length]
43       @pos += out.length
44       @pos %= @string.length
45       return out
46     else
47       out = @string[@pos..-1]
48       @pos = 0
49       out << @string[0,length-out.length]
50     end
51   end
52
53   private
54   # def ensure_open
55     # raise IOError, "closed stream" if closed?
56   # end
57
58   alias close_read close
59   alias close_write close
60
61 end