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