1
|
1 |
/*
|
|
2 |
* Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
// FILEBUFF.HPP - Definitions for parser file buffering routines
|
775
|
26 |
#include <iostream>
|
1
|
27 |
|
775
|
28 |
using namespace std;
|
1
|
29 |
// STRUCTURE FOR HANDLING INPUT AND OUTPUT FILES
|
|
30 |
typedef struct {
|
|
31 |
const char *_name;
|
|
32 |
FILE *_fp;
|
|
33 |
} BufferedFile;
|
|
34 |
|
|
35 |
class ArchDesc;
|
|
36 |
|
|
37 |
//------------------------------FileBuff--------------------------------------
|
|
38 |
// This class defines a nicely behaved buffer of text. Entire file of text
|
|
39 |
// is read into buffer at creation, with sentinals at start and end.
|
|
40 |
class FileBuff {
|
|
41 |
friend class FileBuffRegion;
|
|
42 |
private:
|
|
43 |
long _bufferSize; // Size of text holding buffer.
|
|
44 |
long _offset; // Expected filepointer offset.
|
|
45 |
long _bufoff; // Start of buffer file offset
|
|
46 |
|
|
47 |
char *_buf; // The buffer itself.
|
|
48 |
char *_bigbuf; // The buffer plus sentinals; actual heap area
|
|
49 |
char *_bufmax; // A pointer to the buffer end sentinal
|
|
50 |
char *_bufeol; // A pointer to the last complete line end
|
|
51 |
|
|
52 |
int _err; // Error flag for file seek/read operations
|
|
53 |
long _filepos; // Current offset from start of file
|
|
54 |
|
|
55 |
ArchDesc& _AD; // Reference to Architecture Description
|
|
56 |
|
|
57 |
// Error reporting function
|
|
58 |
void file_error(int flag, int linenum, const char *fmt, ...);
|
|
59 |
|
|
60 |
public:
|
|
61 |
const BufferedFile *_fp; // File to be buffered
|
|
62 |
|
|
63 |
FileBuff(BufferedFile *fp, ArchDesc& archDesc); // Constructor
|
|
64 |
~FileBuff(); // Destructor
|
|
65 |
|
|
66 |
// This returns a pointer to the start of the current line in the buffer,
|
|
67 |
// and increments bufeol and filepos to point at the end of that line.
|
|
68 |
char *get_line(void);
|
|
69 |
|
|
70 |
// This converts a pointer into the buffer to a file offset. It only works
|
|
71 |
// when the pointer is valid (i.e. just obtained from getline()).
|
|
72 |
int getoff(const char *s) { return _bufoff+(int)(s-_buf); }
|
|
73 |
};
|
|
74 |
|
|
75 |
//------------------------------FileBuffRegion---------------------------------
|
|
76 |
// A buffer region is really a region of some file, specified as a linked list
|
|
77 |
// of offsets and lengths. These regions can be merged; overlapping regions
|
|
78 |
// will coalesce.
|
|
79 |
class FileBuffRegion {
|
|
80 |
public: // Workaround dev-studio friend/private bug
|
|
81 |
FileBuffRegion *_next; // Linked list of regions sorted by offset.
|
|
82 |
private:
|
|
83 |
FileBuff *_bfr; // The Buffer of the file
|
|
84 |
int _offset, _length; // The file area
|
|
85 |
int _sol; // Start of line where the file area starts
|
|
86 |
int _line; // First line of region
|
|
87 |
|
|
88 |
public:
|
|
89 |
FileBuffRegion(FileBuff*, int sol, int line, int offset, int len);
|
|
90 |
~FileBuffRegion();
|
|
91 |
|
|
92 |
FileBuffRegion *copy(); // Deep copy
|
|
93 |
FileBuffRegion *merge(FileBuffRegion*); // Merge 2 regions; delete input
|
|
94 |
|
|
95 |
// void print(std::ostream&);
|
|
96 |
// friend std::ostream& operator<< (std::ostream&, FileBuffRegion&);
|
|
97 |
void print(ostream&);
|
|
98 |
friend ostream& operator<< (ostream&, FileBuffRegion&);
|
|
99 |
};
|