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 "GlfRefSection.h" 00019 #include "GlfException.h" 00020 #include "StringBasics.h" 00021 00022 GlfRefSection::GlfRefSection() 00023 : myRefName() 00024 { 00025 resetRefSection(); 00026 } 00027 00028 00029 GlfRefSection::~GlfRefSection() 00030 { 00031 resetRefSection(); 00032 } 00033 00034 00035 // Copy Constructor 00036 GlfRefSection::GlfRefSection(const GlfRefSection& refSection) 00037 : myRefName() 00038 { 00039 copy(refSection); 00040 } 00041 00042 00043 // Overload operator = to copy the passed in refSection into this refSection. 00044 GlfRefSection & GlfRefSection::operator = (const GlfRefSection& refSection) 00045 { 00046 copy(refSection); 00047 return(*this); 00048 } 00049 00050 00051 bool GlfRefSection::copy(const GlfRefSection& refSection) 00052 { 00053 // Check to see if the passed in value is the same as this. 00054 if(this == &refSection) 00055 { 00056 return(true); 00057 } 00058 00059 resetRefSection(); 00060 00061 // Copy the refSection. 00062 myRefName = refSection.myRefName; 00063 myRefLen = refSection.myRefLen; 00064 00065 return(true); 00066 } 00067 00068 00069 // Reset the refSection for a new entry, clearing out previous values. 00070 void GlfRefSection::resetRefSection() 00071 { 00072 myRefName.reset(); 00073 myRefLen = 0; 00074 } 00075 00076 00077 // Read the refSection from the specified file. Assumes the file is in 00078 // the correct position for reading the refSection. 00079 bool GlfRefSection::read(IFILE filePtr) 00080 { 00081 // Read the reference sequence name length 00082 int numRead = 0; 00083 int32_t refNameLen = 0; 00084 int byteLen = sizeof(int32_t); 00085 numRead = ifread(filePtr, &refNameLen, byteLen); 00086 if(numRead != byteLen) 00087 { 00088 // If no bytes were read and it is the end of the file, then return 00089 // false, but do not throw an exception. This is not an error, just 00090 // the end of the file. 00091 if((numRead == 0) && ifeof(filePtr)) 00092 { 00093 return(false); 00094 } 00095 00096 String errorMsg = 00097 "Failed to read the length of the reference sequence name ("; 00098 errorMsg += byteLen; 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 00107 // Read the refSection from the file. 00108 numRead = myRefName.readFromFile(filePtr, refNameLen); 00109 if(numRead != refNameLen) 00110 { 00111 String errorMsg = "Failed to read the reference sequence name ("; 00112 errorMsg += refNameLen; 00113 errorMsg += " bytes). Only read "; 00114 errorMsg += numRead; 00115 errorMsg += " bytes."; 00116 std::string errorString = errorMsg.c_str(); 00117 throw(GlfException(GlfStatus::FAIL_IO, errorString)); 00118 return(false); 00119 } 00120 00121 // Read the ref length. 00122 byteLen = sizeof(uint32_t); 00123 numRead = ifread(filePtr, &myRefLen, byteLen); 00124 if(numRead != byteLen) 00125 { 00126 String errorMsg = "Failed to read the reference sequence length ("; 00127 errorMsg += byteLen; 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 00136 // Successfully read, return success. 00137 return(true); 00138 } 00139 00140 00141 // Write the refSection to the specified file. 00142 bool GlfRefSection::write(IFILE filePtr) const 00143 { 00144 int refNameLen = myRefName.length(); 00145 int byteLen = sizeof(int32_t); 00146 int numWrite = ifwrite(filePtr, &refNameLen, byteLen); 00147 if(numWrite != byteLen) 00148 { 00149 String errorMsg = 00150 "Failed to write the length of the reference sequence name ("; 00151 errorMsg += byteLen; 00152 errorMsg += " bytes). Only wrote "; 00153 errorMsg += numWrite; 00154 errorMsg += " bytes."; 00155 std::string errorString = errorMsg.c_str(); 00156 throw(GlfException(GlfStatus::FAIL_IO, errorString)); 00157 return(false); 00158 } 00159 00160 numWrite = ifwrite(filePtr, myRefName.c_str(), refNameLen); 00161 if(numWrite != refNameLen) 00162 { 00163 String errorMsg = "Failed to write the reference sequence name ("; 00164 errorMsg += refNameLen; 00165 errorMsg += " bytes). Only wrote "; 00166 errorMsg += numWrite; 00167 errorMsg += " bytes."; 00168 std::string errorString = errorMsg.c_str(); 00169 throw(GlfException(GlfStatus::FAIL_IO, errorString)); 00170 return(false); 00171 } 00172 00173 // Write the length of the reference sequence 00174 byteLen = sizeof(uint32_t); 00175 numWrite = ifwrite(filePtr, &myRefLen, byteLen); 00176 if(numWrite != byteLen) 00177 { 00178 String errorMsg = "Failed to write the reference sequence length ("; 00179 errorMsg += byteLen; 00180 errorMsg += " bytes). Only wrote "; 00181 errorMsg += numWrite; 00182 errorMsg += " bytes."; 00183 std::string errorString = errorMsg.c_str(); 00184 throw(GlfException(GlfStatus::FAIL_IO, errorString)); 00185 return(false); 00186 } 00187 00188 // Successfully wrote, return success. 00189 return(true); 00190 } 00191 00192 00193 bool GlfRefSection::getName(std::string& name) const 00194 { 00195 name = myRefName.c_str(); 00196 return(true); 00197 } 00198 00199 00200 uint32_t GlfRefSection::getRefLen() const 00201 { 00202 return(myRefLen); 00203 } 00204 00205 00206 bool GlfRefSection::setName(const std::string& name) 00207 { 00208 myRefName = name; 00209 return(true); 00210 } 00211 00212 00213 bool GlfRefSection::setRefLen(uint32_t refLen) 00214 { 00215 myRefLen = refLen; 00216 return(true); 00217 } 00218 00219 00220 void GlfRefSection::print() const 00221 { 00222 std::cout << "l_name: " << myRefName.length() 00223 << "; name: " << myRefName.c_str() 00224 << "; ref_len: " << myRefLen 00225 << "\n"; 00226 } 00227