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