]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
src/*.cxx removed pragma thingies to src/gdcmCommon.h
[gdcm.git] / src / gdcmException.cxx
1
2 #include "gdcmException.h"
3
4 #include <typeinfo>
5 #include <stdio.h>
6
7 gdcmException::gdcmException(const std::string &f, const std::string& msg) throw()
8 #ifdef __GNUC__
9   try
10 #endif
11   : from(f), error(msg) {
12   }
13 #ifdef __GNUC__
14 catch(...) {
15   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
16 }
17 #endif
18
19
20
21 void gdcmException::fatal(const char *from) throw() {
22   try {
23     std::cerr << "Fatal: exception received in " << from 
24          << " while handling exception." << std::endl;
25     exit(-1);
26   }
27   catch(...) {
28     try {
29       std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
30            << std::endl;
31       exit(-1);
32     }
33     catch(...) {
34       exit(-1);
35     }
36   }  
37 }
38
39
40 std::string gdcmException::getName() const throw() {
41   try {
42 #ifdef __GNUC__   // GNU C++ compiler class name demangling
43       unsigned int nested = 1, i, nb, offset;
44       std::string one;
45
46       std::string name;
47       std::string iname = typeid(*this).name();
48       if(iname[0] == 'Q') {
49         nested = iname[1] - '0';
50         iname = std::string(iname, 2, std::string::npos);
51       }
52       for(i = 0; i < nested; i++) {
53         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
54         iname = std::string(iname, offset, std::string::npos);
55         name += std::string(iname, 0, nb);
56         if(i + 1 < nested) name += "::";
57         iname = std::string(iname, nb, std::string::npos);
58       }
59       return name;
60 #else             // no class name demangling
61       //name = typeid(*this).name();
62       return "Exception";
63 #endif
64   }
65   catch(...) {
66     fatal("Exception::getName(std::string &)");
67     return "";
68   }
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