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