diff --git a/CMakeLists.txt b/CMakeLists.txt index 00cbb87..655ec9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,7 +114,7 @@ set(META_APP_AUTHOR "Martchus") set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities") set(META_VERSION_MAJOR 5) -set(META_VERSION_MINOR 13) +set(META_VERSION_MINOR 14) set(META_VERSION_PATCH 0) # find required 3rd party libraries diff --git a/io/misc.cpp b/io/misc.cpp index ed6290e..61f9cdd 100644 --- a/io/misc.cpp +++ b/io/misc.cpp @@ -1,3 +1,5 @@ +#define CPP_UTILITIES_IOMISC_STRING_VIEW + #include "./misc.h" #include "./nativefilestream.h" @@ -11,13 +13,22 @@ namespace CppUtilities { * \brief Reads all contents of the specified file in a single call. * \throws Throws std::ios_base::failure when an error occurs or the specified \a maxSize * would be exceeded. - * \todo Use std::string_view to pass \a path in v6. */ std::string readFile(const std::string &path, std::string::size_type maxSize) +{ + return readFile(std::string_view(path), maxSize); +} + +/*! + * \brief Reads all contents of the specified file in a single call. + * \throws Throws std::ios_base::failure when an error occurs or the specified \a maxSize + * would be exceeded. + */ +std::string readFile(std::string_view path, std::string_view::size_type maxSize) { NativeFileStream file; file.exceptions(ios_base::failbit | ios_base::badbit); - file.open(path, ios_base::in | ios_base::binary); + file.open(path.data(), ios_base::in | ios_base::binary); file.seekg(0, ios_base::end); string res; const auto size = static_cast(file.tellg()); diff --git a/io/misc.h b/io/misc.h index 86739ba..fcc83a2 100644 --- a/io/misc.h +++ b/io/misc.h @@ -9,6 +9,9 @@ namespace CppUtilities { CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize = std::string::npos); +#ifdef CPP_UTILITIES_IOMISC_STRING_VIEW +CPP_UTILITIES_EXPORT std::string readFile(std::string_view path, std::string_view::size_type maxSize = std::string_view::npos); +#endif CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents); } // namespace CppUtilities diff --git a/io/path.cpp b/io/path.cpp index 70a81db..fdc7c36 100644 --- a/io/path.cpp +++ b/io/path.cpp @@ -1,3 +1,5 @@ +#define CPP_UTILITIES_PATHHELPER_STRING_VIEW + #include "./path.h" #include @@ -12,16 +14,32 @@ namespace CppUtilities { /*! * \brief Returns the file name and extension of the specified \a path string. */ -string fileName(const string &path) +std::string fileName(const std::string &path) { - size_t lastSlash = path.rfind('/'); - size_t lastBackSlash = path.rfind('\\'); - size_t lastSeparator; - if (lastSlash == string::npos && lastBackSlash == string::npos) { + return std::string(fileName(std::string_view(path))); +} + +/*! + * \brief Returns the directory of the specified \a path string (including trailing slash). + */ +std::string directory(const std::string &path) +{ + return std::string(directory(std::string_view(path))); +} + +/*! + * \brief Returns the file name and extension of the specified \a path string. + */ +std::string_view fileName(std::string_view path) +{ + std::size_t lastSlash = path.rfind('/'); + std::size_t lastBackSlash = path.rfind('\\'); + std::size_t lastSeparator; + if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) { return path; - } else if (lastSlash == string::npos) { + } else if (lastSlash == std::string::npos) { lastSeparator = lastBackSlash; - } else if (lastBackSlash == string::npos) { + } else if (lastBackSlash == std::string::npos) { lastSeparator = lastSlash; } else { lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash; @@ -32,16 +50,16 @@ string fileName(const string &path) /*! * \brief Returns the directory of the specified \a path string (including trailing slash). */ -string directory(const string &path) +std::string_view directory(std::string_view path) { - size_t lastSlash = path.rfind('/'); - size_t lastBackSlash = path.rfind('\\'); - size_t lastSeparator; - if (lastSlash == string::npos && lastBackSlash == string::npos) { - return string(); - } else if (lastSlash == string::npos) { + std::size_t lastSlash = path.rfind('/'); + std::size_t lastBackSlash = path.rfind('\\'); + std::size_t lastSeparator; + if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) { + return std::string_view(); + } else if (lastSlash == std::string::npos) { lastSeparator = lastBackSlash; - } else if (lastBackSlash == string::npos) { + } else if (lastBackSlash == std::string::npos) { lastSeparator = lastSlash; } else { lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash; diff --git a/io/path.h b/io/path.h index f42fa1c..8554af4 100644 --- a/io/path.h +++ b/io/path.h @@ -22,6 +22,10 @@ namespace CppUtilities { CPP_UTILITIES_EXPORT std::string fileName(const std::string &path); CPP_UTILITIES_EXPORT std::string directory(const std::string &path); +#ifdef CPP_UTILITIES_PATHHELPER_STRING_VIEW +CPP_UTILITIES_EXPORT std::string_view fileName(std::string_view path); +CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path); +#endif CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName); } // namespace CppUtilities