Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Common/StdOutStream.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include <tchar.h> |
michael@0 | 6 | |
michael@0 | 7 | #include "StdOutStream.h" |
michael@0 | 8 | #include "Common/IntToString.h" |
michael@0 | 9 | #include "Common/StringConvert.h" |
michael@0 | 10 | |
michael@0 | 11 | static const char kNewLineChar = '\n'; |
michael@0 | 12 | |
michael@0 | 13 | static const char *kFileOpenMode = "wt"; |
michael@0 | 14 | |
michael@0 | 15 | CStdOutStream g_StdOut(stdout); |
michael@0 | 16 | CStdOutStream g_StdErr(stderr); |
michael@0 | 17 | |
michael@0 | 18 | bool CStdOutStream::Open(const char *fileName) |
michael@0 | 19 | { |
michael@0 | 20 | Close(); |
michael@0 | 21 | _stream = fopen(fileName, kFileOpenMode); |
michael@0 | 22 | _streamIsOpen = (_stream != 0); |
michael@0 | 23 | return _streamIsOpen; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | bool CStdOutStream::Close() |
michael@0 | 27 | { |
michael@0 | 28 | if(!_streamIsOpen) |
michael@0 | 29 | return true; |
michael@0 | 30 | _streamIsOpen = (fclose(_stream) != 0); |
michael@0 | 31 | return !_streamIsOpen; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | bool CStdOutStream::Flush() |
michael@0 | 35 | { |
michael@0 | 36 | if(!_streamIsOpen) |
michael@0 | 37 | return false; |
michael@0 | 38 | return (fflush(_stream) == 0); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | CStdOutStream::~CStdOutStream () |
michael@0 | 42 | { |
michael@0 | 43 | Close(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*aFunction)(CStdOutStream &)) |
michael@0 | 47 | { |
michael@0 | 48 | (*aFunction)(*this); |
michael@0 | 49 | return *this; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | CStdOutStream & endl(CStdOutStream & outStream) |
michael@0 | 53 | { |
michael@0 | 54 | return outStream << kNewLineChar; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | CStdOutStream & CStdOutStream::operator<<(const char *string) |
michael@0 | 58 | { |
michael@0 | 59 | fputs(string, _stream); |
michael@0 | 60 | return *this; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | CStdOutStream & CStdOutStream::operator<<(const wchar_t *string) |
michael@0 | 64 | { |
michael@0 | 65 | *this << (const char *)UnicodeStringToMultiByte(string, CP_OEMCP); |
michael@0 | 66 | return *this; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | CStdOutStream & CStdOutStream::operator<<(char c) |
michael@0 | 70 | { |
michael@0 | 71 | fputc(c, _stream); |
michael@0 | 72 | return *this; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | CStdOutStream & CStdOutStream::operator<<(int number) |
michael@0 | 76 | { |
michael@0 | 77 | char textString[32]; |
michael@0 | 78 | ConvertInt64ToString(number, textString); |
michael@0 | 79 | return operator<<(textString); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | CStdOutStream & CStdOutStream::operator<<(UInt64 number) |
michael@0 | 83 | { |
michael@0 | 84 | char textString[32]; |
michael@0 | 85 | ConvertUInt64ToString(number, textString); |
michael@0 | 86 | return operator<<(textString); |
michael@0 | 87 | } |