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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/Common/StdInStream.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +// Common/StdInStream.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include <tchar.h>
     1.9 +#include "StdInStream.h"
    1.10 +
    1.11 +static const char kIllegalChar = '\0';
    1.12 +static const char kNewLineChar = '\n';
    1.13 +
    1.14 +static const char *kEOFMessage = "Unexpected end of input stream";
    1.15 +static const char *kReadErrorMessage  ="Error reading input stream"; 
    1.16 +static const char *kIllegalCharMessage = "Illegal character in input stream";
    1.17 +
    1.18 +static LPCTSTR kFileOpenMode = TEXT("r");
    1.19 +
    1.20 +CStdInStream g_StdIn(stdin);
    1.21 +
    1.22 +bool CStdInStream::Open(LPCTSTR fileName)
    1.23 +{
    1.24 +  Close();
    1.25 +  _stream = _tfopen(fileName, kFileOpenMode);
    1.26 +  _streamIsOpen = (_stream != 0);
    1.27 +  return _streamIsOpen;
    1.28 +}
    1.29 +
    1.30 +bool CStdInStream::Close()
    1.31 +{
    1.32 +  if(!_streamIsOpen)
    1.33 +    return true;
    1.34 +  _streamIsOpen = (fclose(_stream) != 0);
    1.35 +  return !_streamIsOpen;
    1.36 +}
    1.37 +
    1.38 +CStdInStream::~CStdInStream()
    1.39 +{
    1.40 +  Close();
    1.41 +}
    1.42 +
    1.43 +AString CStdInStream::ScanStringUntilNewLine()
    1.44 +{
    1.45 +  AString s;
    1.46 +  while(true)
    1.47 +  {
    1.48 +    int intChar = GetChar();
    1.49 +    if(intChar == EOF)
    1.50 +      throw kEOFMessage;
    1.51 +    char c = char(intChar);
    1.52 +    if (c == kIllegalChar)
    1.53 +      throw kIllegalCharMessage;
    1.54 +    if(c == kNewLineChar)
    1.55 +      return s;
    1.56 +    s += c;
    1.57 +  }
    1.58 +}
    1.59 +
    1.60 +void CStdInStream::ReadToString(AString &resultString)
    1.61 +{
    1.62 +  resultString.Empty();
    1.63 +  int c;
    1.64 +  while((c = GetChar()) != EOF)
    1.65 +    resultString += char(c);
    1.66 +}
    1.67 +
    1.68 +bool CStdInStream::Eof()
    1.69 +{
    1.70 +  return (feof(_stream) != 0);
    1.71 +}
    1.72 +
    1.73 +int CStdInStream::GetChar()
    1.74 +{
    1.75 +  int c = getc(_stream);
    1.76 +  if(c == EOF && !Eof())
    1.77 +    throw kReadErrorMessage;
    1.78 +  return c;
    1.79 +}
    1.80 +
    1.81 +

mercurial