ARB
BufferedFileReader.h
Go to the documentation of this file.
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 
32 using std::string;
33 
34 class LineReader : virtual Noncopyable {
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 
44 protected:
45  void reset() {
46  if (next_line) {
47  delete next_line;
48  next_line = NULp;
49  }
50  lineNumber = 0;
51  }
52 
53 public:
55  : lineNumber(0),
56  next_line(NULp),
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 = NULp;
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);
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 
99 MARK_NONFINAL_FUNCTION(LineReader,bool,getLine,(string&),true);
100 
101 const size_t BUFFERSIZE = 64*1024;
102 
103 class BufferedFileReader : public LineReader { // derived from Noncopyable
104  char buf[BUFFERSIZE];
105  size_t read; // chars in buf
106  size_t offset; // offset to next line
107 
108  FILE *fp;
109 
110  string filename;
111 
112  void fillBuffer();
113 
114  bool getLine_intern(string& line) FINAL_OVERRIDE;
115 
116 protected:
117  FILE*& get_fp() { return fp; }
118 
119 public:
120  BufferedFileReader(const string& filename_, FILE *in) {
121  filename = filename_;
122  fp = in;
123 
124  fb_assert(fp);
125  read = BUFFERSIZE;
126  fillBuffer();
127  }
129  if (fp) fclose(fp);
130  }
131 
132  bool good() { return fp; }
133  void rewind();
134 
135  const string& getFilename() const FINAL_OVERRIDE { return filename; }
136 
138 };
140 
141 #else
142 #error BufferedFileReader.h included twice
143 #endif // BUFFEREDFILEREADER_H
return string(buffer, length)
PREPARE_MARK_NONFINAL_CLASS(BufferedFileReader)
BufferedFileReader(const string &filename_, FILE *in)
MARK_NONFINAL_CLASS(BufferedFileReader)
#define FINAL_OVERRIDE
Definition: cxxforward.h:95
void showFilenameInLineError(bool show)
size_t getLineNumber() const
const string & getFilename() const FINAL_OVERRIDE
void backLine(const string &line)
#define true
Definition: ureadseq.h:14
string lineError(const string &msg) const
string lineError(const char *msg) const
fputc('\n', stderr)
virtual bool getLine(string &line)
void copyTo(FILE *out)
virtual const string & getFilename() const =0
fputs(TRACE_PREFIX, stderr)
#define fb_assert(cond)
const size_t BUFFERSIZE
MARK_NONFINAL_FUNCTION(LineReader, bool, getLine,(string &), true)
static int line
Definition: arb_a2ps.c:296
#define NULp
Definition: cxxforward.h:97
virtual ~LineReader()
void setLineNumber(size_t line)