~ubuntu-branches/ubuntu/trusty/codeblocks/trusty-proposed

« back to all changes in this revision

Viewing changes to src/plugins/contrib/codesnippets/memorymappedfile.h

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2011-01-22 20:58:57 UTC
  • mfrom: (3.1.3 maverick)
  • Revision ID: james.westby@ubuntu.com-20110122205857-mlwokxogiic0dnu7
Tags: 10.05-1
* Initial Debian release (Closes: #304570)
* Setting myself as Maintainer, original Ubuntu maintainer set as
  Uploader
* Use debian/watch to get the original tarball, instead of an ad-hoc
  script
* Wrap multivalue fields in debian/control
* Update debian/copyright with lots of missing info, and use DEP-5
* Add missing linkage to libX11 (03-fix_libX11_linkage.patch)
* Use dh7 for debian/rules
* Build-Depend on dh-autoreconf, since we need to re-generate autotools
  machinery (libtool version mismatch)
* Migrate from libwxsmithlib0-dev to libwxsmithlib-dev, to ease
  future SONAME bumps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
\class wxMemoryMappedFile
3
 
\author Blake Madden (blake.madden@oleandersolutions.com)
4
 
\date (C) 2006
5
 
*/
6
 
 
7
 
/***************************************************************************
8
 
 *                                                                         *
9
 
 *   This program is free software; you can redistribute it and/or modify  *
10
 
 *   it under the terms of the wxWidgets License.                          *
11
 
 *                                                                         *
12
 
 ***************************************************************************/
13
 
 
14
 
/** \example wxMemoryMappedFile
15
 
  try<br>
16
 
    {<br>
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>
25
 
    }<br>
26
 
  catch(...)<br>
27
 
    {<br>
28
 
    //handle error here<br>
29
 
    }
30
 
 
31
 
  \todo Currently only supports files under 2GBs.
32
 
*/
33
 
/** \example wxMemoryMappedFile
34
 
  try
35
 
    {
36
 
        wxMemoryMappedFile fileMap(_T("/home/bmadden/file.txt"), true);
37
 
        const char* fileText = (const char*)fileMap.GetStream();
38
 
 
39
 
        //now map another file (note that fileText will not be valid after this)
40
 
        fileMap.UnmapFile();
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);
45
 
    }
46
 
  catch(...)
47
 
    {
48
 
        //handle error here
49
 
    }
50
 
 
51
 
  \todo Currently only supports files under 2GBs.
52
 
*/
53
 
 
54
 
#ifndef __WXMEMMAPPEDFILE_H__
55
 
#define __WXMEMMAPPEDFILE_H__
56
 
 
57
 
// NB: "UNICODE" must be defined for this code to compile
58
 
#if defined(wxUSE_UNICODE)
59
 
#define UNICODE
60
 
#endif
61
 
 
62
 
#ifdef __WXMSW__
63
 
#include <windows.h>
64
 
#else
65
 
#include <unistd.h>
66
 
#include <sys/mman.h>
67
 
#include <sys/fcntl.h>
68
 
#endif
69
 
#include <wx/string.h>
70
 
#include <wx/longlong.h>
71
 
#include <exception>
72
 
 
73
 
///General exception that can be thrown when mapping a file.
74
 
class wxMemoryMappedFileException : public std::exception
75
 
    {};
76
 
///Exception that can be thrown when mapping if the file is zero length.
77
 
class wxMemoryMappedFileEmptyException : public std::exception
78
 
    {};
79
 
///Exception that can be thrown when mapping if the file can't be exclusively locked.
80
 
class wxMemoryMappedFileShareViolationException : public std::exception
81
 
    {};
82
 
///Exception that can be thrown when mapping if the file isn't something that can be mapped.
83
 
class wxMemoryMappedInvalidFileType : public std::exception
84
 
    {};
85
 
///Exception that can be thrown when mapping if the size of the file can't be determined.
86
 
class wxMemoryMappedInvalidFileSize : public std::exception
87
 
    {};
88
 
 
89
 
#ifdef __WXMSW__
90
 
typedef HANDLE wxMemoryMappedFileHandleType;
91
 
#else
92
 
typedef int wxMemoryMappedFileHandleType;
93
 
#endif
94
 
 
95
 
/**Class for mapping a file into your address space
96
 
(rather than having to buffer its contents)*/
97
 
// ----------------------------------------------------------------------------
98
 
class wxMemoryMappedFile
99
 
// ----------------------------------------------------------------------------
100
 
{
101
 
public:
102
 
    ///Default Constructor
103
 
    wxMemoryMappedFile() :
104
 
    #ifdef __WXMSW__
105
 
            m_hFile(INVALID_HANDLE_VALUE),
106
 
            m_hsection(NULL),
107
 
    #else
108
 
            m_hFile(-1),
109
 
    #endif
110
 
            m_data(NULL),
111
 
            m_mapSize(0),
112
 
            m_open(false),
113
 
            m_isReadOnly(false),
114
 
            m_lastError(0)
115
 
    {}
116
 
 
117
 
    /**
118
 
    \brief Constructor which will automatically map the file.
119
 
 
120
 
    \exception wxMemoryMappedInvalidFileSize
121
 
    \exception wxMemoryMappedFileEmptyException
122
 
    \exception wxMemoryMappedFileShareViolationException
123
 
    \exception wxMemoryMappedInvalidFileType
124
 
    \exception wxMemoryMappedInvalidFileSize
125
 
 
126
 
    \param filePath Path to the file to map.
127
 
    \param readOnly Flag specifying whether to open the file as read only.
128
 
    */
129
 
    wxMemoryMappedFile(const wxString& filePath, bool readOnly = true) :
130
 
    #ifdef __WXMSW__
131
 
            m_hFile(INVALID_HANDLE_VALUE),
132
 
            m_hsection(NULL),
133
 
    #else
134
 
            m_hFile(-1),
135
 
    #endif
136
 
            m_data(NULL),
137
 
            m_mapSize(0),
138
 
            m_open(false),
139
 
            m_isReadOnly(readOnly),
140
 
            m_lastError(0)
141
 
    {
142
 
        m_lastError = MapFile(filePath, readOnly);
143
 
    }
144
 
    ///Destructor which implicitly unmaps the file
145
 
    ~wxMemoryMappedFile()
146
 
    {
147
 
        UnmapFile();
148
 
    }
149
 
    ///Last error recorded
150
 
    unsigned GetLastError()
151
 
    {
152
 
        return m_lastError;
153
 
    }
154
 
    ///Indicates whether a file is currently (and successfully) mapped
155
 
    bool IsOk() const
156
 
    {
157
 
        return m_open;
158
 
    }
159
 
    ///Indicates whether the current file mapping is read only
160
 
    bool IsReadOnly() const
161
 
    {
162
 
        return m_isReadOnly;
163
 
    }
164
 
    /**
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.
168
 
 
169
 
    \exception wxMemoryMappedInvalidFileSize
170
 
    \exception wxMemoryMappedFileEmptyException
171
 
    \exception wxMemoryMappedFileShareViolationException
172
 
    \exception wxMemoryMappedInvalidFileType
173
 
    \exception wxMemoryMappedInvalidFileSize
174
 
 
175
 
    \param filePath Path to the file to map.
176
 
    \param readOnly Flag specifying whether to open the file as read only.
177
 
 
178
 
    \return True if file mapping was successful.
179
 
    */
180
 
    long MapFile(const wxString& filePath, const bool readOnly = true);
181
 
    ///Closes the handles and mappings
182
 
    void UnmapFile();
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().
186
 
    */
187
 
    void* GetStream()
188
 
    {
189
 
        return m_data;
190
 
    }
191
 
    ///Returns the length of the mapped file
192
 
    size_t GetMapSize() const
193
 
    {
194
 
        return m_mapSize;
195
 
    }
196
 
    ///Returns the path of the file currently mapped
197
 
    wxString GetFilePath() const
198
 
    {
199
 
        return m_filePath;
200
 
    }
201
 
 
202
 
    ///Returns the size of a large file (as an unsigned long long)
203
 
    static wxULongLong GetFileSize64(const wxMemoryMappedFileHandleType hFile);
204
 
 
205
 
    private:
206
 
        void Reset();
207
 
      #ifdef __WXMSW__
208
 
        HANDLE m_hFile;
209
 
        HANDLE m_hsection;
210
 
      #else
211
 
        int m_hFile;
212
 
      #endif
213
 
        void* m_data;
214
 
        size_t m_mapSize;
215
 
        wxString m_filePath;
216
 
        bool m_open;
217
 
        bool m_isReadOnly;
218
 
        unsigned m_lastError;
219
 
};
220
 
 
221
 
#endif //__WXMEMMAPPEDFILE_H__