OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fxcrt/fx_string.h" | 7 #include "../../include/fxcrt/fx_string.h" |
8 #include "fxcrt_windows.h" | 8 #include "fxcrt_windows.h" |
9 | 9 |
10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
11 FX_BOOL FX_File_Exist(const CFX_ByteStringC& fileName) | 11 FX_BOOL FX_File_Exist(const CFX_ByteStringC& fileName) { |
12 { | 12 FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr()); |
13 FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr()); | 13 if (dwAttri == -1) { |
14 if (dwAttri == -1) { | 14 return FALSE; |
15 return FALSE; | 15 } |
16 } | 16 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; |
17 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; | 17 } |
18 } | 18 FX_BOOL FX_File_Exist(const CFX_WideStringC& fileName) { |
19 FX_BOOL FX_File_Exist(const CFX_WideStringC& fileName) | 19 FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr()); |
20 { | 20 if (dwAttri == -1) { |
21 FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr()); | 21 return FALSE; |
22 if (dwAttri == -1) { | 22 } |
23 return FALSE; | 23 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; |
24 } | 24 } |
25 return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; | 25 IFXCRT_FileAccess* FXCRT_FileAccess_Create() { |
26 } | 26 return new CFXCRT_FileAccess_Win64; |
27 IFXCRT_FileAccess* FXCRT_FileAccess_Create() | 27 } |
28 { | 28 void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, |
29 return new CFXCRT_FileAccess_Win64; | 29 FX_DWORD& dwAccess, |
30 } | 30 FX_DWORD& dwShare, |
31 void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dw
Share, FX_DWORD &dwCreation) | 31 FX_DWORD& dwCreation) { |
32 { | 32 dwAccess = GENERIC_READ; |
33 dwAccess = GENERIC_READ; | 33 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; |
34 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; | 34 if (!(dwMode & FX_FILEMODE_ReadOnly)) { |
35 if (!(dwMode & FX_FILEMODE_ReadOnly)) { | 35 dwAccess |= GENERIC_WRITE; |
36 dwAccess |= GENERIC_WRITE; | 36 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; |
37 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWA
YS; | 37 } else { |
38 } else { | 38 dwCreation = OPEN_EXISTING; |
39 dwCreation = OPEN_EXISTING; | 39 } |
40 } | |
41 } | 40 } |
42 #ifdef __cplusplus | 41 #ifdef __cplusplus |
43 extern "C" { | 42 extern "C" { |
44 #endif | 43 #endif |
45 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); | 44 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); |
46 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceTo
Move, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod); | 45 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, |
| 46 LARGE_INTEGER liDistanceToMove, |
| 47 PLARGE_INTEGER lpNewFilePointer, |
| 48 DWORD dwMoveMethod); |
47 #ifdef __cplusplus | 49 #ifdef __cplusplus |
48 } | 50 } |
49 #endif | 51 #endif |
50 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() | 52 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() : m_hFile(NULL) {} |
51 : m_hFile(NULL) | 53 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() { |
52 { | 54 Close(); |
53 } | 55 } |
54 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() | 56 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, |
55 { | 57 FX_DWORD dwMode) { |
56 Close(); | 58 if (m_hFile) { |
57 } | 59 return FALSE; |
58 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, FX_DWORD
dwMode) | 60 } |
59 { | 61 FX_DWORD dwAccess, dwShare, dwCreation; |
60 if (m_hFile) { | 62 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); |
61 return FALSE; | 63 m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, |
62 } | 64 dwCreation, FILE_ATTRIBUTE_NORMAL, NULL); |
63 FX_DWORD dwAccess, dwShare, dwCreation; | 65 if (m_hFile == INVALID_HANDLE_VALUE) { |
64 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); | |
65 m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreat
ion, FILE_ATTRIBUTE_NORMAL, NULL); | |
66 if (m_hFile == INVALID_HANDLE_VALUE) { | |
67 m_hFile = NULL; | |
68 } | |
69 return m_hFile != NULL; | |
70 } | |
71 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, FX_DWORD
dwMode) | |
72 { | |
73 if (m_hFile) { | |
74 return FALSE; | |
75 } | |
76 FX_DWORD dwAccess, dwShare, dwCreation; | |
77 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); | |
78 m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL,
dwCreation, FILE_ATTRIBUTE_NORMAL, NULL); | |
79 if (m_hFile == INVALID_HANDLE_VALUE) { | |
80 m_hFile = NULL; | |
81 } | |
82 return m_hFile != NULL; | |
83 } | |
84 void CFXCRT_FileAccess_Win64::Close() | |
85 { | |
86 if (!m_hFile) { | |
87 return; | |
88 } | |
89 ::CloseHandle(m_hFile); | |
90 m_hFile = NULL; | 66 m_hFile = NULL; |
91 } | 67 } |
92 void CFXCRT_FileAccess_Win64::Release() | 68 return m_hFile != NULL; |
93 { | 69 } |
94 delete this; | 70 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, |
95 } | 71 FX_DWORD dwMode) { |
96 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const | 72 if (m_hFile) { |
97 { | 73 return FALSE; |
98 if (!m_hFile) { | 74 } |
99 return 0; | 75 FX_DWORD dwAccess, dwShare, dwCreation; |
100 } | 76 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); |
101 LARGE_INTEGER size = {}; | 77 m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, |
102 if (!::GetFileSizeEx(m_hFile, &size)) { | 78 dwCreation, FILE_ATTRIBUTE_NORMAL, NULL); |
103 return 0; | 79 if (m_hFile == INVALID_HANDLE_VALUE) { |
104 } | 80 m_hFile = NULL; |
105 return (FX_FILESIZE)size.QuadPart; | 81 } |
106 } | 82 return m_hFile != NULL; |
107 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const | 83 } |
108 { | 84 void CFXCRT_FileAccess_Win64::Close() { |
109 if (!m_hFile) { | 85 if (!m_hFile) { |
110 return (FX_FILESIZE) - 1; | 86 return; |
111 } | 87 } |
112 LARGE_INTEGER dist = {}; | 88 ::CloseHandle(m_hFile); |
113 LARGE_INTEGER newPos = {}; | 89 m_hFile = NULL; |
114 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) { | 90 } |
115 return (FX_FILESIZE) - 1; | 91 void CFXCRT_FileAccess_Win64::Release() { |
116 } | 92 delete this; |
117 return (FX_FILESIZE)newPos.QuadPart; | 93 } |
118 } | 94 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const { |
119 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) | 95 if (!m_hFile) { |
120 { | 96 return 0; |
121 if (!m_hFile) { | 97 } |
122 return (FX_FILESIZE) - 1; | 98 LARGE_INTEGER size = {}; |
123 } | 99 if (!::GetFileSizeEx(m_hFile, &size)) { |
124 LARGE_INTEGER dist; | 100 return 0; |
125 dist.QuadPart = pos; | 101 } |
126 LARGE_INTEGER newPos = {}; | 102 return (FX_FILESIZE)size.QuadPart; |
127 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) { | 103 } |
128 return (FX_FILESIZE) - 1; | 104 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const { |
129 } | 105 if (!m_hFile) { |
130 return (FX_FILESIZE)newPos.QuadPart; | 106 return (FX_FILESIZE)-1; |
131 } | 107 } |
132 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) | 108 LARGE_INTEGER dist = {}; |
133 { | 109 LARGE_INTEGER newPos = {}; |
134 if (!m_hFile) { | 110 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) { |
135 return 0; | 111 return (FX_FILESIZE)-1; |
136 } | 112 } |
137 size_t szRead = 0; | 113 return (FX_FILESIZE)newPos.QuadPart; |
138 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL))
{ | 114 } |
139 return 0; | 115 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) { |
140 } | 116 if (!m_hFile) { |
141 return szRead; | 117 return (FX_FILESIZE)-1; |
142 } | 118 } |
143 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) | 119 LARGE_INTEGER dist; |
144 { | 120 dist.QuadPart = pos; |
145 if (!m_hFile) { | 121 LARGE_INTEGER newPos = {}; |
146 return 0; | 122 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) { |
147 } | 123 return (FX_FILESIZE)-1; |
148 size_t szWrite = 0; | 124 } |
149 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)
) { | 125 return (FX_FILESIZE)newPos.QuadPart; |
150 return 0; | 126 } |
151 } | 127 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) { |
152 return szWrite; | 128 if (!m_hFile) { |
153 } | 129 return 0; |
154 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILES
IZE pos) | 130 } |
155 { | 131 size_t szRead = 0; |
156 if (!m_hFile) { | 132 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) { |
157 return 0; | 133 return 0; |
158 } | 134 } |
159 if (pos >= GetSize()) { | 135 return szRead; |
160 return 0; | 136 } |
161 } | 137 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) { |
162 if (SetPosition(pos) == (FX_FILESIZE) - 1) { | 138 if (!m_hFile) { |
163 return 0; | 139 return 0; |
164 } | 140 } |
165 return Read(pBuffer, szBuffer); | 141 size_t szWrite = 0; |
166 } | 142 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, |
167 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, F
X_FILESIZE pos) | 143 NULL)) { |
168 { | 144 return 0; |
169 if (!m_hFile) { | 145 } |
170 return 0; | 146 return szWrite; |
171 } | 147 } |
172 if (SetPosition(pos) == (FX_FILESIZE) - 1) { | 148 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, |
173 return 0; | 149 size_t szBuffer, |
174 } | 150 FX_FILESIZE pos) { |
175 return Write(pBuffer, szBuffer); | 151 if (!m_hFile) { |
176 } | 152 return 0; |
177 FX_BOOL CFXCRT_FileAccess_Win64::Flush() | 153 } |
178 { | 154 if (pos >= GetSize()) { |
179 if (!m_hFile) { | 155 return 0; |
180 return FALSE; | 156 } |
181 } | 157 if (SetPosition(pos) == (FX_FILESIZE)-1) { |
182 return ::FlushFileBuffers(m_hFile); | 158 return 0; |
183 } | 159 } |
184 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) | 160 return Read(pBuffer, szBuffer); |
185 { | 161 } |
186 if (SetPosition(szFile) == (FX_FILESIZE) - 1) { | 162 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, |
187 return FALSE; | 163 size_t szBuffer, |
188 } | 164 FX_FILESIZE pos) { |
189 return ::SetEndOfFile(m_hFile); | 165 if (!m_hFile) { |
190 } | 166 return 0; |
191 FX_BOOL FX_File_Delete(const CFX_ByteStringC& fileName) | 167 } |
192 { | 168 if (SetPosition(pos) == (FX_FILESIZE)-1) { |
193 return ::DeleteFileA(fileName.GetCStr()); | 169 return 0; |
194 } | 170 } |
195 FX_BOOL FX_File_Delete(const CFX_WideStringC& fileName) | 171 return Write(pBuffer, szBuffer); |
196 { | 172 } |
197 return ::DeleteFileW((LPCWSTR)fileName.GetPtr()); | 173 FX_BOOL CFXCRT_FileAccess_Win64::Flush() { |
198 } | 174 if (!m_hFile) { |
199 FX_BOOL FX_File_Copy(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC&
fileNameDst) | 175 return FALSE; |
200 { | 176 } |
201 return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE); | 177 return ::FlushFileBuffers(m_hFile); |
202 } | 178 } |
203 FX_BOOL FX_File_Copy(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC&
fileNameDst) | 179 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) { |
204 { | 180 if (SetPosition(szFile) == (FX_FILESIZE)-1) { |
205 return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPt
r(), FALSE); | 181 return FALSE; |
206 } | 182 } |
207 FX_BOOL FX_File_Move(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC&
fileNameDst) | 183 return ::SetEndOfFile(m_hFile); |
208 { | 184 } |
209 return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); | 185 FX_BOOL FX_File_Delete(const CFX_ByteStringC& fileName) { |
210 } | 186 return ::DeleteFileA(fileName.GetCStr()); |
211 FX_BOOL FX_File_Move(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC&
fileNameDst) | 187 } |
212 { | 188 FX_BOOL FX_File_Delete(const CFX_WideStringC& fileName) { |
213 return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPt
r()); | 189 return ::DeleteFileW((LPCWSTR)fileName.GetPtr()); |
| 190 } |
| 191 FX_BOOL FX_File_Copy(const CFX_ByteStringC& fileNameSrc, |
| 192 const CFX_ByteStringC& fileNameDst) { |
| 193 return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE); |
| 194 } |
| 195 FX_BOOL FX_File_Copy(const CFX_WideStringC& fileNameSrc, |
| 196 const CFX_WideStringC& fileNameDst) { |
| 197 return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), |
| 198 (LPCWSTR)fileNameDst.GetPtr(), FALSE); |
| 199 } |
| 200 FX_BOOL FX_File_Move(const CFX_ByteStringC& fileNameSrc, |
| 201 const CFX_ByteStringC& fileNameDst) { |
| 202 return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); |
| 203 } |
| 204 FX_BOOL FX_File_Move(const CFX_WideStringC& fileNameSrc, |
| 205 const CFX_WideStringC& fileNameDst) { |
| 206 return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), |
| 207 (LPCWSTR)fileNameDst.GetPtr()); |
214 } | 208 } |
215 #endif | 209 #endif |
OLD | NEW |