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