Add `std::string_view` overloads to certain I/O functions
This commit is contained in:
parent
77c353fb6c
commit
3d59664021
|
@ -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_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_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
|
||||||
set(META_VERSION_MAJOR 5)
|
set(META_VERSION_MAJOR 5)
|
||||||
set(META_VERSION_MINOR 13)
|
set(META_VERSION_MINOR 14)
|
||||||
set(META_VERSION_PATCH 0)
|
set(META_VERSION_PATCH 0)
|
||||||
|
|
||||||
# find required 3rd party libraries
|
# find required 3rd party libraries
|
||||||
|
|
15
io/misc.cpp
15
io/misc.cpp
|
@ -1,3 +1,5 @@
|
||||||
|
#define CPP_UTILITIES_IOMISC_STRING_VIEW
|
||||||
|
|
||||||
#include "./misc.h"
|
#include "./misc.h"
|
||||||
#include "./nativefilestream.h"
|
#include "./nativefilestream.h"
|
||||||
|
|
||||||
|
@ -11,13 +13,22 @@ namespace CppUtilities {
|
||||||
* \brief Reads all contents of the specified file in a single call.
|
* \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
|
* \throws Throws std::ios_base::failure when an error occurs or the specified \a maxSize
|
||||||
* would be exceeded.
|
* 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)
|
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;
|
NativeFileStream file;
|
||||||
file.exceptions(ios_base::failbit | ios_base::badbit);
|
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);
|
file.seekg(0, ios_base::end);
|
||||||
string res;
|
string res;
|
||||||
const auto size = static_cast<string::size_type>(file.tellg());
|
const auto size = static_cast<string::size_type>(file.tellg());
|
||||||
|
|
|
@ -9,6 +9,9 @@
|
||||||
namespace CppUtilities {
|
namespace CppUtilities {
|
||||||
|
|
||||||
CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize = std::string::npos);
|
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);
|
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents);
|
||||||
} // namespace CppUtilities
|
} // namespace CppUtilities
|
||||||
|
|
||||||
|
|
48
io/path.cpp
48
io/path.cpp
|
@ -1,3 +1,5 @@
|
||||||
|
#define CPP_UTILITIES_PATHHELPER_STRING_VIEW
|
||||||
|
|
||||||
#include "./path.h"
|
#include "./path.h"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
@ -12,16 +14,32 @@ namespace CppUtilities {
|
||||||
/*!
|
/*!
|
||||||
* \brief Returns the file name and extension of the specified \a path string.
|
* \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('/');
|
return std::string(fileName(std::string_view(path)));
|
||||||
size_t lastBackSlash = path.rfind('\\');
|
}
|
||||||
size_t lastSeparator;
|
|
||||||
if (lastSlash == string::npos && lastBackSlash == string::npos) {
|
/*!
|
||||||
|
* \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;
|
return path;
|
||||||
} else if (lastSlash == string::npos) {
|
} else if (lastSlash == std::string::npos) {
|
||||||
lastSeparator = lastBackSlash;
|
lastSeparator = lastBackSlash;
|
||||||
} else if (lastBackSlash == string::npos) {
|
} else if (lastBackSlash == std::string::npos) {
|
||||||
lastSeparator = lastSlash;
|
lastSeparator = lastSlash;
|
||||||
} else {
|
} else {
|
||||||
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
|
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).
|
* \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('/');
|
std::size_t lastSlash = path.rfind('/');
|
||||||
size_t lastBackSlash = path.rfind('\\');
|
std::size_t lastBackSlash = path.rfind('\\');
|
||||||
size_t lastSeparator;
|
std::size_t lastSeparator;
|
||||||
if (lastSlash == string::npos && lastBackSlash == string::npos) {
|
if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
|
||||||
return string();
|
return std::string_view();
|
||||||
} else if (lastSlash == string::npos) {
|
} else if (lastSlash == std::string::npos) {
|
||||||
lastSeparator = lastBackSlash;
|
lastSeparator = lastBackSlash;
|
||||||
} else if (lastBackSlash == string::npos) {
|
} else if (lastBackSlash == std::string::npos) {
|
||||||
lastSeparator = lastSlash;
|
lastSeparator = lastSlash;
|
||||||
} else {
|
} else {
|
||||||
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
|
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
|
||||||
|
|
|
@ -22,6 +22,10 @@ namespace CppUtilities {
|
||||||
|
|
||||||
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path);
|
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path);
|
||||||
CPP_UTILITIES_EXPORT std::string directory(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);
|
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
|
||||||
} // namespace CppUtilities
|
} // namespace CppUtilities
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue