Class: PureRubyStringIO
- Inherits:
-
Object
- Object
- PureRubyStringIO
- Includes:
- Enumerable
- Defined in:
- lib/bijou/prstringio.rb
Overview
This file is is not part of Bijou, but is included as a helper for Ruby installations without stringio.
Reference: rubyforge.org/projects/prstringio/
Original comment:
To load this in emergency situations when loading StringIO fails then use the following:
begin
require 'stringio'
rescue LoadError
require 'purerubystringio' #or just put this entire file in your code at this point.
end
If you need stringio and you know for sure it isn’t going to be available then you can easily fake it without changing your existing code. Just add the following before the rest of your code starts. class StringIO < PureSybyStringIO end Any code that uses StringIO.new will now work. So will any subclass definitions. If you are only subclassing StringIO and you are willing to make small changes to you code you can change your class definitions to:
class MyClass < Object.const_defined?(:StringIO) ? StringIO : PureRubyStringIO
...
end
That will automatically select which ever is avaialable at the time. Savy coders, by now, will have begin to consider the delegator mixin. I’ll leave that exercise for you to finish. ;)
Constant Summary collapse
- SEEK_CUR =
IO::SEEK_CUR
- SEEK_END =
IO::SEEK_END
- SEEK_SET =
IO::SEEK_SET
- @@relayMethods =
[:<<, :all?, :any?, :binmode, :close, :close_read, :close_write, :closed?, :closed_read?, :closed_write?, :collect, :detect, :each, :each_byte, :each_line, :each_with_index, :entries, :eof, :eof?, :fcntl, :fileno, :find, :find_all, :flush, :fsync, :getc, :gets, :grep, :include?, :inject, :isatty, :length, :lineno, :lineno=, :map, :max, :member?, :min, :partition, :path, :pid, :pos, :pos=, :print, :printf, :putc, :puts, :read, :readchar, :readline, :readlines, :reject, :rewind, :seek, :select, :size, :sort, :sort_by, :string, :string=, :sync, :sync=, :sysread, :syswrite, :tell, :truncate, :tty?, :ungetc, :write, :zip]
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #binmode ⇒ Object
- #close ⇒ Object
- #close_read ⇒ Object
- #close_write ⇒ Object
- #closed? ⇒ Boolean
- #closed_read? ⇒ Boolean
- #closed_write? ⇒ Boolean
- #each(sep_string = $/, &block) ⇒ Object (also: #each_line)
- #each_byte(&block) ⇒ Object
- #eof ⇒ Object (also: #eof?)
- #fcntl(integer_cmd, arg) ⇒ Object
- #fileno ⇒ Object
- #flush ⇒ Object
- #fsync ⇒ Object
- #getc ⇒ Object
- #gets(sep_string = $/) ⇒ Object
-
#initialize(string = "", mode = "r+") ⇒ PureRubyStringIO
constructor
A new instance of PureRubyStringIO.
- #isatty ⇒ Object (also: #tty?)
- #length ⇒ Object (also: #size)
- #lineno ⇒ Object
- #lineno=(integer) ⇒ Object
- #path ⇒ Object
- #pid ⇒ Object
- #pos ⇒ Object (also: #tell)
- #pos=(integer) ⇒ Object
- #print(*args) ⇒ Object
- #printf(format_string, *args) ⇒ Object
- #putc(obj) ⇒ Object
- #puts(*args) ⇒ Object
- #read(length = nil, buffer = nil) ⇒ Object
- #readchar ⇒ Object
- #readline ⇒ Object
- #readlines(sep_string = $/) ⇒ Object
- #reopen(string, mode = nil) ⇒ Object
- #rewind ⇒ Object
- #seek(amount, whence = SEEK_SET) ⇒ Object
- #string ⇒ Object
- #string=(newstring) ⇒ Object
- #sync ⇒ Object
- #sync=(boolean) ⇒ Object
- #sysread(length = nil, buffer = nil) ⇒ Object
- #syswrite(string) ⇒ Object (also: #write)
-
#truncate(integer) ⇒ Object
In ruby 1.8.4 truncate differs from the docs in two ways.
- #ungetc(integer) ⇒ Object
Constructor Details
#initialize(string = "", mode = "r+") ⇒ PureRubyStringIO
Returns a new instance of PureRubyStringIO.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/bijou/prstringio.rb', line 142 def initialize(string="", mode="r+") @sio_string = string.to_s @sio_lineno = 0 @mode = mode @relay = nil case mode.delete("b") when "r" @sio_closed_read = false @sio_closed_write = true @sio_pos = 0 when "r+" @sio_closed_read = false @sio_closed_write = false @sio_pos = 0 when "w" @sio_closed_read = true @sio_closed_write = false @sio_pos = 0 @sio_string.replace("") when "w+" @sio_closed_read = false @sio_closed_write = false @sio_pos = 0 @sio_string.replace("") when "a" @sio_closed_read = true @sio_closed_write = false @sio_pos = @sio_string.length when "a+" @sio_closed_read = false @sio_closed_write = false @sio_pos = @sio_string.length else raise ArgumentError, "illegal access mode #{mode}", caller end end |
Class Method Details
.open(string = "", mode = "r+") ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bijou/prstringio.rb', line 43 def self.open(string="", mode="r+") if block_given? then sio = new(string, mode) rc = yield(sio) sio.close rc else new(string, mode) end end |
Instance Method Details
#<<(obj) ⇒ Object
54 55 56 57 58 |
# File 'lib/bijou/prstringio.rb', line 54 def <<(obj) requireWritable write obj self end |
#binmode ⇒ Object
60 61 62 |
# File 'lib/bijou/prstringio.rb', line 60 def binmode self end |
#close ⇒ Object
64 65 66 67 68 69 |
# File 'lib/bijou/prstringio.rb', line 64 def close requireOpen @sio_closed_read = true @sio_closed_write = true self end |
#close_read ⇒ Object
71 72 73 74 75 |
# File 'lib/bijou/prstringio.rb', line 71 def close_read raise IOError, "closing non-duplex IO for reading", caller if closed_read? @sio_closed_read = true self end |
#close_write ⇒ Object
77 78 79 80 81 |
# File 'lib/bijou/prstringio.rb', line 77 def close_write raise IOError, "closing non-duplex IO for writing", caller if closed_write? @sio_closed_read = true self end |
#closed? ⇒ Boolean
83 84 85 |
# File 'lib/bijou/prstringio.rb', line 83 def closed? closed_read? && closed_write? end |
#closed_read? ⇒ Boolean
87 88 89 |
# File 'lib/bijou/prstringio.rb', line 87 def closed_read? @sio_closed_read end |
#closed_write? ⇒ Boolean
91 92 93 |
# File 'lib/bijou/prstringio.rb', line 91 def closed_write? @sio_closed_write end |
#each(sep_string = $/, &block) ⇒ Object Also known as: each_line
95 96 97 98 99 |
# File 'lib/bijou/prstringio.rb', line 95 def each(sep_string=$/, &block) requireReadable @sio_string.each(sep_string, &block) @sio_pos = @sio_string.length end |
#each_byte(&block) ⇒ Object
101 102 103 104 105 |
# File 'lib/bijou/prstringio.rb', line 101 def each_byte(&block) requireReadable @sio_string.each_byte(&block) @sio_pos = @sio_string.length end |
#eof ⇒ Object Also known as: eof?
107 108 109 |
# File 'lib/bijou/prstringio.rb', line 107 def eof requireReadable { @sio_pos >= @sio_string.length } end |
#fcntl(integer_cmd, arg) ⇒ Object
111 112 113 |
# File 'lib/bijou/prstringio.rb', line 111 def fcntl(integer_cmd, arg) raise NotImplementedError, "The fcntl() function is unimplemented on this machine", caller end |
#fileno ⇒ Object
115 116 117 |
# File 'lib/bijou/prstringio.rb', line 115 def fileno nil end |
#flush ⇒ Object
119 120 121 |
# File 'lib/bijou/prstringio.rb', line 119 def flush self end |
#fsync ⇒ Object
123 124 125 |
# File 'lib/bijou/prstringio.rb', line 123 def fsync 0 end |
#getc ⇒ Object
127 128 129 130 131 132 |
# File 'lib/bijou/prstringio.rb', line 127 def getc requireReadable char = @sio_string[@sio_pos] @sio_pos += 1 unless char.nil? char end |
#gets(sep_string = $/) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/bijou/prstringio.rb', line 134 def gets(sep_string=$/) requireReadable @sio_lineno += 1 pstart = @sio_pos @sio_pos = @sio_string.index(sep_string, @sio_pos) || [@sio_string.length, @sio_pos].max @sio_string[pstart..@sio_pos] end |
#isatty ⇒ Object Also known as: tty?
179 180 181 |
# File 'lib/bijou/prstringio.rb', line 179 def isatty flase end |
#length ⇒ Object Also known as: size
183 184 185 |
# File 'lib/bijou/prstringio.rb', line 183 def length @sio_string.length end |
#lineno ⇒ Object
187 188 189 |
# File 'lib/bijou/prstringio.rb', line 187 def lineno @sio_lineno end |
#lineno=(integer) ⇒ Object
191 192 193 |
# File 'lib/bijou/prstringio.rb', line 191 def lineno=(integer) @sio_lineno = integer end |
#path ⇒ Object
195 196 197 |
# File 'lib/bijou/prstringio.rb', line 195 def path nil end |
#pid ⇒ Object
199 200 201 |
# File 'lib/bijou/prstringio.rb', line 199 def pid nil end |
#pos ⇒ Object Also known as: tell
203 204 205 |
# File 'lib/bijou/prstringio.rb', line 203 def pos @sio_pos end |
#pos=(integer) ⇒ Object
207 208 209 210 |
# File 'lib/bijou/prstringio.rb', line 207 def pos=(integer) raise Errno::EINVAL, "Invalid argument", caller if integer < 0 @sio_pos = integer end |
#print(*args) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/bijou/prstringio.rb', line 212 def print(*args) requireWritable args.unshift($_) if args.empty args.each { |obj| write(obj) } write($\) unless $\.nil? nil end |
#printf(format_string, *args) ⇒ Object
220 221 222 223 224 |
# File 'lib/bijou/prstringio.rb', line 220 def printf(format_string, *args) requireWritable write format(format_string, *args) nil end |
#putc(obj) ⇒ Object
226 227 228 229 230 |
# File 'lib/bijou/prstringio.rb', line 226 def putc(obj) requireWritable write(obj.is_a?(Numeric) ? sprintf("%c", obj) : obj.to_s[0..0]) obj end |
#puts(*args) ⇒ Object
232 233 234 235 236 237 238 239 240 |
# File 'lib/bijou/prstringio.rb', line 232 def puts(*args) requireWritable args.unshift("") if args.empty? args.each { |obj| write obj write $/ } nil end |
#read(length = nil, buffer = nil) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/bijou/prstringio.rb', line 242 def read(length=nil, buffer=nil) requireReadable len = length || [@sio_string.length - @sio_pos, 0].max raise ArgumentError, "negative length #{len} given", caller if len < 0 buffer ||= "" pstart = @sio_pos @sio_pos += len buffer.replace(@sio_string[pstart..@sio_pos]) buffer.empty? && !length.nil? ? nil : buffer end |
#readchar ⇒ Object
253 254 255 256 257 |
# File 'lib/bijou/prstringio.rb', line 253 def readchar requireReadable raise EOFError, "End of file reached", caller if eof? getc end |
#readline ⇒ Object
259 260 261 262 263 |
# File 'lib/bijou/prstringio.rb', line 259 def readline requireReadable raise EOFError, "End of file reached", caller if eof? gets end |
#readlines(sep_string = $/) ⇒ Object
265 266 267 268 269 270 271 272 273 |
# File 'lib/bijou/prstringio.rb', line 265 def readlines(sep_string=$/) requireReadable raise EOFError, "End of file reached", caller if eof? rc = [] until eof rc << gets(sep_string) end rc end |
#reopen(string, mode = nil) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/bijou/prstringio.rb', line 275 def reopen(string, mode=nil) if string.is_a?(self.class) then raise ArgumentError, "wrong number of arguments (2 for 1)", caller if !mode.nil? @relay = string instance_eval(%Q{ class << self @@relayMethods.each { |name| define_method(name, ObjectSpace._id2ref(#{@relay.object_id}).method(("original_" + name.to_s).to_sym).to_proc) } end }) else raise ArgumentError, "wrong number of arguments (1 for 2)", caller if mode.nil? class << self @@relayMethods.each { |name| alias_method(name, "original_#{name}".to_sym) public name } @relay = nil end unless @relay.nil? @sio_string = string.to_s @mode = mode end end |
#rewind ⇒ Object
300 301 302 303 |
# File 'lib/bijou/prstringio.rb', line 300 def rewind @sio_pos = 0 @sio_lineno = 0 end |
#seek(amount, whence = SEEK_SET) ⇒ Object
305 306 307 308 309 310 311 312 |
# File 'lib/bijou/prstringio.rb', line 305 def seek(amount, whence=SEEK_SET) if whence == SEEK_CUR then offset += @sio_pos elsif whence == SEEK_END then offset += size end @sio_pos = offset end |
#string ⇒ Object
314 315 316 |
# File 'lib/bijou/prstringio.rb', line 314 def string @sio_string end |
#string=(newstring) ⇒ Object
318 319 320 |
# File 'lib/bijou/prstringio.rb', line 318 def string=(newstring) @sio_string = newstring end |
#sync ⇒ Object
322 323 324 |
# File 'lib/bijou/prstringio.rb', line 322 def sync true end |
#sync=(boolean) ⇒ Object
326 327 328 |
# File 'lib/bijou/prstringio.rb', line 326 def sync=(boolean) boolean end |
#sysread(length = nil, buffer = nil) ⇒ Object
330 331 332 333 334 |
# File 'lib/bijou/prstringio.rb', line 330 def sysread(length=nil, buffer=nil) requireReadable raise EOFError, "End of file reached", caller if eof? read(length, buffer) end |
#syswrite(string) ⇒ Object Also known as: write
336 337 338 339 340 341 342 |
# File 'lib/bijou/prstringio.rb', line 336 def syswrite(string) requireWritable addition = "\000" * (@sio_string.length - @sio_pos) + string.to_s @sio_string[@sio_pos..(addition.length - 1)] = addition @sio_pos += addition.size addition.size end |
#truncate(integer) ⇒ Object
In ruby 1.8.4 truncate differs from the docs in two ways. First, if an integer greater that the length is given then the string is expanded to the new integer length. As this expansion seems to contain junk characters instead of nulls I suspect this may be a flaw in the C code which could cause a core dump if abused/used. Second, the documentation states that truncate returns 0. It returns the integer instead. This implementation follows the documentation in the first instance as I suspect this will be fixed in the C code. In the second instance, it follows the actions of the C code instead of the docs. This was decided as it causes no immedeate harm and this ruby implentation is to be as compatable as possible with the C version. Should the C version change to match the docs the ruby version will be simple to update as well.
354 355 356 357 358 359 |
# File 'lib/bijou/prstringio.rb', line 354 def truncate(integer) requireWritable raise Errno::EINVAL, "Invalid argument - negative length", caller if integer < 0 @sio_string[[integer, @sio_string.length].max..-1] = "" integer end |
#ungetc(integer) ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/bijou/prstringio.rb', line 361 def ungetc(integer) requireWritable if @sio_pos > 0 then @sio_pos -= 1 # BUGBUG: This wreaks havok with the existing string. # putc(integer) # @sio_pos -= 1 # We use this as a simple workaround since we're not modifying the stream. @sio_string[@sio_pos] = integer end end |