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