Salome HOME
update after merging trhe branches CEA_V3_0_x, OCC_V3_1_0_a1_x, and the main
[modules/med.git] / src / MEDMEM / MEDMEM_Exception.cxx
1 /*
2  File MedException.cxx
3  $Header$
4 */
5
6 #include "MEDMEM_Utilities.hxx"
7 #include "MEDMEM_Exception.hxx"
8
9 using namespace std;
10 using namespace MEDMEM;
11
12 extern "C"
13 {
14 #include <math.h>
15 #include <stdio.h>
16 #include <string.h>
17 }
18
19 /*!
20   \internal
21   Function used to duplicate char * 
22 */
23 char* duplicate( const char *const str ) ;
24 char* duplicate( const char *const str )
25 {
26         ASSERT(str!=NULL) ;
27         const size_t length = strlen( str ) ;
28         ASSERT(length>0) ;
29         char *new_str = new char[ 1+length ] ;
30         ASSERT(new_str) ;
31         strcpy( new_str , str ) ;
32         return new_str ;
33 }
34
35 /*!
36   \internal Default Constructor (Should not be used)
37 */
38 // ------------------------------------------------------ //
39 MEDEXCEPTION::MEDEXCEPTION( void ): exception() , _text(0)
40 // ------------------------------------------------------ //
41 {
42   MESSAGE( "You must user the standard builder : MEDEXCEPTION::MEDEXCEPTION( const char *text )" ) ;
43   INTERRUPTION(1) ;
44 }
45
46
47 /*!
48   \internal
49   Function used to elaborate the text of the MEDEXCEPTION
50 */
51 // --------------------------------------------------------------------------------------- //
52 char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
53 // --------------------------------------------------------------------------------------- //
54 {
55   char *newText = 0 ;
56
57   ASSERT(text) ;
58   const size_t l1 = strlen(text) ;
59
60   const char* prefix = "MED Exception" ;
61   const size_t l0 = strlen(prefix) ;
62
63   if ( fileName )
64   {
65       const size_t l2 = strlen(fileName) ;
66
67       ASSERT(lineNumber>=1) ;
68       const size_t l3 = 1+int(log10(float(lineNumber))) ;
69         
70       const size_t l4 =  l0+l1+l2+l3+10+1 ;
71       newText = new char [ l4 ] ;
72       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
73       ASSERT(newText[l4-1] == '\0' );
74
75   }
76   else
77   {
78       newText = new char [ l0+l1+3+1 ] ;
79       sprintf( newText , "%s : %s" , prefix, text ) ;
80    }
81   ASSERT(newText) ;
82   return newText ;
83 }
84
85 /*!
86     Constructor : \n
87     It will create the text of the MEDEXCEPTION from the different parameters.
88     It will take the form : \n
89     MEDEXCEPTION, fileName, lineNumber and text of the exception
90 */
91 // ------------------------------------------------------------------------------------------------ //
92 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : 
93               exception(), _text( makeText( text , fileName , lineNumber ) )
94 // ------------------------------------------------------------------------------------------------ //
95 {
96   MESSAGE(_text);
97 }
98
99 /*!
100   Destructor : \n
101   If necessary desallocates Memory
102 */
103
104 // ------------------------------------//
105 MEDEXCEPTION::~MEDEXCEPTION() throw ()
106 // ------------------------------------//
107 {
108   if ( _text )
109     {
110       delete [] _text ;
111       _text = 0 ;
112     }
113   ASSERT(_text==NULL) ;
114 }
115
116
117 /*!
118   Copy Constructor : \n
119   Should not be used very often
120 */
121 // ----------------------------------------------------------------------- //
122 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
123 // ----------------------------------------------------------------------- //
124 {
125   ;
126 }
127 /*!
128   Operator << : put the message to the given stream.
129 */
130 // ------------------------------------------------------- //
131 ostream & MEDMEM::operator<<( ostream &os , const MEDEXCEPTION &ex )
132 // ------------------------------------------------------- //
133 {
134   os << ex._text ;
135   return os ;
136 }
137
138 /*!
139   Return a char * which contain the message.
140 */
141 // ------------------------------------------------- //
142 const char* MEDEXCEPTION::what( void ) const throw ()
143 // ------------------------------------------------- //
144 {
145   return _text ;
146 }
147
148
149 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
150
151
152 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {};
153
154
155 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
156 (
157  const char *text, const char *fileName/*=0*/, 
158  const unsigned int lineNumber/*=0*/ 
159  ) : MEDEXCEPTION(text, fileName, lineNumber) {};
160
161 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){};