1 | // |
---|
2 | // File.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/File.h#7 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Filesystem |
---|
8 | // Module: File |
---|
9 | // |
---|
10 | // Definition of the File class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, 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_File_INCLUDED |
---|
40 | #define Foundation_File_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Timestamp.h" |
---|
45 | #include <vector> |
---|
46 | |
---|
47 | |
---|
48 | #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8) |
---|
49 | #include "Poco/File_WIN32U.h" |
---|
50 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
---|
51 | #include "Poco/File_WIN32.h" |
---|
52 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
53 | #include "Poco/File_UNIX.h" |
---|
54 | #else |
---|
55 | #include "Poco/File_VMS.h" |
---|
56 | #endif |
---|
57 | |
---|
58 | |
---|
59 | namespace Poco { |
---|
60 | |
---|
61 | |
---|
62 | class Path; |
---|
63 | |
---|
64 | |
---|
65 | class Foundation_API File: private FileImpl |
---|
66 | /// The File class provides methods for working with a file. |
---|
67 | { |
---|
68 | public: |
---|
69 | typedef FileSizeImpl FileSize; |
---|
70 | |
---|
71 | File(); |
---|
72 | /// Creates the file. |
---|
73 | |
---|
74 | File(const std::string& path); |
---|
75 | /// Creates the file. |
---|
76 | |
---|
77 | File(const char* path); |
---|
78 | /// Creates the file. |
---|
79 | |
---|
80 | File(const Path& path); |
---|
81 | /// Creates the file. |
---|
82 | |
---|
83 | File(const File& file); |
---|
84 | /// Copy constructor. |
---|
85 | |
---|
86 | virtual ~File(); |
---|
87 | /// Destroys the file. |
---|
88 | |
---|
89 | File& operator = (const File& file); |
---|
90 | /// Assignment operator. |
---|
91 | |
---|
92 | File& operator = (const std::string& path); |
---|
93 | /// Assignment operator. |
---|
94 | |
---|
95 | File& operator = (const char* path); |
---|
96 | /// Assignment operator. |
---|
97 | |
---|
98 | File& operator = (const Path& path); |
---|
99 | /// Assignment operator. |
---|
100 | |
---|
101 | void swap(File& file); |
---|
102 | /// Swaps the file with another one. |
---|
103 | |
---|
104 | const std::string& path() const; |
---|
105 | /// Returns the path. |
---|
106 | |
---|
107 | bool exists() const; |
---|
108 | /// Returns true iff the file exists. |
---|
109 | |
---|
110 | bool canRead() const; |
---|
111 | /// Returns true iff the file is readable. |
---|
112 | |
---|
113 | bool canWrite() const; |
---|
114 | /// Returns true iff the file is writeable. |
---|
115 | |
---|
116 | bool canExecute() const; |
---|
117 | /// Returns true iff the file is executable. |
---|
118 | /// |
---|
119 | /// On Windows and OpenVMS, the file must have |
---|
120 | /// the extension ".EXE" to be executable. |
---|
121 | /// On Unix platforms, the executable permission |
---|
122 | /// bit must be set. |
---|
123 | |
---|
124 | bool isFile() const; |
---|
125 | /// Returns true iff the file is a regular file. |
---|
126 | |
---|
127 | bool isLink() const; |
---|
128 | /// Returns true iff the file is a symbolic link. |
---|
129 | |
---|
130 | bool isDirectory() const; |
---|
131 | /// Returns true iff the file is a directory. |
---|
132 | |
---|
133 | bool isDevice() const; |
---|
134 | /// Returns true iff the file is a device. |
---|
135 | |
---|
136 | bool isHidden() const; |
---|
137 | /// Returns true if the file is hidden. |
---|
138 | /// |
---|
139 | /// On Windows platforms, the file's hidden |
---|
140 | /// attribute is set for this to be true. |
---|
141 | /// |
---|
142 | /// On Unix platforms, the file name must |
---|
143 | /// begin with a period for this to be true. |
---|
144 | |
---|
145 | Timestamp created() const; |
---|
146 | /// Returns the creation date of the file. |
---|
147 | /// |
---|
148 | /// Not all platforms or filesystems (e.g. Linux and most Unix |
---|
149 | /// platforms with the exception of FreeBSD and Mac OS X) |
---|
150 | /// maintain the creation date of a file. |
---|
151 | /// On such platforms, created() returns |
---|
152 | /// the time of the last inode modification. |
---|
153 | |
---|
154 | Timestamp getLastModified() const; |
---|
155 | /// Returns the modification date of the file. |
---|
156 | |
---|
157 | void setLastModified(const Timestamp& ts); |
---|
158 | /// Sets the modification date of the file. |
---|
159 | |
---|
160 | FileSize getSize() const; |
---|
161 | /// Returns the size of the file in bytes. |
---|
162 | |
---|
163 | void setSize(FileSize size); |
---|
164 | /// Sets the size of the file in bytes. Can be used |
---|
165 | /// to truncate a file. |
---|
166 | |
---|
167 | void setWriteable(bool flag = true); |
---|
168 | /// Makes the file writeable (if flag is true), or |
---|
169 | /// non-writeable (if flag is false) by setting the |
---|
170 | /// file's flags in the filesystem accordingly. |
---|
171 | |
---|
172 | void setReadOnly(bool flag = true); |
---|
173 | /// Makes the file non-writeable (if flag is true), or |
---|
174 | /// writeable (if flag is false) by setting the |
---|
175 | /// file's flags in the filesystem accordingly. |
---|
176 | |
---|
177 | void setExecutable(bool flag = true); |
---|
178 | /// Makes the file executable (if flag is true), or |
---|
179 | /// non-executable (if flag is false) by setting |
---|
180 | /// the file's permission bits accordingly. |
---|
181 | /// |
---|
182 | /// Does nothing on Windows and OpenVMS. |
---|
183 | |
---|
184 | void copyTo(const std::string& path) const; |
---|
185 | /// Copies the file (or directory) to the given path. |
---|
186 | /// The target path can be a directory. |
---|
187 | /// |
---|
188 | /// A directory is copied recursively. |
---|
189 | |
---|
190 | void moveTo(const std::string& path); |
---|
191 | /// Copies the file (or directory) to the given path and |
---|
192 | /// removes the original file. The target path can be a directory. |
---|
193 | |
---|
194 | void renameTo(const std::string& path); |
---|
195 | /// Renames the file to the new name. |
---|
196 | |
---|
197 | void remove(bool recursive = false); |
---|
198 | /// Deletes the file. If recursive is true and the |
---|
199 | /// file is a directory, recursively deletes all |
---|
200 | /// files in the directory. |
---|
201 | |
---|
202 | bool createFile(); |
---|
203 | /// Creates a new, empty file in an atomic operation. |
---|
204 | /// Returns true if the file has been created and false |
---|
205 | /// if the file already exists. Throws an exception if |
---|
206 | /// an error occurs. |
---|
207 | |
---|
208 | bool createDirectory(); |
---|
209 | /// Creates a directory. Returns true if the directory |
---|
210 | /// has been created and false if it already exists. |
---|
211 | /// Throws an exception if an error occurs. |
---|
212 | |
---|
213 | void createDirectories(); |
---|
214 | /// Creates a directory (and all parent directories |
---|
215 | /// if necessary). |
---|
216 | |
---|
217 | void list(std::vector<std::string>& files) const; |
---|
218 | /// Fills the vector with the names of all |
---|
219 | /// files in the directory. |
---|
220 | |
---|
221 | void list(std::vector<File>& files) const; |
---|
222 | /// Fills the vector with the names of all |
---|
223 | /// files in the directory. |
---|
224 | |
---|
225 | bool operator == (const File& file) const; |
---|
226 | bool operator != (const File& file) const; |
---|
227 | bool operator < (const File& file) const; |
---|
228 | bool operator <= (const File& file) const; |
---|
229 | bool operator > (const File& file) const; |
---|
230 | bool operator >= (const File& file) const; |
---|
231 | |
---|
232 | static void handleLastError(const std::string& path); |
---|
233 | /// For internal use only. Throws an appropriate |
---|
234 | /// exception for the last file-related error. |
---|
235 | |
---|
236 | protected: |
---|
237 | void copyDirectory(const std::string& path) const; |
---|
238 | /// Copies a directory. Used internally by copyTo(). |
---|
239 | }; |
---|
240 | |
---|
241 | |
---|
242 | // |
---|
243 | // inlines |
---|
244 | // |
---|
245 | inline const std::string& File::path() const |
---|
246 | { |
---|
247 | return getPathImpl(); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | inline bool File::operator == (const File& file) const |
---|
252 | { |
---|
253 | return getPathImpl() == file.getPathImpl(); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | inline bool File::operator != (const File& file) const |
---|
258 | { |
---|
259 | return getPathImpl() != file.getPathImpl(); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | inline bool File::operator < (const File& file) const |
---|
264 | { |
---|
265 | return getPathImpl() < file.getPathImpl(); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | inline bool File::operator <= (const File& file) const |
---|
270 | { |
---|
271 | return getPathImpl() <= file.getPathImpl(); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | inline bool File::operator > (const File& file) const |
---|
276 | { |
---|
277 | return getPathImpl() > file.getPathImpl(); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | inline bool File::operator >= (const File& file) const |
---|
282 | { |
---|
283 | return getPathImpl() >= file.getPathImpl(); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | inline void swap(File& f1, File& f2) |
---|
288 | { |
---|
289 | f1.swap(f2); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | } // namespace Poco |
---|
294 | |
---|
295 | |
---|
296 | #endif // Foundation_File_INCLUDED |
---|