]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
* To remove warnings
[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 gdcmException::gdcmException(const std::string &f, const std::string& msg) throw()
11 #ifdef __GNUC__
12   try
13 #endif
14   : from(f), error(msg) {
15   }
16 #ifdef __GNUC__
17 catch(...) {
18   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
19 }
20 #endif
21
22
23 void gdcmException::fatal(const char *from) throw() {
24   try {
25     std::cerr << "Fatal: exception received in " << from 
26          << " while handling exception." << std::endl;
27     exit(-1);
28   }
29   catch(...) {
30     try {
31       std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
32            << std::endl;
33       exit(-1);
34     }
35     catch(...) {
36       exit(-1);
37     }
38   }  
39 }
40
41 std::string gdcmException::getName() const throw() {
42   try {
43 #ifdef __GNUC__   // GNU C++ compiler class name demangling
44       unsigned int nested = 1, i, nb, offset;
45       std::string one;
46
47       std::string name;
48       std::string iname = typeid(*this).name();
49       if(iname[0] == 'Q') {
50         nested = iname[1] - '0';
51         iname = std::string(iname, 2, std::string::npos);
52       }
53       for(i = 0; i < nested; i++) {
54         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
55         iname = std::string(iname, offset, std::string::npos);
56         name += std::string(iname, 0, nb);
57         if(i + 1 < nested) name += "::";
58         iname = std::string(iname, nb, std::string::npos);
59       }
60       return name;
61 #else             // no class name demangling
62       //name = typeid(*this).name();
63       return "Exception";
64 #endif
65   }
66   catch(...) {
67     fatal("Exception::getName(std::string &)");
68     return "";
69   }
70 }
71
72 gdcmException::operator const char *() const throw() {
73   return getName().c_str();
74 }
75
76 //-----------------------------------------------------------------------------
77 std::ostream& operator<<(std::ostream &os, const gdcmException &e) {
78   try {  
79     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
80   }
81   catch(...) {
82     gdcmException::fatal("operator<<(std::ostream &, const gdcmException&)");
83   }
84   return os;
85 }
86
87 //-----------------------------------------------------------------------------