]> git.mxchange.org Git - simgear.git/blob - simgear/structure/exception.cxx
overhaul sg_throwable to behave like a proper exception
[simgear.git] / simgear / structure / exception.cxx
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.
4 //
5 // $Id$
6
7
8 #include "exception.hxx"
9 #include <stdio.h>
10 #include <cstring>
11 #include <sstream>
12
13 using std::string;
14 \f
15 ////////////////////////////////////////////////////////////////////////
16 // Implementation of sg_location class.
17 ////////////////////////////////////////////////////////////////////////
18
19 sg_location::sg_location ()
20   : _line(-1),
21     _column(-1),
22     _byte(-1)
23 {
24     _path[0] = '\0';
25 }
26
27 sg_location::sg_location (const string& path, int line, int column)
28   : _line(line),
29     _column(column),
30     _byte(-1)
31 {
32   setPath(path.c_str());
33 }
34
35 sg_location::sg_location (const char* path, int line, int column)
36   : _line(line),
37     _column(column),
38     _byte(-1)
39 {
40   setPath(path);
41 }
42
43 sg_location::~sg_location () throw ()
44 {
45 }
46
47 const char*
48 sg_location::getPath () const
49 {
50   return _path;
51 }
52
53 void
54 sg_location::setPath (const char* path)
55 {
56   if (path) {
57     strncpy(_path, path, MAX_PATH);
58     _path[MAX_PATH -1] = '\0';
59   } else {
60     _path[0] = '\0';
61   }
62 }
63
64 int
65 sg_location::getLine () const
66 {
67   return _line;
68 }
69
70 void
71 sg_location::setLine (int line)
72 {
73   _line = line;
74 }
75
76 int
77 sg_location::getColumn () const
78 {
79   return _column;
80 }
81
82 void
83 sg_location::setColumn (int column)
84 {
85   _column = column;
86 }
87
88 int
89 sg_location::getByte () const
90 {
91   return _byte;
92 }
93
94 void
95 sg_location::setByte (int byte)
96 {
97   _byte = byte;
98 }
99
100 string
101 sg_location::asString () const
102 {
103   std::ostringstream out;
104   if (_path[0]) {
105     out << _path;
106     if (_line != -1 || _column != -1)
107       out << ",\n";
108   }
109   if (_line != -1) {
110     out << "line " << _line;
111     if (_column != -1)
112       out << ", ";
113   }
114   if (_column != -1) {
115     out << "column " << _column;
116   }
117   return out.str();
118 }
119
120
121 \f
122 ////////////////////////////////////////////////////////////////////////
123 // Implementation of sg_throwable class.
124 ////////////////////////////////////////////////////////////////////////
125
126 sg_throwable::sg_throwable ()
127 {
128   _message[0] = '\0';
129   _origin[0] = '\0';
130 }
131
132 sg_throwable::sg_throwable (const char* message, const char* origin)
133 {
134   setMessage(message);
135   setOrigin(origin);
136 }
137
138 sg_throwable::~sg_throwable () throw ()
139 {
140 }
141
142 const char*
143 sg_throwable::getMessage () const
144 {
145   return _message;
146 }
147
148 const string
149 sg_throwable::getFormattedMessage () const
150 {
151   return string(getMessage());
152 }
153
154 void
155 sg_throwable::setMessage (const char* message)
156 {
157   strncpy(_message, message, MAX_TEXT_LEN);
158   _message[MAX_TEXT_LEN - 1] = '\0';
159
160 }
161
162 const char*
163 sg_throwable::getOrigin () const
164 {
165   return _origin;
166 }
167
168 void
169 sg_throwable::setOrigin (const char* origin)
170 {
171   if (origin) {
172     strncpy(_origin, origin, MAX_TEXT_LEN);
173     _origin[MAX_TEXT_LEN - 1] = '\0';
174   } else {
175     _origin[0] = '\0';
176   }
177 }
178
179 const char* sg_throwable::what() const throw()
180 {
181   try {
182     return getMessage();
183   }
184   catch (...) {
185     return "";
186   }
187 }
188
189 \f
190 ////////////////////////////////////////////////////////////////////////
191 // Implementation of sg_error class.
192 ////////////////////////////////////////////////////////////////////////
193
194 sg_error::sg_error ()
195   : sg_throwable ()
196 {
197 }
198
199 sg_error::sg_error (const char* message, const char *origin)
200   : sg_throwable(message, origin)
201 {
202 }
203
204 sg_error::sg_error (const string& message, const string& origin)
205   : sg_throwable(message.c_str(), origin.c_str())
206 {
207 }
208
209 sg_error::~sg_error () throw ()
210 {
211 }
212 \f
213 ////////////////////////////////////////////////////////////////////////
214 // Implementation of sg_exception class.
215 ////////////////////////////////////////////////////////////////////////
216
217 sg_exception::sg_exception ()
218   : sg_throwable ()
219 {
220 }
221
222 sg_exception::sg_exception (const char* message, const char* origin)
223   : sg_throwable(message, origin)
224 {
225 }
226
227 sg_exception::sg_exception (const string& message, const string& origin)
228   : sg_throwable(message.c_str(), origin.c_str())
229 {
230 }
231
232 sg_exception::~sg_exception () throw ()
233 {
234 }
235 \f
236 ////////////////////////////////////////////////////////////////////////
237 // Implementation of sg_io_exception.
238 ////////////////////////////////////////////////////////////////////////
239
240 sg_io_exception::sg_io_exception ()
241   : sg_exception()
242 {
243 }
244
245 sg_io_exception::sg_io_exception (const char* message, const char* origin)
246   : sg_exception(message, origin)
247 {
248 }
249
250 sg_io_exception::sg_io_exception (const char* message,
251                                   const sg_location &location,
252                                   const char* origin)
253   : sg_exception(message, origin),
254     _location(location)
255 {
256 }
257
258 sg_io_exception::sg_io_exception (const string& message, const string& origin)
259   : sg_exception(message, origin)
260 {
261 }
262
263 sg_io_exception::~sg_io_exception () throw ()
264 {
265 }
266
267 const string
268 sg_io_exception::getFormattedMessage () const
269 {
270   string ret = getMessage();
271   string loc = getLocation().asString();
272   if (loc.length()) {
273     ret += "\n at ";
274     ret += loc;
275   }
276   return ret;
277 }
278
279 const sg_location &
280 sg_io_exception::getLocation () const
281 {
282   return _location;
283 }
284
285 void
286 sg_io_exception::setLocation (const sg_location &location)
287 {
288   _location = location;
289 }
290
291
292 \f
293 ////////////////////////////////////////////////////////////////////////
294 // Implementation of sg_format_exception.
295 ////////////////////////////////////////////////////////////////////////
296
297 sg_format_exception::sg_format_exception ()
298   : sg_exception()
299 {
300   _text[0] = '\0';
301 }
302
303 sg_format_exception::sg_format_exception (const char* message,
304                                           const char* text,
305                                           const char* origin)
306   : sg_exception(message, origin)
307 {
308   setText(text);
309 }
310
311 sg_format_exception::sg_format_exception (const string& message,
312                                           const string& text,
313                                           const string& origin)
314   : sg_exception(message, origin)
315 {
316   setText(text.c_str());
317 }
318
319 sg_format_exception::~sg_format_exception () throw ()
320 {
321 }
322
323 const char*
324 sg_format_exception::getText () const
325 {
326   return _text;
327 }
328
329 void
330 sg_format_exception::setText (const char* text)
331 {
332   if (text) {
333     strncpy(_text, text, MAX_TEXT_LEN);
334     _text[MAX_TEXT_LEN] = '\0';
335   } else {
336     _text[0] = '\0';
337   }
338 }
339
340
341 \f
342 ////////////////////////////////////////////////////////////////////////
343 // Implementation of sg_range_exception.
344 ////////////////////////////////////////////////////////////////////////
345
346 sg_range_exception::sg_range_exception ()
347   : sg_exception()
348 {
349 }
350
351 sg_range_exception::sg_range_exception (const char* message,
352                                         const char* origin)
353   : sg_exception(message, origin)
354 {
355 }
356
357 sg_range_exception::sg_range_exception(const string& message,
358                                        const string& origin)
359   : sg_exception(message, origin)
360 {
361 }
362
363 sg_range_exception::~sg_range_exception () throw ()
364 {
365 }
366 // end of exception.cxx