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