1 // Common/StdOutStream.cpp
2
3 #include "StdAfx.h"
4
5 #include <tchar.h>
6
7 #include "IntToString.h"
8 #include "StdOutStream.h"
9 #include "StringConvert.h"
10 #include "UTFConvert.h"
11
12 static const char kNewLineChar = '\n';
13
14 static const char *kFileOpenMode = "wt";
15
16 extern int g_CodePage;
17
18 CStdOutStream g_StdOut(stdout);
19 CStdOutStream g_StdErr(stderr);
20
Open(const char * fileName)21 bool CStdOutStream::Open(const char *fileName) throw()
22 {
23 Close();
24 _stream = fopen(fileName, kFileOpenMode);
25 _streamIsOpen = (_stream != 0);
26 return _streamIsOpen;
27 }
28
Close()29 bool CStdOutStream::Close() throw()
30 {
31 if (!_streamIsOpen)
32 return true;
33 if (fclose(_stream) != 0)
34 return false;
35 _stream = 0;
36 _streamIsOpen = false;
37 return true;
38 }
39
Flush()40 bool CStdOutStream::Flush() throw()
41 {
42 return (fflush(_stream) == 0);
43 }
44
endl(CStdOutStream & outStream)45 CStdOutStream & endl(CStdOutStream & outStream) throw()
46 {
47 return outStream << kNewLineChar;
48 }
49
operator <<(const wchar_t * s)50 CStdOutStream & CStdOutStream::operator<<(const wchar_t *s)
51 {
52 int codePage = g_CodePage;
53 if (codePage == -1)
54 codePage = CP_OEMCP;
55 AString dest;
56 if (codePage == CP_UTF8)
57 ConvertUnicodeToUTF8(s, dest);
58 else
59 UnicodeStringToMultiByte2(dest, s, (UINT)codePage);
60 return operator<<((const char *)dest);
61 }
62
StdOut_Convert_UString_to_AString(const UString & s,AString & temp)63 void StdOut_Convert_UString_to_AString(const UString &s, AString &temp)
64 {
65 int codePage = g_CodePage;
66 if (codePage == -1)
67 codePage = CP_OEMCP;
68 if (codePage == CP_UTF8)
69 ConvertUnicodeToUTF8(s, temp);
70 else
71 UnicodeStringToMultiByte2(temp, s, (UINT)codePage);
72 }
73
PrintUString(const UString & s,AString & temp)74 void CStdOutStream::PrintUString(const UString &s, AString &temp)
75 {
76 StdOut_Convert_UString_to_AString(s, temp);
77 *this << (const char *)temp;
78 }
79
operator <<(Int32 number)80 CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
81 {
82 char s[32];
83 ConvertInt64ToString(number, s);
84 return operator<<(s);
85 }
86
operator <<(Int64 number)87 CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
88 {
89 char s[32];
90 ConvertInt64ToString(number, s);
91 return operator<<(s);
92 }
93
operator <<(UInt32 number)94 CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
95 {
96 char s[16];
97 ConvertUInt32ToString(number, s);
98 return operator<<(s);
99 }
100
operator <<(UInt64 number)101 CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
102 {
103 char s[32];
104 ConvertUInt64ToString(number, s);
105 return operator<<(s);
106 }
107