//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+ Student: Junshan Li + //+ Proj. Assignemnt: Two + //+ Due Date: Febuary 12, 2001 + //+ Course Name: MSCS 518 - Compiler Design + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // FileManager.cpp // Implementing FileManager class. #include #include "FileManager.h" #include #include #include // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // FileManager class constructor. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FileManager::FileManager() { ENDOFPROGRAM = false; lineNumber = 0; for (int i = 0; i < STRSIZE; i++) { // initialize fileName array with nil. fileName[i] = '\0'; } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func1. Opens the named file fileName if it exists or else write an error // message and terminate. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::retrieveProgram(char * aFileName) { fin.open(aFileName); if (fin.fail()) { cout << "Input file: " << aFileName << " opening failed!!!\n"; exit(1); } storeFileName(aFileName); ENDOFPROGRAM = false; } // end of function retrieveProgram() // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func2. Saves fileName in a char array (string) fileName[]. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::storeFileName(char * aFileName) { strcpy(fileName, aFileName); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func3. Retrieve the stored file name from the char array: fileName[]. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ char* FileManager::getProgramName() { return fileName; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func4. Close the file that was opened. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::closeProgram() { // closes the file that was openned; fin.close(); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func5. Reads and returns the next charactor of the input file. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ char FileManager::getNextChar() { // reads and returns next character in file char nextChar; fin.get(nextChar); if (fin.eof()) { ENDOFPROGRAM = true; } //cout << "In FileManager::getNextChar(), nextChar == " << nextChar << endl; return toupper(nextChar); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func6. Returns the next charactor of the input file without extraction. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ char FileManager::peekNextChar() { // returns the next char without extraction. // istream's member function peek does the job. char nextOne; nextOne = fin.peek(); return nextOne; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func7. Displays the entire input file to out put device with line number // in front of each line. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::seeProgramFile() { // displays entire file on output device (include // line number for each program line char buffer[STRSIZE]; while (1) { fin.getline(buffer, STRSIZE); if (fin.eof()) break; lineNumber++; cout << "Line " << lineNumber << ": " << buffer << endl; } closeProgram(); retrieveProgram(fileName); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func8. Internal Use Only. Returns the total number of lines of the input // file. Used by function seeErrorLines(). // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int FileManager::countLines() { char buffer[STRSIZE]; int lineNumber1 = 0; retrieveProgram(fileName); while (1) { fin.getline(buffer, STRSIZE); if (fin.eof()) break; lineNumber1++; } closeProgram(); return (lineNumber1 - 1); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func9. Displays 2 contiguous lines of a file where an error may be detected. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::seeErrorLines(int lineNum) { // displays 2 contiguous lines of file starting with line=lineNum // if lineNum greater than 1 else display only first line. int n = 1; retrieveProgram(fileName); while (!fin.eof()) { fin.getline(buffer, STRSIZE); if ( (n == lineNum - 1) || (n == lineNum) ) { cout.width(3); cout << "Error in " << n << " " << buffer << endl; } // end of if n++; } // end of while loop. closeProgram(); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // func10. Initialized lineNumber to 0. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void FileManager::initLineNumber() { lineNumber = 0; }