Advertisement
Guest User

Frank Chang

a guest
Feb 24th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. cMemoryMappedFile::cMemoryMappedFile(long FileSize_, OpenModes OpenMode_,AccessModes AccessMode_,
  2. ShareModes ShareMode_,long Flags_,void *Security_,FILEHANDLE Template_) {
  3. FileSize = FileSize_;
  4. char buffer[BUFFER_SIZE];
  5. #if defined(__windows)
  6. DWORD dwRetVal = 0;
  7. UINT uRetVal = 0;
  8. DWORD dwPtr = 0;
  9. BOOL isSetEndOfFile = FALSE;
  10. LARGE_INTEGER Distance_;
  11. DWORD ErrorCode = 0;
  12.  
  13. char lpTempPathBuffer[MAX_PATH];
  14.  
  15.  
  16. // Gets the temp path env string (no guarantee it's a valid path).
  17. dwRetVal = GetTempPath(MAX_PATH, // length of the buffer
  18. lpTempPathBuffer); // buffer for path
  19. if (dwRetVal > MAX_PATH || (dwRetVal == 0))
  20. throw cException(ERR_MEMORYMAPPING,"");
  21. }
  22.  
  23. // Generates a temporary file name.
  24. uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
  25. TEXT("DEMO"), // temp file name prefix
  26. 0, // create unique name
  27. TempFileName); // buffer for name
  28. if (uRetVal == 0)
  29. {
  30.  
  31. throw cException(ERR_MEMORYMAPPING,lpTempPathBuffer);
  32. }
  33. // Creates the new file to write to for the upper-case version.
  34. hFile = CreateFile((LPTSTR) TempFileName, // file name
  35. AccessMode_, // open for write
  36. 0, // do not share
  37. (SECURITY_ATTRIBUTES *) Security_, // default security
  38. OpenMode_, // CREATE_ALWAYS, // overwrite existing
  39. Flags_,// normal file
  40. Template_); // no template
  41. if (hFile == INVALID_HANDLE_VALUE)
  42. {
  43. throw cException(ERR_MEMORYMAPPING,TempFileName);
  44. }
  45. Distance_.LowPart = (ULONG)FileSize_;
  46. Distance_.HighPart = 0;
  47. dwPtr = ::SetFilePointer(hFile,Distance_.LowPart,
  48. &(Distance_.HighPart), FileBegin);
  49.  
  50. if (dwPtr == INVALID_SET_FILE_POINTER){
  51. throw cException(ERR_MEMORYMAPPING,TempFileName);
  52. }
  53. isSetEndOfFile = SetEndOfFile(hFile);
  54. if (!isSetEndOfFile){
  55. ErrorCode = GetLastError();
  56. throw cException(ERR_MEMORYMAPPING,TempFileName);
  57. }
  58. hMapping=::CreateFileMapping(hFile,(SECURITY_ATTRIBUTES*)Security_,PAGE_READWRITE,0,0,0);
  59. if (hMapping==NULL)
  60. throw cException(ERR_MEMORYMAPPING,TempFileName);
  61. MapPtr = (void*)::MapViewOfFile( hMapping, FILE_MAP_WRITE | FILE_MAP_READ,0,0,FileSize);
  62. if (MapPtr==NULL){
  63. ErrorCode = GetLastError();
  64. throw cException(ERR_MEMORYMAPPING,TempFileName);
  65. }
  66. #elif defined(__unix)
  67. int oflag = 0;
  68. strcpy(TempFileName,"/var/tmp/pdn-XXXXXXX");
  69. if ((hFile=mkstemp(TempFileName)) < 0){
  70. perror("generating temp filename");
  71. throw cException(ERR_MEMORYMAPPING,TempFileName);
  72. }
  73. FileSize = FileSize_;
  74. // To do this on UNIX, seek to (RequiredFileSize - 1) and then write a byte.
  75. // The value of the byte can be anything, but zero is the obvious choice
  76. if (lseek(hFile,FileSize_-1,0) == -1){
  77. perror("lseek");
  78. throw cException(ERR_MEMORYMAPPING,TempFileName);
  79. }
  80. memset(buffer,'\x0',BUFFER_SIZE);
  81. if (write(hFile,buffer,1) == -1){
  82. perror("write");
  83. throw cException(ERR_MEMORYMAPPING,TempFileName);
  84. }
  85. MapPtr = (void *)mmap(NULL, FileSize, PROT_READ | PROT_WRITE, MAP_SHARED, hFile, 0);
  86. if (MapPtr==NULL)
  87. throw cException(ERR_MEMORYMAPPING,TempFileName);
  88. #endif
  89. FilePath=new char[strlen(TempFileName)+1];
  90. strcpy(FilePath,TempFileName);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement