2
\class wxMemoryMappedFile
3
\author Blake Madden (blake.madden@oleandersolutions.com)
7
/***************************************************************************
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the wxWidgets License. *
12
***************************************************************************/
14
/** \example wxMemoryMappedFile
17
wxMemoryMappedFile fileMap(_T("/home/bmadden/file.txt"), <b>true</b>);<br>
18
const char* fileText = (const char*)fileMap.GetStream();<br><br>
19
<b>//now map another file (note that fileText will not be valid after this)</b><br>
20
fileMap.UnmapFile();<br>
21
fileMap.MapFile(_T("/home/bmadden/DifferentFile.txt"), <b>false</b>);<br>
22
char* writableFileText = (char*)fileMap.GetStream();<br><br>
23
<b>//now write back to the file by simply writing to the pointer</b><br>
24
std::strncpy(writableFileText, "Hello, world!", 13);<br>
28
//handle error here<br>
31
\todo Currently only supports files under 2GBs.
33
/** \example wxMemoryMappedFile
36
wxMemoryMappedFile fileMap(_T("/home/bmadden/file.txt"), true);
37
const char* fileText = (const char*)fileMap.GetStream();
39
//now map another file (note that fileText will not be valid after this)
41
fileMap.MapFile(_T("/home/bmadden/DifferentFile.txt"), false);
42
char* writableFileText = (char*)fileMap.GetStream();
43
//now write back to the file by simply writing to the pointer
44
std::strncpy(writableFileText, "Hello, world!", 13);
51
\todo Currently only supports files under 2GBs.
54
#ifndef __WXMEMMAPPEDFILE_H__
55
#define __WXMEMMAPPEDFILE_H__
57
// NB: "UNICODE" must be defined for this code to compile
58
#if defined(wxUSE_UNICODE)
67
#include <sys/fcntl.h>
69
#include <wx/string.h>
70
#include <wx/longlong.h>
73
///General exception that can be thrown when mapping a file.
74
class wxMemoryMappedFileException : public std::exception
76
///Exception that can be thrown when mapping if the file is zero length.
77
class wxMemoryMappedFileEmptyException : public std::exception
79
///Exception that can be thrown when mapping if the file can't be exclusively locked.
80
class wxMemoryMappedFileShareViolationException : public std::exception
82
///Exception that can be thrown when mapping if the file isn't something that can be mapped.
83
class wxMemoryMappedInvalidFileType : public std::exception
85
///Exception that can be thrown when mapping if the size of the file can't be determined.
86
class wxMemoryMappedInvalidFileSize : public std::exception
90
typedef HANDLE wxMemoryMappedFileHandleType;
92
typedef int wxMemoryMappedFileHandleType;
95
/**Class for mapping a file into your address space
96
(rather than having to buffer its contents)*/
97
// ----------------------------------------------------------------------------
98
class wxMemoryMappedFile
99
// ----------------------------------------------------------------------------
102
///Default Constructor
103
wxMemoryMappedFile() :
105
m_hFile(INVALID_HANDLE_VALUE),
118
\brief Constructor which will automatically map the file.
120
\exception wxMemoryMappedInvalidFileSize
121
\exception wxMemoryMappedFileEmptyException
122
\exception wxMemoryMappedFileShareViolationException
123
\exception wxMemoryMappedInvalidFileType
124
\exception wxMemoryMappedInvalidFileSize
126
\param filePath Path to the file to map.
127
\param readOnly Flag specifying whether to open the file as read only.
129
wxMemoryMappedFile(const wxString& filePath, bool readOnly = true) :
131
m_hFile(INVALID_HANDLE_VALUE),
139
m_isReadOnly(readOnly),
142
m_lastError = MapFile(filePath, readOnly);
144
///Destructor which implicitly unmaps the file
145
~wxMemoryMappedFile()
149
///Last error recorded
150
unsigned GetLastError()
154
///Indicates whether a file is currently (and successfully) mapped
159
///Indicates whether the current file mapping is read only
160
bool IsReadOnly() const
165
\brief Manually maps a new file.
166
\warning If this object is currently mapping another file then
167
you need to call UnmapFile() first.
169
\exception wxMemoryMappedInvalidFileSize
170
\exception wxMemoryMappedFileEmptyException
171
\exception wxMemoryMappedFileShareViolationException
172
\exception wxMemoryMappedInvalidFileType
173
\exception wxMemoryMappedInvalidFileSize
175
\param filePath Path to the file to map.
176
\param readOnly Flag specifying whether to open the file as read only.
178
\return True if file mapping was successful.
180
long MapFile(const wxString& filePath, const bool readOnly = true);
181
///Closes the handles and mappings
183
/**Returns the raw byte stream of the file
184
\warning Do not attempt to write to the returned pointer if you mapped the file as read only.
185
The read only status of the current mapping can be checked by calling IsReadOnly().
191
///Returns the length of the mapped file
192
size_t GetMapSize() const
196
///Returns the path of the file currently mapped
197
wxString GetFilePath() const
202
///Returns the size of a large file (as an unsigned long long)
203
static wxULongLong GetFileSize64(const wxMemoryMappedFileHandleType hFile);
218
unsigned m_lastError;
221
#endif //__WXMEMMAPPEDFILE_H__