FileStream.h 5.3 KB

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