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