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