other-licenses/7zstub/src/Common/StdInStream.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // Common/StdInStream.cpp
     3 #include "StdAfx.h"
     5 #include <tchar.h>
     6 #include "StdInStream.h"
     8 static const char kIllegalChar = '\0';
     9 static const char kNewLineChar = '\n';
    11 static const char *kEOFMessage = "Unexpected end of input stream";
    12 static const char *kReadErrorMessage  ="Error reading input stream"; 
    13 static const char *kIllegalCharMessage = "Illegal character in input stream";
    15 static LPCTSTR kFileOpenMode = TEXT("r");
    17 CStdInStream g_StdIn(stdin);
    19 bool CStdInStream::Open(LPCTSTR fileName)
    20 {
    21   Close();
    22   _stream = _tfopen(fileName, kFileOpenMode);
    23   _streamIsOpen = (_stream != 0);
    24   return _streamIsOpen;
    25 }
    27 bool CStdInStream::Close()
    28 {
    29   if(!_streamIsOpen)
    30     return true;
    31   _streamIsOpen = (fclose(_stream) != 0);
    32   return !_streamIsOpen;
    33 }
    35 CStdInStream::~CStdInStream()
    36 {
    37   Close();
    38 }
    40 AString CStdInStream::ScanStringUntilNewLine()
    41 {
    42   AString s;
    43   while(true)
    44   {
    45     int intChar = GetChar();
    46     if(intChar == EOF)
    47       throw kEOFMessage;
    48     char c = char(intChar);
    49     if (c == kIllegalChar)
    50       throw kIllegalCharMessage;
    51     if(c == kNewLineChar)
    52       return s;
    53     s += c;
    54   }
    55 }
    57 void CStdInStream::ReadToString(AString &resultString)
    58 {
    59   resultString.Empty();
    60   int c;
    61   while((c = GetChar()) != EOF)
    62     resultString += char(c);
    63 }
    65 bool CStdInStream::Eof()
    66 {
    67   return (feof(_stream) != 0);
    68 }
    70 int CStdInStream::GetChar()
    71 {
    72   int c = getc(_stream);
    73   if(c == EOF && !Eof())
    74     throw kReadErrorMessage;
    75   return c;
    76 }

mercurial