aboutsummaryrefslogtreecommitdiff
path: root/CPP/Common/StdOutStream.cpp
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2021-12-27 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2022-03-18 15:35:13 +0500
commitf19f813537c7aea1c20749c914e756b54a9c3cf5 (patch)
tree816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /CPP/Common/StdOutStream.cpp
parent98e06a519b63b81986abe76d28887f6984a7732b (diff)
download7zip-21.07.tar.gz
7zip-21.07.tar.bz2
7zip-21.07.zip
'21.07'21.07
Diffstat (limited to 'CPP/Common/StdOutStream.cpp')
-rw-r--r--CPP/Common/StdOutStream.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/CPP/Common/StdOutStream.cpp b/CPP/Common/StdOutStream.cpp
new file mode 100644
index 0000000..40799e2
--- /dev/null
+++ b/CPP/Common/StdOutStream.cpp
@@ -0,0 +1,158 @@
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
16CStdOutStream g_StdOut(stdout);
17CStdOutStream g_StdErr(stderr);
18
19bool CStdOutStream::Open(const char *fileName) throw()
20{
21 Close();
22 _stream = fopen(fileName, kFileOpenMode);
23 _streamIsOpen = (_stream != 0);
24 return _streamIsOpen;
25}
26
27bool 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
38bool CStdOutStream::Flush() throw()
39{
40 return (fflush(_stream) == 0);
41}
42
43CStdOutStream & endl(CStdOutStream & outStream) throw()
44{
45 return outStream << '\n';
46}
47
48CStdOutStream & CStdOutStream::operator<<(const wchar_t *s)
49{
50 AString temp;
51 UString s2(s);
52 PrintUString(s2, temp);
53 return *this;
54}
55
56void CStdOutStream::PrintUString(const UString &s, AString &temp)
57{
58 Convert_UString_to_AString(s, temp);
59 *this << (const char *)temp;
60}
61
62void 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
74static const wchar_t kReplaceChar = '_';
75
76void 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
90void 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
111void CStdOutStream::NormalizePrint_UString(const UString &s, UString &tempU, AString &tempA)
112{
113 tempU = s;
114 Normalize_UString(tempU);
115 PrintUString(tempU, tempA);
116}
117
118void CStdOutStream::NormalizePrint_UString(const UString &s)
119{
120 NormalizePrint_wstr(s);
121}
122
123void 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
132CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
133{
134 char s[32];
135 ConvertInt64ToString(number, s);
136 return operator<<(s);
137}
138
139CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
140{
141 char s[32];
142 ConvertInt64ToString(number, s);
143 return operator<<(s);
144}
145
146CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
147{
148 char s[16];
149 ConvertUInt32ToString(number, s);
150 return operator<<(s);
151}
152
153CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
154{
155 char s[32];
156 ConvertUInt64ToString(number, s);
157 return operator<<(s);
158}