diff --git a/components/bgsm/stream.cpp b/components/bgsm/stream.cpp index c4fa9c1d8c..455bbf2350 100644 --- a/components/bgsm/stream.cpp +++ b/components/bgsm/stream.cpp @@ -1,5 +1,13 @@ #include "stream.hpp" +#include +#include +#include + +#include +#include +#include + namespace Bgsm { template <> @@ -30,8 +38,9 @@ namespace Bgsm throw std::runtime_error("Requested string length is too large: " + std::to_string(length)); str = std::string(length, '\0'); mStream->read(str.data(), length); - if (mStream->bad()) - throw std::runtime_error("Failed to read sized string of " + std::to_string(length) + " chars"); + if (mStream->fail()) + throw std::runtime_error(std::format( + "Failed to read sized string of {} chars: {}", length, std::generic_category().message(errno))); std::size_t end = str.find('\0'); if (end != std::string::npos) str.erase(end); diff --git a/components/bgsm/stream.hpp b/components/bgsm/stream.hpp index a355523367..86ed529dc3 100644 --- a/components/bgsm/stream.hpp +++ b/components/bgsm/stream.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -23,9 +23,9 @@ namespace Bgsm { static_assert(std::is_arithmetic_v, "Buffer element type is not arithmetic"); pIStream->read(reinterpret_cast(dest), numInstances * sizeof(T)); - if (pIStream->bad()) - throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") buffer of " - + std::to_string(numInstances) + " instances"); + if (pIStream->fail()) + throw std::runtime_error(std::format("Failed to read typed ({}) buffer of {} instances: {}", + typeid(T).name(), numInstances, std::generic_category().message(errno))); if constexpr (Misc::IS_BIG_ENDIAN) for (std::size_t i = 0; i < numInstances; i++) Misc::swapEndiannessInplace(dest[i]); diff --git a/components/nif/nifstream.cpp b/components/nif/nifstream.cpp index 6c361401e8..cd362892a6 100644 --- a/components/nif/nifstream.cpp +++ b/components/nif/nifstream.cpp @@ -1,6 +1,10 @@ #include "nifstream.hpp" +#include +#include #include +#include +#include #include @@ -55,8 +59,9 @@ namespace Nif { std::string str(length, '\0'); mStream->read(str.data(), length); - if (mStream->bad()) - throw std::runtime_error("Failed to read sized string of " + std::to_string(length) + " chars"); + if (mStream->fail()) + throw std::runtime_error(std::format( + "Failed to read sized string of {} chars: {}", length, std::generic_category().message(errno))); size_t end = str.find('\0'); if (end != std::string::npos) str.erase(end); @@ -76,8 +81,9 @@ namespace Nif { std::string result; std::getline(*mStream, result); - if (mStream->bad()) - throw std::runtime_error("Failed to read version string"); + if (mStream->fail()) + throw std::runtime_error( + std::format("Failed to read version string: {}", std::generic_category().message(errno))); return result; } @@ -86,8 +92,9 @@ namespace Nif size_t size = get(); std::string str(size, '\0'); mStream->read(str.data(), size); - if (mStream->bad()) - throw std::runtime_error("Failed to read string palette of " + std::to_string(size) + " chars"); + if (mStream->fail()) + throw std::runtime_error(std::format( + "Failed to read string palette of {} chars: {}", size, std::generic_category().message(errno))); return str; } diff --git a/components/nif/nifstream.hpp b/components/nif/nifstream.hpp index 062f7c6512..6862bf882a 100644 --- a/components/nif/nifstream.hpp +++ b/components/nif/nifstream.hpp @@ -5,10 +5,13 @@ #include #include +#include +#include #include #include #include #include +#include #include #include @@ -40,9 +43,9 @@ namespace Nif std::is_arithmetic_v || std::is_same_v, "Buffer element type is not arithmetic"); static_assert(!std::is_same_v, "Buffer element type is boolean"); pIStream->read((char*)dest, numInstances * sizeof(T)); - if (pIStream->bad()) - throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") buffer of " - + std::to_string(numInstances) + " instances"); + if (pIStream->fail()) + throw std::runtime_error(std::format("Failed to read typed ({}) dynamic buffer of {} instances: {}", + typeid(T).name(), numInstances, std::generic_category().message(errno))); if constexpr (Misc::IS_BIG_ENDIAN) for (std::size_t i = 0; i < numInstances; i++) Misc::swapEndiannessInplace(dest[i]); @@ -61,9 +64,9 @@ namespace Nif std::is_arithmetic_v || std::is_same_v, "Buffer element type is not arithmetic"); static_assert(!std::is_same_v, "Buffer element type is boolean"); pIStream->read((char*)dest, numInstances * sizeof(T)); - if (pIStream->bad()) - throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") dynamic buffer of " - + std::to_string(numInstances) + " instances"); + if (pIStream->fail()) + throw std::runtime_error(std::format("Failed to read typed ({}) dynamic buffer of {} instances: {}", + typeid(T).name(), numInstances, std::generic_category().message(errno))); if constexpr (Misc::IS_BIG_ENDIAN) for (std::size_t i = 0; i < numInstances; i++) Misc::swapEndiannessInplace(dest[i]);