Added Stream functions for reading and writing 0-terminated strings.
This commit is contained in:
parent
50532459ce
commit
2d413764f0
@ -113,6 +113,42 @@ StreamError Stream::writeBinaryString(std::string_view str)
|
|||||||
return writeSpan(str.begin(), str.end());
|
return writeSpan(str.begin(), str.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamError Stream::readZString(std::string& outString)
|
||||||
|
{
|
||||||
|
char chr = '\0';
|
||||||
|
std::string result;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (isAtEnd())
|
||||||
|
{
|
||||||
|
return StreamError::IO_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StreamError error = read(chr); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
if (chr == '\0')
|
||||||
|
{
|
||||||
|
outString = std::move(result);
|
||||||
|
return StreamError::SUCCESS;
|
||||||
|
}
|
||||||
|
result.push_back(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamError Stream::writeZString(std::string_view str)
|
||||||
|
{
|
||||||
|
static const char ZERO = '\0';
|
||||||
|
|
||||||
|
if (StreamError error = writeRaw(str.data(), str.size() * sizeof(char)); error != StreamError::SUCCESS)
|
||||||
|
{
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return write(ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
mijin::Task<StreamError> Stream::c_readBinaryString(std::string& outString)
|
mijin::Task<StreamError> Stream::c_readBinaryString(std::string& outString)
|
||||||
{
|
{
|
||||||
std::uint32_t length; // NOLINT(cppcoreguidelines-init-variables)
|
std::uint32_t length; // NOLINT(cppcoreguidelines-init-variables)
|
||||||
|
@ -221,7 +221,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
StreamError write(const T& value) requires(std::is_trivial_v<T>)
|
StreamError write(const T& value)
|
||||||
{
|
{
|
||||||
return writeRaw(&value, sizeof(T));
|
return writeRaw(&value, sizeof(T));
|
||||||
}
|
}
|
||||||
@ -261,6 +261,9 @@ public:
|
|||||||
StreamError readBinaryString(std::string& outString);
|
StreamError readBinaryString(std::string& outString);
|
||||||
StreamError writeBinaryString(std::string_view str);
|
StreamError writeBinaryString(std::string_view str);
|
||||||
|
|
||||||
|
StreamError readZString(std::string& outString);
|
||||||
|
StreamError writeZString(std::string_view str);
|
||||||
|
|
||||||
mijin::Task<StreamError> c_readBinaryString(std::string& outString);
|
mijin::Task<StreamError> c_readBinaryString(std::string& outString);
|
||||||
mijin::Task<StreamError> c_writeBinaryString(std::string_view str);
|
mijin::Task<StreamError> c_writeBinaryString(std::string_view str);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user