MemoryMap.cpp

Go to the documentation of this file.
00001 
00011 #include "MemoryMap.h"
00012 
00013 
00014 MemoryMap::MemoryMap ()
00015 {
00016     hFile = NULL;
00017     hMap = NULL;
00018     dataPtr = NULL;
00019     fileSizeLow = 0;
00020     fileSizeHigh = 0;
00021 }
00022 
00023 MemoryMap::~MemoryMap()
00024 {
00025     close();
00026 }
00027 
00028 
00029 void MemoryMap::close()
00030 {
00031     if (dataPtr)
00032         UnmapViewOfFile (dataPtr);
00033 
00034     if (hMap)
00035         CloseHandle (hMap);
00036 
00037     if (hFile)
00038         CloseHandle(hFile);
00039 
00040     dataPtr = NULL;
00041     hMap = NULL;
00042     hFile = NULL;
00043 
00044     fileSizeLow = 0;
00045     fileSizeHigh = 0;
00046 }
00047 
00054 void MemoryMap::load (const char *filename)
00055 {
00056     close();
00057 
00058     try 
00059     {
00060         hFile = CreateFile (
00061             filename, 
00062             GENERIC_READ,
00063             FILE_SHARE_READ,
00064             NULL,
00065             OPEN_EXISTING,
00066             FILE_ATTRIBUTE_NORMAL,
00067             NULL);
00068         
00069         if (INVALID_HANDLE_VALUE == hFile)
00070             throw RuntimeError ("Couldn't open file: %s", filename);
00071         
00072         BY_HANDLE_FILE_INFORMATION info;
00073 
00074         GetFileInformationByHandle (
00075             hFile,
00076             &info);
00077 
00078         fileSizeLow = info.nFileSizeLow;
00079         fileSizeHigh = info.nFileSizeHigh;
00080 
00081         hMap = CreateFileMapping (
00082             hFile,
00083             NULL,
00084             PAGE_READONLY,
00085             info.nFileSizeHigh,
00086             info.nFileSizeLow,
00087             NULL);
00088 
00089 
00090         if (hMap == NULL)
00091             throw DiskError ("Couldn't create mapping for file: %s", filename);
00092 
00093         dataPtr = MapViewOfFile (
00094             hMap,
00095             FILE_MAP_READ,
00096             0, 0,
00097             0); // whole file
00098 
00099         if (dataPtr == NULL)
00100             throw DiskError ("Couldn't map view of file: %s", filename);
00101 
00102     }
00103     catch (...)
00104     {
00105         close();
00106         throw;
00107     }
00108 }

Generated on Tue May 21 03:34:51 2002 for Archimedes by doxygen1.2.15