Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM / MEDMEM_Exception.cxx
1 // Copyright (C) 2007-2012  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 /*
24  File MedException.cxx
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 namespace
68 {
69 /*!
70   \internal
71   Function used to elaborate the text of the MEDEXCEPTION
72 */
73 // --------------------------------------------------------------------------------------- //
74 char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
75 // --------------------------------------------------------------------------------------- //
76 {
77   char *newText = 0 ;
78
79   ASSERT_MED(text) ;
80   const size_t l1 = strlen(text) ;
81
82   const char* prefix = "MED Exception" ;
83   const size_t l0 = strlen(prefix) ;
84
85   if ( fileName )
86   {
87       const size_t l2 = strlen(fileName) ;
88
89       ASSERT_MED(lineNumber>=1) ;
90       const size_t l3 = 1+int(log10(float(lineNumber))) ;
91         
92       const size_t l4 =  l0+l1+l2+l3+10+1 ;
93       newText = new char [ l4 ] ;
94       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
95       ASSERT_MED(newText[l4-1] == '\0' );
96
97   }
98   else
99   {
100       newText = new char [ l0+l1+3+1 ] ;
101       sprintf( newText , "%s : %s" , prefix, text ) ;
102    }
103   ASSERT_MED(newText) ;
104   return newText ;
105 }
106 }
107
108 /*!
109     Constructor : \n
110     It will create the text of the MEDEXCEPTION from the different parameters.
111     It will take the form : \n
112     MEDEXCEPTION, fileName, lineNumber and text of the exception
113 */
114 // ------------------------------------------------------------------------------------------------ //
115 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : 
116               exception(), _text( makeText( text , fileName , lineNumber ) )
117 // ------------------------------------------------------------------------------------------------ //
118 {
119   MESSAGE_MED(_text);
120 }
121
122 /*!
123   Destructor : \n
124   If necessary desallocates Memory
125 */
126
127 // ------------------------------------//
128 MEDEXCEPTION::~MEDEXCEPTION() throw ()
129 // ------------------------------------//
130 {
131   if ( _text )
132     {
133       delete [] _text ;
134       _text = 0 ;
135     }
136   ASSERT_MED(_text==NULL) ;
137 }
138
139
140 /*!
141   Copy Constructor : \n
142   Should not be used very often
143 */
144 // ----------------------------------------------------------------------- //
145 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
146 // ----------------------------------------------------------------------- //
147 {
148   ;
149 }
150 /*!
151   Operator << : put the message to the given stream.
152 */
153 // ------------------------------------------------------- //
154 ostream & MEDMEM::operator<<( ostream &os , const MEDEXCEPTION &ex )
155 // ------------------------------------------------------- //
156 {
157   os << ex._text ;
158   return os ;
159 }
160
161 /*!
162   Return a char * which contain the message.
163 */
164 // ------------------------------------------------- //
165 const char* MEDEXCEPTION::what( void ) const throw ()
166 // ------------------------------------------------- //
167 {
168   return _text ;
169 }
170
171
172 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
173
174
175 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {}
176
177
178 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
179 (
180  const char *text, const char *fileName/*=0*/, 
181  const unsigned int lineNumber/*=0*/ 
182  ) : MEDEXCEPTION(text, fileName, lineNumber) {}
183
184 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){}