GlfFile.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __GLF_FILE_H__
00019 #define __GLF_FILE_H__
00020 
00021 #include "InputFile.h"
00022 #include "GlfHeader.h"
00023 #include "GlfRefSection.h"
00024 #include "GlfRecord.h"
00025 #include "GlfStatus.h"
00026 
00027 /// This class allows a user to easily read/write a GLF file.
00028 class GlfFile
00029 {
00030 public:
00031     /// Enum for indicating whether to open the file for read or write.
00032     enum OpenType 
00033         {
00034             READ, ///< open for reading.
00035             WRITE ///< open for writing.
00036         };
00037 
00038     /// Default Constructor.
00039     GlfFile();
00040 
00041     /// Constructor that opens the specified file based on the specified mode
00042     /// (READ/WRITE).  Default is READ.
00043     /// \param filename name of the file to open.
00044     /// \param mode mode to use for opening the file (defaults to READ).
00045     GlfFile(const char* filename, OpenType mode = READ);
00046 
00047     /// Closes the file if there is one open, adding an end marker record
00048     /// if there is a previous section and one has not already been written.
00049     virtual ~GlfFile();
00050    
00051     /// Open a glf file for reading with the specified filename.
00052     /// \param  filename glf file to open for reading.
00053     /// \return true = success; false = failure.   
00054     bool openForRead(const char * filename);
00055 
00056     /// Open a glf file for reading with the specified filename and read the
00057     /// header into the specified header.
00058     /// \param  filename glf file to open for reading.
00059     /// \param  header header object to populate with the file's glf header.
00060     /// \return true = success; false = failure.   
00061     bool openForRead(const char * filename, GlfHeader& header);
00062 
00063     /// Open a glf file for writing with the specified filename.
00064     /// \param  filename glf file to open for writing.
00065     /// \return true = success; false = failure.
00066     bool openForWrite(const char * filename);
00067 
00068     /// Close the file if there is one open, adding an end marker record
00069     /// if there is a previous section and one has not already been written.
00070     void close();
00071 
00072     /// Returns whether or not the end of the file has been reached.
00073     /// \return true = EOF; false = not eof.
00074     /// If the file is not open, false is returned.
00075     bool isEOF();
00076    
00077     /// Reads the header section from the file and stores it in
00078     /// the passed in header.
00079     /// \param  header header object to populate with the file's glf header.
00080     /// \return true = success; false = failure.
00081     bool readHeader(GlfHeader& header);
00082    
00083     /// Writes the specified header into the file.
00084     /// \param  header header object to write into the file.
00085     /// \return true = success; false = failure.
00086     bool writeHeader(GlfHeader& header);
00087 
00088     /// Gets the next reference section from the file & stores it in the
00089     /// passed in section, consuming records until a new section is found.
00090     /// \param  refSection object to populate with the file's next reference 
00091     ///                    section.
00092     /// \return true  = section was successfully set.
00093     ///         false = section was not successfully set.
00094     bool getNextRefSection(GlfRefSection& refSection);
00095    
00096     /// Write the reference section to the file, adding an end marker record
00097     /// if there is a previous section and one has not already been written.
00098     /// \param  refSection reference section to write to the file.
00099     /// \return true = succes; false = failure.
00100     bool writeRefSection(const GlfRefSection& refSection);
00101 
00102     /// Gets the nextrecord from the file & stores it in the
00103     /// passed in record.
00104     /// \param  record object to populate with the file's next record. 
00105     /// \return true  = record was successfully set.
00106     ///         false = record not successfully set or for the endMarker record.
00107     bool getNextRecord(GlfRecord& record);
00108    
00109     /// Writes the specified record into the file.
00110     /// \param record record to write to the file.
00111     /// \return true = success; false = failure.
00112     bool writeRecord(const GlfRecord& record);
00113    
00114     /// Return the number of records that have been read/written so far.
00115     /// \return number of records that have been read/written so far.
00116     uint32_t getCurrentRecordCount();
00117 
00118     /// Get the Status of the last call that sets status.
00119     /// To remain backwards compatable - will be removed later.
00120     inline GlfStatus::Status getFailure()
00121     {
00122         return(getStatus());
00123     }
00124 
00125     /// Get the Status of the last call that sets status.
00126     /// \return status of the last method that sets a status.
00127     inline GlfStatus::Status getStatus()
00128     {
00129         return(myStatus.getStatus());
00130     }
00131 
00132     /// Get the Status of the last call that sets status.
00133     /// \return status message of the last method that sets a status.
00134     inline const char* getStatusMessage()
00135     {
00136         return(myStatus.getStatusMessage());
00137     }
00138 
00139 private:
00140     /// reset this file including all its attributes.
00141     void resetFile();
00142 
00143     /// Pointer to the file
00144     IFILE  myFilePtr;
00145 
00146     /// Flag to indicate if a file is open for reading.
00147     bool myIsOpenForRead;
00148     /// Flag to indicate if a file is open for writing.
00149     bool myIsOpenForWrite;
00150 
00151     /// End marker that is inserted when writing files if a new section
00152     /// is specified without one or if the file is closed without writing
00153     /// an endMarker.
00154     GlfRecord myEndMarker;
00155 
00156     /// Track the state of this file as to what it is expecting to read next.
00157     enum EXPECTED_SECTION
00158     {
00159         HEADER,
00160         REF_SECTION,
00161         RECORD
00162     } myNextSection;
00163     
00164     /// Keep count of the number of records that have been read/written so far.
00165     uint32_t myRecordCount;
00166 
00167     /// The status of the last GlfFile command.
00168     GlfStatus myStatus;
00169 };
00170 
00171 
00172 class GlfFileReader : public GlfFile
00173 {
00174 public:
00175 
00176     /// Default Constructor.
00177     GlfFileReader();
00178 
00179     /// Constructor that opens the specified file for read.
00180      /// \param filename file to open for reading.
00181    GlfFileReader(const char* filename);
00182 
00183     virtual ~GlfFileReader();
00184 };
00185 
00186 
00187 class GlfFileWriter : public GlfFile
00188 {
00189 public:
00190     /// Default Constructor.
00191     GlfFileWriter();
00192 
00193     /// Constructor that opens the specified file for write.
00194     /// \param filename file to open for writing.
00195     GlfFileWriter(const char* filename);
00196 
00197     virtual ~GlfFileWriter();
00198 };
00199 
00200 #endif
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3