Upstream tarball 9445
[amule.git] / src / CFile.h
blob90a91d66ccebf5c0c3e677d78f80af046b68b124
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 1998-2008 Vadim Zeitlin ( zeitlin@dptmaths.ens-cachan.fr )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef CFILE_H
27 #define CFILE_H
29 #include <common/Path.h> // Needed for CPath
30 #include "SafeFile.h" // Needed for CFileDataIO
32 #include <wx/file.h> // Needed for constants
34 #ifdef _MSC_VER // silly warnings about deprecated functions
35 #pragma warning(disable:4996)
36 #endif
38 /**
39 * This class is a modified version of the wxFile class.
41 * In addition to implementing the CFileDataIO interface,
42 * it offers improved support for UTF8 filenames and 64b
43 * file-IO on both windows and unix-like systems.
45 * @see wxFile
47 class CFile : public CFileDataIO
49 public:
50 //! Standard values for file descriptor
51 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
53 /** @see wxFile::OpenMode */
54 enum OpenMode { read, write, read_write, write_append, write_excl };
57 /**
58 * Creates a closed file.
60 CFile();
62 /**
63 * Constructor, calls Open on the specified file.
65 * To check if the file was successfully opened, a
66 * call to IsOpened() is required.
68 CFile(const CPath& path, OpenMode mode = read);
69 CFile(const wxString& path, OpenMode mode = read);
71 /**
72 * Destructor, closes the file if opened.
74 virtual ~CFile();
77 /**
78 * Opens a file.
80 * @param path The full or relative path to the file.
81 * @param mode The opening mode.
82 * @param accessMode The permissions in case a new file is created.
83 * @return True if the file was opened, false otherwise.
85 * Calling Open with the openmodes 'write' or 'write_append' will
86 * create the specified file if it does not already exist.
88 * If an accessMode is not explicitly specified, the accessmode
89 * specified via CPreferences::GetFilePermissions will be used.
91 bool Open(const CPath& path, OpenMode mode = read, int accessMode = wxS_DEFAULT);
92 bool Open(const wxString& path, OpenMode mode = read, int accessMode = wxS_DEFAULT);
94 /**
95 * Calling Create is equivilant of calling open with OpenMode 'write'.
97 * @param overwrite Specifies if the target file should be overwritten,
98 * in case that it already exists.
100 * @see CFile::Open
102 bool Create(const CPath& path, bool overwrite = false, int accessMode = wxS_DEFAULT);
103 bool Create(const wxString& path, bool overwrite = false, int accessMode = wxS_DEFAULT);
106 * Closes the file.
108 * Note that calling Close on an closed file
109 * is an illegal operation.
111 bool Close();
115 * Returns the file descriptior assosiated with the file.
117 * Note that direct manipulation of the descriptor should
118 * be avoided! That's what this class is for.
120 int fd() const;
123 * Flushes data not yet written.
125 * Note that calling Flush on an closed file
126 * is an illegal operation.
128 bool Flush();
132 * @see CSafeFileIO::GetLength
134 * Note that calling GetLength on a closed file
135 * is an illegal operation.
137 virtual uint64 GetLength() const;
140 * Resizes the file to the specified length.
142 bool SetLength(size_t newLength);
145 * @see CSafeFileIO::GetPosition
147 * Note that calling GetPosition on a closed file
148 * is an illegal operation.
150 virtual uint64 GetPosition() const;
153 * Returns the current available bytes to read on the file before EOF
156 virtual uint64 GetAvailable() const;
159 * Returns the path of the currently opened file.
161 * Calling this function on an closed file is
162 * an illegal operation.
164 const CPath& GetFilePath() const;
168 * Returns true if the file is opened, false otherwise.
170 bool IsOpened() const;
172 protected:
173 /** @see CFileDataIO::doRead **/
174 virtual sint64 doRead(void* buffer, size_t count) const;
175 /** @see CFileDataIO::doWrite **/
176 virtual sint64 doWrite(const void* buffer, size_t count);
177 /** @see CFileDataIO::doSeek **/
178 virtual sint64 doSeek(sint64 offset) const;
180 private:
181 //! A CFile is neither copyable nor assignable.
182 //@{
183 CFile(const CFile&);
184 CFile& operator=(const CFile&);
185 //@}
187 //! File descriptor or 'fd_invalid' if not opened
188 int m_fd;
190 //! The full path to the current file.
191 CPath m_filePath;
196 * This exception is thrown by CFile if a seek or tell fails.
198 struct CSeekFailureException : public CIOFailureException {
199 CSeekFailureException(const wxString& desc);
203 #endif // CFILE_H
204 // File_checked_for_headers