1 /* 2 * cFile.h 3 * Avida 4 * 5 * Called "file.hh" prior to 12/7/05. 6 * Copyright 1999-2011 Michigan State University. All rights reserved. 7 * Copyright 1993-2003 California Institute of Technology. 8 * 9 * 10 * This file is part of Avida. 11 * 12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 * 15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along with Avida. 19 * If not, see <http://www.gnu.org/licenses/>. 20 * 21 */ 22 23 #ifndef cFile_h 24 #define cFile_h 25 26 #include "cString.h" 27 28 #include <fstream> 29 30 /** 31 * This class encapsulates file handling. In comparison to @ref cDataFile 32 * it has somewhat different features. It is more intended for reading files. 33 * In particular, by default it does not create a file that doesn't exist. 34 * Its main usage is for the class @ref cInitFile. 35 **/ 36 37 class cFile 38 { 39 private: 40 cFile(const cFile&); // @not_implemented 41 cFile& operator=(const cFile&); // @not_implemented 42 43 protected: 44 std::fstream fp; 45 std::ios::openmode m_openmode; 46 cString filename; 47 bool is_open; // Have we successfully opened this file? 48 bool verbose; // Should file be verbose about warnings to users? 49 50 public: cFile()51 cFile() : filename(""), is_open(false), verbose(false) { ; } 52 53 /** 54 * This constructor opens a file of the given name. 55 * 56 * @param _filename The name of the file to open. 57 **/ cFile(cString _filename)58 cFile(cString _filename) : filename(""), is_open(false) { Open(_filename); } 59 60 /** 61 * The desctructor automatically closes the file. 62 **/ ~cFile()63 ~cFile() { if (is_open == true) fp.close(); filename = ""; } 64 65 /** 66 * @return The name of the file currently open. 67 **/ GetFilename()68 const cString& GetFilename() const { return filename; } 69 70 /** 71 * Open a file of the given name. If another file was open previously, 72 * close that one first. 73 * 74 * @return 0 if something went wrong, and 1 otherwise. 75 * @param _filename The name of the file to open. 76 * @param mode The opening mode. 77 **/ 78 bool Open(cString _filename, std::ios::openmode mode=(std::ios::in)); 79 80 // Access to the underlying implmentation GetFileStream()81 std::fstream* GetFileStream() { return &fp; } 82 83 84 /** 85 * Close the currently open file. 86 **/ 87 bool Close(); 88 89 /** 90 * Reads the next line in the file. 91 **/ 92 bool ReadLine(cString& in_string); 93 94 // Tests IsOpen()95 bool IsOpen() const { return is_open; } Fail()96 bool Fail() const { return (fp.fail()); } Good()97 bool Good() const { return (fp.good()); } Eof()98 bool Eof() const { return (fp.eof()); } 99 100 void SetVerbose(bool _v=true) { verbose = _v; } 101 }; 102 103 #endif 104