summaryrefslogtreecommitdiff
path: root/core/src/fxcrt/fxcrt_platforms.cpp
blob: e7182943be2b59686b78b70406b8809695465eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "../../include/fxcrt/fx_basic.h"
#include "fxcrt_platforms.h"

#if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && _FXM_PLATFORM_ != _FXM_PLATFORM_LINUX_ && _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ && _FXM_PLATFORM_ != _FXM_PLATFORM_ANDROID_)
IFXCRT_FileAccess* FXCRT_FileAccess_Create()
{
    return new CFXCRT_FileAccess_CRT;
}
void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode)
{
    if (dwModes & FX_FILEMODE_ReadOnly) {
        bsMode = FX_BSTRC("rb");
    } else if (dwModes & FX_FILEMODE_Truncate) {
        bsMode = FX_BSTRC("w+b");
    } else {
        bsMode = FX_BSTRC("a+b");
    }
}
void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode)
{
    if (dwModes & FX_FILEMODE_ReadOnly) {
        wsMode = FX_WSTRC(L"rb");
    } else if (dwModes & FX_FILEMODE_Truncate) {
        wsMode = FX_WSTRC(L"w+b");
    } else {
        wsMode = FX_WSTRC(L"a+b");
    }
}
CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT()
    : m_hFile(NULL)
{
}
CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT()
{
    Close();
}
FX_BOOL CFXCRT_FileAccess_CRT::Open(const CFX_ByteStringC& fileName, FX_DWORD dwMode)
{
    if (m_hFile) {
        return FALSE;
    }
    CFX_ByteString strMode;
    FXCRT_GetFileModeString(dwMode, strMode);
    m_hFile = FXSYS_fopen(fileName.GetCStr(), strMode.c_str());
    return m_hFile != NULL;
}
FX_BOOL CFXCRT_FileAccess_CRT::Open(const CFX_WideStringC& fileName, FX_DWORD dwMode)
{
    if (m_hFile) {
        return FALSE;
    }
    CFX_WideString strMode;
    FXCRT_GetFileModeString(dwMode, strMode);
    m_hFile = FXSYS_wfopen(fileName.GetPtr(), strMode.c_str());
    return m_hFile != NULL;
}
void CFXCRT_FileAccess_CRT::Close()
{
    if (!m_hFile) {
        return;
    }
    FXSYS_fclose(m_hFile);
    m_hFile = NULL;
}
void CFXCRT_FileAccess_CRT::Release()
{
    delete this;
}
FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const
{
    if (!m_hFile) {
        return 0;
    }
    FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile);
    FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END);
    FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile);
    FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
    return size;
}
FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const
{
    if (!m_hFile) {
        return (FX_FILESIZE) - 1;
    }
    return (FX_FILESIZE)FXSYS_ftell(m_hFile);
}
FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos)
{
    if (!m_hFile) {
        return (FX_FILESIZE) - 1;
    }
    FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
    return (FX_FILESIZE)FXSYS_ftell(m_hFile);
}
size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer)
{
    if (!m_hFile) {
        return 0;
    }
    return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
}
size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer)
{
    if (!m_hFile) {
        return 0;
    }
    return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
}
size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
{
    if (!m_hFile) {
        return (FX_FILESIZE) - 1;
    }
    FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
    return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
}
size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
{
    if (!m_hFile) {
        return (FX_FILESIZE) - 1;
    }
    FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
    return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
}
FX_BOOL CFXCRT_FileAccess_CRT::Flush()
{
    if (!m_hFile) {
        return FALSE;
    }
    return !FXSYS_fflush(m_hFile);
}
FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile)
{
    return FALSE;
}
FX_BOOL FX_File_Exist(const CFX_ByteStringC& fileName)
{
    return access(fileName.GetCStr(), F_OK) > -1;
}
FX_BOOL FX_File_Exist(const CFX_WideStringC& fileName)
{
    return FX_File_Exist(FX_UTF8Encode(fileName));
}
FX_BOOL FX_File_Delete(const CFX_ByteStringC& fileName)
{
    return remove(fileName.GetCStr()) > -1;
}
FX_BOOL FX_File_Delete(const CFX_WideStringC& fileName)
{
    return FX_File_Delete(FX_UTF8Encode(fileName));
}
FX_BOOL FX_File_Copy(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC& fileNameDst)
{
    CFXCRT_FileAccess_CRT src, dst;
    if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) {
        return FALSE;
    }
    FX_FILESIZE size = src.GetSize();
    if (!size) {
        return FALSE;
    }
    if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) {
        return FALSE;
    }
    FX_FILESIZE num = 0;
    uint8_t* pBuffer = FX_Alloc(uint8_t, 32768);
    while (num = src.Read(pBuffer, 32768)) {
        if (dst.Write(pBuffer, num) != num) {
            break;
        }
    }
    FX_Free(pBuffer);
    return TRUE;
}
FX_BOOL FX_File_Copy(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC& fileNameDst)
{
    return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
}
FX_BOOL FX_File_Move(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC& fileNameDst)
{
    return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
}
FX_BOOL FX_File_Move(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC& fileNameDst)
{
    return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
}
#endif