Home | History | Annotate | Download | only in Common
      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 
     17 static const char kNewLineChar =  '\n';
     18 
     19 static const char *kFileOpenMode = "wt";
     20 
     21 extern int g_CodePage;
     22 
     23 CStdOutStream  g_StdOut(stdout);
     24 CStdOutStream  g_StdErr(stderr);
     25 
     26 bool CStdOutStream::Open(const char *fileName)
     27 {
     28   Close();
     29   _stream = fopen(fileName, kFileOpenMode);
     30   _streamIsOpen = (_stream != 0);
     31   return _streamIsOpen;
     32 }
     33 
     34 bool 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 
     45 bool CStdOutStream::Flush()
     46 {
     47   return (fflush(_stream) == 0);
     48 }
     49 
     50 CStdOutStream::~CStdOutStream ()
     51 {
     52   Close();
     53 }
     54 
     55 CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*aFunction)(CStdOutStream  &))
     56 {
     57   (*aFunction)(*this);
     58   return *this;
     59 }
     60 
     61 CStdOutStream & endl(CStdOutStream & outStream)
     62 {
     63   return outStream << kNewLineChar;
     64 }
     65 
     66 CStdOutStream & CStdOutStream::operator<<(const char *s)
     67 {
     68   fputs(s, _stream);
     69   return *this;
     70 }
     71 
     72 CStdOutStream & 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 
     86 CStdOutStream & CStdOutStream::operator<<(char c)
     87 {
     88   fputc(c, _stream);
     89   return *this;
     90 }
     91 
     92 CStdOutStream & CStdOutStream::operator<<(int number)
     93 {
     94   char textString[32];
     95   ConvertInt64ToString(number, textString);
     96   return operator<<(textString);
     97 }
     98 
     99 CStdOutStream & CStdOutStream::operator<<(UInt64 number)
    100 {
    101   char textString[32];
    102   ConvertUInt64ToString(number, textString);
    103   return operator<<(textString);
    104 }
    105