Added Stream functions for reading and writing 0-terminated strings.
This commit is contained in:
parent
b5546067a8
commit
7939d458b3
@ -113,6 +113,42 @@ StreamError Stream::writeBinaryString(std::string_view str)
|
||||
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)
|
||||
{
|
||||
std::uint32_t length; // NOLINT(cppcoreguidelines-init-variables)
|
||||
|
@ -219,7 +219,7 @@ public:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
StreamError write(const T& value) requires(std::is_trivial_v<T>)
|
||||
StreamError write(const T& value)
|
||||
{
|
||||
return writeRaw(&value, sizeof(T));
|
||||
}
|
||||
@ -259,6 +259,9 @@ public:
|
||||
StreamError readBinaryString(std::string& outString);
|
||||
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_writeBinaryString(std::string_view str);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user