jdk
1/*
2* Copyright (c) 1997, 2023, 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
30FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {31_err = fseek(_fp->_fp, 0, SEEK_END); // Seek to end of file32if (_err) {33file_error(SEMERR, 0, "File seek error reading input file");34exit(1); // Exit on seek error35}36_filepos = ftell(_fp->_fp); // Find offset of end of file37_bufferSize = _filepos + 5; // Filepos points to last char, so add padding38_err = fseek(_fp->_fp, 0, SEEK_SET); // Reset to beginning of file39if (_err) {40file_error(SEMERR, 0, "File seek error reading input file\n");41exit(1); // Exit on seek error42}43_filepos = ftell(_fp->_fp); // Reset current file position44_linenum = 0;45
46_bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser47if( !_bigbuf ) {48file_error(SEMERR, 0, "Buffer allocation failed\n");49exit(1); // Exit on allocation failure50}51*_bigbuf = '\n'; // Lead with a sentinel newline52_buf = _bigbuf+1; // Skip sentinel53_bufmax = _buf; // Buffer is empty54_bufeol = _bigbuf; // _bufeol points at sentinel55_filepos = -1; // filepos is in sync with _bufeol56_bufoff = _offset = 0L; // Offset at file start57
58_bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value59if (_bufmax == _buf) {60file_error(SEMERR, 0, "File read error, no input read\n");61exit(1); // Exit on read error62}63*_bufmax = '\n'; // End with a sentinel new-line64*(_bufmax+1) = '\0'; // Then end with a sentinel null65}
66
67//------------------------------~FileBuff--------------------------------------
68// Nuke the FileBuff
69FileBuff::~FileBuff() {70delete[] _bigbuf;71}
72
73//------------------------------get_line----------------------------------------
74char *FileBuff::get_line(void) {75char *retval;76
77// Check for end of file & return null78if (_bufeol >= _bufmax) return nullptr;79
80_linenum++;81retval = ++_bufeol; // return character following end of previous line82if (*retval == '\0') return nullptr; // Check for EOF sentinel83// Search for newline character which must end each line84for(_filepos++; *_bufeol != '\n'; _bufeol++)85_filepos++; // keep filepos in sync with _bufeol86// _bufeol & filepos point at end of current line, so return pointer to start87return retval;88}
89
90//------------------------------file_error-------------------------------------
91void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)92{
93va_list args;94
95va_start(args, fmt);96switch (flag) {97case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);98break;99case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);100break;101case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);102break;103default: assert(0, ""); break;104}105va_end(args);106_AD._no_output = 1;107}
108