From 53d70feedcfa3222340467d259dade9fca238cc0 Mon Sep 17 00:00:00 2001 From: Julles Date: Thu, 18 Apr 2024 15:48:44 +0800 Subject: [PATCH] fix #79:include for old compilers --- CMakeLists.txt | 2 +- mdflib/src/at4block.cpp | 23 +++++++++++++++-------- mdflib/src/cryptoutil.cpp | 16 ++++++++++++---- mdflib/src/expatxml.cpp | 9 ++++++++- mdflib/src/ichannelconversion.cpp | 2 +- mdflib/src/ixmlfile.cpp | 13 ++++++++++--- mdflib/src/mdfblock.h | 1 + mdflib/src/mdffile.cpp | 11 +++++++++-- mdflib/src/mdfhelper.cpp | 2 +- mdflib/src/mdfreader.cpp | 17 ++++++++++++----- mdflib/src/mdfwriter.cpp | 14 ++++++++++---- mdflib/src/platform.h | 31 +++++++++++++++++++++++++++++++ mdflib/src/zlibutil.cpp | 14 +++++++++++--- mdflibrary/CMakeLists.txt | 5 +++++ mdflibrary_example/src/test.cpp | 8 -------- 15 files changed, 127 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8c367d6..a7233e5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ # Copyright 2021 Ingemar Hedvall # SPDX-License-Identifier: MIT -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.10) if("${CMAKE_TOOLCHAIN_FILE}" STREQUAL "") set(VCPKG OFF) diff --git a/mdflib/src/at4block.cpp b/mdflib/src/at4block.cpp index 18c4686f..d6d2ed71 100644 --- a/mdflib/src/at4block.cpp +++ b/mdflib/src/at4block.cpp @@ -5,7 +5,6 @@ #include "at4block.h" #include -#include #include #include "mdf/cryptoutil.h" @@ -14,7 +13,15 @@ #include "mdf/zlibutil.h" #include "platform.h" -using namespace std::filesystem; +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + +using namespace fs; namespace { @@ -114,7 +121,7 @@ void At4Block::GetBlockProperty(BlockPropertyList& dest) const { std::string name; if (Link(kIndexFilename) > 0) { try { - std::filesystem::path p = std::filesystem::u8path(filename_); + fs::path p = fs::u8path(filename_); const auto& u8str = p.filename().u8string(); name = std::string(u8str.begin(), u8str.end()); } catch (const std::exception&) { @@ -165,7 +172,7 @@ size_t At4Block::Write(std::FILE* file) { ByteArray data_buffer; try { path filename = u8path(filename_); - if (!std::filesystem::exists(filename)) { + if (!fs::exists(filename)) { MDF_ERROR() << "Attachment File doesn't exist. File: " << filename_; return 0; } @@ -244,11 +251,11 @@ void At4Block::ReadData(std::FILE* file, const std::string& dest_file) const { } } else { // Need to copy the source file - std::filesystem::path s = std::filesystem::u8path(filename_); - std::filesystem::path d = std::filesystem::u8path(dest_file); + fs::path s = fs::u8path(filename_); + fs::path d = fs::u8path(dest_file); if (s != d) { - std::filesystem::copy_file( - s, d, std::filesystem::copy_options::overwrite_existing); + fs::copy_file( + s, d, fs::copy_options::overwrite_existing); } } if (flags_ & At4Flags::kUsingMd5) { diff --git a/mdflib/src/cryptoutil.cpp b/mdflib/src/cryptoutil.cpp index 191f05c2..3fd97f12 100644 --- a/mdflib/src/cryptoutil.cpp +++ b/mdflib/src/cryptoutil.cpp @@ -7,11 +7,19 @@ #include #include #include -#include +#include #include "mdf/mdflogstream.h" - #include "platform.h" + +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + namespace { #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) @@ -240,8 +248,8 @@ bool CreateMd5FileChecksum(const std::string &file, std::vector &md5) { } try { - std::filesystem::path p = std::filesystem::u8path(file); - if (std::filesystem::exists(p)) { + fs::path p = fs::u8path(file); + if (fs::exists(p)) { std::FILE *f = nullptr; Platform::fileopen(&f, file.c_str(), "rb"); if (f != nullptr) { diff --git a/mdflib/src/expatxml.cpp b/mdflib/src/expatxml.cpp index 62bb5ff0..6af11913 100644 --- a/mdflib/src/expatxml.cpp +++ b/mdflib/src/expatxml.cpp @@ -5,11 +5,18 @@ #include "expatxml.h" #include -#include #include "mdf/mdflogstream.h" #include "platform.h" +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + namespace { void StartElementHandler(void *userData, const XML_Char *name, const XML_Char **attributes) { diff --git a/mdflib/src/ichannelconversion.cpp b/mdflib/src/ichannelconversion.cpp index 57df3991..c10e796a 100644 --- a/mdflib/src/ichannelconversion.cpp +++ b/mdflib/src/ichannelconversion.cpp @@ -348,7 +348,7 @@ void IChannelConversion::ParameterUint(uint16_t index, uint64_t parameter) { return; } while (index >= value_list_.size()) { - value_list_.emplace_back(0ULL); + value_list_.emplace_back(static_cast(0)); } value_list_[index] = parameter; } diff --git a/mdflib/src/ixmlfile.cpp b/mdflib/src/ixmlfile.cpp index 52542a9d..65af35f5 100644 --- a/mdflib/src/ixmlfile.cpp +++ b/mdflib/src/ixmlfile.cpp @@ -4,7 +4,6 @@ */ #include "ixmlfile.h" -#include #include #include "expatxml.h" @@ -12,11 +11,19 @@ #include "writexml.h" #include "xmlnode.h" +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + namespace mdf { std::string IXmlFile::FileNameWithoutPath() const { try { - auto filename = std::filesystem::u8path(filename_).stem().u8string(); + auto filename = fs::u8path(filename_).stem().u8string(); return std::string(filename.begin(), filename.end()); } catch (const std::exception &error) { MDF_ERROR() << "Invalid path. File: " << filename_ @@ -65,7 +72,7 @@ bool IXmlFile::WriteFile() { return false; } try { - std::ofstream file(std::filesystem::u8path(filename_), + std::ofstream file(fs::u8path(filename_), std::ofstream::out | std::ofstream::trunc); if (!file.is_open()) { MDF_ERROR() << "Couldn't open file for writing. File: " << filename_; diff --git a/mdflib/src/mdfblock.h b/mdflib/src/mdfblock.h index c9c0cf76..f6abfe0c 100644 --- a/mdflib/src/mdfblock.h +++ b/mdflib/src/mdfblock.h @@ -15,6 +15,7 @@ #include "blockproperty.h" #include "littlebuffer.h" #include "mdf/imetadata.h" +#include "platform.h" namespace mdf::detail { diff --git a/mdflib/src/mdffile.cpp b/mdflib/src/mdffile.cpp index 35d6a5ee..26ca18e7 100644 --- a/mdflib/src/mdffile.cpp +++ b/mdflib/src/mdffile.cpp @@ -4,12 +4,19 @@ */ #include "mdf/mdffile.h" -#include #include #include "at4block.h" #include "mdf/mdflogstream.h" +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + namespace mdf { int MdfFile::MainVersion() const { @@ -44,7 +51,7 @@ void MdfFile::FileName(const std::string& filename) { filename_ = filename; if (name_.empty()) { try { - auto name = std::filesystem::u8path(filename).stem().u8string(); + auto name = fs::u8path(filename).stem().u8string(); name_ = std::string(name.begin(), name.end()); } catch (const std::exception& err) { MDF_ERROR() << "Invalid file name detected. Error: " << err.what() diff --git a/mdflib/src/mdfhelper.cpp b/mdflib/src/mdfhelper.cpp index 801e8c5b..b4131174 100644 --- a/mdflib/src/mdfhelper.cpp +++ b/mdflib/src/mdfhelper.cpp @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include "littlebuffer.h" diff --git a/mdflib/src/mdfreader.cpp b/mdflib/src/mdfreader.cpp index bc460c73..ac2ac128 100644 --- a/mdflib/src/mdfreader.cpp +++ b/mdflib/src/mdfreader.cpp @@ -4,9 +4,7 @@ */ #include "mdf/mdfreader.h" - #include -#include #include #include #include @@ -21,6 +19,15 @@ #include "cn4block.h" #include "sr4block.h" #include "sr3block.h" + +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + using namespace std::chrono_literals; namespace mdf { @@ -201,8 +208,8 @@ MdfReader::MdfReader(const std::string &filename) : filename_(filename) { // Need to create MDF3 of MDF4 file bool bExist = false; try { - std::filesystem::path p = std::filesystem::u8path(filename_); - if (std::filesystem::exists(p)) { + fs::path p = fs::u8path(filename_); + if (fs::exists(p)) { bExist = true; } } catch (const std::exception &error) { @@ -251,7 +258,7 @@ MdfReader::~MdfReader() { Close(); } std::string MdfReader::ShortName() const { try { - auto filename = std::filesystem::u8path(filename_).stem().u8string(); + auto filename = fs::u8path(filename_).stem().u8string(); return std::string(filename.begin(), filename.end()); } catch (const std::exception &) { } diff --git a/mdflib/src/mdfwriter.cpp b/mdflib/src/mdfwriter.cpp index 8ebdec95..27aa02e2 100644 --- a/mdflib/src/mdfwriter.cpp +++ b/mdflib/src/mdfwriter.cpp @@ -12,14 +12,20 @@ #include #include #include -#include - #include "dg3block.h" #include "mdfblock.h" #include "platform.h" -using namespace std::filesystem; +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + +using namespace fs; using namespace std::chrono_literals; namespace { @@ -111,7 +117,7 @@ bool MdfWriter::Init(const std::string& filename) { } std::FILE* file = nullptr; try { - if (std::filesystem::exists(filename_)) { + if (fs::exists(filename_)) { // Read in existing file so we can append to it detail::OpenMdfFile(file, filename_, "rb"); diff --git a/mdflib/src/platform.h b/mdflib/src/platform.h index 00312f34..4f941b28 100644 --- a/mdflib/src/platform.h +++ b/mdflib/src/platform.h @@ -7,6 +7,37 @@ #include #include +#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL + +#if defined(__cpp_lib_filesystem) +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0 +#elif defined(__cpp_lib_experimental_filesystem) +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1 +#elif !defined(__has_include) +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1 +#elif __has_include() + +#ifdef _MSC_VER +#if __has_include() +#include +#if defined(_HAS_CXX17) && _HAS_CXX17 +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0 +#endif +#endif +#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1 +#endif +#else // #ifdef _MSC_VER +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0 +#endif + +#elif __has_include() +#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1 +#else +#error Could not find system header "" or "" +#endif +#endif // #ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL + namespace Platform { int stricmp(const char *__s1, const char *__s2); int strnicmp(const char *__s1, const char *__s2, size_t __n); diff --git a/mdflib/src/zlibutil.cpp b/mdflib/src/zlibutil.cpp index 387680b4..e57f163a 100644 --- a/mdflib/src/zlibutil.cpp +++ b/mdflib/src/zlibutil.cpp @@ -8,10 +8,18 @@ #include #include -#include #include #include "platform.h" + +#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + namespace { constexpr size_t kZlibChunk = 16384; } @@ -76,8 +84,8 @@ bool Deflate(FILE* in, FILE* out) { bool Deflate(const std::string& filename, ByteArray& buf_out) { try { - std::filesystem::path name = std::filesystem::u8path(filename); - auto size = std::filesystem::file_size(name); + fs::path name = fs::u8path(filename); + auto size = fs::file_size(name); std::FILE* file = nullptr; Platform::fileopen(&file, filename.c_str(), "rb"); diff --git a/mdflibrary/CMakeLists.txt b/mdflibrary/CMakeLists.txt index 92f6b505..14eda5d5 100644 --- a/mdflibrary/CMakeLists.txt +++ b/mdflibrary/CMakeLists.txt @@ -44,6 +44,11 @@ endif() target_link_libraries(mdflibrary PRIVATE mdf) +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0 AND NOT MINGW) + # fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90050 + target_link_libraries(mdflibrary PRIVATE pthread stdc++fs) +endif() + target_include_directories( mdflibrary PUBLIC $) diff --git a/mdflibrary_example/src/test.cpp b/mdflibrary_example/src/test.cpp index 321d4fd5..f9453c9c 100644 --- a/mdflibrary_example/src/test.cpp +++ b/mdflibrary_example/src/test.cpp @@ -2,7 +2,6 @@ * Copyright 2023 Simplxs * SPDX-License-Identifier: MIT */ -#include #include #include @@ -13,9 +12,6 @@ using namespace MdfLibrary::ExportFunctions; void c_example() { std::cout << "C example" << std::endl; - if (std::filesystem::exists("test_c.mf4")) - std::filesystem::remove("test_c.mf4"); - { std::cout << "Write" << std::endl; auto* Writer = MdfWriterInit(MdfWriterType::Mdf4Basic, "test_c.mf4"); @@ -195,8 +191,6 @@ void c_example() { void cpp_example() { std::cout << "C++ example" << std::endl; { - if (std::filesystem::exists("test_cpp.mf4")) - std::filesystem::remove("test_cpp.mf4"); std::cout << "Write Basic" << std::endl; MdfWriter Writer(MdfWriterType::Mdf4Basic, "test_cpp.mf4"); @@ -310,8 +304,6 @@ void cpp_example() { } { - if (std::filesystem::exists("test_can_cpp.mf4")) - std::filesystem::remove("test_can_cpp.mf4"); std::cout << "Write Can" << std::endl; MdfWriter Writer(MdfWriterType::MdfBusLogger, "test_can_cpp.mf4");