1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2010, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 
33 #ifndef SEQAN_HEADER_STREAM_H
34 #define SEQAN_HEADER_STREAM_H
35 
36 #include <iosfwd>
37 
38 namespace SEQAN_NAMESPACE_MAIN
39 {
40 //////////////////////////////////////////////////////////////////////////////
41 /**
42 .Adaption."std::iostream":
43 ..summary:Standard library stream classes.
44 */
45 
46 //////////////////////////////////////////////////////////////////////////////
47 
48 template <typename TValue, typename TTraits>
49 struct Position< ::std::basic_ios<TValue, TTraits> >
50 {
51 	typedef typename ::std::basic_ios<TValue, TTraits>::pos_type Type;
52 };
53 template <typename TValue, typename TTraits>
54 struct Position< ::std::basic_streambuf<TValue, TTraits> >
55 {
56 	typedef typename ::std::basic_streambuf<TValue, TTraits>::pos_type Type;
57 };
58 template <typename TValue, typename TTraits>
59 struct Position< ::std::basic_istream<TValue, TTraits> >
60 {
61 	typedef typename ::std::basic_istream<TValue, TTraits>::pos_type Type;
62 };
63 template <typename TValue, typename TTraits>
64 struct Position< ::std::basic_ostream<TValue, TTraits> >
65 {
66 	typedef typename ::std::basic_ostream<TValue, TTraits>::pos_type Type;
67 };
68 template <typename TValue, typename TTraits>
69 struct Position< ::std::basic_iostream<TValue, TTraits> >
70 {
71 	typedef typename ::std::basic_iostream<TValue, TTraits>::pos_type Type;
72 };
73 template <typename TValue, typename TTraits>
74 struct Position< ::std::basic_stringbuf<TValue, TTraits> >
75 {
76 	typedef typename ::std::basic_stringbuf<TValue, TTraits>::pos_type Type;
77 };
78 template <typename TValue, typename TTraits>
79 struct Position< ::std::basic_istringstream<TValue, TTraits> >
80 {
81 	typedef typename ::std::basic_istringstream<TValue, TTraits>::pos_type Type;
82 };
83 template <typename TValue, typename TTraits>
84 struct Position< ::std::basic_ostringstream<TValue, TTraits> >
85 {
86 	typedef typename ::std::basic_ostringstream<TValue, TTraits>::pos_type Type;
87 };
88 template <typename TValue, typename TTraits>
89 struct Position< ::std::basic_stringstream<TValue, TTraits> >
90 {
91 	typedef typename ::std::basic_stringstream<TValue, TTraits>::pos_type Type;
92 };
93 template <typename TValue, typename TTraits>
94 struct Position< ::std::basic_filebuf<TValue, TTraits> >
95 {
96 	typedef typename ::std::basic_filebuf<TValue, TTraits>::pos_type Type;
97 };
98 template <typename TValue, typename TTraits>
99 struct Position< ::std::basic_ifstream<TValue, TTraits> >
100 {
101 	typedef typename ::std::basic_ifstream<TValue, TTraits>::pos_type Type;
102 };
103 template <typename TValue, typename TTraits>
104 struct Position< ::std::basic_ofstream<TValue, TTraits> >
105 {
106 	typedef typename ::std::basic_ofstream<TValue, TTraits>::pos_type Type;
107 };
108 template <typename TValue, typename TTraits>
109 struct Position< ::std::basic_fstream<TValue, TTraits> >
110 {
111 	typedef typename ::std::basic_fstream<TValue, TTraits>::pos_type Type;
112 };
113 
114 //////////////////////////////////////////////////////////////////////////////
115 
116 template <typename TValue, typename TTraits>
117 struct Value< ::std::basic_ios<TValue, TTraits> >
118 {
119 	typedef typename ::std::basic_ios<TValue, TTraits>::char_type Type;
120 };
121 template <typename TValue, typename TTraits>
122 struct Value< ::std::basic_streambuf<TValue, TTraits> >
123 {
124 	typedef typename ::std::basic_streambuf<TValue, TTraits>::char_type Type;
125 };
126 template <typename TValue, typename TTraits>
127 struct Value< ::std::basic_istream<TValue, TTraits> >
128 {
129 	typedef typename ::std::basic_istream<TValue, TTraits>::char_type Type;
130 };
131 template <typename TValue, typename TTraits>
132 struct Value< ::std::basic_ostream<TValue, TTraits> >
133 {
134 	typedef typename ::std::basic_ostream<TValue, TTraits>::char_type Type;
135 };
136 template <typename TValue, typename TTraits>
137 struct Value< ::std::basic_iostream<TValue, TTraits> >
138 {
139 	typedef typename ::std::basic_iostream<TValue, TTraits>::char_type Type;
140 };
141 template <typename TValue, typename TTraits>
142 struct Value< ::std::basic_stringbuf<TValue, TTraits> >
143 {
144 	typedef typename ::std::basic_stringbuf<TValue, TTraits>::char_type Type;
145 };
146 template <typename TValue, typename TTraits>
147 struct Value< ::std::basic_istringstream<TValue, TTraits> >
148 {
149 	typedef typename ::std::basic_istringstream<TValue, TTraits>::char_type Type;
150 };
151 template <typename TValue, typename TTraits>
152 struct Value< ::std::basic_ostringstream<TValue, TTraits> >
153 {
154 	typedef typename ::std::basic_ostringstream<TValue, TTraits>::char_type Type;
155 };
156 template <typename TValue, typename TTraits>
157 struct Value< ::std::basic_stringstream<TValue, TTraits> >
158 {
159 	typedef typename ::std::basic_stringstream<TValue, TTraits>::char_type Type;
160 };
161 template <typename TValue, typename TTraits>
162 struct Value< ::std::basic_filebuf<TValue, TTraits> >
163 {
164 	typedef typename ::std::basic_filebuf<TValue, TTraits>::char_type Type;
165 };
166 template <typename TValue, typename TTraits>
167 struct Value< ::std::basic_ifstream<TValue, TTraits> >
168 {
169 	typedef typename ::std::basic_ifstream<TValue, TTraits>::char_type Type;
170 };
171 template <typename TValue, typename TTraits>
172 struct Value< ::std::basic_ofstream<TValue, TTraits> >
173 {
174 	typedef typename ::std::basic_ofstream<TValue, TTraits>::char_type Type;
175 };
176 template <typename TValue, typename TTraits>
177 struct Value< ::std::basic_fstream<TValue, TTraits> >
178 {
179 	typedef typename ::std::basic_fstream<TValue, TTraits>::char_type Type;
180 };
181 
182 //////////////////////////////////////////////////////////////////////////////
183 
184 /**.interal.IsTellAndSeekStream_:
185 ..summary:Determines whether stream supports tell and seek functions.
186 ..cat:Metafunction
187 */
188 
189 template <typename T>
190 struct IsTellAndSeekStream_
191 {
192 	typedef False Type;
193 };
194 
195 
196 template <typename TValue, typename TTraits>
197 struct IsTellAndSeekStream_< ::std::basic_ifstream<TValue, TTraits> >
198 {
199 	typedef True Type;
200 };
201 template <typename TValue, typename TTraits>
202 struct IsTellAndSeekStream_< ::std::basic_fstream<TValue, TTraits> >
203 {
204 	typedef True Type;
205 };
206 
207 //////////////////////////////////////////////////////////////////////////////
208 
209 /**
210 .Internal._streamEOF:
211 ..summary:Test stream for being in eof or error state.
212 ..cat:Streams
213 ..signature:_streamEOF(stream)
214 ..param.stream:A stream object.
215 ...type:Adaption."std::iostream"
216 ..returns:$true$, if stream is at end of file or was set to error state, $false$ otherwise.
217 */
218 template <typename TValue, typename TTraits>
219 inline bool
220 _streamEOF(::std::basic_istream<TValue, TTraits> const & me)
221 {
222 SEQAN_CHECKPOINT
223 	// Andreas missed the fact that eof() of a stream is true after reading the eof character
224 	// So reading the last character eof() is false, reading beyond eof() is true
225 	// To fix that we use peek() to get the next character and compare it with the eof character
226 	typedef ::std::basic_istream<TValue, TTraits> TStream;
227 	return me.fail() || const_cast<TStream &>(me).peek() == TTraits::eof();
228 }
229 
230 //////////////////////////////////////////////////////////////////////////////
231 
232 /**
233 .Internal._streamRead:
234 ..summary:Read some characters from stream into a buffer.
235 ..cat:Streams
236 ..signature:_streamRead(target, stream, limit)
237 ..param.target:A buffer that is filled.
238 ..param.stream:An input stream.
239 ...type:Adaption."std::iostream"
240 ..param.limit:The maximal number of characters that is read from $stream$.
241 ..returns:The number of characters read from $stream$.
242 */
243 template <typename TValue, typename TTraits>
244 inline ::std::streamsize
245 _streamRead(TValue * target,
246 			::std::basic_istream<TValue, TTraits> & source,
247 			::std::streamsize limit)
248 {
249 SEQAN_CHECKPOINT
250 	source.read(target, limit);
251 	return source.gcount();
252 }
253 
254 //////////////////////////////////////////////////////////////////////////////
255 
256 /**
257 .Internal._streamGet:
258 ..summary:Read one character from stream.
259 ..cat:Streams
260 ..signature:_streamGet(stream)
261 ..param.stream:An input stream.
262 ...type:Adaption."std::iostream"
263 ..returns:The character read.
264 */
265 
266 template <typename TValue, typename TTraits>
267 inline TValue
268 _streamGet(::std::basic_istream<TValue, TTraits> & source)
269 {
270 SEQAN_CHECKPOINT
271 	return source.get();
272 }
273 
274 //////////////////////////////////////////////////////////////////////////////
275 
276 /**
277 .Internal._streamPeek:
278 ..summary:Return the next character to be read from stream.
279 ..cat:Streams
280 ..signature:_streamPeek(stream)
281 ..param.stream:An input stream.
282 ...type:Adaption."std::iostream"
283 ..returns:The character to be read.
284 */
285 
286 template <typename TValue, typename TTraits>
287 inline TValue
288 _streamPeek(::std::basic_istream<TValue, TTraits> & source)
289 {
290 SEQAN_CHECKPOINT
291 	return source.peek();
292 }
293 
294 //////////////////////////////////////////////////////////////////////////////
295 
296 /**
297 .Internal._streamUnget:
298 ..summary:Put the last read character back into stream.
299 ..cat:Streams
300 ..signature:_streamUnget(stream)
301 ..param.stream:An input stream.
302 ...type:Adaption."std::iostream"
303 */
304 
305 template <typename TValue, typename TTraits>
306 inline void
307 _streamUnget(::std::basic_istream<TValue, TTraits> & source)
308 {
309 SEQAN_CHECKPOINT
310 	source.unget();
311 }
312 
313 //////////////////////////////////////////////////////////////////////////////
314 
315 /**
316 .Internal._streamPut:
317 ..summary:Writes one character to stream.
318 ..cat:Streams
319 ..signature:_streamPut(stream, character)
320 ..param.stream:An input stream.
321 ...type:Adaption."std::iostream"
322 ..param.character:A character that is written to $stream$.
323 */
324 
325 template <typename TValue, typename TTraits, typename TChar>
326 inline void
327 _streamPut(::std::basic_ostream<TValue, TTraits> & target,
328 		   TChar character)
329 {
330 SEQAN_CHECKPOINT
331 	target.put(convert<TValue>(character));
332 }
333 
334 //////////////////////////////////////////////////////////////////////////////
335 
336 /**
337 .Internal._streamTellG:
338 ..cat:Streams
339 ..summary:Gets current position of input stream.
340 ..signature:_streamTellG(stream)
341 ..param.stream:An input stream.
342 ...type:Adaption."std::iostream"
343 ..returns:The current position in $stream$.
344 */
345 template <typename TValue, typename TTraits>
346 inline typename Position< ::std::basic_istream<TValue, TTraits> >::Type
347 _streamTellG(::std::basic_istream<TValue, TTraits> & me)
348 {
349 SEQAN_CHECKPOINT
350 	return me.tellg();
351 }
352 
353 //////////////////////////////////////////////////////////////////////////////
354 /**
355 .Internal._streamTellP:
356 ..cat:Streams
357 ..summary:Gets current position of output stream.
358 ..signature:_streamTellP(stream)
359 ..param.stream:An ouput stream.
360 ...type:Adaption."std::iostream"
361 ..returns:The current position in $stream$.
362 ..see:Internal._streamTellG
363 */
364 template <typename TValue, typename TTraits>
365 inline typename Position< ::std::basic_ostream<TValue, TTraits> >::Type
366 _streamTellP(::std::basic_ostream<TValue, TTraits> & me)
367 {
368 SEQAN_CHECKPOINT
369 	return me.tellp();
370 }
371 
372 //////////////////////////////////////////////////////////////////////////////
373 
374 /**
375 .Internal._streamSeekG:
376 ..summary:Moves input stream to a position.
377 ..cat:Streams
378 ..signature:_streamSeekG(stream, position)
379 ..param.stream:An input stream.
380 ...type:Adaption."std::iostream"
381 ..param.position:A position within the stream.
382 ...remarks:Use @Function._streamTellG@ to get valid stream positions.
383 ..see:Internal._streamTellG
384 */
385 template <typename TValue, typename TTraits>
386 inline void
387 _streamSeekG(::std::basic_istream<TValue, TTraits> & me,
388 	 typename Position< ::std::basic_istream<TValue, TTraits> >::Type pos)
389 {
390 SEQAN_CHECKPOINT
391 	me.clear();
392 	me.seekg(pos);
393 }
394 
395 //////////////////////////////////////////////////////////////////////////////
396 
397 /**
398 .Internal._streamSeekP:
399 ..summary:Moves output stream to a position.
400 ..cat:Streams
401 ..signature:_streamSeekP(stream, position)
402 ..param.stream:An output stream.
403 ...type:Adaption."std::iostream"
404 ..param.position:A position within the stream.
405 ...remarks:Use @Function._streamTellP@ to get valid stream positions.
406 ..see:Internal._streamTellP
407 ..see:Internal._streamSeekG
408 */
409 template <typename TValue, typename TTraits>
410 inline void
411 _streamSeekP(::std::basic_ostream<TValue, TTraits> & me,
412 	 typename Position< ::std::basic_ostream<TValue, TTraits> >::Type pos)
413 {
414 SEQAN_CHECKPOINT
415 	me.clear();
416 	me.seekp(pos);
417 }
418 
419 //////////////////////////////////////////////////////////////////////////////
420 
421 /**
422 .Internal._streamSeek2G:
423 ..summary:Moves input stream position relative to current position.
424 ..cat:Streams
425 ..signature:_streamSeek2G(stream, offset)
426 ..param.stream:An input stream.
427 ...type:Adaption."std::iostream"
428 ..param.offset:The amout the position is changed.
429 ...remarks:If this value is negative.
430 ..see:Internal._streamSeekG
431 */
432 template <typename TValue, typename TTraits>
433 inline void
434 _streamSeek2G(::std::basic_istream<TValue, TTraits> & me,
435 	 int off)
436 {
437 SEQAN_CHECKPOINT
438 	me.seekg(off, ::std::ios_base::cur);
439 }
440 
441 
442 //////////////////////////////////////////////////////////////////////////////
443 
444 } //namespace SEQAN_NAMESPACE_MAIN
445 
446 #endif //#ifndef SEQAN_HEADER_...
447