Advertisement
Ember

File::Open

Jul 29th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. // -------------------------------------------------------------------------------------
  2. bool File::Open(String filename, File::Mode filemode)
  3. {
  4.     DWORD fileAccessPolicy, fileCreationPolicy;
  5.     HRESULT result;
  6.  
  7.     // Set the properties
  8.     fileAccessPolicy = fileCreationPolicy = 0;
  9.     if((Byte)filemode & (Byte)File::Mode::Read) { fileAccessPolicy += GENERIC_READ; fileCreationPolicy = OPEN_EXISTING; }
  10.     //if(filemode & File::Mode::fWrite) { fileAccessPolicy += GENERIC_WRITE; fileCreationPolicy = CREATE_ALWAYS; }
  11.     if((Byte)filemode & (Byte)File::Mode::Write || (Byte)filemode & (Byte)File::Mode::Overwrite) { fileAccessPolicy += GENERIC_WRITE; fileCreationPolicy = (Byte)filemode & (Byte)File::Mode::Overwrite ? CREATE_ALWAYS : OPEN_ALWAYS; }
  12.  
  13.     // Check if local path
  14.     Int pos = filename.find(":");
  15.     if(pos < 0) { filename = Path + "/" + filename; }
  16.  
  17.     // Open the file
  18.     mFile = CreateFileW(widen(filename).c_str(), fileAccessPolicy, 0, nullptr, fileCreationPolicy, FILE_ATTRIBUTE_NORMAL, nullptr);
  19.     if(FAILED(mFile)) { return false; }
  20.  
  21.     // Get the file info
  22.     result = GetFileInformationByHandle(mFile, &mFileInfo);
  23.     if(FAILED(result)) { return false; }
  24.  
  25.     // Set public file information
  26.     FileSize = mFileInfo.nFileSizeLow;
  27.  
  28.     // Allocate some memory for the read operations
  29.     Data = malloc(1024);
  30.  
  31.     // --
  32.     Opened = true;
  33.     //End = Position = 0;
  34.     //Seek(File::Offset::Begin, 0);
  35.  
  36.     // Create a file object and return it
  37.     return true;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement