]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
ostream replaced by std::ostream
[gdcm.git] / src / gdcmException.cxx
1 #ifdef _MSC_VER
2 //'identifier' : not all control paths return a value
3 //#pragma warning ( disable : 4715 )
4 // 'identifier' : class 'type' needs to have dll-interface to be used by
5 // clients of class 'type2'
6 #pragma warning ( disable : 4251 )
7 // 'identifier' : identifier was truncated to 'number' characters in the
8 // debug information
9 #pragma warning ( disable : 4786 )
10 #endif //_MSC_VER
11
12 #include "gdcmException.h"
13
14 #include <typeinfo>
15 #include <stdio.h>
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 void gdcmException::fatal(const char *from) throw() {
32   try {
33     std::cerr << "Fatal: exception received in " << from 
34          << " while handling exception." << std::endl;
35     exit(-1);
36   }
37   catch(...) {
38     try {
39       std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
40            << std::endl;
41       exit(-1);
42     }
43     catch(...) {
44       exit(-1);
45     }
46   }  
47 }
48
49
50 std::string gdcmException::getName() const throw() {
51   try {
52 #ifdef __GNUC__   // GNU C++ compiler class name demangling
53       unsigned int nested = 1, i, nb, offset;
54       std::string one;
55
56       std::string name;
57       std::string iname = typeid(*this).name();
58       if(iname[0] == 'Q') {
59         nested = iname[1] - '0';
60         iname = std::string(iname, 2, std::string::npos);
61       }
62       for(i = 0; i < nested; i++) {
63         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
64         iname = std::string(iname, offset, std::string::npos);
65         name += std::string(iname, 0, nb);
66         if(i + 1 < nested) name += "::";
67         iname = std::string(iname, nb, std::string::npos);
68       }
69       return name;
70 #else             // no class name demangling
71       //name = typeid(*this).name();
72       return "Exception";
73 #endif
74   }
75   catch(...) {
76     fatal("Exception::getName(std::string &)");
77     return "";
78   }
79 }
80
81
82 gdcmException::operator const char *() const throw() {
83   return getName().c_str();
84 }
85
86
87 std::ostream& operator<<(std::ostream &os, const gdcmException &e) {
88   try {  
89     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
90   }
91   catch(...) {
92     gdcmException::fatal("operator<<(std::ostream &, const gdcmException&)");
93   }
94   return os;
95 }
96
97