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