1 | // |
---|
2 | // FileStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/FileStream.h#2 $ |
---|
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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_FileStream_INCLUDED |
---|
40 | #define Foundation_FileStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
45 | #include "FileStream_WIN32.h" |
---|
46 | #else |
---|
47 | #include "FileStream_POSIX.h" |
---|
48 | #endif |
---|
49 | #include <istream> |
---|
50 | #include <ostream> |
---|
51 | |
---|
52 | |
---|
53 | namespace Poco { |
---|
54 | |
---|
55 | |
---|
56 | class Foundation_API FileIOS: public virtual std::ios |
---|
57 | /// The base class for FileInputStream and FileOutputStream. |
---|
58 | /// |
---|
59 | /// This class is needed to ensure the correct initialization |
---|
60 | /// order of the stream buffer and base classes. |
---|
61 | /// |
---|
62 | /// Files are always opened in binary mode, a text mode |
---|
63 | /// with CR-LF translation is not supported. Thus, the |
---|
64 | /// file is always opened as if the std::ios::binary flag |
---|
65 | /// was specified. |
---|
66 | /// Use an InputLineEndingConverter or OutputLineEndingConverter |
---|
67 | /// if you require CR-LF translation. |
---|
68 | /// |
---|
69 | /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd, |
---|
70 | /// UTF-8 encoded Unicode paths are correctly handled. |
---|
71 | { |
---|
72 | public: |
---|
73 | FileIOS(std::ios::openmode defaultMode); |
---|
74 | /// Creates the basic stream. |
---|
75 | |
---|
76 | ~FileIOS(); |
---|
77 | /// Destroys the stream. |
---|
78 | |
---|
79 | void open(const std::string& path, std::ios::openmode mode); |
---|
80 | /// Opens the file specified by path, using the given mode. |
---|
81 | /// |
---|
82 | /// Throws a FileException (or a similar exception) if the file |
---|
83 | /// does not exist or is not accessible for other reasons and |
---|
84 | /// a new file cannot be created. |
---|
85 | |
---|
86 | void close(); |
---|
87 | /// Closes the file stream. |
---|
88 | /// |
---|
89 | /// If, for an output stream, the close operation fails (because |
---|
90 | /// the contents of the stream buffer cannot synced back to |
---|
91 | /// the filesystem), the bad bit is set in the stream state. |
---|
92 | |
---|
93 | FileStreamBuf* rdbuf(); |
---|
94 | /// Returns a pointer to the underlying streambuf. |
---|
95 | |
---|
96 | protected: |
---|
97 | FileStreamBuf _buf; |
---|
98 | std::ios::openmode _defaultMode; |
---|
99 | }; |
---|
100 | |
---|
101 | |
---|
102 | class Foundation_API FileInputStream: public FileIOS, public std::istream |
---|
103 | /// An input stream for reading from a file. |
---|
104 | /// |
---|
105 | /// Files are always opened in binary mode, a text mode |
---|
106 | /// with CR-LF translation is not supported. Thus, the |
---|
107 | /// file is always opened as if the std::ios::binary flag |
---|
108 | /// was specified. |
---|
109 | /// Use an InputLineEndingConverter if you require CR-LF translation. |
---|
110 | /// |
---|
111 | /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd, |
---|
112 | /// UTF-8 encoded Unicode paths are correctly handled. |
---|
113 | { |
---|
114 | public: |
---|
115 | FileInputStream(); |
---|
116 | /// Creates an unopened FileInputStream. |
---|
117 | |
---|
118 | FileInputStream(const std::string& path, std::ios::openmode mode = std::ios::in); |
---|
119 | /// Creates the FileInputStream for the file given by path, using |
---|
120 | /// the given mode. |
---|
121 | /// |
---|
122 | /// The std::ios::in flag is always set, regardless of the actual |
---|
123 | /// value specified for mode. |
---|
124 | /// |
---|
125 | /// Throws a FileNotFoundException (or a similar exception) if the file |
---|
126 | /// does not exist or is not accessible for other reasons. |
---|
127 | |
---|
128 | ~FileInputStream(); |
---|
129 | /// Destroys the stream. |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | class Foundation_API FileOutputStream: public FileIOS, public std::ostream |
---|
134 | /// An output stream for writing to a file. |
---|
135 | /// |
---|
136 | /// Files are always opened in binary mode, a text mode |
---|
137 | /// with CR-LF translation is not supported. Thus, the |
---|
138 | /// file is always opened as if the std::ios::binary flag |
---|
139 | /// was specified. |
---|
140 | /// Use an OutputLineEndingConverter if you require CR-LF translation. |
---|
141 | /// |
---|
142 | /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd, |
---|
143 | /// UTF-8 encoded Unicode paths are correctly handled. |
---|
144 | { |
---|
145 | public: |
---|
146 | FileOutputStream(); |
---|
147 | /// Creats an unopened FileOutputStream. |
---|
148 | |
---|
149 | FileOutputStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::trunc); |
---|
150 | /// Creates the FileOutputStream for the file given by path, using |
---|
151 | /// the given mode. |
---|
152 | /// |
---|
153 | /// The std::ios::out is always set, regardless of the actual |
---|
154 | /// value specified for mode. |
---|
155 | /// |
---|
156 | /// Throws a FileException (or a similar exception) if the file |
---|
157 | /// does not exist or is not accessible for other reasons and |
---|
158 | /// a new file cannot be created. |
---|
159 | |
---|
160 | ~FileOutputStream(); |
---|
161 | /// Destroys the FileOutputStream. |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | class Foundation_API FileStream: public FileIOS, public std::iostream |
---|
166 | /// A stream for reading from and writing to a file. |
---|
167 | /// |
---|
168 | /// Files are always opened in binary mode, a text mode |
---|
169 | /// with CR-LF translation is not supported. Thus, the |
---|
170 | /// file is always opened as if the std::ios::binary flag |
---|
171 | /// was specified. |
---|
172 | /// Use an InputLineEndingConverter or OutputLineEndingConverter |
---|
173 | /// if you require CR-LF translation. |
---|
174 | /// |
---|
175 | /// A seek (seekg() or seekp()) operation will always set the |
---|
176 | /// read position and the write position simultaneously to the |
---|
177 | /// same value. |
---|
178 | /// |
---|
179 | /// On Windows platforms, if POCO_WIN32_UTF8 is #define'd, |
---|
180 | /// UTF-8 encoded Unicode paths are correctly handled. |
---|
181 | { |
---|
182 | public: |
---|
183 | FileStream(); |
---|
184 | /// Creats an unopened FileStream. |
---|
185 | |
---|
186 | FileStream(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::in); |
---|
187 | /// Creates the FileStream for the file given by path, using |
---|
188 | /// the given mode. |
---|
189 | |
---|
190 | ~FileStream(); |
---|
191 | /// Destroys the FileOutputStream. |
---|
192 | }; |
---|
193 | |
---|
194 | |
---|
195 | } // namespace Poco |
---|
196 | |
---|
197 | |
---|
198 | #endif // Foundation_FileStream_INCLUDED |
---|