[go: up one dir, main page]

File: file_format.h

package info (click to toggle)
seqan 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,368 kB
  • ctags: 17,376
  • sloc: cpp: 81,339; ansic: 58,225; makefile: 96; sh: 14
file content (686 lines) | stat: -rw-r--r-- 17,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
 /*==========================================================================
                SeqAn - The Library for Sequence Analysis
                          http://www.seqan.de 
 ============================================================================
  Copyright (C) 2007

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 3 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

 ============================================================================
  $Id: file_format.h 1448 2007-12-20 15:56:43Z doering@PCPOOL.MI.FU-BERLIN.DE $
 ==========================================================================*/

#ifndef SEQAN_HEADER_FILE_FORMAT_H
#define SEQAN_HEADER_FILE_FORMAT_H

namespace SEQAN_NAMESPACE_MAIN
{

//////////////////////////////////////////////////////////////////////////////

/**
.Tag.File Format:
..summary:A file format.
*/


//////////////////////////////////////////////////////////////////////////////
// Metafunctions
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//Base Class for all FileFormat classes
//////////////////////////////////////////////////////////////////////////////

/**
.Class.FileFormat:
..cat:Input/Output
..summary:Object that stores a file format.
..signature:FileFormat<File, Data [, Format [, Meta] ]>
..see:Tag.File Format
*/

template <
	typename TFile, 
	typename TData,
	typename TMeta,
	typename TFormat = void >
struct FileFormat:
	public FileFormat<TFile, TData, TMeta, void>
{
public:
	typedef typename Size<TData>::Type TSize;

	FileFormat() {}
	FileFormat(FileFormat const &) {}
	~FileFormat() {}
	FileFormat const & operator =(FileFormat const &) {}

	inline void * 
	formatID_() const
	{
SEQAN_CHECKPOINT
		return _ClassIdentifier<TFormat>::getID();
	}

	virtual void
	read_(TFile & file, TData & data) const
	{
SEQAN_CHECKPOINT
		read(file, data, TFormat());
	}
	virtual void
	read_(TFile & file, TData & data, TSize limit) const
	{
SEQAN_CHECKPOINT
		read(file, data, limit, TFormat());
	}

	virtual void
	readMeta_(TFile & file, TMeta & meta) const
	{
SEQAN_CHECKPOINT
		readMeta(file, meta, TFormat());
	}

	virtual void
	goNext_(TFile & file) const
	{
SEQAN_CHECKPOINT
		goNext(file, TFormat());
	}

	virtual TSize
	length_(TFile & file) const
	{
SEQAN_CHECKPOINT
		length(file, TFormat());
	}

	virtual void
	write_(TFile & file, TData & data) const
	{
SEQAN_CHECKPOINT
		write(file, data, TFormat());
	}
	virtual void
	write_(TFile & file, TData & data, TMeta & meta) const
	{
SEQAN_CHECKPOINT
		write(file, data, meta, TFormat());
	}
};

//____________________________________________________________________________

//base class for all file format classes 

template <typename TFile, typename TData, typename TMeta>
struct FileFormat<TFile, TData, TMeta, void>
{
public:
	typedef typename Size<TData>::Type TSize;

	FileFormat() {}
	FileFormat(FileFormat const &) {}
	~FileFormat() {};
	FileFormat const & operator =(FileFormat const &) {}

	virtual void *
	formatID_() const = 0;

	virtual void
	read_(TFile & file, TData & data) const = 0;
	virtual void
	read_(TFile & file, TData & data, TSize limit) const = 0;

	virtual void
	readMeta_(TFile & file, TMeta & meta) const = 0;

	virtual void
	goNext_(TFile & file) const = 0;

	virtual TSize
	length_(TFile & file) const = 0;

	virtual void
	write_(TFile & file, TData & data) const = 0;
	virtual void
	write_(TFile & file, TData & data, TMeta & meta) const = 0;

};

//////////////////////////////////////////////////////////////////////////////
// Wrapper for functions to virtuals
//////////////////////////////////////////////////////////////////////////////


template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void *
formatID(FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	return file_format.formatID_();
}

//////////////////////////////////////////////////////////////////////////////

/**
.Function.Fileformat#read:
..cat:Input/Output
..summary:Loads a record from file.
..signature:read(file, data [, meta], format)
..signature:read(file, data [, meta], tag)
..param.file:An input file.
..param.data:A container that gets the data read from $file$.
..param.meta:A container that gets meta data from $file$. (optional)
..param.format:A file format object.
...type:Class.FileFormat.File Format object
..param.tag:A file format tag.
...type:Tag.File Format.File Format tag
..remarks:The result of this operation is stored in $data$.
..remarks:The function leaves $file$ at the position for reading the next record.
..see:Function.assign
*/
template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
read(TFile & file,
	 TData & data,
	 FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.read_(file, data);
}

template <typename TFile, typename TData, typename TMeta, typename TFormat, typename TSize>
inline void
read(TFile & file,
	 TData & data,
	 TSize limit,
	 FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.read_(file, data, limit);
}

//////////////////////////////////////////////////////////////////////////////

/**
.Function.readMeta:
..cat:Input/Output
..summary:Read meta information from file.
..signature:readMeta(file, meta, file_format)
..param.file:A file that contains data in the format specified by $file_format$.
..param.meta:A data structure that is able to store meta informations stored in $file$.
..param.file_format:A file format.
..returns.param.meta:The meta data read from $file$.
...type:Tag.File Format
*/

template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
readMeta(TFile & file,
		 TMeta & meta,
		 FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.readMeta_(file, meta);
}

//////////////////////////////////////////////////////////////////////////////

/**
.Function.goNext:
..cat:Input/Output
*/

template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
goNext(TFile & file,
	   FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.goNext_(file);
}

//////////////////////////////////////////////////////////////////////////////

/**
.Function.length:
..cat:Input/Output
*/

template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
length(TFile & file,
	   FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.length_(file);
}

//////////////////////////////////////////////////////////////////////////////

/**
.Function.Fileformat#write:
..cat:Input/Output
..summary:Writes to stream.
..signature:write(stream, source)
..signature:write(stream, begin, end)
..param.stream: A stream object.
...type:Adaption."std::iostream"
..param.source: Container that is written to $stream$.
..param.begin: Iterator to the first character of the range.
..param.end: Iterator behind the last character of the range.
..remarks:The content of $source$ is written 'as-is' to $stream$.
*/

template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
write(TFile & file,
	  TData & data,
	  FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.write_(file, data);
}
template <typename TFile, typename TData, typename TMeta, typename TFormat>
inline void
write(TFile & file,
	  TData & data,
	  TMeta & meta,
	  FileFormat<TFile, TData, TMeta, TFormat> const & file_format)
{
SEQAN_CHECKPOINT
	file_format.write_(file, data, meta);
}




//////////////////////////////////////////////////////////////////////////////
// Comparison of two FileFormat objects
//////////////////////////////////////////////////////////////////////////////

template <typename TFileLeft, typename TDataLeft, typename TMetaLeft, typename TFormatLeft, typename TFileRight, typename TDataRight, typename TMetaRight, typename TFormatRight>
inline bool
operator == (FileFormat<TFileLeft, TDataLeft, TMetaLeft, TFormatLeft> const & left, 
			 FileFormat<TFileRight, TDataRight, TMetaRight, TFormatRight> const & right)
{
SEQAN_CHECKPOINT
	return formatID(left) == formatID(right);
}

template <typename TFile, typename TData, typename TMeta, typename TFormat, typename TFormat2>
inline bool
operator == (FileFormat<TFile, TData, TMeta, TFormat> const & left, 
			 Tag<TFormat2> const)
{
SEQAN_CHECKPOINT
	return formatID(left) == _ClassIdentifier<Tag<TFormat2> const>::getID();
}

template <typename TFile, typename TData, typename TMeta, typename TFormat, typename TFormat2>
inline bool
operator == (Tag<TFormat2> const,
			 FileFormat<TFile, TData, TMeta, TFormat> const & right)
{
SEQAN_CHECKPOINT
	return _ClassIdentifier<Tag<TFormat2> const>::getID() == formatID(right);
}

//____________________________________________________________________________

template <typename TFileLeft, typename TDataLeft, typename TMetaLeft, typename TFormatLeft, typename TFileRight, typename TDataRight, typename TMetaRight, typename TFormatRight>
inline bool
operator != (FileFormat<TFileLeft, TDataLeft, TMetaLeft, TFormatLeft> const & left, 
			 FileFormat<TFileRight, TDataRight, TMetaRight, TFormatRight> const & right)
{
SEQAN_CHECKPOINT
	return formatID(left) != formatID(right);
}

template <typename TFile, typename TData, typename TMeta, typename TFormat, typename TFormat2>
inline bool
operator != (FileFormat<TFile, TData, TMeta, TFormat> const & left, 
			 Tag<TFormat2> const)
{
SEQAN_CHECKPOINT
	return formatID(left) != _ClassIdentifier<Tag<TFormat2> const>::getID();
}

template <typename TFile, typename TData, typename TMeta, typename TFormat, typename TFormat2>
inline bool
operator != (Tag<TFormat2> const,
			 FileFormat<TFile, TData, TMeta, TFormat> const & right)
{
SEQAN_CHECKPOINT
	return _ClassIdentifier<Tag<TFormat2> const>::getID() != formatID(right);
}

//////////////////////////////////////////////////////////////////////////////
// allgemeine Funktionen fuer Streams
//////////////////////////////////////////////////////////////////////////////
//TODO??? Das muss in eine extra Datei


/*
template <typename TStream, typename TIterator>
inline void
write(TStream & target,
	  TIterator begin_,
	  TIterator end_)
{
	while (begin_ != end_)
	{
		_streamPut(target, convert<char>(*begin_));
		++begin_;
	}
}

//____________________________________________________________________________

template <typename TStream, typename TSource>
inline void
write(TStream & target,
	  TSource const & source)
{
	write(target, begin(source), end(source));
}
//TODO???: Spezialisierungen zum blockweise schreiben bei contiguous strings von char
//Anmerkungen: write wird nach dem zweiten Argument (source) spezialisiert!

//____________________________________________________________________________

template <typename TStream, typename TSource>
inline void
write(TStream & target,
	  TSource const & source,
	  typename Size<TSource>::Type limit_)
{
	if (length(source) > limit_)
	{
		write(target, begin(source), begin(source) + limit_);
	}
	else
	{
		write(target, begin(source), end(source));
	}
}

*/
//////////////////////////////////////////////////////////////////////////////

// Helper function for scanning a stream
// c = next character, pass it to the next call of the function

template <typename TFile, typename TString, typename TChar>
inline void
_stream_appendLine(TFile & file,
				   TString & str,
				   TChar & c)
{
	while (true)
	{
		if (_streamEOF(file)) break;

		if (c == '\r')
		{
			c = _streamGet(file);
			if (c == '\n') 
			{
				c = _streamGet(file);
			}
			break;
		}
		if (c == '\n')
		{
			c = _streamGet(file);
			break;
		}

		appendValue(str, c);

		c = _streamGet(file);
	}
}
//____________________________________________________________________________

template <typename TFile, typename TChar>
inline void
_stream_countLine(TFile & file,
				  TChar & c)

{
	while (true)
	{
		if (_streamEOF(file)) break;

		if (c == '\r')
		{
			c = _streamGet(file);
			if (c == '\n') 
			{
				c = _streamGet(file);
			}
			break;
		}
		if (c == '\n')
		{
			c = _streamGet(file);
			break;
		}

		c = _streamGet(file);
	}
}

//____________________________________________________________________________

template <typename TFile, typename TChar>
inline typename Size<TFile>::Type
_stream_skipLine(TFile & file,
				 TChar & c)

{
	typename Size<TFile>::Type count = 0;
	while (true)
	{
		if (_streamEOF(file)) break;

		if (c == '\r')
		{
			c = _streamGet(file);
			if (c == '\n') 
			{
				c = _streamGet(file);
			}
			break;
		}
		if (c == '\n')
		{
			c = _streamGet(file);
			break;
		}

		++count;

		c = _streamGet(file);
	}

	return count;
}




////////////////////////////////////////////////////////////////////////////
//new ones

//new ones for streams
template<typename TFile, typename TChar>
inline void 
_stream_skipWhitespace(TFile& file, TChar& c)
{
	if ((c!=' ') && (c != '\t')) return;
	while (!_streamEOF(file)) {
		c = _streamGet(file);
		if ((c!=' ') && (c != '\t')) break;
	}
}

////////////////////////////////////////////////////////////////////////////


template<typename TFile, typename TChar>
inline String<char>
_stream_readWord(TFile & file, TChar& c)
{
	// Read word
	String<char> str(c);
	while (!_streamEOF(file)) {
		c = _streamGet(file);
		if (!_stream_isLetter(c)) break;
		append(str, c);
	}
	return str;
}

////////////////////////////////////////////////////////////////////////////

template<typename TChar>
inline bool
_stream_isLetter(TChar const c)
{
	return ((c == 'a') || (c == 'b') || (c == 'c') || (c == 'd') || (c == 'e') || 
			(c == 'f') || (c == 'g') || (c == 'h') || (c == 'i') || (c == 'j') ||
			(c == 'k') || (c == 'l') || (c == 'm') || (c == 'n') || (c == 'o') || 
			(c == 'p') || (c == 'q') || (c == 'r') || (c == 's') || (c == 't') ||
			(c == 'u') || (c == 'v') || (c == 'w') || (c == 'x') || (c == 'y') || 
			(c == 'z') || (c == 'A') || (c == 'B') || (c == 'C') || (c == 'D') ||
			(c == 'E') || (c == 'F') || (c == 'G') || (c == 'H') || (c == 'I') || 
			(c == 'J') || (c == 'K') || (c == 'L') || (c == 'M') || (c == 'N') ||
			(c == 'O') || (c == 'P') || (c == 'Q') || (c == 'R') || (c == 'S') || 
			(c == 'T') || (c == 'U') || (c == 'V') || (c == 'W') || (c == 'X') ||
			(c == 'Y') || (c == 'Z'));
}


//////////////////////////////////////////////////////////////////////////////

//new ones for strings

template <typename TString, typename TIter>
inline typename Size<TString>::Type
_string_skipLine(TString & str,
				 TIter & it)

{
	typename Size<TString>::Type count = 0;
	typename Iterator<TString,Standard>::Type end_it = end(str,Standard());
	while (true)
	{
		if (it == end_it) break;

		if (*it == '\r')
		{
			++it;
			if (*it == '\n') 
			{
				++it;
			}
			break;
		}
		if (*it == '\n')
		{
			++it;
			break;
		}

		++count;
		++it;
	}

	return count;
}

/////////////////////////////////////////////////////////////////////////

template <typename TString1, typename TString2, typename TIter>
inline void
_string_appendLine(TString1 & str,
				   TString2 & a_str,
				   TIter & it)
{
	typename Iterator<TString1,Standard>::Type end_it = end(str,Standard());
	while (true)
	{
		if (it == end_it) break;

		if (*it == '\r')
		{
			++it; 
			if (*it == '\n') 
			{
				++it;
			}
			break;
		}
		if (*it == '\n')
		{
			++it;
			break;
		}

		appendValue(a_str, getValue(it));
		++it;
	}
}

////////////////////////////////////////////////////////////////////////////

template<typename TString, typename TIter>
inline void 
_string_skipWhitespace(TString& str, TIter& it)
{
	typename Iterator<TString,Standard>::Type end_it = end(str,Standard())-1;
	while (it != end_it) {
		if ((*it!=' ') && (*it != '\t')) break;
		++it;
	}
}

////////////////////////////////////////////////////////////////////////////

template<typename TString, typename TIter>
inline int
_string_readNumber(TString & str, TIter& it)
{
	// Read number
	typename Iterator<TString,Standard>::Type end_it = end(str,Standard())-1;
	String<char> numstr(getValue(it));
	while (it != end_it) {
		++it;
		if (!_parse_isDigit(*it)) break;
		append(numstr, getValue(it));
	}
 	return atoi(toCString(numstr));
}

//////////////////////////////////////////////////////////////////////////////

} //namespace SEQAN_NAMESPACE_MAIN

//////////////////////////////////////////////////////////////////////////////

#endif //#ifndef SEQAN_HEADER_...