Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / file / stream.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: stream.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_STREAM_H
22 #define SEQAN_HEADER_STREAM_H
23
24 #include <iosfwd>
25
26 namespace SEQAN_NAMESPACE_MAIN
27 {
28 //////////////////////////////////////////////////////////////////////////////
29 /**
30 .Adaption."std::iostream":
31 ..summary:Standard library stream classes.
32 */
33
34 //////////////////////////////////////////////////////////////////////////////
35         
36 template <typename TValue, typename TTraits>
37 struct Position< ::std::basic_ios<TValue, TTraits> >
38 {
39         typedef typename ::std::basic_ios<TValue, TTraits>::pos_type Type;
40 };
41 template <typename TValue, typename TTraits>
42 struct Position< ::std::basic_streambuf<TValue, TTraits> >
43 {
44         typedef typename ::std::basic_streambuf<TValue, TTraits>::pos_type Type;
45 };
46 template <typename TValue, typename TTraits>
47 struct Position< ::std::basic_istream<TValue, TTraits> >
48 {
49         typedef typename ::std::basic_istream<TValue, TTraits>::pos_type Type;
50 };
51 template <typename TValue, typename TTraits>
52 struct Position< ::std::basic_ostream<TValue, TTraits> >
53 {
54         typedef typename ::std::basic_ostream<TValue, TTraits>::pos_type Type;
55 };
56 template <typename TValue, typename TTraits>
57 struct Position< ::std::basic_iostream<TValue, TTraits> >
58 {
59         typedef typename ::std::basic_iostream<TValue, TTraits>::pos_type Type;
60 };
61 template <typename TValue, typename TTraits>
62 struct Position< ::std::basic_stringbuf<TValue, TTraits> >
63 {
64         typedef typename ::std::basic_stringbuf<TValue, TTraits>::pos_type Type;
65 };
66 template <typename TValue, typename TTraits>
67 struct Position< ::std::basic_istringstream<TValue, TTraits> >
68 {
69         typedef typename ::std::basic_istringstream<TValue, TTraits>::pos_type Type;
70 };
71 template <typename TValue, typename TTraits>
72 struct Position< ::std::basic_ostringstream<TValue, TTraits> >
73 {
74         typedef typename ::std::basic_ostringstream<TValue, TTraits>::pos_type Type;
75 };
76 template <typename TValue, typename TTraits>
77 struct Position< ::std::basic_stringstream<TValue, TTraits> >
78 {
79         typedef typename ::std::basic_stringstream<TValue, TTraits>::pos_type Type;
80 };
81 template <typename TValue, typename TTraits>
82 struct Position< ::std::basic_filebuf<TValue, TTraits> >
83 {
84         typedef typename ::std::basic_filebuf<TValue, TTraits>::pos_type Type;
85 };
86 template <typename TValue, typename TTraits>
87 struct Position< ::std::basic_ifstream<TValue, TTraits> >
88 {
89         typedef typename ::std::basic_ifstream<TValue, TTraits>::pos_type Type;
90 };
91 template <typename TValue, typename TTraits>
92 struct Position< ::std::basic_ofstream<TValue, TTraits> >
93 {
94         typedef typename ::std::basic_ofstream<TValue, TTraits>::pos_type Type;
95 };
96 template <typename TValue, typename TTraits>
97 struct Position< ::std::basic_fstream<TValue, TTraits> >
98 {
99         typedef typename ::std::basic_fstream<TValue, TTraits>::pos_type Type;
100 };
101
102 //////////////////////////////////////////////////////////////////////////////
103         
104 template <typename TValue, typename TTraits>
105 struct Value< ::std::basic_ios<TValue, TTraits> >
106 {
107         typedef typename ::std::basic_ios<TValue, TTraits>::char_type Type;
108 };
109 template <typename TValue, typename TTraits>
110 struct Value< ::std::basic_streambuf<TValue, TTraits> >
111 {
112         typedef typename ::std::basic_streambuf<TValue, TTraits>::char_type Type;
113 };
114 template <typename TValue, typename TTraits>
115 struct Value< ::std::basic_istream<TValue, TTraits> >
116 {
117         typedef typename ::std::basic_istream<TValue, TTraits>::char_type Type;
118 };
119 template <typename TValue, typename TTraits>
120 struct Value< ::std::basic_ostream<TValue, TTraits> >
121 {
122         typedef typename ::std::basic_ostream<TValue, TTraits>::char_type Type;
123 };
124 template <typename TValue, typename TTraits>
125 struct Value< ::std::basic_iostream<TValue, TTraits> >
126 {
127         typedef typename ::std::basic_iostream<TValue, TTraits>::char_type Type;
128 };
129 template <typename TValue, typename TTraits>
130 struct Value< ::std::basic_stringbuf<TValue, TTraits> >
131 {
132         typedef typename ::std::basic_stringbuf<TValue, TTraits>::char_type Type;
133 };
134 template <typename TValue, typename TTraits>
135 struct Value< ::std::basic_istringstream<TValue, TTraits> >
136 {
137         typedef typename ::std::basic_istringstream<TValue, TTraits>::char_type Type;
138 };
139 template <typename TValue, typename TTraits>
140 struct Value< ::std::basic_ostringstream<TValue, TTraits> >
141 {
142         typedef typename ::std::basic_ostringstream<TValue, TTraits>::char_type Type;
143 };
144 template <typename TValue, typename TTraits>
145 struct Value< ::std::basic_stringstream<TValue, TTraits> >
146 {
147         typedef typename ::std::basic_stringstream<TValue, TTraits>::char_type Type;
148 };
149 template <typename TValue, typename TTraits>
150 struct Value< ::std::basic_filebuf<TValue, TTraits> >
151 {
152         typedef typename ::std::basic_filebuf<TValue, TTraits>::char_type Type;
153 };
154 template <typename TValue, typename TTraits>
155 struct Value< ::std::basic_ifstream<TValue, TTraits> >
156 {
157         typedef typename ::std::basic_ifstream<TValue, TTraits>::char_type Type;
158 };
159 template <typename TValue, typename TTraits>
160 struct Value< ::std::basic_ofstream<TValue, TTraits> >
161 {
162         typedef typename ::std::basic_ofstream<TValue, TTraits>::char_type Type;
163 };
164 template <typename TValue, typename TTraits>
165 struct Value< ::std::basic_fstream<TValue, TTraits> >
166 {
167         typedef typename ::std::basic_fstream<TValue, TTraits>::char_type Type;
168 };
169
170 //////////////////////////////////////////////////////////////////////////////
171
172 /**.interal._IsTellSeekStream:
173 ..summary:Determines whether stream supports tell and seek functions.
174 ..cat:Metafunction
175 */
176
177 template <typename T>
178 struct _IsTellSeekStream
179 {
180         typedef False Type;
181 };
182
183
184 template <typename TValue, typename TTraits>
185 struct _IsTellSeekStream< ::std::basic_ifstream<TValue, TTraits> >
186 {
187         typedef True Type;
188 };
189 template <typename TValue, typename TTraits>
190 struct _IsTellSeekStream< ::std::basic_fstream<TValue, TTraits> >
191 {
192         typedef True Type;
193 };
194
195 //////////////////////////////////////////////////////////////////////////////
196
197 /**
198 .Internal._streamEOF:
199 ..summary:Test stream for being in eof or error state.
200 ..cat:Streams
201 ..signature:_streamEOF(stream)
202 ..param.stream:A stream object.
203 ...type:Adaption."std::iostream"
204 ..returns:$true$, if stream is at end of file or was set to error state, $false$ otherwise.
205 */
206 template <typename TValue, typename TTraits>
207 inline bool 
208 _streamEOF(::std::basic_ios<TValue, TTraits> const & me)
209 {
210 SEQAN_CHECKPOINT
211         return me.eof() || me.fail();
212 }
213
214 //////////////////////////////////////////////////////////////////////////////
215  
216 /**
217 .Internal._streamRead:
218 ..summary:Read some characters from stream into a buffer.
219 ..cat:Streams
220 ..signature:_streamRead(target, stream, limit)
221 ..param.target:A buffer that is filled.
222 ..param.stream:An input stream.
223 ...type:Adaption."std::iostream"
224 ..param.limit:The maximal number of characters that is read from $stream$.
225 ..returns:The number of characters read from $stream$.
226 */
227 template <typename TValue, typename TTraits>
228 inline ::std::streamsize 
229 _streamRead(TValue * target,
230                         ::std::basic_istream<TValue, TTraits> & source,
231                         ::std::streamsize limit)
232 {
233 SEQAN_CHECKPOINT
234         source.read(target, limit);
235         return source.gcount();
236 }
237
238 //////////////////////////////////////////////////////////////////////////////
239
240 /**
241 .Internal._streamGet:
242 ..summary:Read one character from stream.
243 ..cat:Streams
244 ..signature:_streamGet(stream)
245 ..param.stream:An input stream.
246 ...type:Adaption."std::iostream"
247 ..returns:The character read.
248 */
249
250 template <typename TValue, typename TTraits>
251 inline TValue 
252 _streamGet(::std::basic_istream<TValue, TTraits> & source)
253 {
254 SEQAN_CHECKPOINT
255         return source.get();
256 }
257
258 //////////////////////////////////////////////////////////////////////////////
259
260 /**
261 .Internal._streamPeek:
262 ..summary:Return the next character to be read from stream.
263 ..cat:Streams
264 ..signature:_streamPeek(stream)
265 ..param.stream:An input stream.
266 ...type:Adaption."std::iostream"
267 ..returns:The character to be read.
268 */
269
270 template <typename TValue, typename TTraits>
271 inline TValue 
272 _streamPeek(::std::basic_istream<TValue, TTraits> & source)
273 {
274 SEQAN_CHECKPOINT
275         return source.peek();
276 }
277
278 //////////////////////////////////////////////////////////////////////////////
279
280 /**
281 .Internal._streamUnget:
282 ..summary:Put the last read character back into stream.
283 ..cat:Streams
284 ..signature:_streamUnget(stream)
285 ..param.stream:An input stream.
286 ...type:Adaption."std::iostream"
287 */
288
289 template <typename TValue, typename TTraits>
290 inline void
291 _streamUnget(::std::basic_istream<TValue, TTraits> & source)
292 {
293 SEQAN_CHECKPOINT
294         source.unget();
295 }
296
297 //////////////////////////////////////////////////////////////////////////////
298
299 /**
300 .Internal._streamPut:
301 ..summary:Writes one character to stream.
302 ..cat:Streams
303 ..signature:_streamPut(stream, character)
304 ..param.stream:An input stream.
305 ...type:Adaption."std::iostream"
306 ..param.character:A character that is written to $stream$.
307 */
308
309 template <typename TValue, typename TTraits, typename TChar>
310 inline void
311 _streamPut(::std::basic_ostream<TValue, TTraits> & target,
312                    TChar character)
313 {
314 SEQAN_CHECKPOINT
315         target.put(convert<TValue>(character));
316 }
317
318 //////////////////////////////////////////////////////////////////////////////
319
320 /**
321 .Internal._streamTellG:
322 ..cat:Streams
323 ..summary:Gets current position of input stream.
324 ..signature:_streamTellG(stream)
325 ..param.stream:An input stream.
326 ...type:Adaption."std::iostream"
327 ..returns:The current position in $stream$.
328 */
329 template <typename TValue, typename TTraits>
330 inline typename Position< ::std::basic_istream<TValue, TTraits> >::Type
331 _streamTellG(::std::basic_istream<TValue, TTraits> & me)
332 {
333 SEQAN_CHECKPOINT
334         return me.tellg();
335 }
336
337 //////////////////////////////////////////////////////////////////////////////
338 /**
339 .Internal._streamTellP:
340 ..cat:Streams
341 ..summary:Gets current position of output stream.
342 ..signature:_streamTellP(stream)
343 ..param.stream:An ouput stream.
344 ...type:Adaption."std::iostream"
345 ..returns:The current position in $stream$.
346 ..see:Internal._streamTellG
347 */
348 template <typename TValue, typename TTraits>
349 inline typename Position< ::std::basic_ostream<TValue, TTraits> >::Type
350 _streamTellP(::std::basic_ostream<TValue, TTraits> & me)
351 {
352 SEQAN_CHECKPOINT
353         return me.tellp();
354 }
355
356 //////////////////////////////////////////////////////////////////////////////
357
358 /**
359 .Internal._streamSeekG:
360 ..summary:Moves input stream to a position.
361 ..cat:Streams
362 ..signature:_streamSeekG(stream, position)
363 ..param.stream:An input stream.
364 ...type:Adaption."std::iostream"
365 ..param.position:A position within the stream.
366 ...remarks:Use @Function._streamTellG@ to get valid stream positions.
367 ..see:Internal._streamTellG
368 */
369 template <typename TValue, typename TTraits>
370 inline void
371 _streamSeekG(::std::basic_istream<TValue, TTraits> & me,
372          typename Position< ::std::basic_istream<TValue, TTraits> >::Type pos)
373 {
374 SEQAN_CHECKPOINT
375         me.clear();
376         me.seekg(pos);
377 }
378
379 //////////////////////////////////////////////////////////////////////////////
380
381 /**
382 .Internal._streamSeekP:
383 ..summary:Moves output stream to a position.
384 ..cat:Streams
385 ..signature:_streamSeekP(stream, position)
386 ..param.stream:An output stream.
387 ...type:Adaption."std::iostream"
388 ..param.position:A position within the stream.
389 ...remarks:Use @Function._streamTellP@ to get valid stream positions.
390 ..see:Internal._streamTellP
391 ..see:Internal._streamSeekG
392 */
393 template <typename TValue, typename TTraits>
394 inline void
395 _streamSeekP(::std::basic_ostream<TValue, TTraits> & me,
396          typename Position< ::std::basic_ostream<TValue, TTraits> >::Type pos)
397 {
398 SEQAN_CHECKPOINT
399         me.clear();
400         me.seekp(pos);
401 }
402
403 //////////////////////////////////////////////////////////////////////////////
404
405 /**
406 .Internal._streamSeek2G:
407 ..summary:Moves input stream position relative to current position.
408 ..cat:Streams
409 ..signature:_streamSeek2G(stream, offset)
410 ..param.stream:An input stream.
411 ...type:Adaption."std::iostream"
412 ..param.offset:The amout the position is changed.
413 ...remarks:If this value is negative.
414 ..see:Internal._streamSeekG
415 */
416 template <typename TValue, typename TTraits>
417 inline void
418 _streamSeek2G(::std::basic_istream<TValue, TTraits> & me,
419          int off)
420 {
421 SEQAN_CHECKPOINT
422         me.seekg(off, ::std::ios_base::cur);
423 }
424
425
426 //////////////////////////////////////////////////////////////////////////////
427
428 } //namespace SEQAN_NAMESPACE_MAIN
429
430 #endif //#ifndef SEQAN_HEADER_...