FileStream.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // FileStream.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/FileStream.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: FileStream
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/FileStream.h"
  16. #include "Poco/Exception.h"
  17. #if defined(POCO_OS_FAMILY_WINDOWS)
  18. #include "FileStream_WIN32.cpp"
  19. #else
  20. #include "FileStream_POSIX.cpp"
  21. #endif
  22. namespace Poco {
  23. FileIOS::FileIOS(std::ios::openmode defaultMode):
  24. _defaultMode(defaultMode)
  25. {
  26. poco_ios_init(&_buf);
  27. }
  28. FileIOS::~FileIOS()
  29. {
  30. }
  31. void FileIOS::open(const std::string& path, std::ios::openmode mode)
  32. {
  33. clear();
  34. _buf.open(path, mode | _defaultMode);
  35. }
  36. void FileIOS::close()
  37. {
  38. if (!_buf.close())
  39. {
  40. setstate(ios_base::badbit);
  41. }
  42. }
  43. FileStreamBuf* FileIOS::rdbuf()
  44. {
  45. return &_buf;
  46. }
  47. FileInputStream::FileInputStream():
  48. FileIOS(std::ios::in),
  49. std::istream(&_buf)
  50. {
  51. }
  52. FileInputStream::FileInputStream(const std::string& path, std::ios::openmode mode):
  53. FileIOS(std::ios::in),
  54. std::istream(&_buf)
  55. {
  56. open(path, mode);
  57. }
  58. FileInputStream::~FileInputStream()
  59. {
  60. }
  61. FileOutputStream::FileOutputStream():
  62. FileIOS(std::ios::out),
  63. std::ostream(&_buf)
  64. {
  65. }
  66. FileOutputStream::FileOutputStream(const std::string& path, std::ios::openmode mode):
  67. FileIOS(std::ios::out),
  68. std::ostream(&_buf)
  69. {
  70. open(path, mode);
  71. }
  72. FileOutputStream::~FileOutputStream()
  73. {
  74. }
  75. FileStream::FileStream():
  76. FileIOS(std::ios::in | std::ios::out),
  77. std::iostream(&_buf)
  78. {
  79. }
  80. FileStream::FileStream(const std::string& path, std::ios::openmode mode):
  81. FileIOS(std::ios::in | std::ios::out),
  82. std::iostream(&_buf)
  83. {
  84. open(path, mode);
  85. }
  86. FileStream::~FileStream()
  87. {
  88. }
  89. } // namespace Poco