Upload 2.0.2
[physicsfs] / lzma / CPP / Common / StdOutStream.cpp
1 // Common/StdOutStream.cpp
2
3 #include "StdAfx.h"
4
5 #include <tchar.h>
6
7 #include "StdOutStream.h"
8 #include "IntToString.h"
9 #include "StringConvert.h"
10
11 #ifdef _MSC_VER
12 // "was declared deprecated" disabling
13 #pragma warning(disable : 4996 )
14 #endif
15
16 static const char kNewLineChar =  '\n';
17
18 static const char *kFileOpenMode = "wt";
19
20 CStdOutStream  g_StdOut(stdout);
21 CStdOutStream  g_StdErr(stderr);
22
23 bool CStdOutStream::Open(const char *fileName)
24 {
25   Close();
26   _stream = fopen(fileName, kFileOpenMode);
27   _streamIsOpen = (_stream != 0);
28   return _streamIsOpen;
29 }
30
31 bool CStdOutStream::Close()
32 {
33   if(!_streamIsOpen)
34     return true;
35   if (fclose(_stream) != 0)
36     return false;
37   _stream = 0;
38   _streamIsOpen = false;
39   return true;
40 }
41
42 bool CStdOutStream::Flush()
43 {
44   return (fflush(_stream) == 0);
45 }
46
47 CStdOutStream::~CStdOutStream ()
48 {
49   Close();
50 }
51
52 CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*aFunction)(CStdOutStream  &))
53 {
54   (*aFunction)(*this);    
55   return *this;
56 }
57
58 CStdOutStream & endl(CStdOutStream & outStream)
59 {
60   return outStream << kNewLineChar;
61 }
62
63 CStdOutStream & CStdOutStream::operator<<(const char *string)
64 {
65   fputs(string, _stream);
66   return *this;
67 }
68
69 CStdOutStream & CStdOutStream::operator<<(const wchar_t *string)
70 {
71   *this << (const char *)UnicodeStringToMultiByte(string, CP_OEMCP);
72   return *this;
73 }
74
75 CStdOutStream & CStdOutStream::operator<<(char c)
76 {
77   fputc(c, _stream);
78   return *this;
79 }
80
81 CStdOutStream & CStdOutStream::operator<<(int number)
82 {
83   char textString[32];
84   ConvertInt64ToString(number, textString);
85   return operator<<(textString);
86 }
87
88 CStdOutStream & CStdOutStream::operator<<(UInt64 number)
89 {
90   char textString[32];
91   ConvertUInt64ToString(number, textString);
92   return operator<<(textString);
93 }