source: branches/port5/SL/FILE_BUFFER/FileBuffer.h

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