1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 // FILEBUFF.CPP - Routines for handling a parser file buffer
26 #include "adlc.hpp"
27
28 //------------------------------FileBuff---------------------------------------
29 // Create a new parsing buffer
FileBuff(BufferedFile * fptr,ArchDesc & archDesc)30 FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
31 _err = fseek(_fp->_fp, 0, SEEK_END); // Seek to end of file
32 if (_err) {
33 file_error(SEMERR, 0, "File seek error reading input file");
34 exit(1); // Exit on seek error
35 }
36 _filepos = ftell(_fp->_fp); // Find offset of end of file
37 _bufferSize = _filepos + 5; // Filepos points to last char, so add padding
38 _err = fseek(_fp->_fp, 0, SEEK_SET); // Reset to beginning of file
39 if (_err) {
40 file_error(SEMERR, 0, "File seek error reading input file\n");
41 exit(1); // Exit on seek error
42 }
43 _filepos = ftell(_fp->_fp); // Reset current file position
44 _linenum = 0;
45
46 _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
47 if( !_bigbuf ) {
48 file_error(SEMERR, 0, "Buffer allocation failed\n");
49 exit(1); // Exit on allocation failure
50 }
51 *_bigbuf = '\n'; // Lead with a sentinel newline
52 _buf = _bigbuf+1; // Skip sentinel
53 _bufmax = _buf; // Buffer is empty
54 _bufeol = _bigbuf; // _bufeol points at sentinel
55 _filepos = -1; // filepos is in sync with _bufeol
56 _bufoff = _offset = 0L; // Offset at file start
57
58 _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
59 if (_bufmax == _buf) {
60 file_error(SEMERR, 0, "File read error, no input read\n");
61 exit(1); // Exit on read error
62 }
63 *_bufmax = '\n'; // End with a sentinel new-line
64 *(_bufmax+1) = '\0'; // Then end with a sentinel NULL
65 }
66
67 //------------------------------~FileBuff--------------------------------------
68 // Nuke the FileBuff
~FileBuff()69 FileBuff::~FileBuff() {
70 delete[] _bigbuf;
71 }
72
73 //------------------------------get_line----------------------------------------
get_line(void)74 char *FileBuff::get_line(void) {
75 char *retval;
76
77 // Check for end of file & return NULL
78 if (_bufeol >= _bufmax) return NULL;
79
80 _linenum++;
81 retval = ++_bufeol; // return character following end of previous line
82 if (*retval == '\0') return NULL; // Check for EOF sentinel
83 // Search for newline character which must end each line
84 for(_filepos++; *_bufeol != '\n'; _bufeol++)
85 _filepos++; // keep filepos in sync with _bufeol
86 // _bufeol & filepos point at end of current line, so return pointer to start
87 return retval;
88 }
89
90 //------------------------------file_error-------------------------------------
file_error(int flag,int linenum,const char * fmt,...)91 void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
92 {
93 va_list args;
94
95 va_start(args, fmt);
96 switch (flag) {
97 case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
98 break;
99 case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
100 break;
101 case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
102 break;
103 default: assert(0, ""); break;
104 }
105 va_end(args);
106 _AD._no_output = 1;
107 }
108