Kstars

SpatialException.cpp
1 //# Filename: SpatialException.cpp
2 //#
3 //# Author: John Doug Reynolds, P. Kunszt
4 //#
5 //# Date: Mar 1997
6 //#
7 //# SPDX-FileCopyrightText: 2000 Peter Z. Kunszt Alex S. Szalay, Aniruddha R. Thakar
8 //# The Johns Hopkins University
9 //#
10 //# Modification history:
11 //#
12 //# Oct. 1998, P. Kunszt : remove Rogue Wave C-string dependency
13 //# almost all the interface had to be rewritten.
14 //# Also, use some of the inheritance to avoid
15 //# code duplication. Introduced defaultstr[].
16 //# Oct 18, 2001 : Dennis C. Dinge -- Replaced ValVec with std::vector
17 //#
18 
19 #include <cstdio>
20 #include <cstdlib>
21 #include <string.h>
22 #include <SpatialException.h>
23 
24 /* --- SpatialException methods ------------------------------------------------- */
25 const char *SpatialException::defaultstr[] = { "SDSS Science Archive",
26  "generic exception", // These specialized exceptions are
27  "unimplemented functionality", // currently implemented. If no string
28  "failed operation", // is given, this is the standard
29  "array bounds violation", // message.
30  "interface violation" };
31 
32 #define CONTEXT 0 // indices of exceptions
33 #define GENERIC 1
34 #define UNIMPLEMENTED 2
35 #define FAILURE 3
36 #define BOUNDS 4
37 #define INTERFACE 5
38 
40 {
41  clear();
42 }
43 
44 SpatialException::SpatialException(const char *cstr, int defIndex) throw()
45 {
46  try
47  {
48  if (cstr)
49  {
50  str_ = new char[slen(cstr) + 1];
51  strcpy(str_, cstr);
52  }
53  else
54  {
55  str_ = new char[50];
56  sprintf(str_, "%s : %s", defaultstr[CONTEXT], defaultstr[defIndex]);
57  }
58  }
59  catch (...)
60  {
61  clear();
62  }
63 }
64 
65 SpatialException::SpatialException(const char *context, const char *because, int defIndex) throw()
66 {
67  try
68  {
69  const char *tmpc, *tmpb;
70  tmpc = context ? context : defaultstr[CONTEXT];
71  tmpb = because ? because : defaultstr[defIndex];
72  str_ = new char[slen(tmpc) + slen(tmpb) + 50]; // allow extensions
73  sprintf(str_, "%s : %s", tmpc, tmpb);
74  }
75  catch (...)
76  {
77  clear();
78  }
79 }
80 
82 {
83  try
84  {
85  if (oldX.str_)
86  {
87  str_ = new char[slen(oldX.str_) + 1];
88  strcpy(str_, oldX.str_);
89  }
90  }
91  catch (...)
92  {
93  clear();
94  }
95 }
96 
98 {
99  try
100  {
101  if (&oldX != this) // beware of self-assignment
102  {
103  if (oldX.str_)
104  {
105  str_ = new char[slen(oldX.str_) + 1];
106  strcpy(str_, oldX.str_);
107  }
108  }
109  }
110  catch (...)
111  {
112  clear();
113  }
114  return *this;
115 }
116 
117 const char *SpatialException::what() const throw()
118 {
119  try
120  {
121  return str_;
122  }
123  catch (...)
124  {
125  return "";
126  }
127 }
128 
129 int SpatialException::slen(const char *str) const
130 {
131  if (str)
132  return strlen(str);
133  return 0;
134 }
135 
137 {
138  delete[] str_;
139  str_ = nullptr;
140 }
141 /* --- SpatialUnimplemented methods --------------------------------------------- */
142 
143 SpatialUnimplemented::SpatialUnimplemented(const char *cstr) throw() : SpatialException(cstr, UNIMPLEMENTED)
144 {
145 }
146 
147 SpatialUnimplemented::SpatialUnimplemented(const char *context, const char *because) throw()
148  : SpatialException(context, because, UNIMPLEMENTED)
149 {
150 }
151 
153 {
154 }
155 
156 /* --- SpatialFailure methods --------------------------------------------------- */
157 
158 SpatialFailure::SpatialFailure(const char *cstr) throw() : SpatialException(cstr, FAILURE)
159 {
160 }
161 
162 SpatialFailure::SpatialFailure(const char *context, const char *because) throw()
163  : SpatialException(context, because, FAILURE)
164 {
165 }
166 
167 SpatialFailure::SpatialFailure(const char *context, const char *operation, const char *resource,
168  const char *because) throw()
169 {
170  try
171  {
172  clear();
173  if (!operation && !resource && !because)
174  {
175  if (!context)
176  context = defaultstr[CONTEXT];
177  because = "failed operation";
178  }
179  size_t const len = slen(context) + slen(operation) + slen(resource) + slen(because) + 50;
180  str_ = new char[len];
181  *str_ = '\0';
182  char * ptr = str_;
183  if (!context)
184  context = defaultstr[CONTEXT];
185  ptr += sprintf(ptr, "%s: ", context);
186  if (operation)
187  {
188  ptr += sprintf(ptr, " %s failed ", operation);
189  }
190  if (resource)
191  {
192  if (operation)
193  ptr += sprintf(ptr, " on \"%s\"", resource);
194  else
195  ptr += sprintf(ptr, " trouble with \"%s\"", resource);
196  }
197  if (because)
198  {
199  if (operation || resource)
200  ptr += sprintf(ptr, " because %s", because);
201  else
202  ptr += sprintf(ptr, " %s", because);
203  }
204  }
205  catch (...)
206  {
207  clear();
208  }
209 }
210 
212 {
213 }
214 
215 /* --- SpatialBoundsError methods ----------------------------------------------- */
216 
217 SpatialBoundsError::SpatialBoundsError(const char *cstr) throw() : SpatialException(cstr, BOUNDS)
218 {
219 }
220 
221 SpatialBoundsError::SpatialBoundsError(const char *context, const char *array, int32 limit, int32 index) throw()
222  : SpatialException(context, array, BOUNDS)
223 {
224  try
225  {
226  if (limit != -1)
227  {
228  char * ptr = str_;
229  if (array)
230  ptr += sprintf(ptr, "[%d]", index);
231  else
232  ptr += sprintf(ptr, " array index %d ", index);
233  if (index > limit)
234  {
235  ptr += sprintf(ptr, " over upper bound by %d", index - limit);
236  }
237  else
238  {
239  ptr += sprintf(ptr, " under lower bound by %d", limit - index);
240  }
241  }
242  }
243  catch (...)
244  {
245  clear();
246  }
247 }
248 
250 {
251 }
252 
253 /* --- SpatialInterfaceError methods -------------------------------------------- */
254 
255 SpatialInterfaceError::SpatialInterfaceError(const char *cstr) throw() : SpatialException(cstr, INTERFACE)
256 {
257 }
258 
259 SpatialInterfaceError::SpatialInterfaceError(const char *context, const char *because) throw()
260  : SpatialException(context, because, INTERFACE)
261 {
262 }
263 
264 SpatialInterfaceError::SpatialInterfaceError(const char *context, const char *argument, const char *because) throw()
265 {
266  try
267  {
268  clear();
269  str_ = new char[slen(context) + slen(argument) + slen(because) + 128];
270  *str_ = '\0';
271  char * ptr = str_;
272  if (!context)
273  context = defaultstr[CONTEXT];
274  ptr += sprintf(ptr, "%s: ", context);
275  if (argument && because)
276  {
277  ptr += sprintf(ptr, " argument \"%s\" is invalid because %s ", argument, because);
278  }
279  else if (argument && !because)
280  {
281  ptr += sprintf(ptr, " invalid argument \"%s\" ", argument);
282  }
283  else if (!argument)
284  {
285  if (because)
286  ptr += sprintf(str_, " %s", because);
287  else
288  ptr += sprintf(str_, " interface violation");
289  }
290  }
291  catch (...)
292  {
293  clear();
294  }
295 }
296 
298 {
299 }
int slen(const char *) const
return string length also for null strings
SpatialException thrown by unimplemented functions.
char * str_
error string to assemble
SpatialException thrown on violation of array bounds.
SpatialBoundsError(const char *what=nullptr)
Default and explicit constructors.
SpatialException thrown on operational failure.
SpatialInterfaceError(const char *what=nullptr)
Default and explicit constructors.
SpatialException & operator=(const SpatialException &)
Assignment operator.
SpatialException(const char *what=nullptr, int defIndex=1)
Default and explicit constructor.
HTM SpatialIndex Exception base class This is the base class for all Science Archive exceptions.
SpatialFailure(const char *what=nullptr)
Default and explicit constructors.
virtual ~SpatialException()
Destructor.
SpatialException thrown on violation of interface protocols.
virtual const char * what() const
Returns the message as set during construction.
SpatialUnimplemented(const char *what=nullptr)
Default and explicit constructors.
static const char * defaultstr[]
default error string
void clear()
deallocate string
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:14:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.