| 1 | # |
|---|
| 2 | # stringio.rb |
|---|
| 3 | # |
|---|
| 4 | #-- |
|---|
| 5 | # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net> |
|---|
| 6 | # |
|---|
| 7 | # Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 8 | # a copy of this software and associated documentation files (the |
|---|
| 9 | # "Software"), to deal in the Software without restriction, including |
|---|
| 10 | # without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 11 | # distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 12 | # permit persons to whom the Software is furnished to do so, subject to |
|---|
| 13 | # the following conditions: |
|---|
| 14 | # |
|---|
| 15 | # The above copyright notice and this permission notice shall be |
|---|
| 16 | # included in all copies or substantial portions of the Software. |
|---|
| 17 | # |
|---|
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 19 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 20 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 21 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|---|
| 22 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 23 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|---|
| 24 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 25 | # |
|---|
| 26 | # Note: Originally licensed under LGPL v2+. Using MIT license for Rails |
|---|
| 27 | # with permission of Minero Aoki. |
|---|
| 28 | #++ |
|---|
| 29 | |
|---|
| 30 | class StringInput#:nodoc: |
|---|
| 31 | |
|---|
| 32 | include Enumerable |
|---|
| 33 | |
|---|
| 34 | class << self |
|---|
| 35 | |
|---|
| 36 | def new( str ) |
|---|
| 37 | if block_given? |
|---|
| 38 | begin |
|---|
| 39 | f = super |
|---|
| 40 | yield f |
|---|
| 41 | ensure |
|---|
| 42 | f.close if f |
|---|
| 43 | end |
|---|
| 44 | else |
|---|
| 45 | super |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | alias open new |
|---|
| 50 | |
|---|
| 51 | end |
|---|
| 52 | |
|---|
| 53 | def initialize( str ) |
|---|
| 54 | @src = str |
|---|
| 55 | @pos = 0 |
|---|
| 56 | @closed = false |
|---|
| 57 | @lineno = 0 |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | attr_reader :lineno |
|---|
| 61 | |
|---|
| 62 | def string |
|---|
| 63 | @src |
|---|
| 64 | end |
|---|
| 65 | |
|---|
| 66 | def inspect |
|---|
| 67 | "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>" |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | def close |
|---|
| 71 | stream_check! |
|---|
| 72 | @pos = nil |
|---|
| 73 | @closed = true |
|---|
| 74 | end |
|---|
| 75 | |
|---|
| 76 | def closed? |
|---|
| 77 | @closed |
|---|
| 78 | end |
|---|
| 79 | |
|---|
| 80 | def pos |
|---|
| 81 | stream_check! |
|---|
| 82 | [@pos, @src.size].min |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | alias tell pos |
|---|
| 86 | |
|---|
| 87 | def seek( offset, whence = IO::SEEK_SET ) |
|---|
| 88 | stream_check! |
|---|
| 89 | case whence |
|---|
| 90 | when IO::SEEK_SET |
|---|
| 91 | @pos = offset |
|---|
| 92 | when IO::SEEK_CUR |
|---|
| 93 | @pos += offset |
|---|
| 94 | when IO::SEEK_END |
|---|
| 95 | @pos = @src.size - offset |
|---|
| 96 | else |
|---|
| 97 | raise ArgumentError, "unknown seek flag: #{whence}" |
|---|
| 98 | end |
|---|
| 99 | @pos = 0 if @pos < 0 |
|---|
| 100 | @pos = [@pos, @src.size + 1].min |
|---|
| 101 | offset |
|---|
| 102 | end |
|---|
| 103 | |
|---|
| 104 | def rewind |
|---|
| 105 | stream_check! |
|---|
| 106 | @pos = 0 |
|---|
| 107 | end |
|---|
| 108 | |
|---|
| 109 | def eof? |
|---|
| 110 | stream_check! |
|---|
| 111 | @pos > @src.size |
|---|
| 112 | end |
|---|
| 113 | |
|---|
| 114 | def each( &block ) |
|---|
| 115 | stream_check! |
|---|
| 116 | begin |
|---|
| 117 | @src.each(&block) |
|---|
| 118 | ensure |
|---|
| 119 | @pos = 0 |
|---|
| 120 | end |
|---|
| 121 | end |
|---|
| 122 | |
|---|
| 123 | def gets |
|---|
| 124 | stream_check! |
|---|
| 125 | if idx = @src.index(?\n, @pos) |
|---|
| 126 | idx += 1 # "\n".size |
|---|
| 127 | line = @src[ @pos ... idx ] |
|---|
| 128 | @pos = idx |
|---|
| 129 | @pos += 1 if @pos == @src.size |
|---|
| 130 | else |
|---|
| 131 | line = @src[ @pos .. -1 ] |
|---|
| 132 | @pos = @src.size + 1 |
|---|
| 133 | end |
|---|
| 134 | @lineno += 1 |
|---|
| 135 | |
|---|
| 136 | line |
|---|
| 137 | end |
|---|
| 138 | |
|---|
| 139 | def getc |
|---|
| 140 | stream_check! |
|---|
| 141 | ch = @src[@pos] |
|---|
| 142 | @pos += 1 |
|---|
| 143 | @pos += 1 if @pos == @src.size |
|---|
| 144 | ch |
|---|
| 145 | end |
|---|
| 146 | |
|---|
| 147 | def read( len = nil ) |
|---|
| 148 | stream_check! |
|---|
| 149 | return read_all unless len |
|---|
| 150 | str = @src[@pos, len] |
|---|
| 151 | @pos += len |
|---|
| 152 | @pos += 1 if @pos == @src.size |
|---|
| 153 | str |
|---|
| 154 | end |
|---|
| 155 | |
|---|
| 156 | alias sysread read |
|---|
| 157 | |
|---|
| 158 | def read_all |
|---|
| 159 | stream_check! |
|---|
| 160 | return nil if eof? |
|---|
| 161 | rest = @src[@pos ... @src.size] |
|---|
| 162 | @pos = @src.size + 1 |
|---|
| 163 | rest |
|---|
| 164 | end |
|---|
| 165 | |
|---|
| 166 | def stream_check! |
|---|
| 167 | @closed and raise IOError, 'closed stream' |
|---|
| 168 | end |
|---|
| 169 | |
|---|
| 170 | end |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | class StringOutput#:nodoc: |
|---|
| 174 | |
|---|
| 175 | class << self |
|---|
| 176 | |
|---|
| 177 | def new( str = '' ) |
|---|
| 178 | if block_given? |
|---|
| 179 | begin |
|---|
| 180 | f = super |
|---|
| 181 | yield f |
|---|
| 182 | ensure |
|---|
| 183 | f.close if f |
|---|
| 184 | end |
|---|
| 185 | else |
|---|
| 186 | super |
|---|
| 187 | end |
|---|
| 188 | end |
|---|
| 189 | |
|---|
| 190 | alias open new |
|---|
| 191 | |
|---|
| 192 | end |
|---|
| 193 | |
|---|
| 194 | def initialize( str = '' ) |
|---|
| 195 | @dest = str |
|---|
| 196 | @closed = false |
|---|
| 197 | end |
|---|
| 198 | |
|---|
| 199 | def close |
|---|
| 200 | @closed = true |
|---|
| 201 | end |
|---|
| 202 | |
|---|
| 203 | def closed? |
|---|
| 204 | @closed |
|---|
| 205 | end |
|---|
| 206 | |
|---|
| 207 | def string |
|---|
| 208 | @dest |
|---|
| 209 | end |
|---|
| 210 | |
|---|
| 211 | alias value string |
|---|
| 212 | alias to_str string |
|---|
| 213 | |
|---|
| 214 | def size |
|---|
| 215 | @dest.size |
|---|
| 216 | end |
|---|
| 217 | |
|---|
| 218 | alias pos size |
|---|
| 219 | |
|---|
| 220 | def inspect |
|---|
| 221 | "#<#{self.class}:#{@dest ? 'open' : 'closed'},#{id}>" |
|---|
| 222 | end |
|---|
| 223 | |
|---|
| 224 | def print( *args ) |
|---|
| 225 | stream_check! |
|---|
| 226 | raise ArgumentError, 'wrong # of argument (0 for >1)' if args.empty? |
|---|
| 227 | args.each do |s| |
|---|
| 228 | raise ArgumentError, 'nil not allowed' if s.nil? |
|---|
| 229 | @dest << s.to_s |
|---|
| 230 | end |
|---|
| 231 | nil |
|---|
| 232 | end |
|---|
| 233 | |
|---|
| 234 | def puts( *args ) |
|---|
| 235 | stream_check! |
|---|
| 236 | args.each do |str| |
|---|
| 237 | @dest << (s = str.to_s) |
|---|
| 238 | @dest << "\n" unless s[-1] == ?\n |
|---|
| 239 | end |
|---|
| 240 | @dest << "\n" if args.empty? |
|---|
| 241 | nil |
|---|
| 242 | end |
|---|
| 243 | |
|---|
| 244 | def putc( ch ) |
|---|
| 245 | stream_check! |
|---|
| 246 | @dest << ch.chr |
|---|
| 247 | nil |
|---|
| 248 | end |
|---|
| 249 | |
|---|
| 250 | def printf( *args ) |
|---|
| 251 | stream_check! |
|---|
| 252 | @dest << sprintf(*args) |
|---|
| 253 | nil |
|---|
| 254 | end |
|---|
| 255 | |
|---|
| 256 | def write( str ) |
|---|
| 257 | stream_check! |
|---|
| 258 | s = str.to_s |
|---|
| 259 | @dest << s |
|---|
| 260 | s.size |
|---|
| 261 | end |
|---|
| 262 | |
|---|
| 263 | alias syswrite write |
|---|
| 264 | |
|---|
| 265 | def <<( str ) |
|---|
| 266 | stream_check! |
|---|
| 267 | @dest << str.to_s |
|---|
| 268 | self |
|---|
| 269 | end |
|---|
| 270 | |
|---|
| 271 | private |
|---|
| 272 | |
|---|
| 273 | def stream_check! |
|---|
| 274 | @closed and raise IOError, 'closed stream' |
|---|
| 275 | end |
|---|
| 276 | |
|---|
| 277 | end |
|---|