xref: /aosp_15_r20/external/lzma/CPP/Common/StdOutStream.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
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 ((unsigned)codePage == CP_UTF8)
70     ConvertUnicodeToUTF8(src, dest);
71   else
72     UnicodeStringToMultiByte2(dest, src, (UINT)(unsigned)codePage);
73 }
74 
75 
76 static const wchar_t kReplaceChar = '_';
77 
78 /*
79 void CStdOutStream::Normalize_UString_LF_Allowed(UString &s)
80 {
81   if (!IsTerminalMode)
82     return;
83 
84   const unsigned len = s.Len();
85   wchar_t *d = s.GetBuf();
86 
87     for (unsigned i = 0; i < len; i++)
88     {
89       const wchar_t c = d[i];
90       if (c == 0x1b || (c <= 13 && c >= 7 && c != '\n'))
91         d[i] = kReplaceChar;
92     }
93 }
94 */
95 
Normalize_UString(UString & s)96 void CStdOutStream::Normalize_UString(UString &s)
97 {
98   const unsigned len = s.Len();
99   wchar_t *d = s.GetBuf();
100 
101   if (IsTerminalMode)
102     for (unsigned i = 0; i < len; i++)
103     {
104       const wchar_t c = d[i];
105       if ((c <= 13 && c >= 7) || c == 0x1b)
106         d[i] = kReplaceChar;
107     }
108   else
109     for (unsigned i = 0; i < len; i++)
110     {
111       const wchar_t c = d[i];
112       if (c == '\n')
113         d[i] = kReplaceChar;
114     }
115 }
116 
Normalize_UString_Path(UString & s)117 void CStdOutStream::Normalize_UString_Path(UString &s)
118 {
119   if (ListPathSeparatorSlash.Def)
120   {
121 #ifdef _WIN32
122     if (ListPathSeparatorSlash.Val)
123       s.Replace(L'\\', L'/');
124 #else
125     if (!ListPathSeparatorSlash.Val)
126       s.Replace(L'/', L'\\');
127 #endif
128   }
129   Normalize_UString(s);
130 }
131 
132 
133 /*
134 void CStdOutStream::Normalize_UString(UString &src)
135 {
136   const wchar_t *s = src.Ptr();
137   const unsigned len = src.Len();
138   unsigned i;
139   for (i = 0; i < len; i++)
140   {
141     const wchar_t c = s[i];
142 #if 0 && !defined(_WIN32)
143     if (c == '\\') // IsTerminalMode &&
144       break;
145 #endif
146     if ((unsigned)c < 0x20)
147       break;
148   }
149   if (i == len)
150     return;
151 
152   UString temp;
153   for (i = 0; i < len; i++)
154   {
155     wchar_t c = s[i];
156 #if 0 && !defined(_WIN32)
157     if (c == '\\')
158       temp += (wchar_t)L'\\';
159     else
160 #endif
161     if ((unsigned)c < 0x20)
162     {
163       if (c == '\n'
164         || (IsTerminalMode && (c == 0x1b || (c <= 13 && c >= 7))))
165       {
166 #if 1 || defined(_WIN32)
167         c = (wchar_t)kReplaceChar;
168 #else
169         temp += (wchar_t)L'\\';
170              if (c == '\n') c = L'n';
171         else if (c == '\r') c = L'r';
172         else if (c == '\a') c = L'a';
173         else if (c == '\b') c = L'b';
174         else if (c == '\t') c = L't';
175         else if (c == '\v') c = L'v';
176         else if (c == '\f') c = L'f';
177         else
178         {
179           temp += (wchar_t)(L'0' + (unsigned)c / 64);
180           temp += (wchar_t)(L'0' + (unsigned)c / 8 % 8);
181           c     = (wchar_t)(L'0' + (unsigned)c % 8);
182         }
183 #endif
184       }
185     }
186     temp += c;
187   }
188   src = temp;
189 }
190 */
191 
NormalizePrint_UString_Path(const UString & s,UString & tempU,AString & tempA)192 void CStdOutStream::NormalizePrint_UString_Path(const UString &s, UString &tempU, AString &tempA)
193 {
194   tempU = s;
195   Normalize_UString_Path(tempU);
196   PrintUString(tempU, tempA);
197 }
198 
NormalizePrint_UString_Path(const UString & s)199 void CStdOutStream::NormalizePrint_UString_Path(const UString &s)
200 {
201   UString tempU;
202   AString tempA;
203   NormalizePrint_UString_Path(s, tempU, tempA);
204 }
205 
NormalizePrint_wstr_Path(const wchar_t * s)206 void CStdOutStream::NormalizePrint_wstr_Path(const wchar_t *s)
207 {
208   UString tempU = s;
209   Normalize_UString_Path(tempU);
210   AString tempA;
211   PrintUString(tempU, tempA);
212 }
213 
NormalizePrint_UString(const UString & s)214 void CStdOutStream::NormalizePrint_UString(const UString &s)
215 {
216   UString tempU = s;
217   Normalize_UString(tempU);
218   AString tempA;
219   PrintUString(tempU, tempA);
220 }
221 
operator <<(Int32 number)222 CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
223 {
224   char s[32];
225   ConvertInt64ToString(number, s);
226   return operator<<(s);
227 }
228 
operator <<(Int64 number)229 CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
230 {
231   char s[32];
232   ConvertInt64ToString(number, s);
233   return operator<<(s);
234 }
235 
operator <<(UInt32 number)236 CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
237 {
238   char s[16];
239   ConvertUInt32ToString(number, s);
240   return operator<<(s);
241 }
242 
operator <<(UInt64 number)243 CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
244 {
245   char s[32];
246   ConvertUInt64ToString(number, s);
247   return operator<<(s);
248 }
249