2022-03-04 21:04:22 +01:00
|
|
|
#define CPP_UTILITIES_IOMISC_STRING_VIEW
|
|
|
|
|
2017-02-03 00:55:15 +01:00
|
|
|
#include "./misc.h"
|
2018-09-29 18:05:02 +02:00
|
|
|
#include "./nativefilestream.h"
|
2017-02-03 00:55:15 +01:00
|
|
|
|
|
|
|
#include <streambuf>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
namespace CppUtilities {
|
2017-02-03 00:55:15 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \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.
|
|
|
|
*/
|
2019-06-01 15:07:08 +02:00
|
|
|
std::string readFile(const std::string &path, std::string::size_type maxSize)
|
2022-03-04 21:04:22 +01:00
|
|
|
{
|
|
|
|
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)
|
2017-02-03 00:55:15 +01:00
|
|
|
{
|
2018-09-29 18:05:02 +02:00
|
|
|
NativeFileStream file;
|
2017-02-03 00:55:15 +01:00
|
|
|
file.exceptions(ios_base::failbit | ios_base::badbit);
|
2022-03-04 21:04:22 +01:00
|
|
|
file.open(path.data(), ios_base::in | ios_base::binary);
|
2017-02-03 00:55:15 +01:00
|
|
|
file.seekg(0, ios_base::end);
|
|
|
|
string res;
|
|
|
|
const auto size = static_cast<string::size_type>(file.tellg());
|
2017-05-01 03:13:11 +02:00
|
|
|
if (maxSize != string::npos && size > maxSize) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw ios_base::failure("File exceeds max size");
|
2017-02-03 00:55:15 +01:00
|
|
|
}
|
|
|
|
res.reserve(size);
|
|
|
|
file.seekg(ios_base::beg);
|
2022-05-12 20:09:28 +02:00
|
|
|
// ignore warning about null pointer dereference from GCC 12 for now (which is *likely* not correct)
|
|
|
|
#ifdef __GNUC__
|
2022-05-21 12:13:01 +02:00
|
|
|
#pragma GCC diagnostic push
|
2022-05-12 20:09:28 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wnull-dereference"
|
|
|
|
#endif
|
2017-02-03 00:55:15 +01:00
|
|
|
res.assign((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
|
2022-05-12 20:09:28 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2017-02-03 00:55:15 +01:00
|
|
|
return res;
|
|
|
|
}
|
2019-12-15 19:00:15 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes all \a contents to the specified file in a single call.
|
|
|
|
* \throws Throws std::ios_base::failure when an error occurs.
|
|
|
|
* \remarks Closing the file manually to prevent flushing the file within the d'tor which
|
|
|
|
* would suppress an exception
|
|
|
|
*/
|
|
|
|
void writeFile(std::string_view path, std::string_view contents)
|
|
|
|
{
|
|
|
|
NativeFileStream file;
|
|
|
|
file.exceptions(ios_base::failbit | ios_base::badbit);
|
|
|
|
file.open(path.data(), ios_base::out | ios_base::trunc | ios_base::binary);
|
|
|
|
file.write(contents.data(), static_cast<std::streamoff>(contents.size()));
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
} // namespace CppUtilities
|