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