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 18 CStdInStream g_StdIn(stdin); 19 20 /* 21 #define kFileOpenMode TEXT("r") 22 23 bool CStdInStream::Open(LPCTSTR fileName) throw() 24 { 25 Close(); 26 _stream = 27 #ifdef _WIN32 28 _tfopen 29 #else 30 fopen 31 #endif 32 (fileName, kFileOpenMode); 33 _streamIsOpen = (_stream != 0); 34 return _streamIsOpen; 35 } 36 37 bool CStdInStream::Close() throw() 38 { 39 if (!_streamIsOpen) 40 return true; 41 _streamIsOpen = (fclose(_stream) != 0); 42 return !_streamIsOpen; 43 } 44 */ 45 ScanAStringUntilNewLine(AString & s)46bool CStdInStream::ScanAStringUntilNewLine(AString &s) 47 { 48 s.Empty(); 49 for (;;) 50 { 51 const int intChar = GetChar(); 52 if (intChar == EOF) 53 return true; 54 const char c = (char)intChar; 55 if (c == 0) 56 return false; 57 if (c == '\n') 58 return true; 59 s.Add_Char(c); 60 } 61 } 62 ScanUStringUntilNewLine(UString & dest)63bool CStdInStream::ScanUStringUntilNewLine(UString &dest) 64 { 65 dest.Empty(); 66 AString s; 67 const bool res = ScanAStringUntilNewLine(s); 68 int codePage = CodePage; 69 if (codePage == -1) 70 codePage = CP_OEMCP; 71 if ((unsigned)codePage == CP_UTF8) 72 ConvertUTF8ToUnicode(s, dest); 73 else 74 MultiByteToUnicodeString2(dest, s, (UINT)(unsigned)codePage); 75 return res; 76 } 77 78 /* 79 bool CStdInStream::ReadToString(AString &resultString) 80 { 81 resultString.Empty(); 82 for (;;) 83 { 84 int intChar = GetChar(); 85 if (intChar == EOF) 86 return !Error(); 87 char c = (char)intChar; 88 if (c == 0) 89 return false; 90 resultString += c; 91 } 92 } 93 */ 94 GetChar()95int CStdInStream::GetChar() 96 { 97 return fgetc(_stream); // getc() doesn't work in BeOS? 98 } 99