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