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