]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
ENH: Minor cleanup, still removing stdio.h and co ...
[gdcm.git] / src / gdcmException.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/08 04:52:55 $
7   Version:   $Revision: 1.19 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmException.h"
20
21 #include <typeinfo>
22
23 //-----------------------------------------------------------------------------
24 // gdcmException
25
26 /**
27  * \ingroup gdcmException
28  * \brief constructor
29  * @param f
30  * @param msg  
31  */
32 gdcmException::gdcmException(const std::string &f, const std::string& msg) throw()
33 #ifdef __GNUC__
34   try
35 #endif
36   : From(f), Error(msg) {
37   }
38 #ifdef __GNUC__
39 catch(...) {
40   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
41 }
42 #endif
43
44
45 /**
46  * \ingroup gdcmException
47  * \brief fatal
48  * @param from 
49  */
50 void gdcmException::fatal(const char *from) throw() {
51    try
52    {
53       std::cerr << "Fatal: exception received in " << from 
54                 << " while handling exception." << std::endl;
55       exit(-1);
56    }
57    catch(...)
58    {
59       try
60       {
61          std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
62                    << std::endl;
63          exit(-1);
64       }
65       catch(...)
66       {
67          exit(-1);
68       }
69    }
70 }
71
72 /**
73  * \ingroup gdcmException
74  * \brief getName
75  * @return string
76  */
77 std::string gdcmException::getName() const throw()
78 {
79    try
80    {
81 #ifdef __GNUC__   // GNU C++ compiler class name demangling
82       unsigned int nested = 1, i, nb, offset;
83       std::string one;
84
85       std::string name;
86       std::string iname = typeid(*this).name();
87       if(iname[0] == 'Q')
88       {
89          nested = iname[1] - '0';
90          iname = std::string(iname, 2, std::string::npos);
91       }
92       for(i = 0; i < nested; i++)
93       {
94          ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
95          iname = std::string(iname, offset, std::string::npos);
96          name += std::string(iname, 0, nb);
97          if(i + 1 < nested) name += "::";
98          iname = std::string(iname, nb, std::string::npos);
99       }
100       return name;
101 #else           // no class name demangling
102       //name = typeid(*this).name();
103       return "Exception";
104 #endif
105   }
106   catch(...) {
107     fatal("Exception::getName(std::string &)");
108     return "";
109   }
110 }
111
112 /**
113  * \ingroup gdcmException
114  * \brief gdcmException
115  */
116  gdcmException::operator const char *() const throw() {
117   return getName().c_str();
118 }
119
120 //-----------------------------------------------------------------------------
121 /**
122  * \ingroup gdcmException
123  * \brief gdcmException::operator <<
124  */
125  std::ostream& operator<<(std::ostream &os, const gdcmException &e) {
126   try {  
127     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
128   }
129   catch(...) {
130     gdcmException::fatal("operator<<(std::ostream &, const gdcmException&)");
131   }
132   return os;
133 }
134
135 //-----------------------------------------------------------------------------