-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathCFile.h
220 lines (185 loc) · 5.72 KB
/
CFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( [email protected] / http://www.amule.org )
// Copyright (c) 1998-2011 Vadim Zeitlin ( [email protected] )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#ifndef CFILE_H
#define CFILE_H
#include <common/Path.h> // Needed for CPath
#include "SafeFile.h" // Needed for CFileDataIO
#include <wx/file.h> // Needed for constants
#ifdef _MSC_VER // silly warnings about deprecated functions
#pragma warning(disable:4996)
#endif
/**
* This class is a modified version of the wxFile class.
*
* In addition to implementing the CFileDataIO interface,
* it offers improved support for UTF8 filenames and 64b
* file-IO on both windows and unix-like systems.
*
* @see wxFile
*/
class CFile : public CFileDataIO
{
public:
//! Standard values for file descriptor
enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
/** @see wxFile::OpenMode */
enum OpenMode { read, write, read_write, write_append, write_excl, write_safe };
/**
* Creates a closed file.
*/
CFile();
/**
* Constructor, calls Open on the specified file.
*
* To check if the file was successfully opened, a
* call to IsOpened() is required.
*/
CFile(const CPath& path, OpenMode mode = read);
CFile(const wxString& path, OpenMode mode = read);
/**
* Destructor, closes the file if opened.
*/
virtual ~CFile();
/**
* Opens a file.
*
* @param path The full or relative path to the file.
* @param mode The opening mode.
* @param accessMode The permissions in case a new file is created.
* @return True if the file was opened, false otherwise.
*
* Calling Open with the openmodes 'write' or 'write_append' will
* create the specified file if it does not already exist.
*
* Calling Open with the openmode 'write_safe' will append ".new"
* to the file name and otherwise work like 'write'.
* On close it will be renamed to the original name.
* Close() has to be called manually - destruct won't rename the file!
*
* If an accessMode is not explicitly specified, the accessmode
* specified via CPreferences::GetFilePermissions will be used.
*/
bool Open(const CPath& path, OpenMode mode = read, int accessMode = wxS_DEFAULT);
bool Open(const wxString& path, OpenMode mode = read, int accessMode = wxS_DEFAULT);
/**
* Reopens a file which was opened and closed before.
*
* @param mode The opening mode.
*
* The filename used for last open is used again.
* No return value - function throws on failure.
*/
void Reopen(OpenMode mode);
/**
* Calling Create is equivalent of calling open with OpenMode 'write'.
*
* @param overwrite Specifies if the target file should be overwritten,
* in case that it already exists.
*
* @see CFile::Open
*/
bool Create(const CPath& path, bool overwrite = false, int accessMode = wxS_DEFAULT);
bool Create(const wxString& path, bool overwrite = false, int accessMode = wxS_DEFAULT);
/**
* Closes the file.
*
* Note that calling Close on an closed file
* is an illegal operation.
*/
bool Close();
/**
* Returns the file descriptor associated with the file.
*
* Note that direct manipulation of the descriptor should
* be avoided! That's what this class is for.
*/
int fd() const;
/**
* Flushes data not yet written.
*
* Note that calling Flush on an closed file
* is an illegal operation.
*/
bool Flush();
/**
* @see CSafeFileIO::GetLength
*
* Note that calling GetLength on a closed file
* is an illegal operation.
*/
virtual uint64 GetLength() const;
/**
* Resizes the file to the specified length.
*/
bool SetLength(uint64 newLength);
/**
* @see CSafeFileIO::GetPosition
*
* Note that calling GetPosition on a closed file
* is an illegal operation.
*/
virtual uint64 GetPosition() const;
/**
* Returns the current available bytes to read on the file before EOF
*
*/
virtual uint64 GetAvailable() const;
/**
* Returns the path of the currently opened file.
*
*/
const CPath& GetFilePath() const;
/**
* Returns true if the file is opened, false otherwise.
*/
bool IsOpened() const;
protected:
/** @see CFileDataIO::doRead **/
virtual sint64 doRead(void* buffer, size_t count) const;
/** @see CFileDataIO::doWrite **/
virtual sint64 doWrite(const void* buffer, size_t count);
/** @see CFileDataIO::doSeek **/
virtual sint64 doSeek(sint64 offset) const;
private:
//! A CFile is neither copyable nor assignable.
//@{
CFile(const CFile&);
CFile& operator=(const CFile&);
//@}
//! File descriptor or 'fd_invalid' if not opened
int m_fd;
//! The full path to the current file.
CPath m_filePath;
//! Are we using safe write mode?
bool m_safeWrite;
};
/**
* This exception is thrown by CFile if a seek or tell fails.
*/
struct CSeekFailureException : public CIOFailureException {
CSeekFailureException(const wxString& desc);
};
#endif // CFILE_H
// File_checked_for_headers