1 | #ifndef READER_H |
---|
2 | #define READER_H |
---|
3 | |
---|
4 | #ifndef BUFFEREDFILEREADER_H |
---|
5 | #include <BufferedFileReader.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 | BufferedFileReader *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 | MARK_NONFINAL_DTOR(Reader); // silence weird suggestion to make Reader-dtor final |
---|
64 | |
---|
65 | inline const char *shorttimekeep(char *heapcopy) { RETURN_LOCAL_ALLOC(heapcopy); } |
---|
66 | inline const char *shorttimecopy(const char *nocopy) { return shorttimekeep(nulldup(nocopy)); } |
---|
67 | |
---|
68 | struct FormatReader { |
---|
69 | virtual ~FormatReader() {} |
---|
70 | virtual bool read_one_entry(Seq& seq) __ATTR__USERESULT = 0; |
---|
71 | virtual bool failed() const = 0; |
---|
72 | virtual void ignore_rest_of_file() = 0; |
---|
73 | virtual void rewind() = 0; |
---|
74 | virtual InputFormat& get_data() = 0; |
---|
75 | |
---|
76 | static SmartPtr<FormatReader> create(const FormattedFile& in); |
---|
77 | }; |
---|
78 | |
---|
79 | typedef SmartPtr<FormatReader> FormatReaderPtr; |
---|
80 | |
---|
81 | struct SimpleFormatReader : public Reader, public FormatReader { |
---|
82 | SimpleFormatReader(const char *inf) : Reader(inf) {} |
---|
83 | bool failed() const OVERRIDE { return Reader::failed(); } |
---|
84 | void ignore_rest_of_file() OVERRIDE { Reader::ignore_rest_of_file(); } |
---|
85 | void rewind() OVERRIDE { Reader::rewind(); } |
---|
86 | }; |
---|
87 | |
---|
88 | // -------------------------------------------------------------------------------- |
---|
89 | |
---|
90 | #if defined(ASSERTION_USED) |
---|
91 | #define ENFORCE_CHECKED_WRITTEN |
---|
92 | #endif |
---|
93 | |
---|
94 | struct Writer { |
---|
95 | Writer() {} |
---|
96 | virtual ~Writer() {} |
---|
97 | |
---|
98 | virtual bool ok() const = 0; |
---|
99 | virtual void out(char ch) = 0; |
---|
100 | virtual const char *name() const = 0; |
---|
101 | |
---|
102 | virtual void throw_write_error() const __ATTR__NORETURN; |
---|
103 | virtual int out(const char *text) { |
---|
104 | int i = 0; |
---|
105 | while (text[i]) { |
---|
106 | out(text[i++]); |
---|
107 | } |
---|
108 | return i; |
---|
109 | } |
---|
110 | virtual int outf(const char *format, ...) __ATTR__FORMAT_MEMBER(1); |
---|
111 | |
---|
112 | void repeated(char ch, int repeat) { while (repeat--) out(ch); } |
---|
113 | }; |
---|
114 | |
---|
115 | class FileWriter : public Writer, virtual Noncopyable { |
---|
116 | FILE *ofp; |
---|
117 | char *filename; |
---|
118 | int written; // count written sequences |
---|
119 | |
---|
120 | #if defined(ENFORCE_CHECKED_WRITTEN) |
---|
121 | bool checked_written; |
---|
122 | #endif |
---|
123 | |
---|
124 | bool is_fine() const { return ofp && !Convaln_exception::exception_thrown(); } |
---|
125 | |
---|
126 | public: |
---|
127 | FileWriter(const char *outf); |
---|
128 | ~FileWriter() OVERRIDE; |
---|
129 | |
---|
130 | FILE *get_FILE() { return ofp; } |
---|
131 | |
---|
132 | bool ok() const OVERRIDE { return ofp != NULL; } |
---|
133 | void out(char ch) FINAL_OVERRIDE { |
---|
134 | if (fputc(ch, ofp) == EOF) throw_write_error(); |
---|
135 | } |
---|
136 | |
---|
137 | const char *name() const OVERRIDE { return filename; } |
---|
138 | |
---|
139 | int out(const char *text) OVERRIDE { return Writer::out(text); } |
---|
140 | int outf(const char *format, ...) OVERRIDE __ATTR__FORMAT_MEMBER(1); |
---|
141 | |
---|
142 | void seq_done() { ++written; } |
---|
143 | void seq_done(int count) { ca_assert(count >= 0); written += count; } |
---|
144 | |
---|
145 | void expect_written(); |
---|
146 | }; |
---|
147 | MARK_NONFINAL_CLASS(FileWriter); |
---|
148 | |
---|
149 | #else |
---|
150 | #error reader.h included twice |
---|
151 | #endif // READER_H |
---|
152 | |
---|