Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / file / stream_algorithms.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_algorithms.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_STREAM_ALGORITHMS_H
22 #define SEQAN_HEADER_STREAM_ALGORITHMS_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27 //////////////////////////////////////////////////////////////////////////////
28
29
30 /**
31 .Internal._streamPutInt:
32 ..summary:Converts an integer to a character and writes it to stream.
33 ..cat:Streams
34 ..signature:_streamPutInt(stream, number [, format_string])
35 ..param.target:An output stream.
36 ...type:Adaption."std::iostream"
37 ..param.number:A number that is written to $stream$.
38 */
39 template <typename TStream>
40 inline void
41 _streamPutInt(TStream & target,
42                           int number, 
43                           char const * format_string)
44 {
45 SEQAN_CHECKPOINT
46         char str[BitsPerValue<int>::VALUE];
47         sprintf(str, format_string, number);
48         _streamWrite(target, str);
49 }
50 template <typename TStream>
51 inline void
52 _streamPutInt(TStream & target,
53                           int number)
54 {
55 SEQAN_CHECKPOINT
56         _streamPutInt(target, number, "%d");
57 }
58
59 /**
60 .Internal._streamPutFloat:
61 ..summary:Converts a float to a character and writes it to stream.
62 ..cat:Streams
63 ..signature:_streamPutFloat(stream, number [, format_string])
64 ..param.target:An output stream.
65 ...type:Adaption."std::iostream"
66 ..param.number:A number that is written to $stream$.
67 */
68 template <typename TStream>
69 inline void
70 _streamPutFloat(TStream & target,
71                           float number, 
72                           char const * format_string)
73 {
74 SEQAN_CHECKPOINT
75         char str[BitsPerValue<float>::VALUE];
76         sprintf(str, format_string, number);
77         _streamWrite(target, str);
78 }
79 template <typename TStream>
80 inline void
81 _streamPutFloat(TStream & target,
82                                 float number)
83 {
84 SEQAN_CHECKPOINT
85         _streamPutFloat(target, number, "%f");
86 }
87
88 //////////////////////////////////////////////////////////////////////////////
89
90 template <typename TTarget, typename T1, typename T2, typename TCompression>
91 inline void
92 _streamWrite(TTarget & target,
93                          Pair<T1, T2, TCompression> const & source)
94 {
95 SEQAN_CHECKPOINT
96         _streamWrite(target, getValueI1(source));
97         _streamWrite(target, getValueI2(source));
98 }
99
100 template <typename TTarget, typename T1, typename T2, typename T3, typename TCompression>
101 inline void
102 _streamWrite(TTarget & target,
103                          Triple<T1, T2, T3, TCompression> const & source)
104 {
105 SEQAN_CHECKPOINT
106         _streamWrite(target, getValueI1(source));
107         _streamWrite(target, getValueI2(source));
108         _streamWrite(target, getValueI3(source));
109 }
110
111 //////////////////////////////////////////////////////////////////////////////
112
113
114
115 /**
116 .Internal._streamWrite:
117 ..summary:Writes a sequence to stream.
118 ..cat:Streams
119 ..signature:_streamWrite(stream, sequence)
120 ..param.stream:An input stream.
121 ..param.sequence:A sequence that is written to $stream$.
122 */
123
124 template <typename TTarget, typename TSource>
125 inline void
126 _streamWrite(TTarget & target,
127                          TSource const & source)
128 {
129 SEQAN_CHECKPOINT
130         _streamWriteSeq(target, source, typename IsSequence<TSource const>::Type());
131 }
132
133 //____________________________________________________________________________
134
135 template <typename TTarget, typename TSource>
136 inline void
137 _streamWriteSeq(TTarget & target,
138                                 TSource const & source,
139                                 False const)
140 {
141         _streamPut(target, source);
142 }
143
144 //____________________________________________________________________________
145
146 template <typename TTarget, typename TSource>
147 inline void
148 _streamWriteSeq(TTarget & target,
149                                 TSource const & source,
150                                 True const)
151 {
152 SEQAN_CHECKPOINT
153         typename Iterator<TSource const, Standard>::Type it = begin(source, Standard());
154         typename Iterator<TSource const, Standard>::Type it_end = end(source, Standard());
155
156         for (; it < it_end; ++it)
157         {
158                 typename GetValue<TSource const>::Type val_ = getValue(it);
159                 _streamWrite(target, val_);
160         }
161 }
162
163 template <typename TTarget, typename TSourceValue>
164 inline void
165 _streamWriteSeq(TTarget & target,
166                             TSourceValue const * source,
167                                 True const)
168 {
169 SEQAN_CHECKPOINT
170
171         for (; !atEnd(source); ++source)
172         {
173                 _streamWrite(target, *source);
174         }
175 }
176
177 //////////////////////////////////////////////////////////////////////////////
178
179 /**
180 .Internal._streamWriteRange:
181 ..summary:Writes a range to stream.
182 ..cat:Streams
183 ..signature:_streamWriteRange(stream, begin_iterator, end_iterator)
184 ..param.stream:An input stream.
185 ..param.sequence:A sequence that is written to $stream$.
186 */
187
188 template <typename TTarget, typename TIterator>
189 inline void
190 _streamWriteRange(TTarget & target,
191                                   TIterator begin_,
192                                   TIterator end_)
193 {
194 SEQAN_CHECKPOINT
195         for (; begin_ != end_; ++begin_)
196         {
197                 _streamPut(target, *begin_);
198         }
199 }
200
201
202         
203
204 //////////////////////////////////////////////////////////////////////////////
205
206 } //namespace SEQAN_NAMESPACE_MAIN
207
208 #endif //#ifndef SEQAN_HEADER_...