source: branches/profile/CORE/BufferedFileReader.h

Last change on this file was 11874, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1// --------------------------------------------------------------------------------
2// Copyright (C) 2000
3// Ralf Westram
4//
5// Permission to use, copy, modify, distribute and sell this software
6// and its documentation for any purpose is hereby granted without fee,
7// provided that the above copyright notice appear in all copies and
8// that both that copyright notice and this permission notice appear
9// in supporting documentation.  Ralf Westram makes no
10// representations about the suitability of this software for any
11// purpose.  It is provided "as is" without express or implied warranty.
12// --------------------------------------------------------------------------------
13
14#ifndef BUFFEREDFILEREADER_H
15#define BUFFEREDFILEREADER_H
16
17#ifndef _GLIBCXX_CSTDIO
18#include <cstdio>
19#endif
20#ifndef _GLIBCXX_STRING
21#include <string>
22#endif
23#ifndef ARBTOOLS_H
24#include <arbtools.h>
25#endif
26#ifndef ARB_ASSERT_H
27#include <arb_assert.h>
28#endif
29
30#define fb_assert(cond) arb_assert(cond)
31
32using std::string;
33
34class LineReader : virtual Noncopyable {
35    /*! may represent any source that can be read line by line
36     */
37
38    size_t  lineNumber; // current line number
39    string *next_line;
40    bool   showFilename; // @@@ rename (not necessarily a file)
41
42    virtual bool getLine_intern(string& line) = 0;
43
44protected:
45    void reset() {
46        if (next_line) {
47            delete next_line;
48            next_line = NULL;
49        }
50        lineNumber = 0;
51    }
52
53public:
54    LineReader()
55        : lineNumber(0),
56          next_line(NULL),
57          showFilename(true)
58    {}
59    virtual ~LineReader() {
60        delete next_line;
61    }
62
63    string lineError(const string& msg) const;
64    string lineError(const char *msg) const { return lineError(string(msg)); }
65
66    void showFilenameInLineError(bool show) { showFilename = show; } // @@@ rename (not necessarily a file)
67
68    virtual bool getLine(string& line) {
69        lineNumber++;
70        if (next_line) {
71            line = *next_line;
72            delete next_line;
73            next_line = NULL;
74            return true;
75        }
76        return getLine_intern(line);
77    }
78
79    void backLine(const string& line) { // push line back
80        fb_assert(next_line==0);
81        next_line = new string(line);
82        lineNumber--;
83    }
84
85    size_t getLineNumber() const { return lineNumber; }
86    void setLineNumber(size_t line) { lineNumber = line; }
87
88    virtual const string& getFilename() const = 0; // @@@ rename (not necessarily a file)
89
90    void copyTo(FILE *out) {
91        string line;
92        while (getLine(line)) {
93            fputs(line.c_str(), out);
94            fputc('\n', out);
95        }
96    }
97};
98
99const size_t BUFFERSIZE = 64*1024;
100
101class BufferedFileReader : public LineReader { // derived from Noncopyable
102    char buf[BUFFERSIZE];
103    size_t read; // chars in buf
104    size_t offset; // offset to next line
105
106    FILE *fp;
107
108    string filename;
109
110    void fillBuffer();
111
112    bool getLine_intern(string& line) OVERRIDE;
113
114public:
115    BufferedFileReader(const string& filename_, FILE *in) {
116        filename = filename_;
117        fp       = in;
118
119        fb_assert(fp);
120        read = BUFFERSIZE;
121        fillBuffer();
122    }
123    virtual ~BufferedFileReader() {
124        if (fp) fclose(fp);
125    }
126
127    bool good() { return fp!=0; }
128    void rewind();
129
130    const string& getFilename() const OVERRIDE { return filename; }
131
132};
133
134#else
135#error BufferedFileReader.h included twice
136#endif // BUFFEREDFILEREADER_H
Note: See TracBrowser for help on using the repository browser.