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