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