GlfHeader Class Reference

This class allows a user to easily get/set the fields in a GLF header. More...

#include <GlfHeader.h>

Collaboration diagram for GlfHeader:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 GlfHeader (const GlfHeader &header)
 Copy Constructor.
GlfHeaderoperator= (const GlfHeader &header)
 Overload operator= to copy the passed in header into this header.
bool copy (const GlfHeader &header)
 Copy the passed in header into this header.
void resetHeader ()
 Clear this header back to the default setting.
bool read (IFILE filePtr)
 Read the header from the specified file (file MUST be in the correct position for reading the header).
bool write (IFILE filePtr) const
 Write the header to the specified file.
bool getHeaderTextString (std::string &text)
 Set the passed in string to the text string stored in this header.
bool setHeaderTextString (const std::string &text)
 Set the header to the passed in string.

Detailed Description

This class allows a user to easily get/set the fields in a GLF header.

The GlfHeader contains:

Definition at line 29 of file GlfHeader.h.


Constructor & Destructor Documentation

GlfHeader::GlfHeader ( const GlfHeader header  ) 

Copy Constructor.

Parameters:
header glfheader to copy into this one.

Definition at line 40 of file GlfHeader.cpp.

References copy().

00041     : myText()
00042 {
00043     copy(header);
00044 }


Member Function Documentation

bool GlfHeader::copy ( const GlfHeader header  ) 

Copy the passed in header into this header.

Parameters:
header glfheader to copy into this one.

Definition at line 55 of file GlfHeader.cpp.

References resetHeader().

Referenced by GlfHeader(), and operator=().

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 }

bool GlfHeader::getHeaderTextString ( std::string &  text  ) 

Set the passed in string to the text string stored in this header.

Parameters:
text string to populate with the header text string.
Returns:
true if text was successfully returned, false if not.

Definition at line 203 of file GlfHeader.cpp.

00204 {
00205     text = myText.c_str();
00206     return(true);
00207 }

GlfHeader & GlfHeader::operator= ( const GlfHeader header  ) 

Overload operator= to copy the passed in header into this header.

Parameters:
header glfheader to copy into this one.

Definition at line 48 of file GlfHeader.cpp.

References copy().

00049 {
00050     copy(header);
00051     return(*this);
00052 }

bool GlfHeader::read ( IFILE  filePtr  ) 

Read the header from the specified file (file MUST be in the correct position for reading the header).

Parameters:
filePtr file to read from that is in the correct position.
Returns:
true if the header was successfully read from the file, false if not.

Definition at line 81 of file GlfHeader.cpp.

References GlfStatus::FAIL_IO, GlfStatus::FAIL_ORDER, ifread(), and InputFile::isOpen().

Referenced by GlfFile::readHeader().

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 }

bool GlfHeader::setHeaderTextString ( const std::string &  text  ) 

Set the header to the passed in string.

Parameters:
text header text to assign to this header.
Returns:
true if the text was successfully set, false if not.

Definition at line 211 of file GlfHeader.cpp.

00212 {
00213     myText = text;
00214     return(true);
00215 }

bool GlfHeader::write ( IFILE  filePtr  )  const

Write the header to the specified file.

Parameters:
filePtr file to write to that is in the correct position.
Returns:
true if the header was successfully written to the file, false if not.

Definition at line 142 of file GlfHeader.cpp.

References GlfStatus::FAIL_IO, GlfStatus::FAIL_ORDER, ifwrite(), and InputFile::isOpen().

Referenced by GlfFile::writeHeader().

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 }


The documentation for this class was generated from the following files:
Generated on Tue Sep 6 17:52:02 2011 for libStatGen Software by  doxygen 1.6.3