1#ifndef BTLLIB_SEQ_READER_FASTA_MODULE_HPP
2#define BTLLIB_SEQ_READER_FASTA_MODULE_HPP
4#include "btllib/status.hpp"
11class SeqReaderFastaModule
15 friend class SeqReader;
23 Stage stage = Stage::HEADER;
25 static bool buffer_valid(
const char* buffer,
size_t size);
26 template<
typename ReaderType,
typename RecordType>
27 bool read_buffer(ReaderType& reader, RecordType& record);
28 template<
typename ReaderType,
typename RecordType>
29 bool read_transition(ReaderType& reader, RecordType& record);
30 template<
typename ReaderType,
typename RecordType>
31 bool read_file(ReaderType& reader, RecordType& record);
34template<
typename ReaderType,
typename RecordType>
36SeqReaderFastaModule::read_buffer(ReaderType& reader, RecordType& record)
38 record.header.clear();
41 if (reader.buffer.start < reader.buffer.end) {
44 if (!reader.readline_buffer_append(record.header)) {
51 if (!reader.readline_buffer_append(record.seq)) {
54 stage = Stage::HEADER;
58 log_error(
"SeqReader has entered an invalid state.");
59 std::exit(EXIT_FAILURE);
66template<
typename ReaderType,
typename RecordType>
68SeqReaderFastaModule::read_transition(ReaderType& reader, RecordType& record)
70 if (std::ferror(reader.source) == 0 && std::feof(reader.source) == 0) {
71 const auto p = std::fgetc(reader.source);
73 const auto ret = std::ungetc(p, reader.source);
74 check_error(ret == EOF,
"SeqReaderFastaModule: ungetc failed.");
77 reader.readline_file_append(record.header, reader.source);
82 reader.readline_file_append(record.seq, reader.source);
83 stage = Stage::HEADER;
87 log_error(
"SeqReader has entered an invalid state.");
88 std::exit(EXIT_FAILURE);
96template<
typename ReaderType,
typename RecordType>
98SeqReaderFastaModule::read_file(ReaderType& reader, RecordType& record)
100 if (!reader.file_at_end(reader.source)) {
101 reader.readline_file(record.header, reader.source);
102 reader.readline_file(record.seq, reader.source);
void check_error(bool condition, const std::string &msg)
void log_error(const std::string &msg)