/*---------------------------------------------------------------------------------------------- This method opens the given file according to the given mode settings. NOTE: This now creates a file if it doesn't already exist, but opens it if it does exist, if STGM_READWRITE alone is set. @param pszFile Name (or path) of the desired file @param grfstgm Combination of flags kfstgmXxxx from enum in FileStrm.h ----------------------------------------------------------------------------------------------*/ void FileStream::Init(LPCOLESTR pszFile, int grfstgm) { // To access another file this stream should be closed and another created. AssertPsz(pszFile); Assert(*pszFile); Assert(!m_hfile); HANDLE hfile = 0; DWORD dwDesiredAccess = 0; DWORD dwShareMode; DWORD dwCreationDisposition = 0; // Set the dwShareMode parameter. if (grfstgm & kfstgmShareDenyNone) dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; else if (grfstgm & kfstgmShareDenyRead) dwShareMode = FILE_SHARE_WRITE; else if (grfstgm & kfstgmShareExclusive) dwShareMode = 0; else dwShareMode = FILE_SHARE_READ; if (grfstgm & kfstgmReadWrite) dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; else if (grfstgm & kfstgmWrite) dwDesiredAccess = GENERIC_WRITE; else dwDesiredAccess = GENERIC_READ; if (dwDesiredAccess & GENERIC_WRITE) { if (grfstgm & kfstgmCreate) dwCreationDisposition = CREATE_ALWAYS; else if (grfstgm & kfstgmReadWrite) dwCreationDisposition = OPEN_ALWAYS; // Allows writing to existing file. else dwCreationDisposition = CREATE_NEW; } else dwCreationDisposition = OPEN_EXISTING; hfile = ::CreateFileW(pszFile, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); if (hfile == INVALID_HANDLE_VALUE) { HRESULT hr = (HRESULT)::GetLastError(); int stid = ErrorStringId(hr); StrUni stuRes(stid); StrUni stuMsg; stuMsg.Format(L"%s%n%s", stuRes.Chars(), pszFile); ThrowHr(hr, stuMsg.Chars()); // Caller should handle. (This is not a COM method). } m_hfile = hfile; m_staPath = pszFile; m_grfstgm = grfstgm; // Store the access mode for the stat method. }
/*---------------------------------------------------------------------------------------------- This method opens the given file according to the given mode settings. NOTE: This now creates a file if it doesn't already exist, but opens it if it does exist, if STGM_READWRITE alone is set. NOTE: STGM_SHARE_* flags are ignored on Linux, files are always 'shared' @param pszFile Name (or path) of the desired file @param grfstgm Combination of flags kfstgmXxxx from enum in FileStrm.h ----------------------------------------------------------------------------------------------*/ void FileStream::Init(LPCOLESTR pszFile, int grfstgm) { /* TODO: To access another file this stream should be closed and another created. */ AssertPsz(pszFile); Assert(*pszFile); #if WIN32 Assert(!m_hfile); HANDLE hfile = 0; DWORD dwDesiredAccess = 0; DWORD dwShareMode; DWORD dwCreationDisposition = 0; // Set the dwShareMode parameter. if (grfstgm & kfstgmShareDenyNone) dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; else if (grfstgm & kfstgmShareDenyRead) dwShareMode = FILE_SHARE_WRITE; else if (grfstgm & kfstgmShareExclusive) dwShareMode = 0; else dwShareMode = FILE_SHARE_READ; if (grfstgm & kfstgmReadWrite) dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; else if (grfstgm & kfstgmWrite) dwDesiredAccess = GENERIC_WRITE; else dwDesiredAccess = GENERIC_READ; if (dwDesiredAccess & GENERIC_WRITE) { if (grfstgm & kfstgmCreate) dwCreationDisposition = CREATE_ALWAYS; else if (grfstgm & kfstgmReadWrite) dwCreationDisposition = OPEN_ALWAYS; // Allows writing to existing file. else dwCreationDisposition = CREATE_NEW; } else dwCreationDisposition = OPEN_EXISTING; hfile = ::CreateFileW(pszFile, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); if (hfile == INVALID_HANDLE_VALUE) { HRESULT hr = (HRESULT)::GetLastError(); int stid = ErrorStringId(hr); StrUni stuRes(stid); StrUni stuMsg; stuMsg.Format(L"%s%n%s", stuRes.Chars(), pszFile); ThrowHr(hr, stuMsg.Chars()); // Caller should handle. (This is not a COM method). } m_hfile = hfile; m_staPath = pszFile; m_grfstgm = grfstgm; // Store the access mode for the stat method. #else Assert(m_file < 0); int file = -1; int flags = 0; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; //std::cout << kfstgmReadWrite << ", " << kfstgmRead << ", " << kfstgmWrite << ", " << kfstgmCreate << std::endl; //std::cout << O_RDWR << ", " << O_RDONLY << ", " << O_WRONLY << ", " << O_CREAT << std::endl; if (grfstgm & kfstgmReadWrite) { flags |= O_RDWR; } else if (grfstgm & kfstgmWrite) { flags |= O_WRONLY; } else { flags |= O_RDONLY; } if (grfstgm & kfstgmCreate) { flags |= O_CREAT; } if (grfstgm == kfstgmReadWrite) { flags |= O_CREAT; } if (flags & O_WRONLY | flags & O_RDWR) { if (grfstgm & kfstgmCreate) flags |= O_TRUNC; else if (grfstgm & kfstgmReadWrite) flags |= O_CREAT; else flags |= O_CREAT | O_EXCL; } m_staPath = pszFile; file = open(m_staPath, flags, mode); if (file < 0) { //File open failed, throw error ThrowHr(ERROR_OPEN_FAILED); } m_flags = flags; m_file = file; #endif }