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