/* * Copyright (c) 2023 Robin E. R. Davies * All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #pragma once #include #include #include #include #include #include namespace pipedal { inline bool endsWith(const std::string &str, const std::string &suffix) { return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); } void SetThreadName(const std::string &name); std::u32string ToUtf32(const std::string &s); inline bool isPathSeparator(char c) { return c == '/' || c == std::filesystem::path::preferred_separator; } std::string GetHostName(); std::vector split(const std::string &value, char delimiter); inline void ReadBarrier() { std::atomic_thread_fence(std::memory_order::acquire); } inline void WriteBarrier() { std::atomic_thread_fence(std::memory_order::release); } template inline bool contains(const std::vector &vector, const T &value) { for (auto i = vector.begin(); i != vector.end(); ++i) { if ((*i) == value) { return true; } } return false; } inline bool contains(const std::vector &vector, const char *value) { return contains(vector, std::string(value)); } bool HasWritePermissions(const std::filesystem::path &path); class NoCopy { public: NoCopy() {} NoCopy(const NoCopy &) = delete; NoCopy &operator=(const NoCopy &) = delete; ~NoCopy() {} }; class NoMove { public: NoMove() {} NoMove(NoMove &&) = delete; NoMove &operator=(NoMove &&) = delete; ~NoMove() {} }; bool IsChildDirectory(const std::filesystem::path &path, const std::filesystem::path&rootPath); template std::set Intersect(const std::set &left, const std::set &right) { std::set result; for (const T &v : left) { if (right.contains(v)) { result.insert(v); } } return result; } template bool IsSubset(const std::set &subset, const std::set &set) { std::set result; for (const T &v : subset) { if (!set.contains(v)) return false; } return true; } std::filesystem::path MakeRelativePath(const std::filesystem::path &path, const std::filesystem::path&parentPath); bool IsSubdirectory(const std::filesystem::path &path, const std::filesystem::path &basePath); bool HasDotDot(const std::filesystem::path &path); std::string ToLower(const std::string&value); std::string SafeFilenameToString(const std::string &filename); std::string StringToSafeFilename(const std::string &name); std::string ShellEscape(const std::string &input); }