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