Class: Crypt::PureRubyStringIO
- Includes:
- Enumerable
- Defined in:
- lib/flatulent/crypt/purerubystringio.rb
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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 122 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
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 23 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
34 35 36 37 38 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 34 def <<(obj) requireWritable write obj self end |
#binmode ⇒ Object
40 41 42 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 40 def binmode self end |
#close ⇒ Object
44 45 46 47 48 49 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 44 def close requireOpen @sio_closed_read = true @sio_closed_write = true self end |
#close_read ⇒ Object
51 52 53 54 55 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 51 def close_read raise IOError, "closing non-duplex IO for reading", caller if closed_read? @sio_closed_read = true self end |
#close_write ⇒ Object
57 58 59 60 61 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 57 def close_write raise IOError, "closing non-duplex IO for writing", caller if closed_write? @sio_closed_read = true self end |
#closed? ⇒ Boolean
63 64 65 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 63 def closed? closed_read? && closed_write? end |
#closed_read? ⇒ Boolean
67 68 69 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 67 def closed_read? @sio_closed_read end |
#closed_write? ⇒ Boolean
71 72 73 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 71 def closed_write? @sio_closed_write end |
#each(sep_string = $/, &block) ⇒ Object Also known as: each_line
75 76 77 78 79 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 75 def each(sep_string=$/, &block) requireReadable @sio_string.each(sep_string, &block) @sio_pos = @sio_string.length end |
#each_byte(&block) ⇒ Object
81 82 83 84 85 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 81 def each_byte(&block) requireReadable @sio_string.each_byte(&block) @sio_pos = @sio_string.length end |
#eof ⇒ Object Also known as: eof?
87 88 89 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 87 def eof requireReadable { @sio_pos >= @sio_string.length } end |
#fcntl(integer_cmd, arg) ⇒ Object
91 92 93 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 91 def fcntl(integer_cmd, arg) raise NotImplementedError, "The fcntl() function is unimplemented on this machine", caller end |
#fileno ⇒ Object
95 96 97 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 95 def fileno nil end |
#flush ⇒ Object
99 100 101 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 99 def flush self end |
#fsync ⇒ Object
103 104 105 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 103 def fsync 0 end |
#getc ⇒ Object
107 108 109 110 111 112 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 107 def getc requireReadable char = @sio_string[@sio_pos] @sio_pos += 1 unless char.nil? char end |
#gets(sep_string = $/) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 114 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?
159 160 161 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 159 def isatty flase end |
#length ⇒ Object Also known as: size
163 164 165 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 163 def length @sio_string.length end |
#lineno ⇒ Object
167 168 169 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 167 def lineno @sio_lineno end |
#lineno=(integer) ⇒ Object
171 172 173 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 171 def lineno=(integer) @sio_lineno = integer end |
#path ⇒ Object
175 176 177 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 175 def path nil end |
#pid ⇒ Object
179 180 181 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 179 def pid nil end |
#pos ⇒ Object Also known as: tell
183 184 185 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 183 def pos @sio_pos end |
#pos=(integer) ⇒ Object
187 188 189 190 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 187 def pos=(integer) raise Errno::EINVAL, "Invalid argument", caller if integer < 0 @sio_pos = integer end |
#print(*args) ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 192 def print(*args) requireWritable args.unshift($_) if args.empty args.each { |obj| write(obj) } write($\) unless $\.nil? nil end |
#printf(format_string, *args) ⇒ Object
200 201 202 203 204 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 200 def printf(format_string, *args) requireWritable write format(format_string, *args) nil end |
#putc(obj) ⇒ Object
206 207 208 209 210 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 206 def putc(obj) requireWritable write(obj.is_a?(Numeric) ? sprintf("%c", obj) : obj.to_s[0..0]) obj end |
#puts(*args) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 212 def puts(*args) requireWritable args.unshift("") if args.empty? args.each { |obj| write obj write $/ } nil end |
#read(length = nil, buffer = nil) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 222 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
233 234 235 236 237 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 233 def readchar requireReadable raise EOFError, "End of file reached", caller if eof? getc end |
#readline ⇒ Object
239 240 241 242 243 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 239 def readline requireReadable raise EOFError, "End of file reached", caller if eof? gets end |
#readlines(sep_string = $/) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 245 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
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 255 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
280 281 282 283 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 280 def rewind @sio_pos = 0 @sio_lineno = 0 end |
#seek(amount, whence = SEEK_SET) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 285 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
294 295 296 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 294 def string @sio_string end |
#string=(newstring) ⇒ Object
298 299 300 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 298 def string=(newstring) @sio_string = newstring end |
#sync ⇒ Object
302 303 304 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 302 def sync true end |
#sync=(boolean) ⇒ Object
306 307 308 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 306 def sync=(boolean) boolean end |
#sysread(length = nil, buffer = nil) ⇒ Object
310 311 312 313 314 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 310 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
316 317 318 319 320 321 322 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 316 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.
334 335 336 337 338 339 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 334 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
341 342 343 344 345 346 347 348 |
# File 'lib/flatulent/crypt/purerubystringio.rb', line 341 def ungetc(integer) requireWritable if @sio_pos > 0 then @sio_pos -= 1 putc(integer) @sio_pos -= 1 end end |