]> git.mxchange.org Git - simgear.git/blob - simgear/structure/exception.cxx
Working 'noshadow' animation
[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 (const string& message,
264                                   const sg_location &location,
265                                   const string& origin)
266   : sg_exception(message, origin),
267     _location(location)
268 {
269 }
270
271 sg_io_exception::~sg_io_exception () throw ()
272 {
273 }
274
275 const string
276 sg_io_exception::getFormattedMessage () const
277 {
278   string ret = getMessage();
279   string loc = getLocation().asString();
280   if (loc.length()) {
281     ret += "\n at ";
282     ret += loc;
283   }
284   return ret;
285 }
286
287 const sg_location &
288 sg_io_exception::getLocation () const
289 {
290   return _location;
291 }
292
293 void
294 sg_io_exception::setLocation (const sg_location &location)
295 {
296   _location = location;
297 }
298
299
300 \f
301 ////////////////////////////////////////////////////////////////////////
302 // Implementation of sg_format_exception.
303 ////////////////////////////////////////////////////////////////////////
304
305 sg_format_exception::sg_format_exception ()
306   : sg_exception()
307 {
308   _text[0] = '\0';
309 }
310
311 sg_format_exception::sg_format_exception (const char* message,
312                                           const char* text,
313                                           const char* origin)
314   : sg_exception(message, origin)
315 {
316   setText(text);
317 }
318
319 sg_format_exception::sg_format_exception (const string& message,
320                                           const string& text,
321                                           const string& origin)
322   : sg_exception(message, origin)
323 {
324   setText(text.c_str());
325 }
326
327 sg_format_exception::~sg_format_exception () throw ()
328 {
329 }
330
331 const char*
332 sg_format_exception::getText () const
333 {
334   return _text;
335 }
336
337 void
338 sg_format_exception::setText (const char* text)
339 {
340   if (text) {
341     strncpy(_text, text, MAX_TEXT_LEN);
342     _text[MAX_TEXT_LEN-1] = '\0';
343   } else {
344     _text[0] = '\0';
345   }
346 }
347
348
349 \f
350 ////////////////////////////////////////////////////////////////////////
351 // Implementation of sg_range_exception.
352 ////////////////////////////////////////////////////////////////////////
353
354 sg_range_exception::sg_range_exception ()
355   : sg_exception()
356 {
357 }
358
359 sg_range_exception::sg_range_exception (const char* message,
360                                         const char* origin)
361   : sg_exception(message, origin)
362 {
363 }
364
365 sg_range_exception::sg_range_exception(const string& message,
366                                        const string& origin)
367   : sg_exception(message, origin)
368 {
369 }
370
371 sg_range_exception::~sg_range_exception () throw ()
372 {
373 }
374 // end of exception.cxx