Upstream tarball 9574
[amule.git] / src / FileArea.h
blob6fe74072744c190956acd1076f76260665bc3f66
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2009 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2009 Frediano Ziglio (freddy77@gamilc.com)
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifndef FILEAREA_H
23 #define FILEAREA_H
25 #include "CFile.h" // Needed for CFile
27 class CFileAreaSigHandler;
29 /**
30 * This class is used to optimize file read/write using mapped memory
31 * if supported.
33 class CFileArea
35 friend class CFileAreaSigHandler;
36 public:
37 /**
38 * Creates a uninitialized file area.
40 CFileArea();
43 /**
44 * Destructor, closes the file if opened.
46 virtual ~CFileArea();
48 /**
49 * Closes the file.
51 bool Close();
53 /**
54 * Init area with a given piece of file.
56 * @param file file to read.
57 * @param count bytes to read.
59 * Initialize buffer. Buffer will contain data from current file
60 * position for count length. Buffer will be a memory mapped area
61 * or a allocated buffer depending on systems.
63 * At the end file is positioned past readed area.
65 void Read(const CFile& file, size_t count);
67 /**
68 * Flushes data not yet written.
70 bool Flush();
72 /**
73 * Get buffer that contains data readed or to write.
74 * @return allocated buffer or NULL if not initialized
76 byte *GetBuffer() const { return m_buffer; };
78 /**
79 * Report error pending
81 void CheckError();
83 private:
84 //! A CFileArea is neither copyable nor assignable.
85 //@{
86 CFileArea(const CFileArea&);
87 CFileArea& operator=(const CFileArea&);
88 //@}
90 /**
91 * Pointer to buffer used for read/write operations.
92 * If mapped points inside m_mmap_buffer area otherwise
93 * point to an allocated buffer to be freed.
95 byte *m_buffer;
96 /**
97 * Pointer to memory mapped area or NULL if not mapped.
99 byte *m_mmap_buffer;
101 * Length of the mapped region, currently used only for munmap.
103 size_t m_length;
105 * Global chain
107 CFileArea* m_next;
109 * true if error detected
111 bool m_error;
114 #endif // FILEAREA_H
115 // File_checked_for_headers