StdOutStream.cpp revision baa3858d3f5d128a5c8466b700098109edcad5f2
1// Common/StdOutStream.cpp 2 3#include "StdAfx.h" 4 5#include <tchar.h> 6 7#include "IntToString.h" 8#include "StdOutStream.h" 9#include "StringConvert.h" 10#include "UTFConvert.h" 11 12#ifdef _MSC_VER 13// "was declared deprecated" disabling 14#pragma warning(disable : 4996 ) 15#endif 16 17static const char kNewLineChar = '\n'; 18 19static const char *kFileOpenMode = "wt"; 20 21extern int g_CodePage; 22 23CStdOutStream g_StdOut(stdout); 24CStdOutStream g_StdErr(stderr); 25 26bool CStdOutStream::Open(const char *fileName) 27{ 28 Close(); 29 _stream = fopen(fileName, kFileOpenMode); 30 _streamIsOpen = (_stream != 0); 31 return _streamIsOpen; 32} 33 34bool CStdOutStream::Close() 35{ 36 if (!_streamIsOpen) 37 return true; 38 if (fclose(_stream) != 0) 39 return false; 40 _stream = 0; 41 _streamIsOpen = false; 42 return true; 43} 44 45bool CStdOutStream::Flush() 46{ 47 return (fflush(_stream) == 0); 48} 49 50CStdOutStream::~CStdOutStream () 51{ 52 Close(); 53} 54 55CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*aFunction)(CStdOutStream &)) 56{ 57 (*aFunction)(*this); 58 return *this; 59} 60 61CStdOutStream & endl(CStdOutStream & outStream) 62{ 63 return outStream << kNewLineChar; 64} 65 66CStdOutStream & CStdOutStream::operator<<(const char *s) 67{ 68 fputs(s, _stream); 69 return *this; 70} 71 72CStdOutStream & CStdOutStream::operator<<(const wchar_t *s) 73{ 74 int codePage = g_CodePage; 75 if (codePage == -1) 76 codePage = CP_OEMCP; 77 AString dest; 78 if (codePage == CP_UTF8) 79 ConvertUnicodeToUTF8(s, dest); 80 else 81 dest = UnicodeStringToMultiByte(s, (UINT)codePage); 82 *this << (const char *)dest; 83 return *this; 84} 85 86CStdOutStream & CStdOutStream::operator<<(char c) 87{ 88 fputc(c, _stream); 89 return *this; 90} 91 92CStdOutStream & CStdOutStream::operator<<(int number) 93{ 94 char textString[32]; 95 ConvertInt64ToString(number, textString); 96 return operator<<(textString); 97} 98 99CStdOutStream & CStdOutStream::operator<<(UInt64 number) 100{ 101 char textString[32]; 102 ConvertUInt64ToString(number, textString); 103 return operator<<(textString); 104} 105