1 // Common/StdOutStream.cpp
2
3 #include "StdAfx.h"
4
5 #ifdef _WIN32
6 #include <tchar.h>
7 #endif
8
9 #include "IntToString.h"
10 #include "StdOutStream.h"
11 #include "StringConvert.h"
12 #include "UTFConvert.h"
13
14 #define kFileOpenMode "wt"
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 AString temp;
51 UString s2(s);
52 PrintUString(s2, temp);
53 return *this;
54 }
55
PrintUString(const UString & s,AString & temp)56 void CStdOutStream::PrintUString(const UString &s, AString &temp)
57 {
58 Convert_UString_to_AString(s, temp);
59 *this << (const char *)temp;
60 }
61
Convert_UString_to_AString(const UString & src,AString & dest)62 void CStdOutStream::Convert_UString_to_AString(const UString &src, AString &dest)
63 {
64 int codePage = CodePage;
65 if (codePage == -1)
66 codePage = CP_OEMCP;
67 if (codePage == CP_UTF8)
68 ConvertUnicodeToUTF8(src, dest);
69 else
70 UnicodeStringToMultiByte2(dest, src, (UINT)codePage);
71 }
72
73
74 static const wchar_t kReplaceChar = '_';
75
Normalize_UString__LF_Allowed(UString & s)76 void CStdOutStream::Normalize_UString__LF_Allowed(UString &s)
77 {
78 unsigned len = s.Len();
79 wchar_t *d = s.GetBuf();
80
81 if (IsTerminalMode)
82 for (unsigned i = 0; i < len; i++)
83 {
84 wchar_t c = d[i];
85 if (c <= 13 && c >= 7 && c != '\n')
86 d[i] = kReplaceChar;
87 }
88 }
89
Normalize_UString(UString & s)90 void CStdOutStream::Normalize_UString(UString &s)
91 {
92 unsigned len = s.Len();
93 wchar_t *d = s.GetBuf();
94
95 if (IsTerminalMode)
96 for (unsigned i = 0; i < len; i++)
97 {
98 wchar_t c = d[i];
99 if (c <= 13 && c >= 7)
100 d[i] = kReplaceChar;
101 }
102 else
103 for (unsigned i = 0; i < len; i++)
104 {
105 wchar_t c = d[i];
106 if (c == '\n')
107 d[i] = kReplaceChar;
108 }
109 }
110
NormalizePrint_UString(const UString & s,UString & tempU,AString & tempA)111 void CStdOutStream::NormalizePrint_UString(const UString &s, UString &tempU, AString &tempA)
112 {
113 tempU = s;
114 Normalize_UString(tempU);
115 PrintUString(tempU, tempA);
116 }
117
NormalizePrint_UString(const UString & s)118 void CStdOutStream::NormalizePrint_UString(const UString &s)
119 {
120 NormalizePrint_wstr(s);
121 }
122
NormalizePrint_wstr(const wchar_t * s)123 void CStdOutStream::NormalizePrint_wstr(const wchar_t *s)
124 {
125 UString tempU = s;
126 Normalize_UString(tempU);
127 AString tempA;
128 PrintUString(tempU, tempA);
129 }
130
131
operator <<(Int32 number)132 CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
133 {
134 char s[32];
135 ConvertInt64ToString(number, s);
136 return operator<<(s);
137 }
138
operator <<(Int64 number)139 CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
140 {
141 char s[32];
142 ConvertInt64ToString(number, s);
143 return operator<<(s);
144 }
145
operator <<(UInt32 number)146 CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
147 {
148 char s[16];
149 ConvertUInt32ToString(number, s);
150 return operator<<(s);
151 }
152
operator <<(UInt64 number)153 CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
154 {
155 char s[32];
156 ConvertUInt64ToString(number, s);
157 return operator<<(s);
158 }
159