Debian Codegraph @

CFile/CFile2_1.h

raw
1 /* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #ifndef CFILE_H
28 #define CFILE_H
29
30 #include <fstream>
31 #include <string>
32 #include "../faststring2.h"
33
34 // Should not exceed 1 000 000 without making sure that the stack is large enough.
35 #ifndef BUFFERSIZE
36 #define BUFFERSIZE 1000000
37 #endif
38 #define UNDOs 1
39
40
41 // Changes:
42 // 22.08.2009: Added getline function for faststring. Disadvantage: additional dependence on faststring.h
43 // 07.01.2011: Changed Version to CFile2_1.h
44 // 07.01.2011: Now uses the faststring2.h
45
46 // Idea: ios:: set Buffer size
47
48 // TODO: Old mac format not supported.
49 // This requires to allow two successive calls to the internal ungetchar command.
50
51
52 class CFile : private std::ifstream
53 {
54 private:
55 // There is a problem here: The stack size is limited. One should consider to move this to the heap.
56 // allocate it with malloc, new or declare it as static - the latter is not good in a class since a
57 // static class member is the same for all instances of the class, which we do not want here.
58
59 char buffer[BUFFERSIZE];
60 char* buffer_end;
61 char* buffer_pos;
62
63 char __status;
64 unsigned __line;
65
66 // bool __eof;
67 // bool __openFailed;
68
69 unsigned fill_buffer(unsigned overlap)
70 {
71 unsigned good_overlap = buffer_end - buffer;
72 std::streamsize n;
73
74 if (good_overlap < overlap)
75 {
76 overlap = good_overlap;
77 }
78
79 if (overlap > 0)
80 std::memmove(buffer, buffer_end - overlap, overlap);
81
82 std::ifstream::read(buffer + overlap, BUFFERSIZE - overlap);
83 n = std::ifstream::gcount();
84
85 if ( n == 0 )
86 {
87 __status |= __eof_flag; // Set eof flag
88 __status &= ~__good_flag; // Unset good flag
89 __status |= __fail_flag; // Setting the fail flag is not always correct. Needs to be unset in routines that read more than one char.
90 }
91
92 buffer_pos = buffer+overlap;
93 buffer_end = buffer_pos + n;
94
95 return overlap;
96 }
97
98 char getchar_intern() // should only be done if we did not fail yet!!!! This would allow us to recover!!!
99 {
100 if ( buffer_pos == buffer_end )
101 {
102 fill_buffer(UNDOs);
103 if (__status & __fail_flag)
104 return '\0';
105 }
106 return *buffer_pos++;
107 }
108
109 void ungetchar_intern() // should only be done if we did not fail yet!!!! This would allow us to recover!!!
110 {
111 if (buffer_pos != buffer)
112 --buffer_pos;
113 else
114 {
115 __status |= __fail_flag; // Set fail flag.
116 __status &= ~__good_flag; // Unset good flag.
117 }
118 }
119
120 public:
121
122 enum {__eof_flag = 1, __good_flag = 2, __fail_flag = 4, __bad_flag = 8, __fail_reason1 = 16, __fail_reason2 = 32, __fail_reason3 = 64,
123 __fail_reason4 = 128};
124
125 void open(const char *name)
126 {
127 ffopen(name);
128 }
129
130 void open(std::string name)
131 {
132 ffopen(name);
133 }
134
135 void ffopen(const char *name)
136 {
137 __status = __good_flag;
138
139 std::ifstream::open(name);
140
141 if ( std::ifstream::fail() )
142 {
143 __status |= __fail_flag; // Set fail flag.
144 __status &= ~__good_flag; // Unset good flag.
145 }
146
147 __line = 1;
148 buffer_end = buffer;
149 buffer_pos = buffer;
150 }
151
152 void ffopen(std::string name)
153 {
154 ffopen(name.c_str());
155 }
156
157 void ffclose()
158 {
159 std::ifstream::close();
160 }
161
162 void close()
163 {
164 ffclose();
165 }
166
167 bool exists() // -- deprecated -- do not use this any more - check fail() instead !!!!!!!!
168 {
169 return !fail();
170 }
171
172 bool fail()
173 {
174 return (__status & __fail_flag);
175 }
176
177 bool good()
178 {
179 return (__status & __good_flag);
180 }
181
182 unsigned line()
183 {
184 return __line;
185 }
186
187 bool eof()
188 {
189 return (__status & __eof_flag);
190 }
191
192 char status()
193 {
194 return __status;
195 }
196
197 bool fail_reason1()
198 {
199 return (__status & __fail_reason1);
200 }
201
202 bool fail_reason2()
203 {
204 return (__status & __fail_reason2);
205 }
206
207 bool fail_reason3()
208 {
209 return (__status & __fail_reason3);
210 }
211
212 bool fail_reason4()
213 {
214 return (__status & __fail_reason4);
215 }
216
217 void rewind()
218 {
219 clear();
220 std::ifstream::seekg (0, std::ios::beg);
221 // clear();
222 }
223
224 void clear(char s = __good_flag)
225 {
226 __status = s;
227 std::ifstream::clear();
228 }
229
230 void ungetchar()
231 {
232 if (*(buffer_pos-1) == '\n' && !(__status & __fail_flag))
233 --__line;
234 ungetchar_intern();
235 }
236
237 void ignore(int delim = -1)
238 {
239 while (__status == __good_flag && getchar() != delim ){}
240 }
241
242 char peekchar()
243 {
244 char c = getchar();
245 ungetchar();
246 return c;
247 }
248
249 char peek()
250 {
251 return peekchar();
252 }
253
254 char getchar()
255 {
256 register char c;
257
258 c = getchar_intern();
259
260 if ( c < 14 && !(__status & __fail_flag) )
261 {
262 if ( c == '\r' )
263 {
264 // Overwrite the last reading position that contained the \r with \n
265 *(buffer_pos-1) = '\n'; // Should always be valid! Works for 1 Undo
266 c = getchar_intern();
267 if ( c != '\n' && !(__status & __fail_flag) )
268 {
269 // std::cerr << "Old mac file format currently not supported." << std::endl;
270 ungetchar(); /* old mac format, else dos format */
271 }
272 c = '\n';
273 ++__line;
274 }
275 else if ( c == '\n')
276 ++__line;
277 }
278 return c;
279 }
280
281
282 char getrawchar()
283 {
284 return getchar_intern();
285 }
286
287 void getline(faststring& str, char delim='\n')
288 {
289 char c = getchar();
290 str.clear();
291 while ( c != delim && !(__status & __fail_flag) )
292 {
293 str.push_back(c);
294 c = getchar();
295 }
296 if ((__status & __fail_flag) && str.size() > 0)
297 __status &= ~__fail_flag; // Unset fail flag by using & on the complement of the fail flag;
298 }
299
300 void getline(std::string& str, char delim='\n')
301 {
302 char c = getchar();
303 str="";
304 while ( c != delim && !(__status & __fail_flag) )
305 {
306 str.push_back(c);
307 c = getchar();
308 }
309 if ((__status & __fail_flag) && str.size() > 0)
310 __status &= ~__fail_flag; // Unset fail flag by using & on the complement of the fail flag;
311 }
312
313 void getline(char* cstr, unsigned n, char delim='\n')
314 {
315 char c = getchar();
316 unsigned i = 0;
317
318 while ( !(__status & __fail_flag ) && i < n-1 && c != delim)
319 {
320 cstr[i] = c;
321 ++i;
322 c = getchar();
323 }
324 if ((__status & __fail_flag) && i > 0)
325 __status &= ~__fail_flag; // Unset fail flag by using & on the complement of the fail flag;
326 cstr[i] = '\0';
327 }
328
329 void readFileIntoString(faststring &str)
330 {
331 char c;
332
333 str.clear();
334
335 c = getchar();
336 while (!(__status & __fail_flag))
337 {
338 str.push_back(c);
339 c = getchar();
340 }
341 }
342
343
344 char lastchar()
345 {
346 char c;
347
348 if ( buffer_pos != buffer_end )
349 c = *(buffer_pos-1);
350 else
351 {
352 __status |= __fail_flag; // Set fail flag.
353 __status &= ~__good_flag; // Unset good flag.
354 return 0;
355 }
356
357 /* It could be a '\r' in mac format */
358 if (c == '\r')
359 c = '\n';
360
361 return c;
362 }
363
364 char lastrawchar()
365 {
366 char c;
367
368 if ( buffer_pos != buffer_end )
369 c = *(buffer_pos-1);
370 else
371 {
372 __status |= __fail_flag; // Set fail flag.
373 __status &= ~__good_flag; // Unset good flag.
374 return 0;
375 }
376 return c;
377 }
378
379
380 };
381
382
383
384
385 #endif