]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Exception.cxx
Salome HOME
Initialisation module MED_SRC de la base MED
[modules/med.git] / src / MEDMEM / MEDMEM_Exception.cxx
1 /*
2  File MedException.cxx
3  $Header$
4 */
5
6 using namespace std;
7
8 #include "utilities.h"
9 #include "MEDMEM_Exception.hxx"
10
11 extern "C"
12 {
13 #include <math.h>
14 #include <stdio.h>
15 #include <string.h>
16 }
17
18
19 const char* duplicate( const char *const str ) ;
20
21 const char* duplicate( const char *const str )
22 {
23         ASSERT(str!=NULL) ;
24         const size_t length = strlen( str ) ;
25         ASSERT(length>0) ;
26         char *new_str = new char[ 1+length ] ;
27         ASSERT(new_str) ;
28         strcpy( new_str , str ) ;
29         return new_str ;
30 }
31
32 MEDEXCEPTION::MEDEXCEPTION( void ): exception() , _text(0)
33 {
34   MESSAGE( "You must user the standard builder : MEDEXCEPTION::MEDEXCEPTION( const char *text )" ) ;
35   INTERRUPTION(1) ;
36 }
37
38
39 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
40 {
41   char *newText = 0 ;
42
43   ASSERT(text) ;
44   const size_t l1 = 1+strlen(text) ;
45   ASSERT(l1>1) ;
46
47   const char* prefix = "MED Exception" ;
48   const size_t l0 = 2+strlen(prefix) ;
49
50   if ( fileName )
51     {
52       const size_t l2 = 4+strlen(fileName) ;
53       ASSERT(l2>4) ;
54
55       ASSERT(lineNumber>=1) ;
56       const size_t l3 = 4+int(log10(float(lineNumber))) ;
57                 
58       newText = new char [ 1+l0+l1+l2+l3 ] ;
59       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
60     }
61   else
62     {
63       newText = new char [ 1+l0+l1 ] ;
64       sprintf( newText , "%s : %s" , prefix, text ) ;
65     }
66   ASSERT(newText) ;
67   return newText ;
68 }
69
70
71 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : exception(), _text( makeText( text , fileName , lineNumber ) )
72 {
73   MESSAGE(_text);
74 }
75
76
77 MEDEXCEPTION::~MEDEXCEPTION() throw ()
78 {
79   if ( _text )
80     {
81       delete [] _text ;
82       char *& txt = (char*)_text ;
83       txt = 0 ;
84     }
85   ASSERT(_text==NULL) ;
86 }
87
88
89
90 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
91 {
92   ;
93 }
94
95
96 ostream & operator<<( ostream &os , const MEDEXCEPTION &ex )
97 {
98   os << ex._text ;
99   return os ;
100 }
101
102
103
104 const char* MEDEXCEPTION::what( void ) const throw ()
105 {
106   return _text ;
107 }
108
109
110 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
111
112
113 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {};
114
115
116 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
117 (
118  const char *text, const char *fileName=0, 
119  const unsigned int lineNumber=0 
120  ) : MEDEXCEPTION(text, fileName, lineNumber) {};
121
122 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){};