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>
18 ////////////////////////////////////////////////////////////////////////
19 // Implementation of sg_location class.
20 ////////////////////////////////////////////////////////////////////////
22 sg_location::sg_location ()
30 sg_location::sg_location (const std::string& path, int line, int column)
35 setPath(path.c_str());
38 sg_location::sg_location (const char* path, int line, int column)
46 sg_location::~sg_location () throw ()
51 sg_location::getPath () const
57 sg_location::setPath (const char* path)
60 strncpy(_path, path, max_path);
61 _path[max_path -1] = '\0';
68 sg_location::getLine () const
74 sg_location::setLine (int line)
80 sg_location::getColumn () const
86 sg_location::setColumn (int column)
92 sg_location::getByte () const
98 sg_location::setByte (int byte)
104 sg_location::asString () const
106 std::ostringstream out;
109 if (_line != -1 || _column != -1)
113 out << "line " << _line;
118 out << "column " << _column;
125 ////////////////////////////////////////////////////////////////////////
126 // Implementation of sg_throwable class.
127 ////////////////////////////////////////////////////////////////////////
129 sg_throwable::sg_throwable ()
135 sg_throwable::sg_throwable (const char* message, const char* origin)
141 sg_throwable::~sg_throwable () throw ()
146 sg_throwable::getMessage () const
152 sg_throwable::getFormattedMessage () const
154 return string(getMessage());
158 sg_throwable::setMessage (const char* message)
160 strncpy(_message, message, MAX_TEXT_LEN);
161 _message[MAX_TEXT_LEN - 1] = '\0';
166 sg_throwable::getOrigin () const
172 sg_throwable::setOrigin (const char* origin)
175 strncpy(_origin, origin, MAX_TEXT_LEN);
176 _origin[MAX_TEXT_LEN - 1] = '\0';
182 const char* sg_throwable::what() const throw()
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of sg_error class.
195 ////////////////////////////////////////////////////////////////////////
197 sg_error::sg_error ()
202 sg_error::sg_error (const char* message, const char *origin)
203 : sg_throwable(message, origin)
207 sg_error::sg_error (const string& message, const string& origin)
208 : sg_throwable(message.c_str(), origin.c_str())
212 sg_error::~sg_error () throw ()
216 ////////////////////////////////////////////////////////////////////////
217 // Implementation of sg_exception class.
218 ////////////////////////////////////////////////////////////////////////
220 sg_exception::sg_exception ()
225 sg_exception::sg_exception (const char* message, const char* origin)
226 : sg_throwable(message, origin)
230 sg_exception::sg_exception (const string& message, const string& origin)
231 : sg_throwable(message.c_str(), origin.c_str())
235 sg_exception::~sg_exception () throw ()
239 ////////////////////////////////////////////////////////////////////////
240 // Implementation of sg_io_exception.
241 ////////////////////////////////////////////////////////////////////////
243 sg_io_exception::sg_io_exception ()
248 sg_io_exception::sg_io_exception (const char* message, const char* origin)
249 : sg_exception(message, origin)
253 sg_io_exception::sg_io_exception (const char* message,
254 const sg_location &location,
256 : sg_exception(message, origin),
261 sg_io_exception::sg_io_exception (const string& message, const string& origin)
262 : sg_exception(message, origin)
266 sg_io_exception::sg_io_exception (const string& message,
267 const sg_location &location,
268 const string& origin)
269 : sg_exception(message, origin),
274 sg_io_exception::sg_io_exception (const string &message, const SGPath& origin)
275 : sg_exception(message, origin.str())
280 sg_io_exception::~sg_io_exception () throw ()
285 sg_io_exception::getFormattedMessage () const
287 string ret = getMessage();
288 string loc = getLocation().asString();
297 sg_io_exception::getLocation () const
303 sg_io_exception::setLocation (const sg_location &location)
305 _location = location;
310 ////////////////////////////////////////////////////////////////////////
311 // Implementation of sg_format_exception.
312 ////////////////////////////////////////////////////////////////////////
314 sg_format_exception::sg_format_exception ()
320 sg_format_exception::sg_format_exception (const char* message,
323 : sg_exception(message, origin)
328 sg_format_exception::sg_format_exception (const string& message,
330 const string& origin)
331 : sg_exception(message, origin)
333 setText(text.c_str());
336 sg_format_exception::~sg_format_exception () throw ()
341 sg_format_exception::getText () const
347 sg_format_exception::setText (const char* text)
350 strncpy(_text, text, MAX_TEXT_LEN);
351 _text[MAX_TEXT_LEN-1] = '\0';
359 ////////////////////////////////////////////////////////////////////////
360 // Implementation of sg_range_exception.
361 ////////////////////////////////////////////////////////////////////////
363 sg_range_exception::sg_range_exception ()
368 sg_range_exception::sg_range_exception (const char* message,
370 : sg_exception(message, origin)
374 sg_range_exception::sg_range_exception(const string& message,
375 const string& origin)
376 : sg_exception(message, origin)
380 sg_range_exception::~sg_range_exception () throw ()
383 // end of exception.cxx