From d15281a08c6652f4171c3294ece6343dbab81ed9 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Wed, 14 Aug 2024 13:37:50 -0400 Subject: [PATCH] Reimplement basic string ops for C++17 compatibility --- src/libraries/JANA/Services/JPluginLoader.cc | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libraries/JANA/Services/JPluginLoader.cc b/src/libraries/JANA/Services/JPluginLoader.cc index 6f4444eb0..702958047 100644 --- a/src/libraries/JANA/Services/JPluginLoader.cc +++ b/src/libraries/JANA/Services/JPluginLoader.cc @@ -261,9 +261,22 @@ JPlugin::~JPlugin() { LOG_DEBUG(m_logger) << "Unloaded plugin \"" << m_name << "\"" << LOG_END; } +std::string path_filename_stem(const std::string& filesystem_path) { + // Ideally we would just do this, but we want to be C++17 compatible + // return std::filesystem::path(filesystem_path).filename().stem().string(); + + size_t pos_begin = filesystem_path.find_last_of('/'); + if (pos_begin == std::string::npos) pos_begin = 0; + + size_t pos_end = filesystem_path.find_last_of('.'); + //if (pos_end == std::string::npos) pos_end = filesystem_path.size(); + + return filesystem_path.substr(pos_begin+1, pos_end-pos_begin-1); +} + std::pair> JPluginLoader::extract_name_and_maybe_path(std::string user_name_or_path) { if (user_name_or_path.find('/') != -1) { - std::string name = std::filesystem::path(user_name_or_path).filename().stem().string(); + std::string name = path_filename_stem(user_name_or_path); std::string path = user_name_or_path; return {name, path}; } @@ -275,11 +288,17 @@ std::pair> JPluginLoader::extract_name_a } } +bool ends_with(const std::string& s, char c) { + // Ideally we would just do s.ends_with(c), but we want to be C++17 compatible + if (s.empty()) return false; + return s.back() == c; +} + std::string JPluginLoader::make_path_from_name(std::string name, const std::string& path_prefix) { std::ostringstream oss; oss << path_prefix; - if (!path_prefix.ends_with('/')) { + if (!ends_with(path_prefix, '/')) { oss << "/"; } oss << name; @@ -303,6 +322,7 @@ std::optional JPluginLoader::find_first_valid_path(std::string name return std::nullopt; } + bool JPluginLoader::validate_path(const std::string& path) { return (access(path.c_str(), F_OK) != -1); }