]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
* src/gdcmUtil.[cxx|h] _cleanString C style function (replaced with
[gdcm.git] / src / gdcmException.cxx
1 // gdcmException.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmException.h"
4
5 #include <typeinfo>
6 #include <stdio.h>
7
8 //-----------------------------------------------------------------------------
9 // gdcmException
10
11 /**
12  * \ingroup gdcmException
13  * \brief constructor
14  * @param f
15  * @param msg  
16  */
17 gdcmException::gdcmException(const std::string &f, const std::string& msg) throw()
18 #ifdef __GNUC__
19   try
20 #endif
21   : from(f), error(msg) {
22   }
23 #ifdef __GNUC__
24 catch(...) {
25   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
26 }
27 #endif
28
29
30 /**
31  * \ingroup gdcmException
32  * \brief fatal
33  * @param from 
34  */
35  void gdcmException::fatal(const char *from) throw() {
36   try {
37     std::cerr << "Fatal: exception received in " << from 
38          << " while handling exception." << std::endl;
39     exit(-1);
40   }
41   catch(...) {
42     try {
43       std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
44            << std::endl;
45       exit(-1);
46     }
47     catch(...) {
48       exit(-1);
49     }
50   }  
51 }
52
53 /**
54  * \ingroup gdcmException
55  * \brief getName
56  * @return string
57  */
58  std::string gdcmException::getName() const throw() {
59   try {
60 #ifdef __GNUC__   // GNU C++ compiler class name demangling
61       unsigned int nested = 1, i, nb, offset;
62       std::string one;
63
64       std::string name;
65       std::string iname = typeid(*this).name();
66       if(iname[0] == 'Q') {
67         nested = iname[1] - '0';
68         iname = std::string(iname, 2, std::string::npos);
69       }
70       for(i = 0; i < nested; i++) {
71         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
72         iname = std::string(iname, offset, std::string::npos);
73         name += std::string(iname, 0, nb);
74         if(i + 1 < nested) name += "::";
75         iname = std::string(iname, nb, std::string::npos);
76       }
77       return name;
78 #else           // no class name demangling
79       //name = typeid(*this).name();
80       return "Exception";
81 #endif
82   }
83   catch(...) {
84     fatal("Exception::getName(std::string &)");
85     return "";
86   }
87 }
88
89 /**
90  * \ingroup gdcmException
91  * \brief gdcmException
92  */
93  gdcmException::operator const char *() const throw() {
94   return getName().c_str();
95 }
96
97 //-----------------------------------------------------------------------------
98 /**
99  * \ingroup gdcmException
100  * \brief gdcmException::operator <<
101  */
102  std::ostream& operator<<(std::ostream &os, const gdcmException &e) {
103   try {  
104     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
105   }
106   catch(...) {
107     gdcmException::fatal("operator<<(std::ostream &, const gdcmException&)");
108   }
109   return os;
110 }
111
112 //-----------------------------------------------------------------------------