source: tags/svn.1.5.4/CONVERTALN/reader.h

Last change on this file was 7623, checked in by westram, 13 years ago
  • merge from dev [7450] [7452] [7456] [7457] [7458] [7459] [7460] [7461] [7464] [7465] [7466] [7467] [7468] [7469] [7482]
    • tweaked compiler options
      • activated -Weffc++
        • postfilter warnings where Scott Meyers' advices are too general.
          • base classes should not always have virtual destructors, since that renders tiny classes useless and
          • members should not always be initialized via initialization list, since that often violates the DRY principle
        • fix gcc's inability to detect that Noncopyable implements a private copy-ctor and op=
        • this slows down complete ARB recompilation by ~5%
    • added -Wold-style-cast (inactive)
    • removed -Wno-non-template-friend added in [7447]
  • postcompile.pl
    • added option —original to show unmodified compiler output
  • declared op= for classes which had a copy-ctor
  • moved op= macros to arbtools.h
  • derived classes containing pointers from Noncopyable (use Noncopyable virtually) or
  • made them copyable if needed (awt_mask_item, KnownDB, Code, AWT_registered_itemtype, GEN_gene, PosGene, PartialSequence, PlugIn, Range, Convaln_exception)
  • other related changes
    • user mask destruction working now
File size: 3.2 KB
Line 
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
21class 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
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 { return Reader::failed(); }
83    void ignore_rest_of_file() { Reader::ignore_rest_of_file(); }
84    void rewind() { Reader::rewind(); }
85};
86
87// --------------------------------------------------------------------------------
88
89class Writer {
90public:
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
105class FileWriter : public Writer, virtual Noncopyable {
106    FILE *ofp;
107    char *filename;
108    int   written; // count written sequences
109
110public:
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
Note: See TracBrowser for help on using the repository browser.