~vcs-imports/wengophone/trunk

« back to all changes in this revision

Viewing changes to libs/util/util/File.h

  • Committer: tanguy_k
  • Date: 2006-11-15 14:12:35 UTC
  • Revision ID: vcs-imports@canonical.com-20061115141235-6efc6e38eaa40ca0
* (compilation fix) OWBuild: rename portaudio to PORTAUDIO
* Libutil has only one include path now -> faster compilation
* Remove old directories
* (compilation fix) phapi: ph_msession_stopped() was a macro inside phmedia.h, now it is a standard function. Don't know why but it was not compiling under Windows using CMake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * WengoPhone, a voice over Internet phone
 
3
 * Copyright (C) 2004-2006  Wengo
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#ifndef OWFILE_H
 
21
#define OWFILE_H
 
22
 
 
23
#include <util/owutildll.h>
 
24
#include <util/Interface.h>
 
25
#include <util/StringList.h>
 
26
 
 
27
#include <string>
 
28
#include <fstream>
 
29
 
 
30
/**
 
31
 * File interface.
 
32
 *
 
33
 * An abstract representation of file and directory pathnames.
 
34
 *
 
35
 * TODO use FileNotFoundException?
 
36
 *
 
37
 * @see QFile
 
38
 * @see java.io.*
 
39
 * @author Tanguy Krotoff
 
40
 */
 
41
class File : NonCopyable {
 
42
public:
 
43
 
 
44
        /**
 
45
         * Filename encoding.
 
46
         */
 
47
        enum Encoding {
 
48
                DEFAULT,
 
49
                UTF8
 
50
        };
 
51
 
 
52
        OWUTIL_API File(const std::string & filename, File::Encoding enc = File::DEFAULT);
 
53
 
 
54
        OWUTIL_API File(const File & file);
 
55
 
 
56
        OWUTIL_API virtual ~File() {
 
57
        }
 
58
 
 
59
        OWUTIL_API File & operator=(const File & file);
 
60
 
 
61
        /**
 
62
         * Gets the file extension.
 
63
         *
 
64
         * @return the file extension or empty string if there's no extension
 
65
         */
 
66
        OWUTIL_API std::string getExtension() const;
 
67
 
 
68
        /**
 
69
         * Moves a file.
 
70
         *
 
71
         * @param newPath the path we want to move the file to
 
72
         * @param overwrite true to overwrite the file if it exists.
 
73
         *
 
74
         * @return true if the operation succeeds otherwise false.
 
75
         */
 
76
        OWUTIL_API bool move(const std::string & newPath, bool overwrite = false);
 
77
 
 
78
        /**
 
79
         * Removes a file or a directory recursively.
 
80
         *
 
81
         * @return true if the operation succeeds otherwise false.
 
82
         */
 
83
        OWUTIL_API bool remove();
 
84
 
 
85
        /**
 
86
         * Copies a file or a directory recursively.
 
87
         * 
 
88
         * @param dstPath destination path. Will be created if does not exist
 
89
         * @return true if operation successful
 
90
         */
 
91
        bool copy(const std::string & path);
 
92
 
 
93
        /**
 
94
         * Copy a single file.
 
95
         * 
 
96
         * @param dst destination path or file
 
97
         * @param src source
 
98
         * @return true if successful
 
99
         */
 
100
        static bool copyFile(const std::string & dst, const std::string & src);
 
101
 
 
102
        /**
 
103
         * Gets the path to the file.
 
104
         *
 
105
         * The path does not contain the filename, it stops at the last /
 
106
         *
 
107
         * @return path to the file
 
108
         */
 
109
        OWUTIL_API std::string getPath() const;
 
110
 
 
111
        /**
 
112
         * Gets the full path to the file.
 
113
         *
 
114
         * @return path to the file
 
115
         */
 
116
        OWUTIL_API std::string getFullPath() const;
 
117
 
 
118
        /**
 
119
        * Gets the name of the file without its path.
 
120
        *
 
121
        * @return the name of the file without its path
 
122
        */
 
123
        OWUTIL_API std::string getFileName() const;
 
124
 
 
125
        /**
 
126
         * Gets directory list.
 
127
         *
 
128
         * @return a list of directories contained in 'this' directory
 
129
         */
 
130
        OWUTIL_API StringList getDirectoryList() const;
 
131
 
 
132
        /**
 
133
         * Gets file list.
 
134
         *
 
135
         * TODO Not implemented yet.
 
136
         *
 
137
         * @return a list of files contained in 'this' directory
 
138
         */
 
139
        OWUTIL_API StringList getFileList() const;
 
140
 
 
141
        /**
 
142
         * Gets the file size.
 
143
         *
 
144
         * @return the file size in bytes.
 
145
         */
 
146
        OWUTIL_API unsigned getSize() const;
 
147
 
 
148
        /**
 
149
         * Gets pathName with the '/' separators converted to separators that are appropriate for the underlying operating system.
 
150
         *
 
151
         * On Windows, convertPathSeparators("c:/winnt/system32") returns "c:\winnt\system32".
 
152
         *
 
153
         * @param path path to convert
 
154
         * @return path converted
 
155
         */
 
156
        OWUTIL_API static std::string convertPathSeparators(const std::string & path);
 
157
 
 
158
        /**
 
159
         * Gets the native directory separator: "/" under Unix (including Mac OS X) and "\" under Windows.
 
160
         *
 
161
         * @return native system path separator
 
162
         */
 
163
        OWUTIL_API static std::string getPathSeparator();
 
164
 
 
165
        /**
 
166
         * Creates directories recursively if the path does not exist.
 
167
         *
 
168
         * If path exists, nothing happends.
 
169
         *
 
170
         * @param path the path to create
 
171
         */
 
172
        OWUTIL_API static void createPath(const std::string & path);
 
173
 
 
174
        /**
 
175
         * Creates a temporary file.
 
176
         *
 
177
         * @return the temporary file
 
178
         */
 
179
        OWUTIL_API static File createTemporaryFile();
 
180
 
 
181
        /**
 
182
         * @return true if the given path exists.
 
183
         */
 
184
        OWUTIL_API static bool exists(const std::string & path);
 
185
 
 
186
        /**
 
187
         * @return true if the file is a directory.
 
188
         */
 
189
        OWUTIL_API static bool isDirectory(const std::string & filename);
 
190
 
 
191
protected:
 
192
 
 
193
        std::string _filename;
 
194
 
 
195
        File::Encoding _encoding;
 
196
};
 
197
 
 
198
 
 
199
/**
 
200
 * Interface for FileReader and FileWriter.
 
201
 *
 
202
 * Code factorization.
 
203
 *
 
204
 * @author Tanguy Krotoff
 
205
 */
 
206
class IFile : Interface {
 
207
public:
 
208
 
 
209
        /**
 
210
         * Closes the file.
 
211
         */
 
212
        virtual void close() = 0;
 
213
 
 
214
protected:
 
215
 
 
216
        /**
 
217
         * Opens the file for writing or reading.
 
218
         *
 
219
         * @return true if success; false otherwise
 
220
         */
 
221
        virtual bool open() = 0;
 
222
 
 
223
        /**
 
224
         * Gets if the file is open or not.
 
225
         *
 
226
         * @return true if file open; false otherwise
 
227
         */
 
228
        virtual bool isOpen() /*const*/ = 0;
 
229
};
 
230
 
 
231
 
 
232
/**
 
233
 * Reads from a file.
 
234
 *
 
235
 * @author Tanguy Krotoff
 
236
 */
 
237
class FileReader : public File, public IFile {
 
238
public:
 
239
 
 
240
        OWUTIL_API FileReader(const std::string & filename);
 
241
 
 
242
        OWUTIL_API FileReader(const FileReader & fileReader);
 
243
 
 
244
        OWUTIL_API FileReader(const File & file);
 
245
 
 
246
        OWUTIL_API ~FileReader();
 
247
 
 
248
        OWUTIL_API bool open();
 
249
 
 
250
        /**
 
251
         * Reads data from the file.
 
252
         *
 
253
         * You must call open() first and check the returned value otherwise
 
254
         * it will make an assertion (a crash).
 
255
         *
 
256
         * @return data read from the file
 
257
         */
 
258
        OWUTIL_API std::string read();
 
259
 
 
260
        OWUTIL_API void close();
 
261
 
 
262
private:
 
263
 
 
264
        bool isOpen();
 
265
 
 
266
        std::ifstream _file;
 
267
};
 
268
 
 
269
 
 
270
/**
 
271
 * Writes to a file.
 
272
 *
 
273
 * @author Tanguy Krotoff
 
274
 */
 
275
class FileWriter : public File, public IFile {
 
276
public:
 
277
 
 
278
        OWUTIL_API FileWriter(const std::string & filename, bool binaryMode = true);
 
279
 
 
280
        OWUTIL_API FileWriter(const FileWriter & fileWriter, bool binaryMode = true);
 
281
 
 
282
        OWUTIL_API FileWriter(const File & file, bool binaryMode = true);
 
283
 
 
284
        OWUTIL_API ~FileWriter();
 
285
 
 
286
        /**
 
287
         * Writes data to the file.
 
288
         *
 
289
         * You must call open() first and check the returned value otherwise
 
290
         * it will make an assertion (a crash).
 
291
         *
 
292
         * @param data data to write to the file
 
293
         */
 
294
        OWUTIL_API void write(const std::string & data);
 
295
 
 
296
        OWUTIL_API void close();
 
297
 
 
298
private:
 
299
 
 
300
        bool open();
 
301
 
 
302
        bool isOpen();
 
303
 
 
304
        std::ofstream _file;
 
305
 
 
306
        bool _fileOpen;
 
307
 
 
308
        bool _binaryMode;
 
309
};
 
310
 
 
311
#endif  //OWFILE_H