Salome HOME
NRI : Comment unused ComponentType enum.
[modules/kernel.git] / src / Utils / Utils_SALOME_Exception.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : Utils_SALOME_Exception.cxx
4 // Created   : Mon Nov  5 17:01:47 CET 2001
5 // Author    : Antoine YESSAYAN, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2001
8 // $Header$
9 //=============================================================================
10
11 using namespace std;
12 #include <iostream>
13 #include "Utils_SALOME_Exception.hxx"
14 #include "utilities.h"
15 extern "C"
16 {
17 #include <math.h>
18 #include <stdio.h>
19 #include <string.h>
20 }
21
22
23 const char* duplicate( const char *const str ) ;
24
25 SALOME_Exception::SALOME_Exception( void ): exception() , _text(0)
26 {
27         MESSAGE( "You must user the standard builder : SALOME_Exception::SALOME_Exception( const char *text )" ) ;
28         INTERRUPTION(1) ;
29 }
30
31
32
33 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber )
34 {
35         char *newText = 0 ;
36
37         ASSERT(text) ;
38         const size_t l1 = 1+strlen(text) ;
39         ASSERT(l1>1) ;
40
41         const char* prefix = "Salome Exception" ;
42         const size_t l0 = 2+strlen(prefix) ;
43
44         if ( fileName )
45         {
46                 const size_t l2 = 4+strlen(fileName) ;
47                 ASSERT(l2>4) ;
48
49                 ASSERT(lineNumber>=1) ;
50                 const size_t l3 = 4+int(log10(float(lineNumber))) ;
51                 
52                 newText = new char [ 1+l0+l1+l2+l3 ] ;
53                 sprintf( newText , "%s in %s [%u] : %s" , prefix, fileName, lineNumber, text ) ;
54         }
55         else
56         {
57                 newText = new char [ 1+l0+l1 ] ;
58                 sprintf( newText , "%s : %s" , prefix, text ) ;
59         }
60         ASSERT(newText) ;
61         return newText ;
62 }
63
64
65 SALOME_Exception::SALOME_Exception( const char *text, const char *fileName, const unsigned int lineNumber ) : exception(), _text( makeText( text , fileName , lineNumber ) )
66 {
67 }
68
69
70 SALOME_Exception::~SALOME_Exception() throw ()
71 {
72         if ( _text )
73         {
74                 delete [] _text ;
75                 char *& txt = (char*)_text ;
76                 txt = 0 ;
77         }
78         ASSERT(_text==NULL) ;
79 }
80
81
82
83 SALOME_Exception::SALOME_Exception( const SALOME_Exception &ex ): _text(duplicate(ex._text))
84 {
85         ;
86 }
87
88
89 ostream & operator<<( ostream &os , const SALOME_Exception &ex )
90 {
91         os << ex._text ;
92         return os ;
93 }
94
95
96
97 const char* SALOME_Exception::what( void ) const throw ()
98 {
99         return _text ;
100 }