1 // exception.cxx - implementation of SimGear base exceptions.
2 // Started Summer 2001 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
8 #include "exception.hxx"
15 ////////////////////////////////////////////////////////////////////////
16 // Implementation of sg_location class.
17 ////////////////////////////////////////////////////////////////////////
19 sg_location::sg_location ()
27 sg_location::sg_location (const string& path, int line, int column)
32 setPath(path.c_str());
35 sg_location::sg_location (const char* path, int line, int column)
43 sg_location::~sg_location () throw ()
48 sg_location::getPath () const
54 sg_location::setPath (const char* path)
57 strncpy(_path, path, max_path);
58 _path[max_path -1] = '\0';
65 sg_location::getLine () const
71 sg_location::setLine (int line)
77 sg_location::getColumn () const
83 sg_location::setColumn (int column)
89 sg_location::getByte () const
95 sg_location::setByte (int byte)
101 sg_location::asString () const
103 std::ostringstream out;
106 if (_line != -1 || _column != -1)
110 out << "line " << _line;
115 out << "column " << _column;
122 ////////////////////////////////////////////////////////////////////////
123 // Implementation of sg_throwable class.
124 ////////////////////////////////////////////////////////////////////////
126 sg_throwable::sg_throwable ()
132 sg_throwable::sg_throwable (const char* message, const char* origin)
138 sg_throwable::~sg_throwable () throw ()
143 sg_throwable::getMessage () const
149 sg_throwable::getFormattedMessage () const
151 return string(getMessage());
155 sg_throwable::setMessage (const char* message)
157 strncpy(_message, message, MAX_TEXT_LEN);
158 _message[MAX_TEXT_LEN - 1] = '\0';
163 sg_throwable::getOrigin () const
169 sg_throwable::setOrigin (const char* origin)
172 strncpy(_origin, origin, MAX_TEXT_LEN);
173 _origin[MAX_TEXT_LEN - 1] = '\0';
179 const char* sg_throwable::what() const throw()
190 ////////////////////////////////////////////////////////////////////////
191 // Implementation of sg_error class.
192 ////////////////////////////////////////////////////////////////////////
194 sg_error::sg_error ()
199 sg_error::sg_error (const char* message, const char *origin)
200 : sg_throwable(message, origin)
204 sg_error::sg_error (const string& message, const string& origin)
205 : sg_throwable(message.c_str(), origin.c_str())
209 sg_error::~sg_error () throw ()
213 ////////////////////////////////////////////////////////////////////////
214 // Implementation of sg_exception class.
215 ////////////////////////////////////////////////////////////////////////
217 sg_exception::sg_exception ()
222 sg_exception::sg_exception (const char* message, const char* origin)
223 : sg_throwable(message, origin)
227 sg_exception::sg_exception (const string& message, const string& origin)
228 : sg_throwable(message.c_str(), origin.c_str())
232 sg_exception::~sg_exception () throw ()
236 ////////////////////////////////////////////////////////////////////////
237 // Implementation of sg_io_exception.
238 ////////////////////////////////////////////////////////////////////////
240 sg_io_exception::sg_io_exception ()
245 sg_io_exception::sg_io_exception (const char* message, const char* origin)
246 : sg_exception(message, origin)
250 sg_io_exception::sg_io_exception (const char* message,
251 const sg_location &location,
253 : sg_exception(message, origin),
258 sg_io_exception::sg_io_exception (const string& message, const string& origin)
259 : sg_exception(message, origin)
263 sg_io_exception::~sg_io_exception () throw ()
268 sg_io_exception::getFormattedMessage () const
270 string ret = getMessage();
271 string loc = getLocation().asString();
280 sg_io_exception::getLocation () const
286 sg_io_exception::setLocation (const sg_location &location)
288 _location = location;
293 ////////////////////////////////////////////////////////////////////////
294 // Implementation of sg_format_exception.
295 ////////////////////////////////////////////////////////////////////////
297 sg_format_exception::sg_format_exception ()
303 sg_format_exception::sg_format_exception (const char* message,
306 : sg_exception(message, origin)
311 sg_format_exception::sg_format_exception (const string& message,
313 const string& origin)
314 : sg_exception(message, origin)
316 setText(text.c_str());
319 sg_format_exception::~sg_format_exception () throw ()
324 sg_format_exception::getText () const
330 sg_format_exception::setText (const char* text)
333 strncpy(_text, text, MAX_TEXT_LEN);
334 _text[MAX_TEXT_LEN-1] = '\0';
342 ////////////////////////////////////////////////////////////////////////
343 // Implementation of sg_range_exception.
344 ////////////////////////////////////////////////////////////////////////
346 sg_range_exception::sg_range_exception ()
351 sg_range_exception::sg_range_exception (const char* message,
353 : sg_exception(message, origin)
357 sg_range_exception::sg_range_exception(const string& message,
358 const string& origin)
359 : sg_exception(message, origin)
363 sg_range_exception::~sg_range_exception () throw ()
366 // end of exception.cxx