• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxcrt_platforms.h"
8 
9 #include "core/include/fxcrt/fx_basic.h"
10 
11 #if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && \
12      _FXM_PLATFORM_ != _FXM_PLATFORM_LINUX_ &&   \
13      _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ &&   \
14      _FXM_PLATFORM_ != _FXM_PLATFORM_ANDROID_)
FXCRT_FileAccess_Create()15 IFXCRT_FileAccess* FXCRT_FileAccess_Create() {
16   return new CFXCRT_FileAccess_CRT;
17 }
FXCRT_GetFileModeString(FX_DWORD dwModes,CFX_ByteString & bsMode)18 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString& bsMode) {
19   if (dwModes & FX_FILEMODE_ReadOnly) {
20     bsMode = "rb";
21   } else if (dwModes & FX_FILEMODE_Truncate) {
22     bsMode = "w+b";
23   } else {
24     bsMode = "a+b";
25   }
26 }
FXCRT_GetFileModeString(FX_DWORD dwModes,CFX_WideString & wsMode)27 void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString& wsMode) {
28   if (dwModes & FX_FILEMODE_ReadOnly) {
29     wsMode = FX_WSTRC(L"rb");
30   } else if (dwModes & FX_FILEMODE_Truncate) {
31     wsMode = FX_WSTRC(L"w+b");
32   } else {
33     wsMode = FX_WSTRC(L"a+b");
34   }
35 }
CFXCRT_FileAccess_CRT()36 CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT() : m_hFile(NULL) {}
~CFXCRT_FileAccess_CRT()37 CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT() {
38   Close();
39 }
Open(const CFX_ByteStringC & fileName,FX_DWORD dwMode)40 FX_BOOL CFXCRT_FileAccess_CRT::Open(const CFX_ByteStringC& fileName,
41                                     FX_DWORD dwMode) {
42   if (m_hFile) {
43     return FALSE;
44   }
45   CFX_ByteString strMode;
46   FXCRT_GetFileModeString(dwMode, strMode);
47   m_hFile = FXSYS_fopen(fileName.GetCStr(), strMode.c_str());
48   return m_hFile != NULL;
49 }
Open(const CFX_WideStringC & fileName,FX_DWORD dwMode)50 FX_BOOL CFXCRT_FileAccess_CRT::Open(const CFX_WideStringC& fileName,
51                                     FX_DWORD dwMode) {
52   if (m_hFile) {
53     return FALSE;
54   }
55   CFX_WideString strMode;
56   FXCRT_GetFileModeString(dwMode, strMode);
57   m_hFile = FXSYS_wfopen(fileName.GetPtr(), strMode.c_str());
58   return m_hFile != NULL;
59 }
Close()60 void CFXCRT_FileAccess_CRT::Close() {
61   if (!m_hFile) {
62     return;
63   }
64   FXSYS_fclose(m_hFile);
65   m_hFile = NULL;
66 }
Release()67 void CFXCRT_FileAccess_CRT::Release() {
68   delete this;
69 }
GetSize() const70 FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const {
71   if (!m_hFile) {
72     return 0;
73   }
74   FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile);
75   FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END);
76   FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile);
77   FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
78   return size;
79 }
GetPosition() const80 FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const {
81   if (!m_hFile) {
82     return (FX_FILESIZE)-1;
83   }
84   return (FX_FILESIZE)FXSYS_ftell(m_hFile);
85 }
SetPosition(FX_FILESIZE pos)86 FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos) {
87   if (!m_hFile) {
88     return (FX_FILESIZE)-1;
89   }
90   FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
91   return (FX_FILESIZE)FXSYS_ftell(m_hFile);
92 }
Read(void * pBuffer,size_t szBuffer)93 size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer) {
94   if (!m_hFile) {
95     return 0;
96   }
97   return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
98 }
Write(const void * pBuffer,size_t szBuffer)99 size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer) {
100   if (!m_hFile) {
101     return 0;
102   }
103   return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
104 }
ReadPos(void * pBuffer,size_t szBuffer,FX_FILESIZE pos)105 size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer,
106                                       size_t szBuffer,
107                                       FX_FILESIZE pos) {
108   if (!m_hFile) {
109     return (FX_FILESIZE)-1;
110   }
111   FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
112   return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
113 }
WritePos(const void * pBuffer,size_t szBuffer,FX_FILESIZE pos)114 size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer,
115                                        size_t szBuffer,
116                                        FX_FILESIZE pos) {
117   if (!m_hFile) {
118     return (FX_FILESIZE)-1;
119   }
120   FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
121   return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
122 }
Flush()123 FX_BOOL CFXCRT_FileAccess_CRT::Flush() {
124   if (!m_hFile) {
125     return FALSE;
126   }
127   return !FXSYS_fflush(m_hFile);
128 }
Truncate(FX_FILESIZE szFile)129 FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile) {
130   return FALSE;
131 }
132 #endif
133