Back to index

salome-med  6.5.0
MEDMEM_Exception.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 /*
00024  File MedException.cxx
00025 */
00026 
00027 #include "MEDMEM_Utilities.hxx"
00028 #include "MEDMEM_Exception.hxx"
00029 
00030 using namespace std;
00031 using namespace MEDMEM;
00032 
00033 extern "C"
00034 {
00035 #include <math.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 }
00039 
00044 char* duplicate( const char *const str ) ;
00045 char* duplicate( const char *const str )
00046 {
00047         ASSERT_MED(str!=NULL) ;
00048         const size_t length = strlen( str ) ;
00049         ASSERT_MED(length>0) ;
00050         char *new_str = new char[ 1+length ] ;
00051         ASSERT_MED(new_str) ;
00052         strcpy( new_str , str ) ;
00053         return new_str ;
00054 }
00055 
00059 // ------------------------------------------------------ //
00060 MEDEXCEPTION::MEDEXCEPTION( void ): exception() , _text(0)
00061 // ------------------------------------------------------ //
00062 {
00063   MESSAGE_MED( "You must user the standard builder : MEDEXCEPTION::MEDEXCEPTION( const char *text )" ) ;
00064   INTERRUPTION_MED(1) ;
00065 }
00066 
00067 namespace
00068 {
00073 // --------------------------------------------------------------------------------------- //
00074 char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
00075 // --------------------------------------------------------------------------------------- //
00076 {
00077   char *newText = 0 ;
00078 
00079   ASSERT_MED(text) ;
00080   const size_t l1 = strlen(text) ;
00081 
00082   const char* prefix = "MED Exception" ;
00083   const size_t l0 = strlen(prefix) ;
00084 
00085   if ( fileName )
00086   {
00087       const size_t l2 = strlen(fileName) ;
00088 
00089       ASSERT_MED(lineNumber>=1) ;
00090       const size_t l3 = 1+int(log10(float(lineNumber))) ;
00091         
00092       const size_t l4 =  l0+l1+l2+l3+10+1 ;
00093       newText = new char [ l4 ] ;
00094       sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
00095       ASSERT_MED(newText[l4-1] == '\0' );
00096 
00097   }
00098   else
00099   {
00100       newText = new char [ l0+l1+3+1 ] ;
00101       sprintf( newText , "%s : %s" , prefix, text ) ;
00102    }
00103   ASSERT_MED(newText) ;
00104   return newText ;
00105 }
00106 }
00107 
00114 // ------------------------------------------------------------------------------------------------ //
00115 MEDEXCEPTION::MEDEXCEPTION( const char *text, const char *fileName, const unsigned int lineNumber ) : 
00116               exception(), _text( makeText( text , fileName , lineNumber ) )
00117 // ------------------------------------------------------------------------------------------------ //
00118 {
00119   MESSAGE_MED(_text);
00120 }
00121 
00127 // ------------------------------------//
00128 MEDEXCEPTION::~MEDEXCEPTION() throw ()
00129 // ------------------------------------//
00130 {
00131   if ( _text )
00132     {
00133       delete [] _text ;
00134       _text = 0 ;
00135     }
00136   ASSERT_MED(_text==NULL) ;
00137 }
00138 
00139 
00144 // ----------------------------------------------------------------------- //
00145 MEDEXCEPTION::MEDEXCEPTION( const MEDEXCEPTION &ex ): _text(duplicate(ex._text))
00146 // ----------------------------------------------------------------------- //
00147 {
00148   ;
00149 }
00153 // ------------------------------------------------------- //
00154 ostream & MEDMEM::operator<<( ostream &os , const MEDEXCEPTION &ex )
00155 // ------------------------------------------------------- //
00156 {
00157   os << ex._text ;
00158   return os ;
00159 }
00160 
00164 // ------------------------------------------------- //
00165 const char* MEDEXCEPTION::what( void ) const throw ()
00166 // ------------------------------------------------- //
00167 {
00168   return _text ;
00169 }
00170 
00171 
00172 // -------------------- class MED_DRIVER_NOT_FOUND_EXCEPTION
00173 
00174 
00175 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION(const MED_DRIVER_NOT_FOUND_EXCEPTION &ex ):MEDEXCEPTION ( ex ) {}
00176 
00177 
00178 MED_DRIVER_NOT_FOUND_EXCEPTION::MED_DRIVER_NOT_FOUND_EXCEPTION
00179 (
00180  const char *text, const char *fileName/*=0*/, 
00181  const unsigned int lineNumber/*=0*/ 
00182  ) : MEDEXCEPTION(text, fileName, lineNumber) {}
00183 
00184 MED_DRIVER_NOT_FOUND_EXCEPTION::~MED_DRIVER_NOT_FOUND_EXCEPTION() throw (){}