]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
Fix mistypings
[gdcm.git] / src / gdcmException.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:10 $
7   Version:   $Revision: 1.29 $
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 #include <stdlib.h> // for exit
23
24 namespace GDCM_NAME_SPACE 
25 {
26 //-----------------------------------------------------------------------------
27 // Exception
28 /**
29  * \brief constructor
30  * @param f f
31  * @param msg msg 
32  */
33 Exception::Exception(const std::string &f, const std::string &msg) throw()
34 #ifdef __GNUC__
35    try
36 #endif
37    : From(f), Error(msg) 
38    {
39    }
40 #ifdef __GNUC__
41    catch(...) 
42    {
43       fatal("Exception::Exception(const std::string&, const std::string&, const std::string&)");
44    }
45 #endif
46
47 /**
48  * \brief fatal
49  * @param from from
50  */
51 void Exception::fatal(const char *from) throw() 
52 {
53    try
54    {
55       std::cerr << "Fatal: exception received in " << from 
56                 << " while handling exception." << std::endl;
57       exit(-1);
58    }
59    catch(...)
60    {
61       try
62       {
63          std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
64                    << std::endl;
65          exit(-1);
66       }
67       catch(...)
68       {
69          exit(-1);
70       }
71    }
72 }
73
74 /**
75  * \brief getName
76  * @return string
77  */
78 std::string Exception::getName() const throw()
79 {
80    try
81    {
82 #if defined(__GNUC__) && 0   // GNU C++ compiler class name demangling
83       unsigned int nested = 1, i, nb;
84       int offset;
85       std::string one;
86
87       std::string name;
88       std::string iname = typeid(*this).name();
89       if ( iname[0] == 'Q' )
90       {
91          nested = iname[1] - '0';
92          iname = std::string(iname, 2, std::string::npos);
93       }
94       for(i = 0; i < nested; i++)
95       {
96          ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
97          iname = std::string(iname, offset, std::string::npos);
98          name += std::string(iname, 0, nb);
99          if ( i + 1 < nested) name += "::";
100          iname = std::string(iname, nb, std::string::npos );
101       }
102       return name;
103 #else           // no class name demangling
104       std::string name = typeid(*this).name();
105       return name;
106 #endif
107    }
108    catch(...) 
109    {
110       fatal("Exception::getName(std::string &)");
111       return "";
112    }
113 }
114
115 /**
116  * \brief Exception
117  */
118 Exception::operator const char *() const throw() 
119 {
120    return getName().c_str();
121 }
122
123 //-----------------------------------------------------------------------------
124 /**
125  * \brief Exception::operator <<
126  * @param os ostream to write to
127  * @param e exception to raise
128  */
129 std::ostream& operator<<(std::ostream &os, const Exception &e) 
130 {
131    try 
132    {
133       os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
134    }
135    catch(...) 
136    {
137       Exception::fatal("operator<<(std::ostream &, const Exception&)");
138    }
139    return os;
140 }
141
142 } // end namespace gdcm
143 //-----------------------------------------------------------------------------