Back to index

salome-kernel  6.5.0
Utils_SALOME_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 //  SALOME Utils : general SALOME's definitions and tools
00024 //  File   : Utils_SALOME_Exception.cxx
00025 //  Author : Antoine YESSAYAN, EDF
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Utils/Utils_SALOME_Exception.cxx,v 1.10.2.1.10.2.12.1 2012-04-12 14:05:32 vsr Exp $
00028 //
00029 #include <iostream>
00030 #include "Utils_SALOME_Exception.hxx"
00031 #include "utilities.h"
00032 
00033 #ifndef WIN32
00034 extern "C"
00035 {
00036 #endif
00037 #include <math.h>
00038 #include <stdio.h>
00039 #include <string.h>
00040 #ifndef WIN32
00041 }
00042 #endif
00043 
00044 
00045 const char* duplicate( const char *const str ) ;
00046 
00047 SALOME_Exception::SALOME_Exception( void ): exception() , _text(0)
00048 {
00049         MESSAGE( "You must use the standard builder: SALOME_Exception::SALOME_Exception( const char *text )" ) ;
00050         INTERRUPTION(1) ;
00051 }
00052 
00053 
00054 
00055 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
00056 {
00057         char *newText = 0 ;
00058 
00059         ASSERT(text) ;
00060         const size_t l1 = 1+strlen(text) ;
00061         ASSERT(l1>1) ;
00062 
00063         const char* prefix = "Salome Exception" ;
00064         const size_t l0 = 2+strlen(prefix) ;
00065 
00066         if ( fileName )
00067         {
00068                 const size_t l2 = 4+strlen(fileName) ;
00069                 ASSERT(l2>4) ;
00070 
00071                 ASSERT(lineNumber>=1) ;
00072                 const size_t l3 = 4+int(log10(float(lineNumber))) ;
00073                 
00074                 newText = new char [ 1+l0+l1+l2+l3 ] ;
00075                 sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
00076         }
00077         else
00078         {
00079                 newText = new char [ 1+l0+l1 ] ;
00080                 sprintf( newText , "%s : %s" , prefix, text ) ;
00081         }
00082         ASSERT(newText) ;
00083         return newText ;
00084 }
00085 
00086 
00087 SALOME_Exception::SALOME_Exception( const char *text, const char *fileName, const unsigned int lineNumber ) : exception(), _text( makeText( text , fileName , lineNumber ) )
00088 {
00089 }
00090 
00091 
00092 SALOME_Exception::~SALOME_Exception() throw ()
00093 {
00094         if ( _text )
00095         {
00096                 delete [] ((char*)_text);
00097                 char** pRef = (char**)&_text;
00098                 *pRef = 0;
00099         }
00100         ASSERT(_text==NULL) ;
00101 }
00102 
00103 
00104 
00105 SALOME_Exception::SALOME_Exception( const SALOME_Exception &ex ): _text(duplicate(ex._text))
00106 {
00107         ;
00108 }
00109 
00110 
00111 std::ostream & operator<<( std::ostream &os , const SALOME_Exception &ex )
00112 {
00113         os << ex._text ;
00114         return os ;
00115 }
00116 
00117 
00118 
00119 const char* SALOME_Exception::what( void ) const throw ()
00120 {
00121         return _text ;
00122 }