libStatGen Software  1
GlfHeader Class Reference

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

#include <GlfHeader.h>

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:

  • Variable length text string

Definition at line 29 of file GlfHeader.h.


Constructor & Destructor Documentation

GlfHeader::GlfHeader ( const GlfHeader header)

Copy Constructor.

Parameters:
headerglfheader to copy into this one.

Definition at line 39 of file GlfHeader.cpp.

References copy().

    : myText()
{
    copy(header);
}

Member Function Documentation

bool GlfHeader::copy ( const GlfHeader header)

Copy the passed in header into this header.

Parameters:
headerglfheader to copy into this one.

Definition at line 54 of file GlfHeader.cpp.

References resetHeader().

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

{
    // Check to see if the passed in value is the same as this.
    if(this == &header)
    {
        return(true);
    }

    resetHeader();

    // Copy the header.
    myText = header.myText;

    return(true);
}
bool GlfHeader::getHeaderTextString ( std::string &  text)

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

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

Definition at line 202 of file GlfHeader.cpp.

{
    text = myText.c_str();
    return(true);
}
GlfHeader & GlfHeader::operator= ( const GlfHeader header)

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

Parameters:
headerglfheader to copy into this one.

Definition at line 47 of file GlfHeader.cpp.

References copy().

{
    copy(header);
    return(*this);
}
bool GlfHeader::read ( IFILE  filePtr)

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

Parameters:
filePtrfile 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 80 of file GlfHeader.cpp.

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

Referenced by GlfFile::readHeader().

{
    if((filePtr == NULL) || (filePtr->isOpen() == false))
    {
        // File is not open, return failure.
        std::string errorString = 
            "Failed to read the header since the file is not open.";
        throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
        return(false);
    }

    // Read the magic
    int numRead = 0;
    char magic[GLF_MAGIC_LEN];
    numRead = ifread(filePtr, &magic, GLF_MAGIC_LEN);
    if(numRead != GLF_MAGIC_LEN)
    {
        String errorMsg = "Failed to read the magic number (";
        errorMsg += GLF_MAGIC_LEN;
        errorMsg += " bytes).  Only read ";
        errorMsg += numRead;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }
    // Read the header length.
    int32_t headerLen = 0;
    int byteLen = sizeof(int32_t);
    numRead = ifread(filePtr, &headerLen, byteLen);
    if(numRead != byteLen)
    {
        String errorMsg = "Failed to read the length of the header text (";
        errorMsg += byteLen;
        errorMsg += " bytes).  Only read ";
        errorMsg += numRead;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }
       
    // Read the header from the file.
    numRead = myText.readFromFile(filePtr, headerLen);
    if(numRead != headerLen)
    {
        String errorMsg = "Failed to read the header text (";
        errorMsg += headerLen;
        errorMsg += " bytes).  Only read ";
        errorMsg += numRead;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }
    // Successfully read, return success.
    return(true);
}
bool GlfHeader::setHeaderTextString ( const std::string &  text)

Set the header to the passed in string.

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

Definition at line 210 of file GlfHeader.cpp.

{
    myText = text;
    return(true);
}
bool GlfHeader::write ( IFILE  filePtr) const

Write the header to the specified file.

Parameters:
filePtrfile 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 141 of file GlfHeader.cpp.

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

Referenced by GlfFile::writeHeader().

{
    if((filePtr == NULL) || (filePtr->isOpen() == false))
    {
        // File is not open, return failure.
        std::string errorString = 
            "Failed to write the header since the file is not open.";
        throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
        return(false);
    }

    int numWrite = 0;
    // Write the magic
    numWrite = ifwrite(filePtr, GLF_MAGIC.c_str(), GLF_MAGIC_LEN);
    if(numWrite != GLF_MAGIC_LEN)
    {
        String errorMsg = "Failed to write the magic number (";
        errorMsg += GLF_MAGIC_LEN;
        errorMsg += " bytes).  Only wrote ";
        errorMsg += numWrite;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }

    // Write the header length.
    int32_t headerLen = myText.length();
    int byteLen = sizeof(int32_t);
    numWrite = ifwrite(filePtr, &headerLen, byteLen);
    if(numWrite != byteLen)
    {
        String errorMsg = "Failed to write the length of the header text (";
        errorMsg += byteLen;
        errorMsg += " bytes).  Only wrote ";
        errorMsg += numWrite;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }
       
    // Write the header to the file.
    numWrite = ifwrite(filePtr, myText.c_str(), headerLen);
    if(numWrite != headerLen)
    {
        String errorMsg = "Failed to write the header text (";
        errorMsg += headerLen;
        errorMsg += " bytes).  Only wrote ";
        errorMsg += numWrite;
        errorMsg += " bytes.";
        std::string errorString = errorMsg.c_str();
        throw(GlfException(GlfStatus::FAIL_IO, errorString));
        return(false);
    }
    // Successfully wrote, return success.
    return(true);
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends