Class: StringInput

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/tmail/stringio.rb

Overview

– Copyright © 1998-2003 Minero Aoki <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note: Originally licensed under LGPL v2+. Using MIT license for Rails with permission of Minero Aoki. ++

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#reject, #sort_by

Constructor Details

#initialize(str) ⇒ StringInput

Returns a new instance of StringInput.



55
56
57
58
59
60
# File 'lib/tmail/stringio.rb', line 55

def initialize( str )
  @src = str
  @pos = 0
  @closed = false
  @lineno = 0
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



62
63
64
# File 'lib/tmail/stringio.rb', line 62

def lineno
  @lineno
end

Class Method Details

.new(str) ⇒ Object Also known as: open



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tmail/stringio.rb', line 38

def new( str )
  if block_given?
    begin
      f = super
      yield f
    ensure
      f.close if f
    end
  else
    super
  end
end

Instance Method Details

#closeObject



72
73
74
75
76
# File 'lib/tmail/stringio.rb', line 72

def close
  stream_check!
  @pos = nil
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/tmail/stringio.rb', line 78

def closed?
  @closed
end

#each(&block) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/tmail/stringio.rb', line 116

def each( &block )
  stream_check!
  begin
    @src.each(&block)
  ensure
    @pos = 0
  end
end

#eof?Boolean

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/tmail/stringio.rb', line 111

def eof?
  stream_check!
  @pos > @src.size
end

#getcObject



141
142
143
144
145
146
147
# File 'lib/tmail/stringio.rb', line 141

def getc
  stream_check!
  ch = @src[@pos]
  @pos += 1
  @pos += 1 if @pos == @src.size
  ch
end

#getsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tmail/stringio.rb', line 125

def gets
  stream_check!
  if idx = @src.index(?\n, @pos)
    idx += 1  # "\n".size
    line = @src[ @pos ... idx ]
    @pos = idx
    @pos += 1 if @pos == @src.size
  else
    line = @src[ @pos .. -1 ]
    @pos = @src.size + 1
  end
  @lineno += 1

  line
end

#inspectObject



68
69
70
# File 'lib/tmail/stringio.rb', line 68

def inspect
  "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
end

#posObject Also known as: tell



82
83
84
85
# File 'lib/tmail/stringio.rb', line 82

def pos
  stream_check!
  [@pos, @src.size].min
end

#read(len = nil) ⇒ Object Also known as: sysread



149
150
151
152
153
154
155
156
# File 'lib/tmail/stringio.rb', line 149

def read( len = nil )
  stream_check!
  return read_all unless len
  str = @src[@pos, len]
  @pos += len
  @pos += 1 if @pos == @src.size
  str
end

#read_allObject



160
161
162
163
164
165
166
# File 'lib/tmail/stringio.rb', line 160

def read_all
  stream_check!
  return nil if eof?
  rest = @src[@pos ... @src.size]
  @pos = @src.size + 1
  rest
end

#rewindObject



106
107
108
109
# File 'lib/tmail/stringio.rb', line 106

def rewind
  stream_check!
  @pos = 0
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tmail/stringio.rb', line 89

def seek( offset, whence = IO::SEEK_SET )
  stream_check!
  case whence
  when IO::SEEK_SET
    @pos = offset
  when IO::SEEK_CUR
    @pos += offset
  when IO::SEEK_END
    @pos = @src.size - offset
  else
    raise ArgumentError, "unknown seek flag: #{whence}"
  end
  @pos = 0 if @pos < 0
  @pos = [@pos, @src.size + 1].min
  offset
end

#stream_check!Object



168
169
170
# File 'lib/tmail/stringio.rb', line 168

def stream_check!
  @closed and raise IOError, 'closed stream'
end

#stringObject



64
65
66
# File 'lib/tmail/stringio.rb', line 64

def string
  @src
end