libStatGen Software  1
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     /// \param  compressed whether or not to compress the file, defaults to true
00066     /// \return true = success; false = failure.
00067     bool openForWrite(const char * filename, bool compressed = true);
00068 
00069     /// Close the file if there is one open, adding an end marker record
00070     /// if there is a previous section and one has not already been written.
00071     void close();
00072 
00073     /// Returns whether or not the end of the file has been reached.
00074     /// \return true = EOF; false = not eof.
00075     /// If the file is not open, true is returned.
00076     bool isEOF();
00077    
00078     /// Reads the header section from the file and stores it in
00079     /// the passed in header.
00080     /// \param  header header object to populate with the file's glf header.
00081     /// \return true = success; false = failure.
00082     bool readHeader(GlfHeader& header);
00083    
00084     /// Writes the specified header into the file.
00085     /// \param  header header object to write into the file.
00086     /// \return true = success; false = failure.
00087     bool writeHeader(GlfHeader& header);
00088 
00089     /// Gets the next reference section from the file & stores it in the
00090     /// passed in section, consuming records until a new section is found.
00091     /// \param  refSection object to populate with the file's next reference 
00092     ///                    section.
00093     /// \return true  = section was successfully set.
00094     ///         false = section was not successfully set.
00095     bool getNextRefSection(GlfRefSection& refSection);
00096    
00097     /// Write the reference section to the file, adding an end marker record
00098     /// if there is a previous section and one has not already been written.
00099     /// \param  refSection reference section to write to the file.
00100     /// \return true = succes; false = failure.
00101     bool writeRefSection(const GlfRefSection& refSection);
00102 
00103     /// Gets the nextrecord from the file & stores it in the
00104     /// passed in record.
00105     /// \param  record object to populate with the file's next record. 
00106     /// \return true  = record was successfully set.
00107     ///         false = record not successfully set or for the endMarker record.
00108     bool getNextRecord(GlfRecord& record);
00109    
00110     /// Writes the specified record into the file.
00111     /// \param record record to write to the file.
00112     /// \return true = success; false = failure.
00113     bool writeRecord(const GlfRecord& record);
00114    
00115     /// Return the number of records that have been read/written so far.
00116     /// \return number of records that have been read/written so far.
00117     uint32_t getCurrentRecordCount();
00118 
00119     /// Get the Status of the last call that sets status.
00120     /// To remain backwards compatable - will be removed later.
00121     inline GlfStatus::Status getFailure()
00122     {
00123         return(getStatus());
00124     }
00125 
00126     /// Get the Status of the last call that sets status.
00127     /// \return status of the last method that sets a status.
00128     inline GlfStatus::Status getStatus()
00129     {
00130         return(myStatus.getStatus());
00131     }
00132 
00133     /// Get the Status of the last call that sets status.
00134     /// \return status message of the last method that sets a status.
00135     inline const char* getStatusMessage()
00136     {
00137         return(myStatus.getStatusMessage());
00138     }
00139 
00140 private:
00141     /// reset this file including all its attributes.
00142     void resetFile();
00143 
00144     /// Pointer to the file
00145     IFILE  myFilePtr;
00146 
00147     /// Flag to indicate if a file is open for reading.
00148     bool myIsOpenForRead;
00149     /// Flag to indicate if a file is open for writing.
00150     bool myIsOpenForWrite;
00151 
00152     /// End marker that is inserted when writing files if a new section
00153     /// is specified without one or if the file is closed without writing
00154     /// an endMarker.
00155     GlfRecord myEndMarker;
00156 
00157     /// Track the state of this file as to what it is expecting to read next.
00158     enum EXPECTED_SECTION
00159     {
00160         HEADER,
00161         REF_SECTION,
00162         RECORD
00163     } myNextSection;
00164     
00165     /// Keep count of the number of records that have been read/written so far.
00166     uint32_t myRecordCount;
00167 
00168     /// The status of the last GlfFile command.
00169     GlfStatus myStatus;
00170 };
00171 
00172 
00173 class GlfFileReader : public GlfFile
00174 {
00175 public:
00176 
00177     /// Default Constructor.
00178     GlfFileReader();
00179 
00180     /// Constructor that opens the specified file for read.
00181      /// \param filename file to open for reading.
00182    GlfFileReader(const char* filename);
00183 
00184     virtual ~GlfFileReader();
00185 };
00186 
00187 
00188 class GlfFileWriter : public GlfFile
00189 {
00190 public:
00191     /// Default Constructor.
00192     GlfFileWriter();
00193 
00194     /// Constructor that opens the specified file for write.
00195     /// \param filename file to open for writing.
00196     GlfFileWriter(const char* filename);
00197 
00198     virtual ~GlfFileWriter();
00199 };
00200 
00201 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends