| 1 | #ifndef READER_H |
|---|
| 2 | #define READER_H |
|---|
| 3 | |
|---|
| 4 | #ifndef FILEBUFFER_H |
|---|
| 5 | #include <FileBuffer.h> |
|---|
| 6 | #endif |
|---|
| 7 | #ifndef DEFS_H |
|---|
| 8 | #include "defs.h" |
|---|
| 9 | #endif |
|---|
| 10 | #ifndef FUN_H |
|---|
| 11 | #include "fun.h" |
|---|
| 12 | #endif |
|---|
| 13 | #ifndef SEQ_H |
|---|
| 14 | #include "seq.h" |
|---|
| 15 | #endif |
|---|
| 16 | #ifndef INPUT_FORMAT_H |
|---|
| 17 | #include "input_format.h" |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class Reader : virtual Noncopyable { |
|---|
| 22 | FILE *fp; |
|---|
| 23 | FileBuffer *file; |
|---|
| 24 | char linebuf[LINESIZE]; |
|---|
| 25 | const char *curr; // == NULL means "EOF reached" |
|---|
| 26 | bool failure; |
|---|
| 27 | |
|---|
| 28 | void reset() { |
|---|
| 29 | curr = linebuf; |
|---|
| 30 | failure = false; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void read(); |
|---|
| 34 | |
|---|
| 35 | public: |
|---|
| 36 | Reader(const char *inf); |
|---|
| 37 | virtual ~Reader(); |
|---|
| 38 | |
|---|
| 39 | void rewind() { file->rewind(); reset(); read(); } |
|---|
| 40 | |
|---|
| 41 | Reader& operator++() { read(); return *this; } |
|---|
| 42 | |
|---|
| 43 | const char *line() const { return curr; } |
|---|
| 44 | |
|---|
| 45 | void set_line(const char *new_line) { |
|---|
| 46 | ca_assert(new_line); |
|---|
| 47 | ca_assert(strlen(new_line)<LINESIZE); |
|---|
| 48 | strcpy(linebuf, new_line); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | bool failed() const { return failure; } |
|---|
| 52 | bool ok() const { return !failure; } |
|---|
| 53 | |
|---|
| 54 | void abort() { failure = true; curr = NULL; } |
|---|
| 55 | void ignore_rest_of_file() { curr = NULL; } |
|---|
| 56 | |
|---|
| 57 | template<class PRED> |
|---|
| 58 | void skipOverLinesThat(const PRED& match_condition) { |
|---|
| 59 | while (line() && match_condition(line())) |
|---|
| 60 | ++(*this); |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | inline const char *shorttimekeep(char *heapcopy) { RETURN_LOCAL_ALLOC(heapcopy); } |
|---|
| 65 | inline const char *shorttimecopy(const char *nocopy) { return shorttimekeep(nulldup(nocopy)); } |
|---|
| 66 | |
|---|
| 67 | struct FormatReader { |
|---|
| 68 | virtual ~FormatReader() {} |
|---|
| 69 | virtual bool read_one_entry(Seq& seq) __ATTR__USERESULT = 0; |
|---|
| 70 | virtual bool failed() const = 0; |
|---|
| 71 | virtual void ignore_rest_of_file() = 0; |
|---|
| 72 | virtual void rewind() = 0; |
|---|
| 73 | virtual InputFormat& get_data() = 0; |
|---|
| 74 | |
|---|
| 75 | static SmartPtr<FormatReader> create(const FormattedFile& in); |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | typedef SmartPtr<FormatReader> FormatReaderPtr; |
|---|
| 79 | |
|---|
| 80 | struct SimpleFormatReader : public Reader, public FormatReader { |
|---|
| 81 | SimpleFormatReader(const char *inf) : Reader(inf) {} |
|---|
| 82 | bool failed() const { return Reader::failed(); } |
|---|
| 83 | void ignore_rest_of_file() { Reader::ignore_rest_of_file(); } |
|---|
| 84 | void rewind() { Reader::rewind(); } |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | // -------------------------------------------------------------------------------- |
|---|
| 88 | |
|---|
| 89 | class Writer { |
|---|
| 90 | public: |
|---|
| 91 | Writer() {} |
|---|
| 92 | virtual ~Writer() {} |
|---|
| 93 | |
|---|
| 94 | virtual bool ok() const = 0; |
|---|
| 95 | virtual void out(char ch) = 0; |
|---|
| 96 | virtual const char *name() const = 0; |
|---|
| 97 | |
|---|
| 98 | virtual void throw_write_error() const __ATTR__NORETURN; |
|---|
| 99 | virtual int out(const char *text); |
|---|
| 100 | virtual int outf(const char *format, ...) __ATTR__FORMAT_MEMBER(1); |
|---|
| 101 | |
|---|
| 102 | void repeated(char ch, int repeat) { while (repeat--) out(ch); } |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | class FileWriter : public Writer, virtual Noncopyable { |
|---|
| 106 | FILE *ofp; |
|---|
| 107 | char *filename; |
|---|
| 108 | int written; // count written sequences |
|---|
| 109 | |
|---|
| 110 | public: |
|---|
| 111 | FileWriter(const char *outf); |
|---|
| 112 | ~FileWriter(); |
|---|
| 113 | |
|---|
| 114 | FILE *get_FILE() { return ofp; } |
|---|
| 115 | |
|---|
| 116 | bool ok() const { return ofp != NULL; } |
|---|
| 117 | void out(char ch); |
|---|
| 118 | const char *name() const { return filename; } |
|---|
| 119 | |
|---|
| 120 | int out(const char *text) { return Writer::out(text); } |
|---|
| 121 | int outf(const char *format, ...) __ATTR__FORMAT_MEMBER(1); |
|---|
| 122 | |
|---|
| 123 | void seq_done() { ++written; } |
|---|
| 124 | void seq_done(int count) { ca_assert(count >= 0); written += count; } |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | #else |
|---|
| 128 | #error reader.h included twice |
|---|
| 129 | #endif // READER_H |
|---|
| 130 | |
|---|