1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/fxcrt/fx_ext.h"
8 #include "fxcrt_windows.h"
9 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
10 FX_BOOL FX_File_Exist(FX_BSTR fileName)
12 FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr());
16 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
18 FX_BOOL FX_File_Exist(FX_WSTR fileName)
20 FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr());
24 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
26 IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator)
29 return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Win64;
31 return FX_NEW CFXCRT_FileAccess_Win64;
34 void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dwShare, FX_DWORD &dwCreation)
36 dwAccess = GENERIC_READ;
37 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
38 if (!(dwMode & FX_FILEMODE_ReadOnly)) {
39 dwAccess |= GENERIC_WRITE;
40 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS;
42 dwCreation = OPEN_EXISTING;
48 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
49 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
53 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64()
57 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64()
61 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_BSTR fileName, FX_DWORD dwMode)
66 FX_DWORD dwAccess, dwShare, dwCreation;
67 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
68 m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
69 if (m_hFile == INVALID_HANDLE_VALUE) {
72 return m_hFile != NULL;
74 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_WSTR fileName, FX_DWORD dwMode)
79 FX_DWORD dwAccess, dwShare, dwCreation;
80 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
81 m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
82 if (m_hFile == INVALID_HANDLE_VALUE) {
85 return m_hFile != NULL;
87 void CFXCRT_FileAccess_Win64::Close()
92 ::CloseHandle(m_hFile);
95 void CFXCRT_FileAccess_Win64::Release(IFX_Allocator* pAllocator)
98 FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Win64);
103 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const
108 LARGE_INTEGER size = {0, 0};
109 if (!::GetFileSizeEx(m_hFile, &size)) {
112 return (FX_FILESIZE)size.QuadPart;
114 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const
117 return (FX_FILESIZE) - 1;
119 LARGE_INTEGER dist = {0, 0};
120 LARGE_INTEGER newPos = {0, 0};
121 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) {
122 return (FX_FILESIZE) - 1;
124 return (FX_FILESIZE)newPos.QuadPart;
126 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos)
129 return (FX_FILESIZE) - 1;
133 LARGE_INTEGER newPos = {0, 0};
134 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) {
135 return (FX_FILESIZE) - 1;
137 return (FX_FILESIZE)newPos.QuadPart;
139 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer)
145 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) {
150 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer)
156 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)) {
161 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
166 if (pos >= GetSize()) {
169 if (SetPosition(pos) == (FX_FILESIZE) - 1) {
172 return Read(pBuffer, szBuffer);
174 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
179 if (SetPosition(pos) == (FX_FILESIZE) - 1) {
182 return Write(pBuffer, szBuffer);
184 FX_BOOL CFXCRT_FileAccess_Win64::Flush()
189 return ::FlushFileBuffers(m_hFile);
191 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile)
193 if (SetPosition(szFile) == (FX_FILESIZE) - 1) {
196 return ::SetEndOfFile(m_hFile);
198 FX_BOOL FX_File_Delete(FX_BSTR fileName)
200 return ::DeleteFileA(fileName.GetCStr());
202 FX_BOOL FX_File_Delete(FX_WSTR fileName)
204 return ::DeleteFileW((LPCWSTR)fileName.GetPtr());
206 FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
208 return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE);
210 FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
212 return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr(), FALSE);
214 FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
216 return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
218 FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
220 return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr());