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