FileStream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // FileStream.h
  3. //
  4. // Library: Foundation
  5. // Package: Streams
  6. // Module: FileStream
  7. //
  8. // Definition of the FileStreamBuf, FileInputStream and FileOutputStream classes.
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_FileStream_INCLUDED
  16. #define Foundation_FileStream_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #if defined(POCO_OS_FAMILY_WINDOWS)
  19. #include "Poco/FileStream_WIN32.h"
  20. #else
  21. #include "Poco/FileStream_POSIX.h"
  22. #endif
  23. #include <istream>
  24. #include <ostream>
  25. namespace Poco {
  26. class Foundation_API FileIOS: public virtual std::ios
  27. /// The base class for FileInputStream and FileOutputStream.
  28. ///
  29. /// This class is needed to ensure the correct initialization
  30. /// order of the stream buffer and base classes.
  31. ///
  32. /// Files are always opened in binary mode, a text mode
  33. /// with CR-LF translation is not supported. Thus, the
  34. /// file is always opened as if the std::ios::binary flag
  35. /// was specified.
  36. /// Use an InputLineEndingConverter or OutputLineEndingConverter
  37. /// if you require CR-LF translation.
  38. ///
  39. /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd,
  40. /// UTF-8 encoded Unicode paths are correctly handled.
  41. {
  42. public:
  43. FileIOS(std::ios::openmode defaultMode);
  44. /// Creates the basic stream.
  45. ~FileIOS();
  46. /// Destroys the stream.
  47. void open(const std::string& path, std::ios::openmode mode);
  48. /// Opens the file specified by path, using the given mode.
  49. ///
  50. /// Throws a FileException (or a similar exception) if the file
  51. /// does not exist or is not accessible for other reasons and
  52. /// a new file cannot be created.
  53. void close();
  54. /// Closes the file stream.
  55. ///
  56. /// If, for an output stream, the close operation fails (because
  57. /// the contents of the stream buffer cannot synced back to
  58. /// the filesystem), the bad bit is set in the stream state.
  59. FileStreamBuf* rdbuf();
  60. /// Returns a pointer to the underlying streambuf.
  61. protected:
  62. FileStreamBuf _buf;
  63. std::ios::openmode _defaultMode;
  64. };
  65. class Foundation_API FileInputStream: public FileIOS, public std::istream
  66. /// An input stream for reading from a file.
  67. ///
  68. /// Files are always opened in binary mode, a text mode
  69. /// with CR-LF translation is not supported. Thus, the
  70. /// file is always opened as if the std::ios::binary flag
  71. /// was specified.
  72. /// Use an InputLineEndingConverter if you require CR-LF translation.
  73. ///
  74. /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd,
  75. /// UTF-8 encoded Unicode paths are correctly handled.
  76. {
  77. public:
  78. FileInputStream();
  79. /// Creates an unopened FileInputStream.
  80. FileInputStream(const std::string& path, std::ios::openmode mode = std::ios::in);
  81. /// Creates the FileInputStream for the file given by path, using
  82. /// the given mode.
  83. ///
  84. /// The std::ios::in flag is always set, regardless of the actual
  85. /// value specified for mode.
  86. ///
  87. /// Throws a FileNotFoundException (or a similar exception) if the file
  88. /// does not exist or is not accessible for other reasons.
  89. ~FileInputStream();
  90. /// Destroys the stream.
  91. };
  92. class Foundation_API FileOutputStream: public FileIOS, public std::ostream
  93. /// An output stream for writing to a file.
  94. ///
  95. /// Files are always opened in binary mode, a text mode
  96. /// with CR-LF translation is not supported. Thus, the
  97. /// file is always opened as if the std::ios::binary flag
  98. /// was specified.
  99. /// Use an OutputLineEndingConverter if you require CR-LF translation.
  100. ///
  101. /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd,
  102. /// UTF-8 encoded Unicode paths are correctly handled.
  103. {
  104. public:
  105. FileOutputStream();
  106. /// Creats an unopened FileOutputStream.
  107. FileOutputStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::trunc);
  108. /// Creates the FileOutputStream for the file given by path, using
  109. /// the given mode.
  110. ///
  111. /// The std::ios::out is always set, regardless of the actual
  112. /// value specified for mode.
  113. ///
  114. /// Throws a FileException (or a similar exception) if the file
  115. /// does not exist or is not accessible for other reasons and
  116. /// a new file cannot be created.
  117. ~FileOutputStream();
  118. /// Destroys the FileOutputStream.
  119. };
  120. class Foundation_API FileStream: public FileIOS, public std::iostream
  121. /// A stream for reading from and writing to a file.
  122. ///
  123. /// Files are always opened in binary mode, a text mode
  124. /// with CR-LF translation is not supported. Thus, the
  125. /// file is always opened as if the std::ios::binary flag
  126. /// was specified.
  127. /// Use an InputLineEndingConverter or OutputLineEndingConverter
  128. /// if you require CR-LF translation.
  129. ///
  130. /// A seek (seekg() or seekp()) operation will always set the
  131. /// read position and the write position simultaneously to the
  132. /// same value.
  133. ///
  134. /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd,
  135. /// UTF-8 encoded Unicode paths are correctly handled.
  136. {
  137. public:
  138. FileStream();
  139. /// Creats an unopened FileStream.
  140. FileStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::in);
  141. /// Creates the FileStream for the file given by path, using
  142. /// the given mode.
  143. ~FileStream();
  144. /// Destroys the FileOutputStream.
  145. };
  146. } // namespace Poco
  147. #endif // Foundation_FileStream_INCLUDED