]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
* src/*.cxx : first parss to normalize file organisation
[gdcm.git] / src / gdcmException.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/01 10:29:55 $
7   Version:   $Revision: 1.26 $
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 namespace gdcm 
23 {
24 //-----------------------------------------------------------------------------
25 // Exception
26 /**
27  * \brief constructor
28  * @param f f
29  * @param msg msg 
30  */
31 Exception::Exception(const std::string &f, const std::string &msg) throw()
32 #ifdef __GNUC__
33    try
34 #endif
35    : From(f), Error(msg) 
36    {
37    }
38 #ifdef __GNUC__
39    catch(...) 
40    {
41       fatal("Exception::Exception(const std::string&, const std::string&, const std::string&)");
42    }
43 #endif
44
45 /**
46  * \brief fatal
47  * @param from from
48  */
49 void Exception::fatal(const char *from) throw() 
50 {
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  * \brief getName
74  * @return string
75  */
76 std::string Exception::getName() const throw()
77 {
78    try
79    {
80 #if defined(__GNUC__) && 0   // GNU C++ compiler class name demangling
81       unsigned int nested = 1, i, nb;
82       int 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       std::string name = typeid(*this).name();
103       return name;
104 #endif
105    }
106    catch(...) 
107    {
108       fatal("Exception::getName(std::string &)");
109       return "";
110    }
111 }
112
113 /**
114  * \brief Exception
115  */
116 Exception::operator const char *() const throw() 
117 {
118    return getName().c_str();
119 }
120
121 //-----------------------------------------------------------------------------
122 /**
123  * \brief Exception::operator <<
124  * @param os ostream to write to
125  * @param e exception to raise
126  */
127 std::ostream& operator<<(std::ostream &os, const Exception &e) 
128 {
129    try 
130    {
131       os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
132    }
133    catch(...) 
134    {
135       Exception::fatal("operator<<(std::ostream &, const Exception&)");
136    }
137    return os;
138 }
139
140 } // end namespace gdcm
141 //-----------------------------------------------------------------------------