2
* WengoPhone, a voice over Internet phone
3
* Copyright (C) 2004-2006 Wengo
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include <util/owutildll.h>
24
#include <util/Interface.h>
25
#include <util/StringList.h>
33
* An abstract representation of file and directory pathnames.
35
* TODO use FileNotFoundException?
39
* @author Tanguy Krotoff
41
class File : NonCopyable {
52
OWUTIL_API File(const std::string & filename, File::Encoding enc = File::DEFAULT);
54
OWUTIL_API File(const File & file);
56
OWUTIL_API virtual ~File() {
59
OWUTIL_API File & operator=(const File & file);
62
* Gets the file extension.
64
* @return the file extension or empty string if there's no extension
66
OWUTIL_API std::string getExtension() const;
71
* @param newPath the path we want to move the file to
72
* @param overwrite true to overwrite the file if it exists.
74
* @return true if the operation succeeds otherwise false.
76
OWUTIL_API bool move(const std::string & newPath, bool overwrite = false);
79
* Removes a file or a directory recursively.
81
* @return true if the operation succeeds otherwise false.
83
OWUTIL_API bool remove();
86
* Copies a file or a directory recursively.
88
* @param dstPath destination path. Will be created if does not exist
89
* @return true if operation successful
91
bool copy(const std::string & path);
96
* @param dst destination path or file
98
* @return true if successful
100
static bool copyFile(const std::string & dst, const std::string & src);
103
* Gets the path to the file.
105
* The path does not contain the filename, it stops at the last /
107
* @return path to the file
109
OWUTIL_API std::string getPath() const;
112
* Gets the full path to the file.
114
* @return path to the file
116
OWUTIL_API std::string getFullPath() const;
119
* Gets the name of the file without its path.
121
* @return the name of the file without its path
123
OWUTIL_API std::string getFileName() const;
126
* Gets directory list.
128
* @return a list of directories contained in 'this' directory
130
OWUTIL_API StringList getDirectoryList() const;
135
* TODO Not implemented yet.
137
* @return a list of files contained in 'this' directory
139
OWUTIL_API StringList getFileList() const;
142
* Gets the file size.
144
* @return the file size in bytes.
146
OWUTIL_API unsigned getSize() const;
149
* Gets pathName with the '/' separators converted to separators that are appropriate for the underlying operating system.
151
* On Windows, convertPathSeparators("c:/winnt/system32") returns "c:\winnt\system32".
153
* @param path path to convert
154
* @return path converted
156
OWUTIL_API static std::string convertPathSeparators(const std::string & path);
159
* Gets the native directory separator: "/" under Unix (including Mac OS X) and "\" under Windows.
161
* @return native system path separator
163
OWUTIL_API static std::string getPathSeparator();
166
* Creates directories recursively if the path does not exist.
168
* If path exists, nothing happends.
170
* @param path the path to create
172
OWUTIL_API static void createPath(const std::string & path);
175
* Creates a temporary file.
177
* @return the temporary file
179
OWUTIL_API static File createTemporaryFile();
182
* @return true if the given path exists.
184
OWUTIL_API static bool exists(const std::string & path);
187
* @return true if the file is a directory.
189
OWUTIL_API static bool isDirectory(const std::string & filename);
193
std::string _filename;
195
File::Encoding _encoding;
200
* Interface for FileReader and FileWriter.
202
* Code factorization.
204
* @author Tanguy Krotoff
206
class IFile : Interface {
212
virtual void close() = 0;
217
* Opens the file for writing or reading.
219
* @return true if success; false otherwise
221
virtual bool open() = 0;
224
* Gets if the file is open or not.
226
* @return true if file open; false otherwise
228
virtual bool isOpen() /*const*/ = 0;
235
* @author Tanguy Krotoff
237
class FileReader : public File, public IFile {
240
OWUTIL_API FileReader(const std::string & filename);
242
OWUTIL_API FileReader(const FileReader & fileReader);
244
OWUTIL_API FileReader(const File & file);
246
OWUTIL_API ~FileReader();
248
OWUTIL_API bool open();
251
* Reads data from the file.
253
* You must call open() first and check the returned value otherwise
254
* it will make an assertion (a crash).
256
* @return data read from the file
258
OWUTIL_API std::string read();
260
OWUTIL_API void close();
273
* @author Tanguy Krotoff
275
class FileWriter : public File, public IFile {
278
OWUTIL_API FileWriter(const std::string & filename, bool binaryMode = true);
280
OWUTIL_API FileWriter(const FileWriter & fileWriter, bool binaryMode = true);
282
OWUTIL_API FileWriter(const File & file, bool binaryMode = true);
284
OWUTIL_API ~FileWriter();
287
* Writes data to the file.
289
* You must call open() first and check the returned value otherwise
290
* it will make an assertion (a crash).
292
* @param data data to write to the file
294
OWUTIL_API void write(const std::string & data);
296
OWUTIL_API void close();