]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Exception.cxx
Salome HOME
Final version V2_0_1 which works with Med File V2.1 and the KERNEL
[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 char* duplicate( const char *const str ) ;
26 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 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       _text = 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 & MEDMEM::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 (){};