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
file_util.cpp
Go to the documentation of this file.
1// Copyright 2019 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#include "testing/utils/file_util.h"
6
7#include <stdio.h>
8
9#include <utility>
10#include <vector>
11
12#include "core/fxcrt/span_util.h"
13#include "testing/utils/path_service.h"
14#include "third_party/base/numerics/safe_conversions.h"
15
16std::vector<uint8_t> GetFileContents(const char* filename) {
17 FILE* file = fopen(filename, "rb");
18 if (!file) {
19 fprintf(stderr, "Failed to open: %s\n", filename);
20 return {};
21 }
22 (void)fseek(file, 0, SEEK_END);
23 size_t file_length = ftell(file);
24 if (!file_length) {
25 return {};
26 }
27 (void)fseek(file, 0, SEEK_SET);
28 std::vector<uint8_t> buffer(file_length);
29 size_t bytes_read = fread(buffer.data(), 1, file_length, file);
30 (void)fclose(file);
31 if (bytes_read != file_length) {
32 fprintf(stderr, "Failed to read: %s\n", filename);
33 return {};
34 }
35 return buffer;
36}
37
38FileAccessForTesting::FileAccessForTesting(const std::string& file_name) {
39 std::string file_path = PathService::GetTestFilePath(file_name);
40 if (file_path.empty()) {
41 return;
42 }
43
44 file_contents_ = GetFileContents(file_path.c_str());
45 if (file_contents_.empty()) {
46 return;
47 }
48
49 m_FileLen = pdfium::base::checked_cast<unsigned long>(file_contents_.size());
50 m_GetBlock = SGetBlock;
51 m_Param = this;
52}
53
54int FileAccessForTesting::GetBlockImpl(unsigned long pos,
55 unsigned char* pBuf,
56 unsigned long size) {
57 fxcrt::spancpy(pdfium::make_span(pBuf, size),
58 pdfium::make_span(file_contents_).subspan(pos, size));
59 return size ? 1 : 0;
60}
61
62// static
63int FileAccessForTesting::SGetBlock(void* param,
64 unsigned long pos,
65 unsigned char* pBuf,
66 unsigned long size) {
67 auto* file_access = static_cast<FileAccessForTesting*>(param);
68 return file_access->GetBlockImpl(pos, pBuf, size);
69}
std::vector< uint8_t > GetFileContents(const char *filename)
Definition file_util.cpp:16