00001 #pragma ident "$Id: Exception.cpp 2937 2011-10-23 19:23:33Z 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 Exception& e)
00082 throw()
00083 : errorId(e.errorId),
00084 locations(e.locations),
00085 severity(e.severity),
00086 text(e.text),
00087 streamBuffer(e.streamBuffer)
00088 {}
00089
00090 Exception& Exception::operator=(const Exception& e)
00091 throw()
00092 {
00093 errorId = e.errorId;
00094 locations = e.locations;
00095 severity = e.severity;
00096 text = e.text;
00097
00098
00099 streamBuffer = e.streamBuffer;
00100
00101 return *this;
00102 }
00103
00104 Exception& Exception::addLocation(
00105 const ExceptionLocation& location)
00106 throw()
00107 {
00108 locations.push_back(location);
00109 return *this;
00110 }
00111
00112 const ExceptionLocation Exception::getLocation(
00113 const size_t& index) const
00114 throw()
00115 {
00116 if (index < 0 || index>=getLocationCount())
00117 {
00118 return ExceptionLocation();
00119 }
00120 else
00121 {
00122 return locations[index];
00123 }
00124 }
00125
00126 size_t Exception::getLocationCount() const
00127 throw()
00128 {
00129 return locations.size();
00130 }
00131
00132 Exception& Exception::addText(const string& errorText)
00133 throw()
00134 {
00135 text.push_back(errorText);
00136 return *this;
00137 }
00138
00139 string Exception::getText(const size_t& index) const
00140 throw()
00141 {
00142 if (index < 0 || index>=getTextCount())
00143 {
00144 string tmp;
00145 return tmp;
00146 }
00147 else
00148 {
00149 return text[index];
00150 }
00151 }
00152
00153 size_t Exception::getTextCount() const
00154 throw()
00155 {
00156 return text.size();
00157 }
00158
00159 void Exception::dump(ostream& s) const
00160 throw()
00161 {
00162 int i;
00163 for (i=0; i<getTextCount(); i++)
00164 {
00165 s << "text " << i << ":" << this->getText(i) << endl;
00166 }
00167 for (i=0; i<getLocationCount(); i++)
00168 {
00169 s << "location " << i << ":" << getLocation(i).what() << endl;
00170 }
00171 }
00172
00173 int Exception::overflow(int c)
00174 {
00175 if (c == '\n' || !c)
00176 {
00177 if (streamBuffer.length() == 0)
00178 {
00179 return c;
00180 }
00181 addText(streamBuffer);
00182 streamBuffer = "";
00183 return c;
00184 }
00185 streamBuffer.append(1, (char)c);
00186 return c;
00187 }
00188
00189 string ExceptionLocation::what() const
00190 throw()
00191 {
00192 ostringstream oss;
00193 this->dump(oss);
00194 return oss.str();
00195 }
00196
00197 const char* Exception::what() const
00198 throw()
00199 {
00200 ostringstream oss;
00201 this->dump(oss);
00202 whatBuffer = oss.str();
00203
00204 return whatBuffer.c_str();
00205 }
00206
00207 ostream& operator<<( ostream& s,
00208 const Exception& e )
00209 throw()
00210 {
00211 e.dump(s);
00212 return s;
00213 }
00214
00215 ostream& operator<<( ostream& s,
00216 const ExceptionLocation& e )
00217 throw()
00218 {
00219 e.dump(s);
00220 return s;
00221 }
00222
00223 }
00224