• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Common/StdInStream.cpp
2 
3 #include "StdAfx.h"
4 
5 #include <tchar.h>
6 
7 #include "StdInStream.h"
8 #include "StringConvert.h"
9 #include "UTFConvert.h"
10 
11 // #define kEOFMessage "Unexpected end of input stream"
12 // #define kReadErrorMessage "Error reading input stream"
13 // #define kIllegalCharMessage "Illegal zero character in input stream"
14 
15 #define kFileOpenMode TEXT("r")
16 
17 extern int g_CodePage;
18 
19 CStdInStream g_StdIn(stdin);
20 
Open(LPCTSTR fileName)21 bool CStdInStream::Open(LPCTSTR fileName) throw()
22 {
23   Close();
24   _stream = _tfopen(fileName, kFileOpenMode);
25   _streamIsOpen = (_stream != 0);
26   return _streamIsOpen;
27 }
28 
Close()29 bool CStdInStream::Close() throw()
30 {
31   if (!_streamIsOpen)
32     return true;
33   _streamIsOpen = (fclose(_stream) != 0);
34   return !_streamIsOpen;
35 }
36 
ScanAStringUntilNewLine(AString & s)37 bool CStdInStream::ScanAStringUntilNewLine(AString &s)
38 {
39   s.Empty();
40   for (;;)
41   {
42     int intChar = GetChar();
43     if (intChar == EOF)
44       return true;
45     char c = (char)intChar;
46     if (c == 0)
47       return false;
48     if (c == '\n')
49       return true;
50     s += c;
51   }
52 }
53 
ScanUStringUntilNewLine(UString & dest)54 bool CStdInStream::ScanUStringUntilNewLine(UString &dest)
55 {
56   dest.Empty();
57   AString s;
58   bool res = ScanAStringUntilNewLine(s);
59   int codePage = g_CodePage;
60   if (codePage == -1)
61     codePage = CP_OEMCP;
62   if (codePage == CP_UTF8)
63     ConvertUTF8ToUnicode(s, dest);
64   else
65     MultiByteToUnicodeString2(dest, s, (UINT)codePage);
66   return res;
67 }
68 
69 /*
70 bool CStdInStream::ReadToString(AString &resultString)
71 {
72   resultString.Empty();
73   for (;;)
74   {
75     int intChar = GetChar();
76     if (intChar == EOF)
77       return !Error();
78     char c = (char)intChar;
79     if (c == 0)
80       return false;
81     resultString += c;
82   }
83 }
84 */
85 
GetChar()86 int CStdInStream::GetChar()
87 {
88   return fgetc(_stream); // getc() doesn't work in BeOS?
89 }
90