OLD | NEW |
| (Empty) |
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 | |
13 static const char kIllegalChar = '\0'; | |
14 static const char kNewLineChar = '\n'; | |
15 | |
16 static const char *kEOFMessage = "Unexpected end of input stream"; | |
17 static const char *kReadErrorMessage ="Error reading input stream"; | |
18 static const char *kIllegalCharMessage = "Illegal character in input stream"; | |
19 | |
20 static LPCTSTR kFileOpenMode = TEXT("r"); | |
21 | |
22 CStdInStream g_StdIn(stdin); | |
23 | |
24 bool CStdInStream::Open(LPCTSTR fileName) | |
25 { | |
26 Close(); | |
27 _stream = _tfopen(fileName, kFileOpenMode); | |
28 _streamIsOpen = (_stream != 0); | |
29 return _streamIsOpen; | |
30 } | |
31 | |
32 bool CStdInStream::Close() | |
33 { | |
34 if (!_streamIsOpen) | |
35 return true; | |
36 _streamIsOpen = (fclose(_stream) != 0); | |
37 return !_streamIsOpen; | |
38 } | |
39 | |
40 CStdInStream::~CStdInStream() | |
41 { | |
42 Close(); | |
43 } | |
44 | |
45 AString 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 | |
63 void CStdInStream::ReadToString(AString &resultString) | |
64 { | |
65 resultString.Empty(); | |
66 int c; | |
67 while ((c = GetChar()) != EOF) | |
68 resultString += char(c); | |
69 } | |
70 | |
71 bool CStdInStream::Eof() | |
72 { | |
73 return (feof(_stream) != 0); | |
74 } | |
75 | |
76 int 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 | |
OLD | NEW |