source: tags/arb-6.0/CONVERTALN/reader.h

Last change on this file was 11844, checked in by westram, 10 years ago
File size: 3.6 KB
Line 
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
21class 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
35public:
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
64inline const char *shorttimekeep(char *heapcopy) { RETURN_LOCAL_ALLOC(heapcopy); }
65inline const char *shorttimecopy(const char *nocopy) { return shorttimekeep(nulldup(nocopy)); }
66
67struct 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
78typedef SmartPtr<FormatReader> FormatReaderPtr;
79
80struct SimpleFormatReader : public Reader, public FormatReader {
81    SimpleFormatReader(const char *inf) : Reader(inf) {}
82    bool failed() const OVERRIDE { return Reader::failed(); }
83    void ignore_rest_of_file() OVERRIDE { Reader::ignore_rest_of_file(); }
84    void rewind() OVERRIDE { Reader::rewind(); }
85};
86
87// --------------------------------------------------------------------------------
88
89#if defined(ASSERTION_USED)
90#define ENFORCE_CHECKED_WRITTEN
91#endif
92
93struct Writer {
94    Writer() {}
95    virtual ~Writer() {}
96
97    virtual bool ok() const          = 0;
98    virtual void out(char ch)        = 0;
99    virtual const char *name() const = 0;
100
101    virtual void throw_write_error() const __ATTR__NORETURN;
102    virtual int out(const char *text);
103    virtual int outf(const char *format, ...) __ATTR__FORMAT_MEMBER(1);
104
105    void repeated(char ch, int repeat) { while (repeat--) out(ch); }
106};
107
108class FileWriter : public Writer, virtual Noncopyable {
109    FILE *ofp;
110    char *filename;
111    int   written; // count written sequences
112
113#if defined(ENFORCE_CHECKED_WRITTEN)
114    bool checked_written;
115#endif
116
117    bool is_fine() const { return ofp && !Convaln_exception::exception_thrown(); }
118
119public:
120    FileWriter(const char *outf);
121    ~FileWriter() OVERRIDE;
122
123    FILE *get_FILE() { return ofp; }
124
125    bool ok() const OVERRIDE { return ofp != NULL; }
126    void out(char ch) OVERRIDE;
127    const char *name() const OVERRIDE { return filename; }
128
129    int out(const char *text) OVERRIDE { return Writer::out(text); }
130    int outf(const char *format, ...) OVERRIDE __ATTR__FORMAT_MEMBER(1);
131
132    void seq_done() { ++written; }
133    void seq_done(int count) { ca_assert(count >= 0); written += count; }
134
135    void expect_written();
136};
137
138#else
139#error reader.h included twice
140#endif // READER_H
141
Note: See TracBrowser for help on using the repository browser.