]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
* src/*.[h] all occurences of stl classes are now prefixed with
[gdcm.git] / src / gdcmException.cxx
1 #include "gdcmException.h"
2
3 #include <typeinfo>
4 #include <stdio.h>
5 using namespace std;
6
7
8 gdcmException::gdcmException(const string &f, const string& msg) throw()
9 #ifdef __GNUC__
10   try
11 #endif
12   : from(f), error(msg) {
13   }
14 #ifdef __GNUC__
15 catch(...) {
16   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
17 }
18 #endif
19
20
21
22 void gdcmException::fatal(const char *from) throw() {
23   try {
24     cerr << "Fatal: exception received in " << from 
25          << " while handling exception." << endl;
26     exit(-1);
27   }
28   catch(...) {
29     try {
30       cerr << "Fatal: exception received in Exception::fatal while handling exception."
31            << endl;
32       exit(-1);
33     }
34     catch(...) {
35       exit(-1);
36     }
37   }  
38 }
39
40
41 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       string one;
46
47       string name;
48       string iname = typeid(*this).name();
49       if(iname[0] == 'Q') {
50         nested = iname[1] - '0';
51         iname = 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 = string(iname, offset, std::string::npos);
56         name += string(iname, 0, nb);
57         if(i + 1 < nested) name += "::";
58         iname = 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(string &)");
68     return "";
69   }
70 }
71
72
73 gdcmException::operator const char *() const throw() {
74   return getName().c_str();
75 }
76
77
78 ostream& operator<<(ostream &os, const gdcmException &e) {
79   try {  
80     os << "Exception " << e.getName() << " thrown: " << e.getError() << endl;
81   }
82   catch(...) {
83     gdcmException::fatal("operator<<(ostream &, const gdcmException&)");
84   }
85   return os;
86 }
87
88