Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_Exception.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  File MedException.cxx
24  $Header$
25 */
26
27 #include "MEDMEM_Utilities.hxx"
28 #include "MEDMEM_Exception.hxx"
29
30 using namespace std;
31 using namespace MEDMEM;
32
33 extern "C"
34 {
35 #include <math.h>
36 #include <stdio.h>
37 #include <string.h>
38 }
39
40 /*!
41   \internal
42   Function used to duplicate char * 
43 */
44 char* duplicate( const char *const str ) ;
45 char* duplicate( const char *const str )
46 {
47         ASSERT_MED(str!=NULL) ;
48         const size_t length = strlen( str ) ;
49         ASSERT_MED(length>0) ;
50         char *new_str = new char[ 1+length ] ;
51         ASSERT_MED(new_str) ;
52         strcpy( new_str , str ) ;
53         return new_str ;
54 }
55
56 /*!
57   \internal Default Constructor (Should not be used)
58 */
59 // ------------------------------------------------------ //
60 MEDEXCEPTION::MEDEXCEPTION( void ): exception() , _text(0)
61 // ------------------------------------------------------ //
62 {
63   MESSAGE_MED( "You must user the standard builder : MEDEXCEPTION::MEDEXCEPTION( const char *text )" ) ;
64   INTERRUPTION_MED(1) ;
65 }
66
67
68 /*!
69   \internal
70   Function used to elaborate the text of the MEDEXCEPTION
71 */
72 // --------------------------------------------------------------------------------------- //
73 char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
74 // --------------------------------------------------------------------------------------- //
75 {
76   char *newText = 0 ;
77
78   ASSERT_MED(text) ;
79   const size_t l1 = strlen(text) ;
80
81   const char* prefix = "MED Exception" ;
82   const size_t l0 = strlen(prefix) ;
83
84   if ( fileName )
85   {
86       const size_t l2 = strlen(fileName) ;
87
88       ASSERT_MED(lineNumber>=1) ;
89       const size_t l3 = 1+int(log10(float(lineNumber))) ;
90         
91       const size_t l4 =  l0+l1+l2+l3+10+1 ;
92       newText = new char [ l4 ] ;
93       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
94       ASSERT_MED(newText[l4-1] == '\0' );
95
96   }
97   else
98   {
99       newText = new char [ l0+l1+3+1 ] ;
100       sprintf( newText , "%s : %s" , prefix, text ) ;
101    }
102   ASSERT_MED(newText) ;
103   return newText ;
104 }
105
106 /*!
107     Constructor : \n
108     It will create the text of the MEDEXCEPTION from the different parameters.
109     It will take the form : \n
110     MEDEXCEPTION, fileName, lineNumber and text of the exception
111 */
112 // ------------------------------------------------------------------------------------------------ //
113 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : 
114               exception(), _text( makeText( text , fileName , lineNumber ) )
115 // ------------------------------------------------------------------------------------------------ //
116 {
117   MESSAGE_MED(_text);
118 }
119
120 /*!
121   Destructor : \n
122   If necessary desallocates Memory
123 */
124
125 // ------------------------------------//
126 MEDEXCEPTION::~MEDEXCEPTION() throw ()
127 // ------------------------------------//
128 {
129   if ( _text )
130     {
131       delete [] _text ;
132       _text = 0 ;
133     }
134   ASSERT_MED(_text==NULL) ;
135 }
136
137
138 /*!
139   Copy Constructor : \n
140   Should not be used very often
141 */
142 // ----------------------------------------------------------------------- //
143 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
144 // ----------------------------------------------------------------------- //
145 {
146   ;
147 }
148 /*!
149   Operator << : put the message to the given stream.
150 */
151 // ------------------------------------------------------- //
152 ostream & MEDMEM::operator<<( ostream &os , const MEDEXCEPTION &ex )
153 // ------------------------------------------------------- //
154 {
155   os << ex._text ;
156   return os ;
157 }
158
159 /*!
160   Return a char * which contain the message.
161 */
162 // ------------------------------------------------- //
163 const char* MEDEXCEPTION::what( void ) const throw ()
164 // ------------------------------------------------- //
165 {
166   return _text ;
167 }
168
169
170 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
171
172
173 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {};
174
175
176 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
177 (
178  const char *text, const char *fileName/*=0*/, 
179  const unsigned int lineNumber/*=0*/ 
180  ) : MEDEXCEPTION(text, fileName, lineNumber) {};
181
182 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){};