]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Exception.cxx
Salome HOME
NRI : Update 1.1a and New organisation.
[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 const char* duplicate( const char *const str ) ;
21
22 const char* duplicate( const char *const str )
23 {
24         ASSERT(str!=NULL) ;
25         const size_t length = strlen( str ) ;
26         ASSERT(length>0) ;
27         char *new_str = new char[ 1+length ] ;
28         ASSERT(new_str) ;
29         strcpy( new_str , str ) ;
30         return new_str ;
31 }
32
33 MEDEXCEPTION::MEDEXCEPTION( void ): exception() , _text(0)
34 {
35   MESSAGE( "You must user the standard builder : MEDEXCEPTION::MEDEXCEPTION( const char *text )" ) ;
36   INTERRUPTION(1) ;
37 }
38
39
40 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
41 {
42   char *newText = 0 ;
43
44   ASSERT(text) ;
45   const size_t l1 = strlen(text) ;
46
47   const char* prefix = "MED Exception" ;
48   const size_t l0 = strlen(prefix) ;
49
50   if ( fileName )
51     {
52       const size_t l2 = strlen(fileName) ;
53
54       ASSERT(lineNumber>=1) ;
55       const size_t l3 = 1+int(log10(float(lineNumber))) ;
56         
57       const size_t l4 =  l0+l1+l2+l3+10+1 ;
58       newText = new char [ l4 ] ;
59       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
60       ASSERT(newText[l4-1] == '\0' );
61
62     }
63   else
64     {
65       newText = new char [ l0+l1+3+1 ] ;
66       sprintf( newText , "%s : %s" , prefix, text ) ;
67     }
68   ASSERT(newText) ;
69   return newText ;
70 }
71
72
73 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : exception(), _text( makeText( text , fileName , lineNumber ) )
74 {
75   MESSAGE(_text);
76 }
77
78
79 MEDEXCEPTION::~MEDEXCEPTION() throw ()
80 {
81   if ( _text )
82     {
83       delete [] _text ;
84       char *& txt = (char*)_text ;
85       txt = 0 ;
86     }
87   ASSERT(_text==NULL) ;
88 }
89
90
91
92 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
93 {
94   ;
95 }
96
97
98 ostream & operator<<( ostream &os , const MEDEXCEPTION &ex )
99 {
100   os << ex._text ;
101   return os ;
102 }
103
104
105
106 const char* MEDEXCEPTION::what( void ) const throw ()
107 {
108   return _text ;
109 }
110
111
112 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
113
114
115 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {};
116
117
118 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
119 (
120  const char *text, const char *fileName/*=0*/, 
121  const unsigned int lineNumber/*=0*/ 
122  ) : MEDEXCEPTION(text, fileName, lineNumber) {};
123
124 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){};