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

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:42eb23dbf3b0
1 // Common/StdInStream.cpp
2
3 #include "StdAfx.h"
4
5 #include <tchar.h>
6 #include "StdInStream.h"
7
8 static const char kIllegalChar = '\0';
9 static const char kNewLineChar = '\n';
10
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";
14
15 static LPCTSTR kFileOpenMode = TEXT("r");
16
17 CStdInStream g_StdIn(stdin);
18
19 bool CStdInStream::Open(LPCTSTR fileName)
20 {
21 Close();
22 _stream = _tfopen(fileName, kFileOpenMode);
23 _streamIsOpen = (_stream != 0);
24 return _streamIsOpen;
25 }
26
27 bool CStdInStream::Close()
28 {
29 if(!_streamIsOpen)
30 return true;
31 _streamIsOpen = (fclose(_stream) != 0);
32 return !_streamIsOpen;
33 }
34
35 CStdInStream::~CStdInStream()
36 {
37 Close();
38 }
39
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 }
56
57 void CStdInStream::ReadToString(AString &resultString)
58 {
59 resultString.Empty();
60 int c;
61 while((c = GetChar()) != EOF)
62 resultString += char(c);
63 }
64
65 bool CStdInStream::Eof()
66 {
67 return (feof(_stream) != 0);
68 }
69
70 int CStdInStream::GetChar()
71 {
72 int c = getc(_stream);
73 if(c == EOF && !Eof())
74 throw kReadErrorMessage;
75 return c;
76 }
77
78

mercurial