michael@0: // Common/StdInStream.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include michael@0: #include "StdInStream.h" michael@0: michael@0: static const char kIllegalChar = '\0'; michael@0: static const char kNewLineChar = '\n'; michael@0: michael@0: static const char *kEOFMessage = "Unexpected end of input stream"; michael@0: static const char *kReadErrorMessage ="Error reading input stream"; michael@0: static const char *kIllegalCharMessage = "Illegal character in input stream"; michael@0: michael@0: static LPCTSTR kFileOpenMode = TEXT("r"); michael@0: michael@0: CStdInStream g_StdIn(stdin); michael@0: michael@0: bool CStdInStream::Open(LPCTSTR fileName) michael@0: { michael@0: Close(); michael@0: _stream = _tfopen(fileName, kFileOpenMode); michael@0: _streamIsOpen = (_stream != 0); michael@0: return _streamIsOpen; michael@0: } michael@0: michael@0: bool CStdInStream::Close() michael@0: { michael@0: if(!_streamIsOpen) michael@0: return true; michael@0: _streamIsOpen = (fclose(_stream) != 0); michael@0: return !_streamIsOpen; michael@0: } michael@0: michael@0: CStdInStream::~CStdInStream() michael@0: { michael@0: Close(); michael@0: } michael@0: michael@0: AString CStdInStream::ScanStringUntilNewLine() michael@0: { michael@0: AString s; michael@0: while(true) michael@0: { michael@0: int intChar = GetChar(); michael@0: if(intChar == EOF) michael@0: throw kEOFMessage; michael@0: char c = char(intChar); michael@0: if (c == kIllegalChar) michael@0: throw kIllegalCharMessage; michael@0: if(c == kNewLineChar) michael@0: return s; michael@0: s += c; michael@0: } michael@0: } michael@0: michael@0: void CStdInStream::ReadToString(AString &resultString) michael@0: { michael@0: resultString.Empty(); michael@0: int c; michael@0: while((c = GetChar()) != EOF) michael@0: resultString += char(c); michael@0: } michael@0: michael@0: bool CStdInStream::Eof() michael@0: { michael@0: return (feof(_stream) != 0); michael@0: } michael@0: michael@0: int CStdInStream::GetChar() michael@0: { michael@0: int c = getc(_stream); michael@0: if(c == EOF && !Eof()) michael@0: throw kReadErrorMessage; michael@0: return c; michael@0: } michael@0: michael@0: