mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-07 16:07:05 -06:00
121 lines
2.3 KiB
C++
121 lines
2.3 KiB
C++
// Common/StdOutStream.cpp
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include <tchar.h>
|
|
|
|
#include "IntToString.h"
|
|
#include "StdOutStream.h"
|
|
#include "StringConvert.h"
|
|
#include "UTFConvert.h"
|
|
|
|
static const char kNewLineChar = '\n';
|
|
|
|
static const char *kFileOpenMode = "wt";
|
|
|
|
extern int g_CodePage;
|
|
|
|
CStdOutStream g_StdOut(stdout);
|
|
CStdOutStream g_StdErr(stderr);
|
|
|
|
bool CStdOutStream::Open(const char *fileName)
|
|
{
|
|
Close();
|
|
_stream = fopen(fileName, kFileOpenMode);
|
|
_streamIsOpen = (_stream != 0);
|
|
return _streamIsOpen;
|
|
}
|
|
|
|
bool CStdOutStream::Close()
|
|
{
|
|
if (!_streamIsOpen)
|
|
return true;
|
|
if (fclose(_stream) != 0)
|
|
return false;
|
|
_stream = 0;
|
|
_streamIsOpen = false;
|
|
return true;
|
|
}
|
|
|
|
bool CStdOutStream::Flush()
|
|
{
|
|
return (fflush(_stream) == 0);
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*func)(CStdOutStream &))
|
|
{
|
|
(*func)(*this);
|
|
return *this;
|
|
}
|
|
|
|
CStdOutStream & endl(CStdOutStream & outStream)
|
|
{
|
|
return outStream << kNewLineChar;
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(const char *s)
|
|
{
|
|
fputs(s, _stream);
|
|
return *this;
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(const wchar_t *s)
|
|
{
|
|
int codePage = g_CodePage;
|
|
if (codePage == -1)
|
|
codePage = CP_OEMCP;
|
|
AString dest;
|
|
if (codePage == CP_UTF8)
|
|
ConvertUnicodeToUTF8(s, dest);
|
|
else
|
|
dest = UnicodeStringToMultiByte(s, (UINT)codePage);
|
|
*this << (const char *)dest;
|
|
return *this;
|
|
}
|
|
|
|
void CStdOutStream::PrintUString(const UString &s, AString &temp)
|
|
{
|
|
int codePage = g_CodePage;
|
|
if (codePage == -1)
|
|
codePage = CP_OEMCP;
|
|
if (codePage == CP_UTF8)
|
|
ConvertUnicodeToUTF8(s, temp);
|
|
else
|
|
UnicodeStringToMultiByte2(temp, s, (UINT)codePage);
|
|
*this << (const char *)temp;
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(char c)
|
|
{
|
|
fputc(c, _stream);
|
|
return *this;
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(Int32 number)
|
|
{
|
|
char s[32];
|
|
ConvertInt64ToString(number, s);
|
|
return operator<<(s);
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(Int64 number)
|
|
{
|
|
char s[32];
|
|
ConvertInt64ToString(number, s);
|
|
return operator<<(s);
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(UInt32 number)
|
|
{
|
|
char s[16];
|
|
ConvertUInt32ToString(number, s);
|
|
return operator<<(s);
|
|
}
|
|
|
|
CStdOutStream & CStdOutStream::operator<<(UInt64 number)
|
|
{
|
|
char s[32];
|
|
ConvertUInt64ToString(number, s);
|
|
return operator<<(s);
|
|
}
|