0024792: Remove unused hacks for compilers without STL
[occt.git] / src / Message / Message_PrinterOStream.cxx
1 // Created on: 2001-01-06
2 // Created by: OCC Team
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Message_PrinterOStream.ixx>
17
18 #include <Message_Gravity.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TCollection_ExtendedString.hxx>
21 #include <Standard_Mutex.hxx>
22 #include <Standard_Stream.hxx>
23
24 //=======================================================================
25 //function : Constructor
26 //purpose  : Empty constructor, defaulting to cerr
27 //=======================================================================
28
29 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
30 : myStream  (&std::cout),
31   myIsFile  (Standard_False),
32   myUseUtf8 (Standard_False)
33 {
34   myTraceLevel = theTraceLevel;
35 }
36
37 //=======================================================================
38 //function : Constructor
39 //purpose  : Opening a file as an ostream
40 //           for specific file names standard streams are created
41 //=======================================================================
42 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
43                                                 const Standard_Boolean theToAppend,
44                                                 const Message_Gravity  theTraceLevel)
45 : myStream (&std::cout),
46   myIsFile (Standard_False)
47 {
48   myTraceLevel = theTraceLevel;
49   if (strcasecmp(theFileName, "cout") == 0)
50   {
51     myStream = &std::cerr;
52     return;
53   }
54   else if (strcasecmp(theFileName, "cerr") == 0)
55   {
56     myStream = &std::cout;
57     return;
58   }
59
60   TCollection_AsciiString aFileName (theFileName);
61 #ifdef _WIN32
62   aFileName.ChangeAll ('/', '\\');
63 #endif
64
65   std::ofstream* aFile = new std::ofstream (aFileName.ToCString(),
66                                             (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
67   if (aFile->is_open())
68   {
69     myStream = (Standard_OStream* )aFile;
70     myIsFile = Standard_True;
71   }
72   else
73   {
74     delete aFile;
75     myStream = &std::cout;
76     std::cerr << "Error opening " << theFileName << std::endl << std::flush;
77   }
78 }
79
80 //=======================================================================
81 //function : Close
82 //purpose  : 
83 //=======================================================================
84
85 void Message_PrinterOStream::Close ()
86 {
87   if ( ! myStream ) return;
88   Standard_OStream* ostr = (Standard_OStream*)myStream;
89   myStream = 0;
90
91   ostr->flush();
92   if ( myIsFile )
93   {
94     std::ofstream* ofile = (std::ofstream* )ostr;
95     ofile->close();
96     delete ofile;
97     myIsFile = Standard_False;
98   }
99 }
100
101 //=======================================================================
102 //function : Send
103 //purpose  : 
104 //=======================================================================
105
106 void Message_PrinterOStream::Send (const Standard_CString theString,
107                                    const Message_Gravity theGravity,
108                                    const Standard_Boolean putEndl) const
109 {
110   if ( theGravity < myTraceLevel || ! myStream ) return;
111   Standard_OStream* ostr = (Standard_OStream*)myStream;
112   (*ostr) << theString;
113   if ( putEndl ) (*ostr) << endl;
114 }
115
116 //=======================================================================
117 //function : Send
118 //purpose  : 
119 //=======================================================================
120
121 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
122                                    const Message_Gravity theGravity,
123                                    const Standard_Boolean putEndl) const
124 {
125   Send ( theString.ToCString(), theGravity, putEndl );
126 }
127
128 //=======================================================================
129 //function : Send
130 //purpose  : 
131 //=======================================================================
132
133 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
134                                    const Message_Gravity theGravity,
135                                    const Standard_Boolean putEndl) const
136 {
137   // Note: the string might need to be converted to Ascii
138   if ( myUseUtf8 ) {
139     char* astr = new char[theString.LengthOfCString()+1];
140     theString.ToUTF8CString (astr);
141     Send ( astr, theGravity, putEndl );
142     delete [] astr;
143     astr = 0;
144   }
145   else {
146     TCollection_AsciiString aStr ( theString, '?' );
147     Send ( aStr.ToCString(), theGravity, putEndl );
148   }
149 }