Skip to content

Commit

Permalink
fix #79:include <experimental/filesystem> for old compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
ijulles committed Apr 18, 2024
1 parent 9bd5309 commit 53d70fe
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
23 changes: 15 additions & 8 deletions mdflib/src/at4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "at4block.h"

#include <cerrno>
#include <filesystem>
#include <sstream>

#include "mdf/cryptoutil.h"
Expand All @@ -14,7 +13,15 @@
#include "mdf/zlibutil.h"
#include "platform.h"

using namespace std::filesystem;
#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

using namespace fs;

namespace {

Expand Down Expand Up @@ -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&) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions mdflib/src/cryptoutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
#include <array>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <iomanip>

#include "mdf/mdflogstream.h"

#include "platform.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

namespace {

#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Expand Down Expand Up @@ -240,8 +248,8 @@ bool CreateMd5FileChecksum(const std::string &file, std::vector<uint8_t> &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) {
Expand Down
9 changes: 8 additions & 1 deletion mdflib/src/expatxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
#include "expatxml.h"

#include <array>
#include <filesystem>

#include "mdf/mdflogstream.h"
#include "platform.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

namespace {
void StartElementHandler(void *userData, const XML_Char *name,
const XML_Char **attributes) {
Expand Down
2 changes: 1 addition & 1 deletion mdflib/src/ichannelconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(0));
}
value_list_[index] = parameter;
}
Expand Down
13 changes: 10 additions & 3 deletions mdflib/src/ixmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
*/
#include "ixmlfile.h"

#include <filesystem>
#include <fstream>

#include "expatxml.h"
#include "mdf/mdflogstream.h"
#include "writexml.h"
#include "xmlnode.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
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_
Expand Down Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions mdflib/src/mdfblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "blockproperty.h"
#include "littlebuffer.h"
#include "mdf/imetadata.h"
#include "platform.h"

namespace mdf::detail {

Expand Down
11 changes: 9 additions & 2 deletions mdflib/src/mdffile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
*/
#include "mdf/mdffile.h"

#include <filesystem>
#include <string>

#include "at4block.h"
#include "mdf/mdflogstream.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

namespace mdf {

int MdfFile::MainVersion() const {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion mdflib/src/mdfhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <codecvt>
#include <cstring>
#include <ctime>
#include <filesystem>
#include <iomanip>
#include <sstream>
#include <chrono>

#include "littlebuffer.h"

Expand Down
17 changes: 12 additions & 5 deletions mdflib/src/mdfreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*/
#include "mdf/mdfreader.h"


#include <cstdio>
#include <filesystem>
#include <string>
#include <thread>
#include <vector>
Expand All @@ -21,6 +19,15 @@
#include "cn4block.h"
#include "sr4block.h"
#include "sr3block.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

using namespace std::chrono_literals;

namespace mdf {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 &) {
}
Expand Down
14 changes: 10 additions & 4 deletions mdflib/src/mdfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <filesystem>


#include "dg3block.h"
#include "mdfblock.h"
#include "platform.h"

using namespace std::filesystem;
#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

using namespace fs;
using namespace std::chrono_literals;

namespace {
Expand Down Expand Up @@ -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");
Expand Down
31 changes: 31 additions & 0 deletions mdflib/src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@
#include <cstdint>
#include <cstdio>

#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(<filesystem>)

#ifdef _MSC_VER
#if __has_include(<yvals_core.h>)
#include <yvals_core.h>
#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(<experimental/filesystem>)
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
#else
#error Could not find system header "<filesystem>" or "<experimental/filesystem>"
#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);
Expand Down
14 changes: 11 additions & 3 deletions mdflib/src/zlibutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@

#include <cstdint>
#include <cstring>
#include <filesystem>
#include <string>

#include "platform.h"

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

namespace {
constexpr size_t kZlibChunk = 16384;
}
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions mdflibrary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>)

Expand Down
Loading

0 comments on commit 53d70fe

Please sign in to comment.