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_ext.h" | 7 #include "../../include/fxcrt/fx_ext.h" |
8 #include "fxcrt_platforms.h" | 8 #include "fxcrt_platforms.h" |
9 #if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && _FXM_PLATFORM_ != _FXM_PLATFORM
_LINUX_ && _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ && _FXM_PLATFORM_ != _FXM_PLAT
FORM_ANDROID_) | 9 #if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && \ |
10 IFXCRT_FileAccess* FXCRT_FileAccess_Create() | 10 _FXM_PLATFORM_ != _FXM_PLATFORM_LINUX_ && \ |
11 { | 11 _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ && \ |
12 return FX_NEW CFXCRT_FileAccess_CRT; | 12 _FXM_PLATFORM_ != _FXM_PLATFORM_ANDROID_) |
| 13 IFXCRT_FileAccess* FXCRT_FileAccess_Create() { |
| 14 return FX_NEW CFXCRT_FileAccess_CRT; |
13 } | 15 } |
14 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode) | 16 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString& bsMode) { |
15 { | 17 if (dwModes & FX_FILEMODE_ReadOnly) { |
16 if (dwModes & FX_FILEMODE_ReadOnly) { | 18 bsMode = FX_BSTRC("rb"); |
17 bsMode = FX_BSTRC("rb"); | 19 } else if (dwModes & FX_FILEMODE_Truncate) { |
18 } else if (dwModes & FX_FILEMODE_Truncate) { | 20 bsMode = FX_BSTRC("w+b"); |
19 bsMode = FX_BSTRC("w+b"); | 21 } else { |
20 } else { | 22 bsMode = FX_BSTRC("a+b"); |
21 bsMode = FX_BSTRC("a+b"); | 23 } |
| 24 } |
| 25 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString& wsMode) { |
| 26 if (dwModes & FX_FILEMODE_ReadOnly) { |
| 27 wsMode = FX_WSTRC(L"rb"); |
| 28 } else if (dwModes & FX_FILEMODE_Truncate) { |
| 29 wsMode = FX_WSTRC(L"w+b"); |
| 30 } else { |
| 31 wsMode = FX_WSTRC(L"a+b"); |
| 32 } |
| 33 } |
| 34 CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT() : m_hFile(NULL) { |
| 35 } |
| 36 CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT() { |
| 37 Close(); |
| 38 } |
| 39 FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_BSTR fileName, FX_DWORD dwMode) { |
| 40 if (m_hFile) { |
| 41 return FALSE; |
| 42 } |
| 43 CFX_ByteString strMode; |
| 44 FXCRT_GetFileModeString(dwMode, strMode); |
| 45 m_hFile = FXSYS_fopen(fileName.GetCStr(), (FX_LPCSTR)strMode); |
| 46 return m_hFile != NULL; |
| 47 } |
| 48 FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_WSTR fileName, FX_DWORD dwMode) { |
| 49 if (m_hFile) { |
| 50 return FALSE; |
| 51 } |
| 52 CFX_WideString strMode; |
| 53 FXCRT_GetFileModeString(dwMode, strMode); |
| 54 m_hFile = FXSYS_wfopen(fileName.GetPtr(), (FX_LPCWSTR)strMode); |
| 55 return m_hFile != NULL; |
| 56 } |
| 57 void CFXCRT_FileAccess_CRT::Close() { |
| 58 if (!m_hFile) { |
| 59 return; |
| 60 } |
| 61 FXSYS_fclose(m_hFile); |
| 62 m_hFile = NULL; |
| 63 } |
| 64 void CFXCRT_FileAccess_CRT::Release() { |
| 65 delete this; |
| 66 } |
| 67 FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const { |
| 68 if (!m_hFile) { |
| 69 return 0; |
| 70 } |
| 71 FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile); |
| 72 FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END); |
| 73 FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile); |
| 74 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); |
| 75 return size; |
| 76 } |
| 77 FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const { |
| 78 if (!m_hFile) { |
| 79 return (FX_FILESIZE)-1; |
| 80 } |
| 81 return (FX_FILESIZE)FXSYS_ftell(m_hFile); |
| 82 } |
| 83 FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos) { |
| 84 if (!m_hFile) { |
| 85 return (FX_FILESIZE)-1; |
| 86 } |
| 87 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); |
| 88 return (FX_FILESIZE)FXSYS_ftell(m_hFile); |
| 89 } |
| 90 size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer) { |
| 91 if (!m_hFile) { |
| 92 return 0; |
| 93 } |
| 94 return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); |
| 95 } |
| 96 size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer) { |
| 97 if (!m_hFile) { |
| 98 return 0; |
| 99 } |
| 100 return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); |
| 101 } |
| 102 size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer, |
| 103 size_t szBuffer, |
| 104 FX_FILESIZE pos) { |
| 105 if (!m_hFile) { |
| 106 return (FX_FILESIZE)-1; |
| 107 } |
| 108 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); |
| 109 return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); |
| 110 } |
| 111 size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer, |
| 112 size_t szBuffer, |
| 113 FX_FILESIZE pos) { |
| 114 if (!m_hFile) { |
| 115 return (FX_FILESIZE)-1; |
| 116 } |
| 117 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); |
| 118 return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); |
| 119 } |
| 120 FX_BOOL CFXCRT_FileAccess_CRT::Flush() { |
| 121 if (!m_hFile) { |
| 122 return FALSE; |
| 123 } |
| 124 return !FXSYS_fflush(m_hFile); |
| 125 } |
| 126 FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile) { |
| 127 return FALSE; |
| 128 } |
| 129 FX_BOOL FX_File_Exist(FX_BSTR fileName) { |
| 130 return access(fileName.GetCStr(), F_OK) > -1; |
| 131 } |
| 132 FX_BOOL FX_File_Exist(FX_WSTR fileName) { |
| 133 return FX_File_Exist(FX_UTF8Encode(fileName)); |
| 134 } |
| 135 FX_BOOL FX_File_Delete(FX_BSTR fileName) { |
| 136 return remove(fileName.GetCStr()) > -1; |
| 137 } |
| 138 FX_BOOL FX_File_Delete(FX_WSTR fileName) { |
| 139 return FX_File_Delete(FX_UTF8Encode(fileName)); |
| 140 } |
| 141 FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) { |
| 142 CFXCRT_FileAccess_CRT src, dst; |
| 143 if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { |
| 144 return FALSE; |
| 145 } |
| 146 FX_FILESIZE size = src.GetSize(); |
| 147 if (!size) { |
| 148 return FALSE; |
| 149 } |
| 150 if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { |
| 151 return FALSE; |
| 152 } |
| 153 FX_FILESIZE num = 0; |
| 154 FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768); |
| 155 if (!pBuffer) { |
| 156 return FALSE; |
| 157 } |
| 158 while (num = src.Read(pBuffer, 32768)) { |
| 159 if (dst.Write(pBuffer, num) != num) { |
| 160 break; |
22 } | 161 } |
| 162 } |
| 163 FX_Free(pBuffer); |
| 164 return TRUE; |
23 } | 165 } |
24 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode) | 166 FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) { |
25 { | 167 return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); |
26 if (dwModes & FX_FILEMODE_ReadOnly) { | |
27 wsMode = FX_WSTRC(L"rb"); | |
28 } else if (dwModes & FX_FILEMODE_Truncate) { | |
29 wsMode = FX_WSTRC(L"w+b"); | |
30 } else { | |
31 wsMode = FX_WSTRC(L"a+b"); | |
32 } | |
33 } | 168 } |
34 CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT() | 169 FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) { |
35 : m_hFile(NULL) | 170 return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); |
36 { | |
37 } | 171 } |
38 CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT() | 172 FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) { |
39 { | 173 return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); |
40 Close(); | |
41 } | |
42 FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_BSTR fileName, FX_DWORD dwMode) | |
43 { | |
44 if (m_hFile) { | |
45 return FALSE; | |
46 } | |
47 CFX_ByteString strMode; | |
48 FXCRT_GetFileModeString(dwMode, strMode); | |
49 m_hFile = FXSYS_fopen(fileName.GetCStr(), (FX_LPCSTR)strMode); | |
50 return m_hFile != NULL; | |
51 } | |
52 FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_WSTR fileName, FX_DWORD dwMode) | |
53 { | |
54 if (m_hFile) { | |
55 return FALSE; | |
56 } | |
57 CFX_WideString strMode; | |
58 FXCRT_GetFileModeString(dwMode, strMode); | |
59 m_hFile = FXSYS_wfopen(fileName.GetPtr(), (FX_LPCWSTR)strMode); | |
60 return m_hFile != NULL; | |
61 } | |
62 void CFXCRT_FileAccess_CRT::Close() | |
63 { | |
64 if (!m_hFile) { | |
65 return; | |
66 } | |
67 FXSYS_fclose(m_hFile); | |
68 m_hFile = NULL; | |
69 } | |
70 void CFXCRT_FileAccess_CRT::Release() | |
71 { | |
72 delete this; | |
73 } | |
74 FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const | |
75 { | |
76 if (!m_hFile) { | |
77 return 0; | |
78 } | |
79 FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile); | |
80 FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END); | |
81 FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile); | |
82 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); | |
83 return size; | |
84 } | |
85 FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const | |
86 { | |
87 if (!m_hFile) { | |
88 return (FX_FILESIZE) - 1; | |
89 } | |
90 return (FX_FILESIZE)FXSYS_ftell(m_hFile); | |
91 } | |
92 FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos) | |
93 { | |
94 if (!m_hFile) { | |
95 return (FX_FILESIZE) - 1; | |
96 } | |
97 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); | |
98 return (FX_FILESIZE)FXSYS_ftell(m_hFile); | |
99 } | |
100 size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer) | |
101 { | |
102 if (!m_hFile) { | |
103 return 0; | |
104 } | |
105 return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); | |
106 } | |
107 size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer) | |
108 { | |
109 if (!m_hFile) { | |
110 return 0; | |
111 } | |
112 return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); | |
113 } | |
114 size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZ
E pos) | |
115 { | |
116 if (!m_hFile) { | |
117 return (FX_FILESIZE) - 1; | |
118 } | |
119 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); | |
120 return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); | |
121 } | |
122 size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer, size_t szBuffer, FX_
FILESIZE pos) | |
123 { | |
124 if (!m_hFile) { | |
125 return (FX_FILESIZE) - 1; | |
126 } | |
127 FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); | |
128 return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); | |
129 } | |
130 FX_BOOL CFXCRT_FileAccess_CRT::Flush() | |
131 { | |
132 if (!m_hFile) { | |
133 return FALSE; | |
134 } | |
135 return !FXSYS_fflush(m_hFile); | |
136 } | |
137 FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile) | |
138 { | |
139 return FALSE; | |
140 } | |
141 FX_BOOL FX_File_Exist(FX_BSTR fileName) | |
142 { | |
143 return access(fileName.GetCStr(), F_OK) > -1; | |
144 } | |
145 FX_BOOL FX_File_Exist(FX_WSTR fileName) | |
146 { | |
147 return FX_File_Exist(FX_UTF8Encode(fileName)); | |
148 } | |
149 FX_BOOL FX_File_Delete(FX_BSTR fileName) | |
150 { | |
151 return remove(fileName.GetCStr()) > -1; | |
152 } | |
153 FX_BOOL FX_File_Delete(FX_WSTR fileName) | |
154 { | |
155 return FX_File_Delete(FX_UTF8Encode(fileName)); | |
156 } | |
157 FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) | |
158 { | |
159 CFXCRT_FileAccess_CRT src, dst; | |
160 if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { | |
161 return FALSE; | |
162 } | |
163 FX_FILESIZE size = src.GetSize(); | |
164 if (!size) { | |
165 return FALSE; | |
166 } | |
167 if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { | |
168 return FALSE; | |
169 } | |
170 FX_FILESIZE num = 0; | |
171 FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768); | |
172 if (!pBuffer) { | |
173 return FALSE; | |
174 } | |
175 while (num = src.Read(pBuffer, 32768)) { | |
176 if (dst.Write(pBuffer, num) != num) { | |
177 break; | |
178 } | |
179 } | |
180 FX_Free(pBuffer); | |
181 return TRUE; | |
182 } | |
183 FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) | |
184 { | |
185 return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); | |
186 } | |
187 FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) | |
188 { | |
189 return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); | |
190 } | |
191 FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) | |
192 { | |
193 return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); | |
194 } | 174 } |
195 #endif | 175 #endif |
OLD | NEW |