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"
14 #include <simgear/misc/sg_path.hxx>
16 ////////////////////////////////////////////////////////////////////////
17 // Implementation of sg_location class.
18 ////////////////////////////////////////////////////////////////////////
20 sg_location::sg_location ()
28 sg_location::sg_location (const std::string& path, int line, int column)
33 setPath(path.c_str());
36 sg_location::sg_location (const char* path, int line, int column)
44 sg_location::~sg_location () throw ()
49 sg_location::getPath () const
55 sg_location::setPath (const char* path)
58 strncpy(_path, path, max_path);
59 _path[max_path -1] = '\0';
66 sg_location::getLine () const
72 sg_location::setLine (int line)
78 sg_location::getColumn () const
84 sg_location::setColumn (int column)
90 sg_location::getByte () const
96 sg_location::setByte (int byte)
102 sg_location::asString () const
104 std::ostringstream out;
107 if (_line != -1 || _column != -1)
111 out << "line " << _line;
116 out << "column " << _column;
123 ////////////////////////////////////////////////////////////////////////
124 // Implementation of sg_throwable class.
125 ////////////////////////////////////////////////////////////////////////
127 sg_throwable::sg_throwable ()
133 sg_throwable::sg_throwable (const char* message, const char* origin)
139 sg_throwable::~sg_throwable () throw ()
144 sg_throwable::getMessage () const
150 sg_throwable::getFormattedMessage () const
152 return std::string(getMessage());
156 sg_throwable::setMessage (const char* message)
158 strncpy(_message, message, MAX_TEXT_LEN);
159 _message[MAX_TEXT_LEN - 1] = '\0';
164 sg_throwable::getOrigin () const
170 sg_throwable::setOrigin (const char* origin)
173 strncpy(_origin, origin, MAX_TEXT_LEN);
174 _origin[MAX_TEXT_LEN - 1] = '\0';
180 const char* sg_throwable::what() const throw()
191 ////////////////////////////////////////////////////////////////////////
192 // Implementation of sg_error class.
193 ////////////////////////////////////////////////////////////////////////
195 sg_error::sg_error ()
200 sg_error::sg_error (const char* message, const char *origin)
201 : sg_throwable(message, origin)
205 sg_error::sg_error(const std::string& message, const std::string& origin)
206 : sg_throwable(message.c_str(), origin.c_str())
210 sg_error::~sg_error () throw ()
214 ////////////////////////////////////////////////////////////////////////
215 // Implementation of sg_exception class.
216 ////////////////////////////////////////////////////////////////////////
218 sg_exception::sg_exception ()
223 sg_exception::sg_exception (const char* message, const char* origin)
224 : sg_throwable(message, origin)
228 sg_exception::sg_exception( const std::string& message,
229 const std::string& origin )
230 : sg_throwable(message.c_str(), origin.c_str())
234 sg_exception::~sg_exception () throw ()
238 ////////////////////////////////////////////////////////////////////////
239 // Implementation of sg_io_exception.
240 ////////////////////////////////////////////////////////////////////////
242 sg_io_exception::sg_io_exception ()
247 sg_io_exception::sg_io_exception (const char* message, const char* origin)
248 : sg_exception(message, origin)
252 sg_io_exception::sg_io_exception (const char* message,
253 const sg_location &location,
255 : sg_exception(message, origin),
260 sg_io_exception::sg_io_exception( const std::string& message,
261 const std::string& origin )
262 : sg_exception(message, origin)
266 sg_io_exception::sg_io_exception( const std::string& message,
267 const sg_location &location,
268 const std::string& origin )
269 : sg_exception(message, origin),
274 sg_io_exception::sg_io_exception( const std::string &message,
275 const SGPath& origin )
276 : sg_exception(message, origin.str())
281 sg_io_exception::~sg_io_exception () throw ()
286 sg_io_exception::getFormattedMessage () const
288 std::string ret = getMessage();
289 std::string loc = getLocation().asString();
298 sg_io_exception::getLocation () const
304 sg_io_exception::setLocation (const sg_location &location)
306 _location = location;
311 ////////////////////////////////////////////////////////////////////////
312 // Implementation of sg_format_exception.
313 ////////////////////////////////////////////////////////////////////////
315 sg_format_exception::sg_format_exception ()
321 sg_format_exception::sg_format_exception (const char* message,
324 : sg_exception(message, origin)
329 sg_format_exception::sg_format_exception( const std::string& message,
330 const std::string& text,
331 const std::string& origin )
332 : sg_exception(message, origin)
334 setText(text.c_str());
337 sg_format_exception::~sg_format_exception () throw ()
342 sg_format_exception::getText () const
348 sg_format_exception::setText (const char* text)
351 strncpy(_text, text, MAX_TEXT_LEN);
352 _text[MAX_TEXT_LEN-1] = '\0';
360 ////////////////////////////////////////////////////////////////////////
361 // Implementation of sg_range_exception.
362 ////////////////////////////////////////////////////////////////////////
364 sg_range_exception::sg_range_exception ()
369 sg_range_exception::sg_range_exception (const char* message,
371 : sg_exception(message, origin)
375 sg_range_exception::sg_range_exception(const std::string& message,
376 const std::string& origin)
377 : sg_exception(message, origin)
381 sg_range_exception::~sg_range_exception () throw ()
384 // end of exception.cxx