Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_fileaccess_windows.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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 "core/fxcrt/cfx_fileaccess_windows.h"
8
9#include <memory>
10
11#include "core/fxcrt/fx_stream.h"
12#include "core/fxcrt/fx_string.h"
13
14// static
15std::unique_ptr<FileAccessIface> FileAccessIface::Create() {
16 return std::make_unique<CFX_FileAccess_Windows>();
17}
18
19CFX_FileAccess_Windows::CFX_FileAccess_Windows() = default;
20
21CFX_FileAccess_Windows::~CFX_FileAccess_Windows() {
22 Close();
23}
24
25bool CFX_FileAccess_Windows::Open(ByteStringView fileName) {
26 if (m_hFile)
27 return false;
28
29 WideString wname = WideString::FromUTF8(fileName);
30 m_hFile = ::CreateFileW(wname.c_str(), GENERIC_READ,
31 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
32 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
33 if (m_hFile == INVALID_HANDLE_VALUE)
34 m_hFile = nullptr;
35
36 return !!m_hFile;
37}
38
39void CFX_FileAccess_Windows::Close() {
40 if (!m_hFile)
41 return;
42
43 ::CloseHandle(m_hFile);
44 m_hFile = nullptr;
45}
46
47FX_FILESIZE CFX_FileAccess_Windows::GetSize() const {
48 if (!m_hFile)
49 return 0;
50
51 LARGE_INTEGER size = {};
52 if (!::GetFileSizeEx(m_hFile, &size))
53 return 0;
54
55 return (FX_FILESIZE)size.QuadPart;
56}
57
58FX_FILESIZE CFX_FileAccess_Windows::GetPosition() const {
59 if (!m_hFile)
60 return (FX_FILESIZE)-1;
61
62 LARGE_INTEGER dist = {};
63 LARGE_INTEGER newPos = {};
64 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT))
65 return (FX_FILESIZE)-1;
66
67 return (FX_FILESIZE)newPos.QuadPart;
68}
69
70FX_FILESIZE CFX_FileAccess_Windows::SetPosition(FX_FILESIZE pos) {
71 if (!m_hFile)
72 return (FX_FILESIZE)-1;
73
74 LARGE_INTEGER dist;
75 dist.QuadPart = pos;
76 LARGE_INTEGER newPos = {};
77 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN))
78 return (FX_FILESIZE)-1;
79
80 return (FX_FILESIZE)newPos.QuadPart;
81}
82
83size_t CFX_FileAccess_Windows::Read(pdfium::span<uint8_t> buffer) {
84 if (!m_hFile)
85 return 0;
86
87 size_t szRead = 0;
88 if (!::ReadFile(m_hFile, buffer.data(), (DWORD)buffer.size(),
89 (LPDWORD)&szRead, nullptr)) {
90 return 0;
91 }
92 return szRead;
93}
94
95size_t CFX_FileAccess_Windows::Write(pdfium::span<const uint8_t> buffer) {
96 if (!m_hFile)
97 return 0;
98
99 size_t szWrite = 0;
100 if (!::WriteFile(m_hFile, buffer.data(), (DWORD)buffer.size(),
101 (LPDWORD)&szWrite, nullptr)) {
102 return 0;
103 }
104 return szWrite;
105}
106
107size_t CFX_FileAccess_Windows::ReadPos(pdfium::span<uint8_t> buffer,
108 FX_FILESIZE pos) {
109 if (!m_hFile)
110 return 0;
111
112 if (pos >= GetSize())
113 return 0;
114
115 if (SetPosition(pos) == (FX_FILESIZE)-1)
116 return 0;
117
118 return Read(buffer);
119}
120
121bool CFX_FileAccess_Windows::Flush() {
122 if (!m_hFile)
123 return false;
124
125 return !!::FlushFileBuffers(m_hFile);
126}
127
128bool CFX_FileAccess_Windows::Truncate(FX_FILESIZE szFile) {
129 if (SetPosition(szFile) == (FX_FILESIZE)-1)
130 return false;
131
132 return !!::SetEndOfFile(m_hFile);
133}
size_t ReadPos(pdfium::span< uint8_t > buffer, FX_FILESIZE pos) override
bool Open(ByteStringView fileName) override
size_t Write(pdfium::span< const uint8_t > buffer) override
size_t Read(pdfium::span< uint8_t > buffer) override
FX_FILESIZE GetSize() const override
FX_FILESIZE SetPosition(FX_FILESIZE pos) override
FX_FILESIZE GetPosition() const override
bool Truncate(FX_FILESIZE szFile) override
static WideString FromUTF8(ByteStringView str)
#define FX_FILESIZE
Definition fx_types.h:19
fxcrt::ByteStringView ByteStringView
fxcrt::WideString WideString
Definition widestring.h:207