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 CStdOutStream g_StdOut(stdout);
15 CStdOutStream g_StdErr(stderr);
16
17 /*
18 // #define kFileOpenMode "wt"
19
20 bool CStdOutStream::Open(const char *fileName) throw()
21 {
22 Close();
23 _stream = fopen(fileName, kFileOpenMode);
24 _streamIsOpen = (_stream != 0);
25 return _streamIsOpen;
26 }
27
28 bool CStdOutStream::Close() throw()
29 {
30 if (!_streamIsOpen)
31 return true;
32 if (fclose(_stream) != 0)
33 return false;
34 _stream = 0;
35 _streamIsOpen = false;
36 return true;
37 }
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 << '\n';
48 }
49
operator <<(const wchar_t * s)50 CStdOutStream & CStdOutStream::operator<<(const wchar_t *s)
51 {
52 AString temp;
53 UString s2(s);
54 PrintUString(s2, temp);
55 return *this;
56 }
57
PrintUString(const UString & s,AString & temp)58 void CStdOutStream::PrintUString(const UString &s, AString &temp)
59 {
60 Convert_UString_to_AString(s, temp);
61 *this << (const char *)temp;
62 }
63
Convert_UString_to_AString(const UString & src,AString & dest)64 void CStdOutStream::Convert_UString_to_AString(const UString &src, AString &dest)
65 {
66 int codePage = CodePage;
67 if (codePage == -1)
68 codePage = CP_OEMCP;
69 if (codePage == CP_UTF8)
70 ConvertUnicodeToUTF8(src, dest);
71 else
72 UnicodeStringToMultiByte2(dest, src, (UINT)codePage);
73 }
74
75
76 static const wchar_t kReplaceChar = '_';
77
Normalize_UString_LF_Allowed(UString & s)78 void CStdOutStream::Normalize_UString_LF_Allowed(UString &s)
79 {
80 unsigned len = s.Len();
81 wchar_t *d = s.GetBuf();
82
83 if (IsTerminalMode)
84 for (unsigned i = 0; i < len; i++)
85 {
86 wchar_t c = d[i];
87 if (c <= 13 && c >= 7 && c != '\n')
88 d[i] = kReplaceChar;
89 }
90 }
91
Normalize_UString(UString & s)92 void CStdOutStream::Normalize_UString(UString &s)
93 {
94 unsigned len = s.Len();
95 wchar_t *d = s.GetBuf();
96
97 if (IsTerminalMode)
98 for (unsigned i = 0; i < len; i++)
99 {
100 wchar_t c = d[i];
101 if (c <= 13 && c >= 7)
102 d[i] = kReplaceChar;
103 }
104 else
105 for (unsigned i = 0; i < len; i++)
106 {
107 wchar_t c = d[i];
108 if (c == '\n')
109 d[i] = kReplaceChar;
110 }
111 }
112
NormalizePrint_UString(const UString & s,UString & tempU,AString & tempA)113 void CStdOutStream::NormalizePrint_UString(const UString &s, UString &tempU, AString &tempA)
114 {
115 tempU = s;
116 Normalize_UString(tempU);
117 PrintUString(tempU, tempA);
118 }
119
NormalizePrint_UString(const UString & s)120 void CStdOutStream::NormalizePrint_UString(const UString &s)
121 {
122 NormalizePrint_wstr(s);
123 }
124
NormalizePrint_wstr(const wchar_t * s)125 void CStdOutStream::NormalizePrint_wstr(const wchar_t *s)
126 {
127 UString tempU = s;
128 Normalize_UString(tempU);
129 AString tempA;
130 PrintUString(tempU, tempA);
131 }
132
133
operator <<(Int32 number)134 CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
135 {
136 char s[32];
137 ConvertInt64ToString(number, s);
138 return operator<<(s);
139 }
140
operator <<(Int64 number)141 CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
142 {
143 char s[32];
144 ConvertInt64ToString(number, s);
145 return operator<<(s);
146 }
147
operator <<(UInt32 number)148 CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
149 {
150 char s[16];
151 ConvertUInt32ToString(number, s);
152 return operator<<(s);
153 }
154
operator <<(UInt64 number)155 CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
156 {
157 char s[32];
158 ConvertUInt64ToString(number, s);
159 return operator<<(s);
160 }
161