* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / lib / yaml / stringio.rb
blob8ad949fa2bcb61fea137ed626d639db624472d68
2 # Limited StringIO if no core lib is available
4 begin
5 require 'stringio'
6 rescue LoadError
7     # StringIO based on code by MoonWolf
8     class StringIO
9         def initialize(string="")
10             @string=string
11             @pos=0
12             @eof=(string.size==0)
13         end
14         def pos
15             @pos
16         end    
17         def eof
18             @eof
19         end
20         alias eof? eof
21         def readline(rs=$/)
22             if @eof
23                 raise EOFError
24             else
25                 if p = @string[@pos..-1]=~rs
26                     line = @string[@pos,p+1]
27                 else
28                     line = @string[@pos..-1]
29                 end
30                 @pos+=line.size
31                 @eof =true if @pos==@string.size
32                 $_ = line
33             end
34         end
35         def rewind
36             seek(0,0)
37         end
38         def seek(offset,whence)
39             case whence
40             when 0
41                 @pos=offset
42             when 1
43                 @pos+=offset
44             when 2
45                 @pos=@string.size+offset
46             end
47             @eof=(@pos>=@string.size)
48             0
49         end
50     end
52         #
53         # Class method for creating streams
54         #
55         def YAML.make_stream( io )
56         if String === io
57             io = StringIO.new( io )
58         elsif not IO === io
59             raise YAML::Error, "YAML stream must be an IO or String object."
60         end
61         if YAML::unicode
62             def io.readline
63                 YAML.utf_to_internal( readline( @ln_sep ), @utf_encoding )
64             end
65             def io.check_unicode
66                 @utf_encoding = YAML.sniff_encoding( read( 4 ) )
67                 @ln_sep = YAML.enc_separator( @utf_encoding )
68                 seek( -4, IO::SEEK_CUR )
69             end
70                     def io.utf_encoding
71                         @utf_encoding
72                     end
73             io.check_unicode
74         else
75             def io.utf_encoding
76                 :None
77             end
78         end
79         io
80         end
82 end