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