Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / C++

Project Line Counter Add-In v2.10 for VS.NET and VC6

Rate me:
Please Sign up or sign in to vote.
4.92/5 (38 votes)
29 Jun 2003 448K   5.3K   142  
Get statistics about your source code with a click of a button
// ReadOnlyMemMappedFile.cpp: implementation of the CReadOnlyMemMappedFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ReadOnlyMemMappedFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CReadOnlyMemMappedFile::CReadOnlyMemMappedFile(LPCTSTR pszFileName) : 
    m_pBuf(NULL)
{
    // define a threshold beyond which a file is mapped rather than read
    // into memory
    const int threshold = 64 * 1024 - 1;

    m_hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ,
        NULL, OPEN_EXISTING, 0, NULL);
    if (m_hFile == INVALID_HANDLE_VALUE) 
        throw "Could not open file";

    DWORD dwSizeLow, dwSizeHigh;
    dwSizeLow = ::GetFileSize(m_hFile, &dwSizeHigh);
    ASSERT(dwSizeHigh == 0); // not actually built for use with huge files
    m_iFileSize = dwSizeLow;
    if (dwSizeLow > threshold)
    {
        // use memory map
        m_hMap = CreateFileMapping(m_hFile, NULL, PAGE_READONLY,
            0, 0, NULL);
        ASSERT(m_hMap != INVALID_HANDLE_VALUE);

        // map the file and store the memory pointer
        m_pBuf = (BYTE *)MapViewOfFileEx(m_hMap, FILE_MAP_READ, 0, 0, 0, 0);
    }
    else
    {
        // <= threshold, read the entire file into memory
        m_pBuf = new BYTE[m_iFileSize];
        DWORD dwRead;
        ReadFile(m_hFile, m_pBuf, m_iFileSize, &dwRead, NULL);
        ASSERT(dwRead == m_iFileSize);
        CloseHandle(m_hFile);
        m_hFile = INVALID_HANDLE_VALUE;  // not using the file any more
        m_hMap  = INVALID_HANDLE_VALUE;  // not using a memory map
    }
}

CReadOnlyMemMappedFile::~CReadOnlyMemMappedFile()
{
    if (m_hMap != INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_hMap);
    }
    else
    {
        // if it wasn't a memory map, it means we have a regular buffer
        delete [] m_pBuf;
    }
    if (m_hFile)
    {
        CloseHandle(m_hFile);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Experion
Canada Canada
You may know Oz from his WndTabs days. Oz has long since left client side development to work on web technologies and to consult in the R&D management field.

Comments and Discussions