00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "exception.hxx"
00009 #include <stdio.h>
00010 #include <cstring>
00011 #include <sstream>
00012 
00013 using std::string;
00014 
00016 
00018 
00019 sg_location::sg_location ()
00020   : _line(-1),
00021     _column(-1),
00022     _byte(-1)
00023 {
00024     _path[0] = '\0';
00025 }
00026 
00027 sg_location::sg_location (const string& path, int line, int column)
00028   : _line(line),
00029     _column(column),
00030     _byte(-1)
00031 {
00032   setPath(path.c_str());
00033 }
00034 
00035 sg_location::sg_location (const char* path, int line, int column)
00036   : _line(line),
00037     _column(column),
00038     _byte(-1)
00039 {
00040   setPath(path);
00041 }
00042 
00043 sg_location::~sg_location () throw ()
00044 {
00045 }
00046 
00047 const char*
00048 sg_location::getPath () const
00049 {
00050   return _path;
00051 }
00052 
00053 void
00054 sg_location::setPath (const char* path)
00055 {
00056   if (path) {
00057     strncpy(_path, path, max_path);
00058     _path[max_path -1] = '\0';
00059   } else {
00060     _path[0] = '\0';
00061   }
00062 }
00063 
00064 int
00065 sg_location::getLine () const
00066 {
00067   return _line;
00068 }
00069 
00070 void
00071 sg_location::setLine (int line)
00072 {
00073   _line = line;
00074 }
00075 
00076 int
00077 sg_location::getColumn () const
00078 {
00079   return _column;
00080 }
00081 
00082 void
00083 sg_location::setColumn (int column)
00084 {
00085   _column = column;
00086 }
00087 
00088 int
00089 sg_location::getByte () const
00090 {
00091   return _byte;
00092 }
00093 
00094 void
00095 sg_location::setByte (int byte)
00096 {
00097   _byte = byte;
00098 }
00099 
00100 string
00101 sg_location::asString () const
00102 {
00103   std::ostringstream out;
00104   if (_path[0]) {
00105     out << _path;
00106     if (_line != -1 || _column != -1)
00107       out << ",\n";
00108   }
00109   if (_line != -1) {
00110     out << "line " << _line;
00111     if (_column != -1)
00112       out << ", ";
00113   }
00114   if (_column != -1) {
00115     out << "column " << _column;
00116   }
00117   return out.str();
00118 }
00119 
00120 
00121 
00123 
00125 
00126 sg_throwable::sg_throwable ()
00127 {
00128   _message[0] = '\0';
00129   _origin[0] = '\0';
00130 }
00131 
00132 sg_throwable::sg_throwable (const char* message, const char* origin)
00133 {
00134   setMessage(message);
00135   setOrigin(origin);
00136 }
00137 
00138 sg_throwable::~sg_throwable () throw ()
00139 {
00140 }
00141 
00142 const char*
00143 sg_throwable::getMessage () const
00144 {
00145   return _message;
00146 }
00147 
00148 const string
00149 sg_throwable::getFormattedMessage () const
00150 {
00151   return string(getMessage());
00152 }
00153 
00154 void
00155 sg_throwable::setMessage (const char* message)
00156 {
00157   strncpy(_message, message, MAX_TEXT_LEN);
00158   _message[MAX_TEXT_LEN - 1] = '\0';
00159 
00160 }
00161 
00162 const char*
00163 sg_throwable::getOrigin () const
00164 {
00165   return _origin;
00166 }
00167 
00168 void
00169 sg_throwable::setOrigin (const char* origin)
00170 {
00171   if (origin) {
00172     strncpy(_origin, origin, MAX_TEXT_LEN);
00173     _origin[MAX_TEXT_LEN - 1] = '\0';
00174   } else {
00175     _origin[0] = '\0';
00176   }
00177 }
00178 
00179 const char* sg_throwable::what() const throw()
00180 {
00181   try {
00182     return getMessage();
00183   }
00184   catch (...) {
00185     return "";
00186   }
00187 }
00188 
00189 
00191 
00193 
00194 sg_error::sg_error ()
00195   : sg_throwable ()
00196 {
00197 }
00198 
00199 sg_error::sg_error (const char* message, const char *origin)
00200   : sg_throwable(message, origin)
00201 {
00202 }
00203 
00204 sg_error::sg_error (const string& message, const string& origin)
00205   : sg_throwable(message.c_str(), origin.c_str())
00206 {
00207 }
00208 
00209 sg_error::~sg_error () throw ()
00210 {
00211 }
00212 
00214 
00216 
00217 sg_exception::sg_exception ()
00218   : sg_throwable ()
00219 {
00220 }
00221 
00222 sg_exception::sg_exception (const char* message, const char* origin)
00223   : sg_throwable(message, origin)
00224 {
00225 }
00226 
00227 sg_exception::sg_exception (const string& message, const string& origin)
00228   : sg_throwable(message.c_str(), origin.c_str())
00229 {
00230 }
00231 
00232 sg_exception::~sg_exception () throw ()
00233 {
00234 }
00235 
00237 
00239 
00240 sg_io_exception::sg_io_exception ()
00241   : sg_exception()
00242 {
00243 }
00244 
00245 sg_io_exception::sg_io_exception (const char* message, const char* origin)
00246   : sg_exception(message, origin)
00247 {
00248 }
00249 
00250 sg_io_exception::sg_io_exception (const char* message,
00251                                   const sg_location &location,
00252                                   const char* origin)
00253   : sg_exception(message, origin),
00254     _location(location)
00255 {
00256 }
00257 
00258 sg_io_exception::sg_io_exception (const string& message, const string& origin)
00259   : sg_exception(message, origin)
00260 {
00261 }
00262 
00263 sg_io_exception::~sg_io_exception () throw ()
00264 {
00265 }
00266 
00267 const string
00268 sg_io_exception::getFormattedMessage () const
00269 {
00270   string ret = getMessage();
00271   string loc = getLocation().asString();
00272   if (loc.length()) {
00273     ret += "\n at ";
00274     ret += loc;
00275   }
00276   return ret;
00277 }
00278 
00279 const sg_location &
00280 sg_io_exception::getLocation () const
00281 {
00282   return _location;
00283 }
00284 
00285 void
00286 sg_io_exception::setLocation (const sg_location &location)
00287 {
00288   _location = location;
00289 }
00290 
00291 
00292 
00294 
00296 
00297 sg_format_exception::sg_format_exception ()
00298   : sg_exception()
00299 {
00300   _text[0] = '\0';
00301 }
00302 
00303 sg_format_exception::sg_format_exception (const char* message,
00304                                           const char* text,
00305                                           const char* origin)
00306   : sg_exception(message, origin)
00307 {
00308   setText(text);
00309 }
00310 
00311 sg_format_exception::sg_format_exception (const string& message,
00312                                           const string& text,
00313                                           const string& origin)
00314   : sg_exception(message, origin)
00315 {
00316   setText(text.c_str());
00317 }
00318 
00319 sg_format_exception::~sg_format_exception () throw ()
00320 {
00321 }
00322 
00323 const char*
00324 sg_format_exception::getText () const
00325 {
00326   return _text;
00327 }
00328 
00329 void
00330 sg_format_exception::setText (const char* text)
00331 {
00332   if (text) {
00333     strncpy(_text, text, MAX_TEXT_LEN);
00334     _text[MAX_TEXT_LEN-1] = '\0';
00335   } else {
00336     _text[0] = '\0';
00337   }
00338 }
00339 
00340 
00341 
00343 
00345 
00346 sg_range_exception::sg_range_exception ()
00347   : sg_exception()
00348 {
00349 }
00350 
00351 sg_range_exception::sg_range_exception (const char* message,
00352                                         const char* origin)
00353   : sg_exception(message, origin)
00354 {
00355 }
00356 
00357 sg_range_exception::sg_range_exception(const string& message,
00358                                        const string& origin)
00359   : sg_exception(message, origin)
00360 {
00361 }
00362 
00363 sg_range_exception::~sg_range_exception () throw ()
00364 {
00365 }
00366