diff options
Diffstat (limited to 'sources/shiboken6/tests/libsample/simplefile.cpp')
-rw-r--r-- | sources/shiboken6/tests/libsample/simplefile.cpp | 99 |
1 files changed, 32 insertions, 67 deletions
diff --git a/sources/shiboken6/tests/libsample/simplefile.cpp b/sources/shiboken6/tests/libsample/simplefile.cpp index 3b68e02c3..e51b14088 100644 --- a/sources/shiboken6/tests/libsample/simplefile.cpp +++ b/sources/shiboken6/tests/libsample/simplefile.cpp @@ -1,108 +1,73 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of Qt for Python. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#include <stdlib.h> -#include <string.h> -#include <fstream> #include "simplefile.h" -class SimpleFile_p +#include <cstdlib> +#include <cstdio> +#include <string> +#include <filesystem> + +class SimpleFilePrivate { public: - SimpleFile_p(const char* filename) : m_descriptor(nullptr), m_size(0) - { - m_filename = strdup(filename); - } + LIBMINIMAL_DISABLE_COPY_MOVE(SimpleFilePrivate) - ~SimpleFile_p() - { - free(m_filename); - } + SimpleFilePrivate(const char *filename) : m_filename(filename) {} + ~SimpleFilePrivate() = default; - char* m_filename; - FILE* m_descriptor; - long m_size; + std::string m_filename; + FILE *m_descriptor = nullptr; + long m_size = 0; }; -SimpleFile::SimpleFile(const char* filename) +SimpleFile::SimpleFile(const char *filename) : + p(std::make_unique<SimpleFilePrivate>(filename)) { - p = new SimpleFile_p(filename); } SimpleFile::~SimpleFile() { close(); - delete p; } -const char* SimpleFile::filename() +const char *SimpleFile::filename() { - return p->m_filename; + return p->m_filename.c_str(); } -long SimpleFile::size() +long SimpleFile::size() const { return p->m_size; } -bool -SimpleFile::open() +bool SimpleFile::open() { - if ((p->m_descriptor = fopen(p->m_filename, "rb")) == nullptr) + auto *descriptor = std::fopen(p->m_filename.c_str(), "rb"); + if (descriptor == nullptr) return false; - fseek(p->m_descriptor, 0, SEEK_END); - p->m_size = ftell(p->m_descriptor); - rewind(p->m_descriptor); + p->m_descriptor = descriptor; + const auto size = std::filesystem::file_size(std::filesystem::path(p->m_filename)); + p->m_size = long(size); return true; } -void -SimpleFile::close() +void SimpleFile::close() { - if (p->m_descriptor) { - fclose(p->m_descriptor); + if (p->m_descriptor != nullptr) { + std::fclose(p->m_descriptor); p->m_descriptor = nullptr; } } -bool -SimpleFile::exists() const +bool SimpleFile::exists() const { - std::ifstream ifile(p->m_filename); - return !ifile.fail(); + return std::filesystem::exists(std::filesystem::path(p->m_filename)); } -bool -SimpleFile::exists(const char* filename) +bool SimpleFile::exists(const char *filename) { - std::ifstream ifile(filename); - return !ifile.fail(); + return std::filesystem::exists(std::filesystem::path(filename)); } - |