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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial