/**---------------------------------------------------------------------------- \brief 파일 IO 를 위해 파일을 오픈한다. \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOpenForRead( IN std::wstring FilePath ) { if (TRUE == Initialized()) { FIOClose(); } mReadOnly = TRUE; if (TRUE != is_file_existsW(FilePath.c_str())) { log_err "no file exists. file=%ws", FilePath.c_str() log_end return DTS_NO_FILE_EXIST; }
/**---------------------------------------------------------------------------- \brief 파일 IO 를 위해 파일을 오픈한다. \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOpenForRead( IN std::wstring FilePath ) { if (TRUE == Initialized()) { FIOClose(); } mReadOnly = TRUE; if (TRUE != is_file_existsW(FilePath.c_str())) { printf( "no file exists. file=%ws", FilePath.c_str() ); return DTS_NO_FILE_EXIST; } #pragma warning(disable: 4127) DTSTATUS status = DTS_WINAPI_FAILED; do { mFileHandle = CreateFileW( FilePath.c_str(), GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (INVALID_HANDLE_VALUE == mFileHandle) { printf( "CreateFile(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } // check file size // if (TRUE != GetFileSizeEx(mFileHandle, &mFileSize)) { printf( "%ws, can not get file size, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } mFileMap = CreateFileMapping( mFileHandle, NULL, PAGE_READONLY, 0, 0, NULL ); if (NULL == mFileMap) { printf( "CreateFileMapping(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } status = DTS_SUCCESS; } while (FALSE); #pragma warning(default: 4127) if (TRUE != DT_SUCCEEDED(status)) { if (INVALID_HANDLE_VALUE != mFileHandle) { CloseHandle(mFileHandle); mFileHandle = INVALID_HANDLE_VALUE; } if (NULL != mFileMap) CloseHandle(mFileMap); } return status; }
/**---------------------------------------------------------------------------- \brief FileSize 바이트 짜리 파일을 생성한다. \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOCreateFile( IN std::wstring FilePath, IN LARGE_INTEGER FileSize ) { if (TRUE == Initialized()) { FIOClose(); } if (FileSize.QuadPart == 0) return DTS_INVALID_PARAMETER; mReadOnly = FALSE; #pragma warning(disable: 4127) DTSTATUS status = DTS_WINAPI_FAILED; do { mFileSize = FileSize; mFileHandle = CreateFileW( FilePath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, // write 도중 다른 프로세스에서 읽기가 가능 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if (INVALID_HANDLE_VALUE == mFileHandle) { printf( "CreateFile(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } // increase file size // if (TRUE != SetFilePointerEx(mFileHandle, mFileSize, NULL, FILE_BEGIN)) { printf( "SetFilePointerEx( move to %I64d ) failed, gle=0x%08x", FileSize.QuadPart, GetLastError() ); break; } if (TRUE != SetEndOfFile(mFileHandle)) { printf("SetEndOfFile( ) failed, gle=0x%08x", GetLastError()); break; } mFileMap = CreateFileMapping( mFileHandle, NULL, PAGE_READWRITE, 0, 0, NULL ); if (NULL == mFileMap) { printf( "CreateFileMapping(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } status = DTS_SUCCESS; } while (FALSE); #pragma warning(default: 4127) if (TRUE != DT_SUCCEEDED(status)) { if (INVALID_HANDLE_VALUE != mFileHandle) { CloseHandle(mFileHandle); mFileHandle = INVALID_HANDLE_VALUE; } if (NULL != mFileMap) CloseHandle(mFileMap); } return status; }