Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

memorymappedfile.h

/**
\class wxMemoryMappedFile
\author Blake Madden (blake.madden@oleandersolutions.com)
\date (C) 2006
*/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the wxWidgets License.                          *
 *                                                                         *
 ***************************************************************************/

/** \example wxMemoryMappedFile
  try<br>
    {<br>
    wxMemoryMappedFile fileMap(_T("/home/bmadden/file.txt"), <b>true</b>);<br>
    const char* fileText = (const char*)fileMap.GetStream();<br><br>
    <b>//now map another file (note that fileText will not be valid after this)</b><br>
    fileMap.UnmapFile();<br>
    fileMap.MapFile(_T("/home/bmadden/DifferentFile.txt"), <b>false</b>);<br>
    char* writableFileText = (char*)fileMap.GetStream();<br><br>
    <b>//now write back to the file by simply writing to the pointer</b><br>
    std::strncpy(writableFileText, "Hello, world!", 13);<br>
    }<br>
  catch(...)<br>
    {<br>
    //handle error here<br>
    }

  \todo Currently only supports files under 2GBs.
*/
/** \example wxMemoryMappedFile
  try
    {
        wxMemoryMappedFile fileMap(_T("/home/bmadden/file.txt"), true);
        const char* fileText = (const char*)fileMap.GetStream();

        //now map another file (note that fileText will not be valid after this)
        fileMap.UnmapFile();
        fileMap.MapFile(_T("/home/bmadden/DifferentFile.txt"), false);
        char* writableFileText = (char*)fileMap.GetStream();
        //now write back to the file by simply writing to the pointer
        std::strncpy(writableFileText, "Hello, world!", 13);
    }
  catch(...)
    {
        //handle error here
    }

  \todo Currently only supports files under 2GBs.
*/

#ifndef __WXMEMMAPPEDFILE_H__
#define __WXMEMMAPPEDFILE_H__

// NB: "UNICODE" must be defined for this code to compile
#if defined(wxUSE_UNICODE)
#define UNICODE
#endif

#ifdef __WXMSW__
#include <windows.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#endif
#include <wx/string.h>
#include <wx/longlong.h>
#include <exception>

///General exception that can be thrown when mapping a file.
00074 class wxMemoryMappedFileException : public std::exception
    {};
///Exception that can be thrown when mapping if the file is zero length.
00077 class wxMemoryMappedFileEmptyException : public std::exception
    {};
///Exception that can be thrown when mapping if the file can't be exclusively locked.
00080 class wxMemoryMappedFileShareViolationException : public std::exception
    {};
///Exception that can be thrown when mapping if the file isn't something that can be mapped.
00083 class wxMemoryMappedInvalidFileType : public std::exception
    {};
///Exception that can be thrown when mapping if the size of the file can't be determined.
00086 class wxMemoryMappedInvalidFileSize : public std::exception
    {};

#ifdef __WXMSW__
typedef HANDLE wxMemoryMappedFileHandleType;
#else
typedef int wxMemoryMappedFileHandleType;
#endif

/**Class for mapping a file into your address space
(rather than having to buffer its contents)*/
// ----------------------------------------------------------------------------
00098 class wxMemoryMappedFile
// ----------------------------------------------------------------------------
{
public:
    ///Default Constructor
00103     wxMemoryMappedFile() :
    #ifdef __WXMSW__
            m_hFile(INVALID_HANDLE_VALUE),
            m_hsection(NULL),
    #else
            m_hFile(-1),
    #endif
            m_data(NULL),
            m_mapSize(0),
            m_open(false),
            m_isReadOnly(false),
            m_lastError(0)
    {}

    /**
    \brief Constructor which will automatically map the file.

    \exception wxMemoryMappedInvalidFileSize
    \exception wxMemoryMappedFileEmptyException
    \exception wxMemoryMappedFileShareViolationException
    \exception wxMemoryMappedInvalidFileType
    \exception wxMemoryMappedInvalidFileSize

    \param filePath Path to the file to map.
    \param readOnly Flag specifying whether to open the file as read only.
    */
00129     wxMemoryMappedFile(const wxString& filePath, bool readOnly = true) :
    #ifdef __WXMSW__
            m_hFile(INVALID_HANDLE_VALUE),
            m_hsection(NULL),
    #else
            m_hFile(-1),
    #endif
            m_data(NULL),
            m_mapSize(0),
            m_open(false),
            m_isReadOnly(readOnly),
            m_lastError(0)
    {
        m_lastError = MapFile(filePath, readOnly);
    }
    ///Destructor which implicitly unmaps the file
00145     ~wxMemoryMappedFile()
    {
        UnmapFile();
    }
    ///Last error recorded
00150     unsigned GetLastError()
    {
        return m_lastError;
    }
    ///Indicates whether a file is currently (and successfully) mapped
00155     bool IsOk() const
    {
        return m_open;
    }
    ///Indicates whether the current file mapping is read only
00160     bool IsReadOnly() const
    {
        return m_isReadOnly;
    }
    /**
    \brief Manually maps a new file.
    \warning  If this object is currently mapping another file then
    you need to call UnmapFile() first.

    \exception wxMemoryMappedInvalidFileSize
    \exception wxMemoryMappedFileEmptyException
    \exception wxMemoryMappedFileShareViolationException
    \exception wxMemoryMappedInvalidFileType
    \exception wxMemoryMappedInvalidFileSize

    \param filePath Path to the file to map.
    \param readOnly Flag specifying whether to open the file as read only.

    \return True if file mapping was successful.
    */
    long MapFile(const wxString& filePath, const bool readOnly = true);
    ///Closes the handles and mappings
    void UnmapFile();
    /**Returns the raw byte stream of the file
    \warning Do not attempt to write to the returned pointer if you mapped the file as read only.
    The read only status of the current mapping can be checked by calling IsReadOnly().
    */
00187     void* GetStream()
    {
        return m_data;
    }
    ///Returns the length of the mapped file
00192     size_t GetMapSize() const
    {
        return m_mapSize;
    }
    ///Returns the path of the file currently mapped
00197     wxString GetFilePath() const
    {
        return m_filePath;
    }

    ///Returns the size of a large file (as an unsigned long long)
    static wxULongLong GetFileSize64(const wxMemoryMappedFileHandleType hFile);

    private:
        void Reset();
      #ifdef __WXMSW__
        HANDLE m_hFile;
        HANDLE m_hsection;
      #else
        int m_hFile;
      #endif
        void* m_data;
        size_t m_mapSize;
        wxString m_filePath;
        bool m_open;
        bool m_isReadOnly;
        unsigned m_lastError;
};

#endif //__WXMEMMAPPEDFILE_H__

Generated by  Doxygen 1.6.0   Back to index