Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cMemoryMappedFile::cMemoryMappedFile(long FileSize_, OpenModes OpenMode_,AccessModes AccessMode_,
- ShareModes ShareMode_,long Flags_,void *Security_,FILEHANDLE Template_) {
- FileSize = FileSize_;
- char buffer[BUFFER_SIZE];
- #if defined(__windows)
- DWORD dwRetVal = 0;
- UINT uRetVal = 0;
- DWORD dwPtr = 0;
- BOOL isSetEndOfFile = FALSE;
- LARGE_INTEGER Distance_;
- DWORD ErrorCode = 0;
- char lpTempPathBuffer[MAX_PATH];
- // Gets the temp path env string (no guarantee it's a valid path).
- dwRetVal = GetTempPath(MAX_PATH, // length of the buffer
- lpTempPathBuffer); // buffer for path
- if (dwRetVal > MAX_PATH || (dwRetVal == 0))
- throw cException(ERR_MEMORYMAPPING,"");
- }
- // Generates a temporary file name.
- uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
- TEXT("DEMO"), // temp file name prefix
- 0, // create unique name
- TempFileName); // buffer for name
- if (uRetVal == 0)
- {
- throw cException(ERR_MEMORYMAPPING,lpTempPathBuffer);
- }
- // Creates the new file to write to for the upper-case version.
- hFile = CreateFile((LPTSTR) TempFileName, // file name
- AccessMode_, // open for write
- 0, // do not share
- (SECURITY_ATTRIBUTES *) Security_, // default security
- OpenMode_, // CREATE_ALWAYS, // overwrite existing
- Flags_,// normal file
- Template_); // no template
- if (hFile == INVALID_HANDLE_VALUE)
- {
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- Distance_.LowPart = (ULONG)FileSize_;
- Distance_.HighPart = 0;
- dwPtr = ::SetFilePointer(hFile,Distance_.LowPart,
- &(Distance_.HighPart), FileBegin);
- if (dwPtr == INVALID_SET_FILE_POINTER){
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- isSetEndOfFile = SetEndOfFile(hFile);
- if (!isSetEndOfFile){
- ErrorCode = GetLastError();
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- hMapping=::CreateFileMapping(hFile,(SECURITY_ATTRIBUTES*)Security_,PAGE_READWRITE,0,0,0);
- if (hMapping==NULL)
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- MapPtr = (void*)::MapViewOfFile( hMapping, FILE_MAP_WRITE | FILE_MAP_READ,0,0,FileSize);
- if (MapPtr==NULL){
- ErrorCode = GetLastError();
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- #elif defined(__unix)
- int oflag = 0;
- strcpy(TempFileName,"/var/tmp/pdn-XXXXXXX");
- if ((hFile=mkstemp(TempFileName)) < 0){
- perror("generating temp filename");
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- FileSize = FileSize_;
- // To do this on UNIX, seek to (RequiredFileSize - 1) and then write a byte.
- // The value of the byte can be anything, but zero is the obvious choice
- if (lseek(hFile,FileSize_-1,0) == -1){
- perror("lseek");
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- memset(buffer,'\x0',BUFFER_SIZE);
- if (write(hFile,buffer,1) == -1){
- perror("write");
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- }
- MapPtr = (void *)mmap(NULL, FileSize, PROT_READ | PROT_WRITE, MAP_SHARED, hFile, 0);
- if (MapPtr==NULL)
- throw cException(ERR_MEMORYMAPPING,TempFileName);
- #endif
- FilePath=new char[strlen(TempFileName)+1];
- strcpy(FilePath,TempFileName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement