Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / file / cstream.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: cstream.h,v 1.2 2009/03/03 18:47:37 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_CSTREAM_H
22 #define SEQAN_HEADER_CSTREAM_H
23
24 #include <cstdio>
25
26 namespace SEQAN_NAMESPACE_MAIN
27 {
28 //////////////////////////////////////////////////////////////////////////////
29 /**
30 .Adaption."std::FILE *":
31 ..summary:Standard library C style streams.
32 */
33
34 //////////////////////////////////////////////////////////////////////////////
35 // Position is now defined in file/file_cstyle.h
36 /*
37 template <>
38 struct Position<FILE *>
39 {
40         typedef long Type;
41 };
42 */
43 //////////////////////////////////////////////////////////////////////////////
44
45 template <>
46 struct Value<FILE *>
47 {
48         typedef char Type;
49 };
50
51 //////////////////////////////////////////////////////////////////////////////
52 /*
53 template <>
54 struct Position<FILE *>
55 {
56         typedef ::std::fpos_t Type;
57 };
58 */
59
60 //////////////////////////////////////////////////////////////////////////////
61
62 template <typename T>
63 struct _IsTellSeekStream;
64
65 template <>
66 struct _IsTellSeekStream<FILE *>
67 {
68         typedef True Type;
69 };
70
71 //////////////////////////////////////////////////////////////////////////////
72
73 inline bool
74 _streamOpen(::std::FILE * & me, String<char> path, bool for_read = true)
75 {
76 SEQAN_CHECKPOINT
77         size_t plen = length(path);
78         char *s = new char[plen + 1];
79         for(size_t i = 0; i < plen; i++) {
80                 s[i] = path[i];
81         }
82         s[plen] = '\0';
83         if (for_read)
84         {
85                 me = fopen(s, "rb");
86         }
87         else
88         {
89                 me = fopen(s, "wb");
90         }
91         delete[] s;
92         return (me != 0);
93 }
94
95
96 //////////////////////////////////////////////////////////////////////////////
97
98 inline void
99 _streamClose(::std::FILE * & me)
100 {
101 SEQAN_CHECKPOINT
102         if (me)
103         {
104                 fclose(me);
105                 me = 0;
106         }
107 }
108
109 //////////////////////////////////////////////////////////////////////////////
110
111 ///.Internal._streamEOF.param.stream.type:Adaption."std::FILE *"
112
113 inline bool
114 _streamEOF(::std::FILE * me)
115 {
116 SEQAN_CHECKPOINT
117         return feof(me) || ferror(me);
118 }
119
120 //////////////////////////////////////////////////////////////////////////////
121
122 ///.Internal._streamRead.param.stream.type:Adaption."std::FILE *"
123
124 template <typename TValue>
125 inline size_t
126 _streamRead(TValue * target,
127                         ::std::FILE * source,
128                         size_t limit)
129 {
130 SEQAN_CHECKPOINT
131         return ::std::fread(target, sizeof(TValue), limit, source);
132 }
133
134 //////////////////////////////////////////////////////////////////////////////
135
136 ///.Internal._streamGet.param.stream.type:Adaption."std::FILE *"
137
138 inline char
139 _streamGet(::std::FILE * source)
140 {
141 SEQAN_CHECKPOINT
142         return getc(source);
143 }
144
145 //////////////////////////////////////////////////////////////////////////////
146
147 ///.Internal._streamPut.param.stream.type:Adaption."std::FILE *"
148
149 inline void
150 _streamPut(::std::FILE * target,
151                    char character)
152 {
153 SEQAN_CHECKPOINT
154         putc(character, target);
155 }
156
157
158 //////////////////////////////////////////////////////////////////////////////
159
160 ///.Internal._streamPut.param.stream.type:Adaption."std::FILE *"
161
162 //////////////////////////////////////////////////////////////////////////////
163
164 ///.Internal._streamTellG.param.stream.type:Adaption."std::FILE *"
165
166 inline Position<FILE *>::Type
167 _streamTellG(FILE * me)
168 {
169 SEQAN_CHECKPOINT
170         return ::std::ftell(me);
171 }
172
173 //////////////////////////////////////////////////////////////////////////////
174
175 ///.Internal._streamTellP.param.stream.type:Adaption."std::FILE *"
176
177 inline Position<FILE *>::Type
178 _streamTellP(FILE * me)
179 {
180 SEQAN_CHECKPOINT
181         return ::std::ftell(me);
182 }
183
184 //////////////////////////////////////////////////////////////////////////////
185
186 ///.Internal._streamSeekG.param.stream.type:Adaption."std::FILE *"
187
188 inline void
189 _streamSeekG(FILE * me,
190                          Position<FILE *>::Type pos)
191 {
192 SEQAN_CHECKPOINT
193         ::std::fseek(me, pos, SEEK_SET);
194 }
195
196 //////////////////////////////////////////////////////////////////////////////
197
198 ///.Internal._streamSeekP.param.stream.type:Adaption."std::FILE *"
199
200 inline void
201 _streamSeekP(FILE * me,
202                          Position<FILE *>::Type pos)
203 {
204 SEQAN_CHECKPOINT
205         ::std::fseek(me, pos, SEEK_SET);
206 }
207
208 //////////////////////////////////////////////////////////////////////////////
209
210 ///.Internal._streamSeek2G.param.stream.type:Adaption."std::FILE *"
211
212 inline void
213 _streamSeek2G(FILE * me,
214          int off)
215 {
216 SEQAN_CHECKPOINT
217         ::std::fseek(me, off, SEEK_CUR);
218 }
219
220 //////////////////////////////////////////////////////////////////////////////
221
222 ///.Internal._streamUnget.param.stream.type:Adaption."std::FILE *"
223
224 inline void
225 _streamUnget(::std::FILE * stream)
226 {
227 SEQAN_CHECKPOINT
228         _streamSeek2G(stream, -1);
229 }
230
231 //////////////////////////////////////////////////////////////////////////////
232 // Stream operators for FILE *
233 //////////////////////////////////////////////////////////////////////////////
234
235 // ISO C++ operators are only allowed for classes, not for pointers
236
237 /*
238 template <typename TSource>
239 inline FILE *
240 operator << (FILE * target,
241                          TSource & source)
242 {
243 SEQAN_CHECKPOINT
244         write(target, source);
245         return target;
246 }
247 template <typename TSource>
248 inline FILE *
249 operator << (FILE * target,
250                          TSource const & source)
251 {
252 SEQAN_CHECKPOINT
253         write(target, source);
254         return target;
255 }
256
257 //____________________________________________________________________________
258
259 template <typename TTarget>
260 inline FILE *
261 operator >> (FILE * source,
262                          TTarget & target)
263 {
264 SEQAN_CHECKPOINT
265         read(source, target);
266         return source;
267 }
268 template <typename TTarget>
269 inline FILE *
270 operator >> (FILE * source,
271                          TTarget const & target)
272 {
273 SEQAN_CHECKPOINT
274         read(source, target);
275         return source;
276 }
277 */
278
279 //////////////////////////////////////////////////////////////////////////////
280
281 } //namespace SEQAN_NAMESPACE_MAIN
282
283 #endif //#ifndef SEQAN_HEADER_...