1#ifndef BTLLIB_SEQ_READER_FASTQ_MODULE_HPP
2#define BTLLIB_SEQ_READER_FASTQ_MODULE_HPP
4#include "btllib/cstring.hpp"
5#include "btllib/status.hpp"
12class SeqReaderFastqModule
16 friend class SeqReader;
26 Stage stage = Stage::HEADER;
29 static bool buffer_valid(
const char* buffer,
size_t size);
30 template<
typename ReaderType,
typename RecordType>
31 bool read_buffer(ReaderType& reader, RecordType& record);
32 template<
typename ReaderType,
typename RecordType>
33 bool read_transition(ReaderType& reader, RecordType& record);
34 template<
typename ReaderType,
typename RecordType>
35 bool read_file(ReaderType& reader, RecordType& record);
38template<
typename ReaderType,
typename RecordType>
40SeqReaderFastqModule::read_buffer(ReaderType& reader, RecordType& record)
42 record.header.clear();
45 if (reader.buffer.start < reader.buffer.end) {
48 if (!reader.readline_buffer_append(record.header)) {
55 if (!reader.readline_buffer_append(record.seq)) {
62 if (!reader.readline_buffer_append(tmp)) {
70 if (!reader.readline_buffer_append(record.qual)) {
73 stage = Stage::HEADER;
77 log_error(
"SeqReader has entered an invalid state.");
78 std::exit(EXIT_FAILURE);
85template<
typename ReaderType,
typename RecordType>
87SeqReaderFastqModule::read_transition(ReaderType& reader, RecordType& record)
89 if (std::ferror(reader.source) == 0 && std::feof(reader.source) == 0) {
90 const auto p = std::fgetc(reader.source);
92 const auto ret = std::ungetc(p, reader.source);
93 check_error(ret == EOF,
"SeqReaderFastqModule: ungetc failed.");
96 reader.readline_file_append(record.header, reader.source);
101 reader.readline_file_append(record.seq, reader.source);
106 reader.readline_file_append(tmp, reader.source);
112 reader.readline_file_append(record.qual, reader.source);
113 stage = Stage::HEADER;
117 log_error(
"SeqReader has entered an invalid state.");
118 std::exit(EXIT_FAILURE);
126template<
typename ReaderType,
typename RecordType>
128SeqReaderFastqModule::read_file(ReaderType& reader, RecordType& record)
130 if (!reader.file_at_end(reader.source)) {
131 reader.readline_file(record.header, reader.source);
132 reader.readline_file(record.seq, reader.source);
133 reader.readline_file(tmp, reader.source);
134 reader.readline_file(record.qual, reader.source);
void check_error(bool condition, const std::string &msg)
void log_error(const std::string &msg)