StdInStream.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Common/StdInStream.cpp
  2. #include "StdAfx.h"
  3. #include <tchar.h>
  4. #include "StdInStream.h"
  5. #ifdef _MSC_VER
  6. // "was declared deprecated" disabling
  7. #pragma warning(disable : 4996 )
  8. #endif
  9. static const char kIllegalChar = '\0';
  10. static const char kNewLineChar = '\n';
  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. static LPCTSTR kFileOpenMode = TEXT("r");
  15. CStdInStream g_StdIn(stdin);
  16. bool CStdInStream::Open(LPCTSTR fileName)
  17. {
  18. Close();
  19. _stream = _tfopen(fileName, kFileOpenMode);
  20. _streamIsOpen = (_stream != 0);
  21. return _streamIsOpen;
  22. }
  23. bool CStdInStream::Close()
  24. {
  25. if(!_streamIsOpen)
  26. return true;
  27. _streamIsOpen = (fclose(_stream) != 0);
  28. return !_streamIsOpen;
  29. }
  30. CStdInStream::~CStdInStream()
  31. {
  32. Close();
  33. }
  34. AString CStdInStream::ScanStringUntilNewLine()
  35. {
  36. AString s;
  37. for (;;)
  38. {
  39. int intChar = GetChar();
  40. if(intChar == EOF)
  41. throw kEOFMessage;
  42. char c = char(intChar);
  43. if (c == kIllegalChar)
  44. throw kIllegalCharMessage;
  45. if(c == kNewLineChar)
  46. break;
  47. s += c;
  48. }
  49. return s;
  50. }
  51. void CStdInStream::ReadToString(AString &resultString)
  52. {
  53. resultString.Empty();
  54. int c;
  55. while((c = GetChar()) != EOF)
  56. resultString += char(c);
  57. }
  58. bool CStdInStream::Eof()
  59. {
  60. return (feof(_stream) != 0);
  61. }
  62. int CStdInStream::GetChar()
  63. {
  64. int c = fgetc(_stream); // getc() doesn't work in BeOS?
  65. if(c == EOF && !Eof())
  66. throw kReadErrorMessage;
  67. return c;
  68. }