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>

Public Member Functions

 GlfHeader (const GlfHeader &header)
 Copy Constructor
More...
 
GlfHeaderoperator= (const GlfHeader &header)
 Overload operator= to copy the passed in header into this header. More...
 
bool copy (const GlfHeader &header)
 Copy the passed in header into this header. More...
 
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). More...
 
bool write (IFILE filePtr) const
 Write the header to the specified file. More...
 
bool getHeaderTextString (std::string &text)
 Set the passed in string to the text string stored in this header. More...
 
bool setHeaderTextString (const std::string &text)
 Set the header to the passed in string. More...
 

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::GlfHeader ( const GlfHeader header)

Copy Constructor

Parameters
headerglfheader to copy into this one.

Definition at line 39 of file GlfHeader.cpp.

40  : myText()
41 {
42  copy(header);
43 }
bool copy(const GlfHeader &header)
Copy the passed in header into this header.
Definition: GlfHeader.cpp:54

References copy().

Member Function Documentation

◆ copy()

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.

55 {
56  // Check to see if the passed in value is the same as this.
57  if(this == &header)
58  {
59  return(true);
60  }
61 
62  resetHeader();
63 
64  // Copy the header.
65  myText = header.myText;
66 
67  return(true);
68 }
void resetHeader()
Clear this header back to the default setting.
Definition: GlfHeader.cpp:72

References resetHeader().

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

◆ getHeaderTextString()

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.

203 {
204  text = myText.c_str();
205  return(true);
206 }

◆ operator=()

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.

48 {
49  copy(header);
50  return(*this);
51 }

References copy().

◆ read()

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.

81 {
82  if((filePtr == NULL) || (filePtr->isOpen() == false))
83  {
84  // File is not open, return failure.
85  std::string errorString =
86  "Failed to read the header since the file is not open.";
87  throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
88  return(false);
89  }
90 
91  // Read the magic
92  int numRead = 0;
93  char magic[GLF_MAGIC_LEN];
94  numRead = ifread(filePtr, &magic, GLF_MAGIC_LEN);
95  if(numRead != GLF_MAGIC_LEN)
96  {
97  String errorMsg = "Failed to read the magic number (";
98  errorMsg += GLF_MAGIC_LEN;
99  errorMsg += " bytes). Only read ";
100  errorMsg += numRead;
101  errorMsg += " bytes.";
102  std::string errorString = errorMsg.c_str();
103  throw(GlfException(GlfStatus::FAIL_IO, errorString));
104  return(false);
105  }
106  // Read the header length.
107  int32_t headerLen = 0;
108  int byteLen = sizeof(int32_t);
109  numRead = ifread(filePtr, &headerLen, byteLen);
110  if(numRead != byteLen)
111  {
112  String errorMsg = "Failed to read the length of the header text (";
113  errorMsg += byteLen;
114  errorMsg += " bytes). Only read ";
115  errorMsg += numRead;
116  errorMsg += " bytes.";
117  std::string errorString = errorMsg.c_str();
118  throw(GlfException(GlfStatus::FAIL_IO, errorString));
119  return(false);
120  }
121 
122  // Read the header from the file.
123  numRead = myText.readFromFile(filePtr, headerLen);
124  if(numRead != headerLen)
125  {
126  String errorMsg = "Failed to read the header text (";
127  errorMsg += headerLen;
128  errorMsg += " bytes). Only read ";
129  errorMsg += numRead;
130  errorMsg += " bytes.";
131  std::string errorString = errorMsg.c_str();
132  throw(GlfException(GlfStatus::FAIL_IO, errorString));
133  return(false);
134  }
135  // Successfully read, return success.
136  return(true);
137 }
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition: InputFile.h:600
GlfException objects should be thrown by functions that operate on Glf files for exceptions.
Definition: GlfException.h:28
@ FAIL_ORDER
method failed because it was called out of order, like trying to read a file without opening it for r...
Definition: GlfStatus.h:35
@ FAIL_IO
method failed due to an I/O issue.
Definition: GlfStatus.h:34
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition: InputFile.h:423

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

Referenced by GlfFile::readHeader().

◆ setHeaderTextString()

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.

211 {
212  myText = text;
213  return(true);
214 }

◆ write()

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.

142 {
143  if((filePtr == NULL) || (filePtr->isOpen() == false))
144  {
145  // File is not open, return failure.
146  std::string errorString =
147  "Failed to write the header since the file is not open.";
148  throw(GlfException(GlfStatus::FAIL_ORDER, errorString));
149  return(false);
150  }
151 
152  int numWrite = 0;
153  // Write the magic
154  numWrite = ifwrite(filePtr, GLF_MAGIC.c_str(), GLF_MAGIC_LEN);
155  if(numWrite != GLF_MAGIC_LEN)
156  {
157  String errorMsg = "Failed to write the magic number (";
158  errorMsg += GLF_MAGIC_LEN;
159  errorMsg += " bytes). Only wrote ";
160  errorMsg += numWrite;
161  errorMsg += " bytes.";
162  std::string errorString = errorMsg.c_str();
163  throw(GlfException(GlfStatus::FAIL_IO, errorString));
164  return(false);
165  }
166 
167  // Write the header length.
168  int32_t headerLen = myText.length();
169  int byteLen = sizeof(int32_t);
170  numWrite = ifwrite(filePtr, &headerLen, byteLen);
171  if(numWrite != byteLen)
172  {
173  String errorMsg = "Failed to write the length of the header text (";
174  errorMsg += byteLen;
175  errorMsg += " bytes). Only wrote ";
176  errorMsg += numWrite;
177  errorMsg += " bytes.";
178  std::string errorString = errorMsg.c_str();
179  throw(GlfException(GlfStatus::FAIL_IO, errorString));
180  return(false);
181  }
182 
183  // Write the header to the file.
184  numWrite = ifwrite(filePtr, myText.c_str(), headerLen);
185  if(numWrite != headerLen)
186  {
187  String errorMsg = "Failed to write the header text (";
188  errorMsg += headerLen;
189  errorMsg += " bytes). Only wrote ";
190  errorMsg += numWrite;
191  errorMsg += " bytes.";
192  std::string errorString = errorMsg.c_str();
193  throw(GlfException(GlfStatus::FAIL_IO, errorString));
194  return(false);
195  }
196  // Successfully wrote, return success.
197  return(true);
198 }
unsigned int ifwrite(IFILE file, const void *buffer, unsigned int size)
Write the specified number of bytes from the specified buffer into the file.
Definition: InputFile.h:669

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

Referenced by GlfFile::writeHeader().


The documentation for this class was generated from the following files: