libStatGen Software 1
Loading...
Searching...
No Matches
GlfHeader.cpp
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "GlfHeader.h"
19#include "GlfStatus.h"
20#include "GlfException.h"
21#include "StringBasics.h"
22
23const std::string GlfHeader::GLF_MAGIC = "GLF\3";
24
25GlfHeader::GlfHeader()
26 : myText()
27{
28 resetHeader();
29}
30
31
32GlfHeader::~GlfHeader()
33{
35}
36
37
38// Copy Constructor
39GlfHeader::GlfHeader(const GlfHeader& header)
40 : myText()
41{
42 copy(header);
43}
44
45
46// Overload operator = to copy the passed in header into this header.
48{
49 copy(header);
50 return(*this);
51}
52
53
54bool GlfHeader::copy(const GlfHeader& header)
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
63
64 // Copy the header.
65 myText = header.myText;
66
67 return(true);
68}
69
70
71// Reset the header for a new entry, clearing out previous values.
73{
74 myText.reset();
75}
76
77
78// Read the header from the specified file. Assumes the file is in
79// the correct position for reading the header.
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}
138
139
140// Write the header to the specified file.
141bool GlfHeader::write(IFILE filePtr) const
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}
199
200
201// Set the passed in string to the text string stored in this header.
202bool GlfHeader::getHeaderTextString(std::string& text)
203{
204 text = myText.c_str();
205 return(true);
206}
207
208
209// Set the header to the passed in string.
210bool GlfHeader::setHeaderTextString(const std::string& text)
211{
212 myText = text;
213 return(true);
214}
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
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
GlfException objects should be thrown by functions that operate on Glf files for exceptions.
This class allows a user to easily get/set the fields in a GLF header.
Definition GlfHeader.h:30
bool setHeaderTextString(const std::string &text)
Set the header to the passed in string.
void resetHeader()
Clear this header back to the default setting.
Definition GlfHeader.cpp:72
bool copy(const GlfHeader &header)
Copy the passed in header into this header.
Definition GlfHeader.cpp:54
GlfHeader & operator=(const GlfHeader &header)
Overload operator= to copy the passed in header into this header.
Definition GlfHeader.cpp:47
bool read(IFILE filePtr)
Read the header from the specified file (file MUST be in the correct position for reading the header)...
Definition GlfHeader.cpp:80
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.
@ 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
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition InputFile.h:37
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition InputFile.h:423