tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StdOutStream.cpp (3438B)


      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 #define kFileOpenMode "wt"
     13 
     14 extern int g_CodePage;
     15 
     16 CStdOutStream g_StdOut(stdout);
     17 CStdOutStream g_StdErr(stderr);
     18 
     19 bool CStdOutStream::Open(const char *fileName) throw()
     20 {
     21  Close();
     22  _stream = fopen(fileName, kFileOpenMode);
     23  _streamIsOpen = (_stream != 0);
     24  return _streamIsOpen;
     25 }
     26 
     27 bool 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 
     38 bool CStdOutStream::Flush() throw()
     39 {
     40  return (fflush(_stream) == 0);
     41 }
     42 
     43 CStdOutStream & endl(CStdOutStream & outStream) throw()
     44 {
     45  return outStream << '\n';
     46 }
     47 
     48 CStdOutStream & CStdOutStream::operator<<(const wchar_t *s)
     49 {
     50  int codePage = g_CodePage;
     51  if (codePage == -1)
     52    codePage = CP_OEMCP;
     53  AString dest;
     54  if (codePage == CP_UTF8)
     55    ConvertUnicodeToUTF8(s, dest);
     56  else
     57    UnicodeStringToMultiByte2(dest, s, (UINT)codePage);
     58  return operator<<((const char *)dest);
     59 }
     60 
     61 void StdOut_Convert_UString_to_AString(const UString &s, AString &temp)
     62 {
     63  int codePage = g_CodePage;
     64  if (codePage == -1)
     65    codePage = CP_OEMCP;
     66  if (codePage == CP_UTF8)
     67    ConvertUnicodeToUTF8(s, temp);
     68  else
     69    UnicodeStringToMultiByte2(temp, s, (UINT)codePage);
     70 }
     71 
     72 void CStdOutStream::PrintUString(const UString &s, AString &temp)
     73 {
     74  StdOut_Convert_UString_to_AString(s, temp);
     75  *this << (const char *)temp;
     76 }
     77 
     78 
     79 static const wchar_t kReplaceChar = '_';
     80 
     81 void CStdOutStream::Normalize_UString__LF_Allowed(UString &s)
     82 {
     83  unsigned len = s.Len();
     84  wchar_t *d = s.GetBuf();
     85 
     86  if (IsTerminalMode)
     87    for (unsigned i = 0; i < len; i++)
     88    {
     89      wchar_t c = d[i];
     90      if (c <= 13 && c >= 7 && c != '\n')
     91        d[i] = kReplaceChar;
     92    }
     93 }
     94 
     95 void CStdOutStream::Normalize_UString(UString &s)
     96 {
     97  unsigned len = s.Len();
     98  wchar_t *d = s.GetBuf();
     99 
    100  if (IsTerminalMode)
    101    for (unsigned i = 0; i < len; i++)
    102    {
    103      wchar_t c = d[i];
    104      if (c <= 13 && c >= 7)
    105        d[i] = kReplaceChar;
    106    }
    107  else
    108    for (unsigned i = 0; i < len; i++)
    109    {
    110      wchar_t c = d[i];
    111      if (c == '\n')
    112        d[i] = kReplaceChar;
    113    }
    114 }
    115 
    116 void CStdOutStream::NormalizePrint_UString(const UString &s, UString &tempU, AString &tempA)
    117 {
    118  tempU = s;
    119  Normalize_UString(tempU);
    120  PrintUString(tempU, tempA);
    121 }
    122 
    123 void CStdOutStream::NormalizePrint_UString(const UString &s)
    124 {
    125  NormalizePrint_wstr(s);
    126 }
    127 
    128 void CStdOutStream::NormalizePrint_wstr(const wchar_t *s)
    129 {
    130  UString tempU = s;
    131  Normalize_UString(tempU);
    132  AString tempA;
    133  PrintUString(tempU, tempA);
    134 }
    135 
    136 
    137 CStdOutStream & CStdOutStream::operator<<(Int32 number) throw()
    138 {
    139  char s[32];
    140  ConvertInt64ToString(number, s);
    141  return operator<<(s);
    142 }
    143 
    144 CStdOutStream & CStdOutStream::operator<<(Int64 number) throw()
    145 {
    146  char s[32];
    147  ConvertInt64ToString(number, s);
    148  return operator<<(s);
    149 }
    150 
    151 CStdOutStream & CStdOutStream::operator<<(UInt32 number) throw()
    152 {
    153  char s[16];
    154  ConvertUInt32ToString(number, s);
    155  return operator<<(s);
    156 }
    157 
    158 CStdOutStream & CStdOutStream::operator<<(UInt64 number) throw()
    159 {
    160  char s[32];
    161  ConvertUInt64ToString(number, s);
    162  return operator<<(s);
    163 }