Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/yacs.git] / src / Utils / Utils_SALOME_Exception.cxx
1 //  SALOME Utils : general SALOME's definitions and tools
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   : Utils_SALOME_Exception.cxx
25 //  Author : Antoine YESSAYAN, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include <iostream>
30 #include "Utils_SALOME_Exception.hxx"
31 #include "utilities.h"
32 extern "C"
33 {
34 #include <math.h>
35 #include <stdio.h>
36 }
37 #include <string>
38 using namespace std;
39
40 const char* duplicate( const char *const str ) ;
41
42 SALOME_Exception::SALOME_Exception( void ): exception() , _text(0)
43 {
44         MESSAGE( "You must use the standard builder: SALOME_Exception::SALOME_Exception( const char *text )" ) ;
45         INTERRUPTION(1) ;
46 }
47
48
49
50 char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
51 {
52         char *newText = 0 ;
53
54         ASSERT(text) ;
55         const size_t l1 = 1+strlen(text) ;
56         ASSERT(l1>1) ;
57
58         const char* prefix = "Salome Exception" ;
59         const size_t l0 = 2+strlen(prefix) ;
60
61         if ( fileName )
62         {
63                 const size_t l2 = 4+strlen(fileName) ;
64                 ASSERT(l2>4) ;
65
66                 ASSERT(lineNumber>=1) ;
67                 const size_t l3 = 4+int(log10(float(lineNumber))) ;
68                 
69                 newText = new char [ 1+l0+l1+l2+l3 ] ;
70                 sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
71         }
72         else
73         {
74                 newText = new char [ 1+l0+l1 ] ;
75                 sprintf( newText , "%s : %s" , prefix, text ) ;
76         }
77         ASSERT(newText) ;
78         return newText ;
79 }
80
81
82 SALOME_Exception::SALOME_Exception( const char *text, const char *fileName, const unsigned int lineNumber ) : exception(), _text( makeText( text , fileName , lineNumber ) )
83 {
84 }
85
86
87 SALOME_Exception::~SALOME_Exception() throw ()
88 {
89         if ( _text )
90         {
91           delete [] _text ;
92           _text = NULL;
93         }
94 }
95
96
97
98 SALOME_Exception::SALOME_Exception( const SALOME_Exception &ex ): _text((char*)duplicate(ex._text))
99 {
100         ;
101 }
102
103
104 ostream & operator<<( ostream &os , const SALOME_Exception &ex )
105 {
106         os << ex._text ;
107         return os ;
108 }
109
110
111
112 const char* SALOME_Exception::what( void ) const throw ()
113 {
114         return _text ;
115 }