00001 #pragma ident "$Id: Exception.cpp 3234 2012-07-31 12:15:03Z yanweignss $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00044 #include <sstream>
00045 #include "Exception.hpp"
00046
00047 using std::ostream;
00048 using std::ostringstream;
00049 using std::streambuf;
00050 using std::string;
00051 using std::endl;
00052
00053 namespace gpstk
00054 {
00055
00056 void ExceptionLocation::dump(ostream& s) const
00057 throw()
00058 {
00059 s << getFileName() << ":"
00060 #ifdef __FUNCTION__
00061 << getFunctionName() << ":"
00062 #endif
00063 << getLineNumber();
00064 }
00065
00066 Exception::Exception()
00067 throw()
00068 {
00069 }
00070
00071 Exception::Exception(const string& errorText,
00072 const unsigned long& errId,
00073 const Severity& sever)
00074 throw()
00075 {
00076 text.push_back(errorText);
00077 errorId = errId;
00078 severity = sever;
00079 }
00080
00081 Exception::Exception(const char* errorText,
00082 const unsigned long& errId,
00083 const Severity& sever)
00084 throw()
00085 {
00086 text.push_back(string(errorText));
00087 errorId = errId;
00088 severity = sever;
00089 }
00090
00091 Exception::Exception(const Exception& e)
00092 throw()
00093 : errorId(e.errorId),
00094 locations(e.locations),
00095 severity(e.severity),
00096 text(e.text),
00097 streamBuffer(e.streamBuffer)
00098 {}
00099
00100 Exception& Exception::operator=(const Exception& e)
00101 throw()
00102 {
00103 errorId = e.errorId;
00104 locations = e.locations;
00105 severity = e.severity;
00106 text = e.text;
00107
00108
00109 streamBuffer = e.streamBuffer;
00110
00111 return *this;
00112 }
00113
00114 Exception& Exception::addLocation(
00115 const ExceptionLocation& location)
00116 throw()
00117 {
00118 locations.push_back(location);
00119 return *this;
00120 }
00121
00122 const ExceptionLocation Exception::getLocation(
00123 const size_t& index) const
00124 throw()
00125 {
00126 if (index < 0 || index>=getLocationCount())
00127 {
00128 return ExceptionLocation();
00129 }
00130 else
00131 {
00132 return locations[index];
00133 }
00134 }
00135
00136 size_t Exception::getLocationCount() const
00137 throw()
00138 {
00139 return locations.size();
00140 }
00141
00142 Exception& Exception::addText(const string& errorText)
00143 throw()
00144 {
00145 text.push_back(errorText);
00146 return *this;
00147 }
00148
00149 string Exception::getText(const size_t& index) const
00150 throw()
00151 {
00152 if (index < 0 || index>=getTextCount())
00153 {
00154 string tmp;
00155 return tmp;
00156 }
00157 else
00158 {
00159 return text[index];
00160 }
00161 }
00162
00163 size_t Exception::getTextCount() const
00164 throw()
00165 {
00166 return text.size();
00167 }
00168
00169 void Exception::dump(ostream& s) const
00170 throw()
00171 {
00172 int i;
00173 for (i=0; i<getTextCount(); i++)
00174 {
00175 s << "text " << i << ":" << this->getText(i) << endl;
00176 }
00177 for (i=0; i<getLocationCount(); i++)
00178 {
00179 s << "location " << i << ":" << getLocation(i).what() << endl;
00180 }
00181 }
00182
00183 int Exception::overflow(int c)
00184 {
00185 if (c == '\n' || !c)
00186 {
00187 if (streamBuffer.length() == 0)
00188 {
00189 return c;
00190 }
00191 addText(streamBuffer);
00192 streamBuffer = "";
00193 return c;
00194 }
00195 streamBuffer.append(1, (char)c);
00196 return c;
00197 }
00198
00199 string ExceptionLocation::what() const
00200 throw()
00201 {
00202 ostringstream oss;
00203 this->dump(oss);
00204 return oss.str();
00205 }
00206
00207 string Exception::what() const
00208 throw()
00209 {
00210 ostringstream oss;
00211 this->dump(oss);
00212 return oss.str();
00213 }
00214
00215 ostream& operator<<( ostream& s,
00216 const Exception& e )
00217 throw()
00218 {
00219 e.dump(s);
00220 return s;
00221 }
00222
00223 ostream& operator<<( ostream& s,
00224 const ExceptionLocation& e )
00225 throw()
00226 {
00227 e.dump(s);
00228 return s;
00229 }
00230
00231 }
00232