GlfHeader.cpp

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 #include "GlfHeader.h"
00019 #include "GlfStatus.h"
00020 #include "GlfException.h"
00021 #include "StringBasics.h"
00022 
00023 const std::string GlfHeader::GLF_MAGIC = "GLF\3";
00024 const int GlfHeader::GLF_MAGIC_LEN = 4;
00025 
00026 GlfHeader::GlfHeader()
00027     : myText()
00028 {
00029     resetHeader();
00030 }
00031 
00032 
00033 GlfHeader::~GlfHeader()
00034 {
00035     resetHeader();
00036 }
00037 
00038 
00039 // Copy Constructor   
00040 GlfHeader::GlfHeader(const GlfHeader& header)
00041     : myText()
00042 {
00043     copy(header);
00044 }
00045 
00046 
00047 // Overload operator = to copy the passed in header into this header.
00048 GlfHeader & GlfHeader::operator = (const GlfHeader& header)
00049 {
00050     copy(header);
00051     return(*this);
00052 }
00053 
00054 
00055 bool GlfHeader::copy(const GlfHeader& header)
00056 {
00057     // Check to see if the passed in value is the same as this.
00058     if(this == &header)
00059     {
00060         return(true);
00061     }
00062 
00063     resetHeader();
00064 
00065     // Copy the header.
00066     myText = header.myText;
00067 
00068     return(true);
00069 }
00070 
00071 
00072 // Reset the header for a new entry, clearing out previous values.
00073 void GlfHeader::resetHeader()
00074 {
00075    myText.reset();
00076 }
00077 
00078 
00079 // Read the header from the specified file.  Assumes the file is in
00080 // the correct position for reading the header.
00081 bool GlfHeader::read(IFILE filePtr)
00082 {
00083     if((filePtr == NULL) || (filePtr->isOpen() == false))
00084     {
00085         // File is not open, return failure.
00086         std::string errorString = 
00087             "Failed to read the header since the file is not open.";
00088         throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
00089         return(false);
00090     }
00091 
00092     // Read the magic
00093     int numRead = 0;
00094     char magic[GLF_MAGIC_LEN];
00095     numRead = ifread(filePtr, &magic, GLF_MAGIC_LEN);
00096     if(numRead != GLF_MAGIC_LEN)
00097     {
00098         String errorMsg = "Failed to read the magic number (";
00099         errorMsg += GLF_MAGIC_LEN;
00100         errorMsg += " bytes).  Only read ";
00101         errorMsg += numRead;
00102         errorMsg += " bytes.";
00103         std::string errorString = errorMsg.c_str();
00104         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00105         return(false);
00106     }
00107     // Read the header length.
00108     int32_t headerLen = 0;
00109     int byteLen = sizeof(int32_t);
00110     numRead = ifread(filePtr, &headerLen, byteLen);
00111     if(numRead != byteLen)
00112     {
00113         String errorMsg = "Failed to read the length of the header text (";
00114         errorMsg += byteLen;
00115         errorMsg += " bytes).  Only read ";
00116         errorMsg += numRead;
00117         errorMsg += " bytes.";
00118         std::string errorString = errorMsg.c_str();
00119         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00120         return(false);
00121     }
00122        
00123     // Read the header from the file.
00124     numRead = myText.readFromFile(filePtr, headerLen);
00125     if(numRead != headerLen)
00126     {
00127         String errorMsg = "Failed to read the header text (";
00128         errorMsg += headerLen;
00129         errorMsg += " bytes).  Only read ";
00130         errorMsg += numRead;
00131         errorMsg += " bytes.";
00132         std::string errorString = errorMsg.c_str();
00133         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00134         return(false);
00135     }
00136     // Successfully read, return success.
00137     return(true);
00138 }
00139 
00140 
00141 // Write the header to the specified file.
00142 bool GlfHeader::write(IFILE filePtr) const
00143 {
00144     if((filePtr == NULL) || (filePtr->isOpen() == false))
00145     {
00146         // File is not open, return failure.
00147         std::string errorString = 
00148             "Failed to write the header since the file is not open.";
00149         throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
00150         return(false);
00151     }
00152 
00153     int numWrite = 0;
00154     // Write the magic
00155     numWrite = ifwrite(filePtr, GLF_MAGIC.c_str(), GLF_MAGIC_LEN);
00156     if(numWrite != GLF_MAGIC_LEN)
00157     {
00158         String errorMsg = "Failed to write the magic number (";
00159         errorMsg += GLF_MAGIC_LEN;
00160         errorMsg += " bytes).  Only wrote ";
00161         errorMsg += numWrite;
00162         errorMsg += " bytes.";
00163         std::string errorString = errorMsg.c_str();
00164         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00165         return(false);
00166     }
00167 
00168     // Write the header length.
00169     int32_t headerLen = myText.length();
00170     int byteLen = sizeof(int32_t);
00171     numWrite = ifwrite(filePtr, &headerLen, byteLen);
00172     if(numWrite != byteLen)
00173     {
00174         String errorMsg = "Failed to write the length of the header text (";
00175         errorMsg += byteLen;
00176         errorMsg += " bytes).  Only wrote ";
00177         errorMsg += numWrite;
00178         errorMsg += " bytes.";
00179         std::string errorString = errorMsg.c_str();
00180         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00181         return(false);
00182     }
00183        
00184     // Write the header to the file.
00185     numWrite = ifwrite(filePtr, myText.c_str(), headerLen);
00186     if(numWrite != headerLen)
00187     {
00188         String errorMsg = "Failed to write the header text (";
00189         errorMsg += headerLen;
00190         errorMsg += " bytes).  Only wrote ";
00191         errorMsg += numWrite;
00192         errorMsg += " bytes.";
00193         std::string errorString = errorMsg.c_str();
00194         throw(GlfException(GlfStatus::FAIL_IO, errorString));
00195         return(false);
00196     }
00197     // Successfully wrote, return success.
00198     return(true);
00199 }
00200 
00201 
00202 // Set the passed in string to the text string stored in this header.
00203 bool GlfHeader::getHeaderTextString(std::string& text)
00204 {
00205     text = myText.c_str();
00206     return(true);
00207 }
00208 
00209 
00210 // Set the header to the passed in string.
00211 bool GlfHeader::setHeaderTextString(const std::string& text)
00212 {
00213     myText = text;
00214     return(true);
00215 }
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3