Add `std::string_view` overloads to certain I/O functions

This commit is contained in:
Martchus 2022-03-04 21:04:22 +01:00
parent 77c353fb6c
commit 3d59664021
5 changed files with 54 additions and 18 deletions

View File

@ -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

View File

@ -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<string::size_type>(file.tellg());

View File

@ -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

View File

@ -1,3 +1,5 @@
#define CPP_UTILITIES_PATHHELPER_STRING_VIEW
#include "./path.h"
#include <cstdlib>
@ -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;

View File

@ -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