Deprecated readString() method so it is always clear if you want to read a binary encoded string or the file as string. Also added possibility to open a memory stream from any span.
This commit is contained in:
parent
463f4ca19c
commit
c214398fac
@ -34,7 +34,7 @@ namespace mijin
|
||||
|
||||
void Stream::flush() {}
|
||||
|
||||
StreamError Stream::readString(std::string& outString)
|
||||
StreamError Stream::readBinaryString(std::string& outString)
|
||||
{
|
||||
std::uint32_t length; // NOLINT(cppcoreguidelines-init-variables)
|
||||
StreamError error = read(length);
|
||||
@ -52,7 +52,7 @@ StreamError Stream::readString(std::string& outString)
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
StreamError Stream::writeString(std::string_view str)
|
||||
StreamError Stream::writeBinaryString(std::string_view str)
|
||||
{
|
||||
assert(str.length() <= std::numeric_limits<std::uint32_t>::max());
|
||||
const std::uint32_t length = static_cast<std::uint32_t>(str.length());
|
||||
@ -275,10 +275,10 @@ void MemoryStream::openRW(std::span<std::uint8_t> data)
|
||||
canWrite_ = true;
|
||||
}
|
||||
|
||||
void MemoryStream::openRO(std::span<const std::uint8_t> data)
|
||||
void MemoryStream::openROImpl(const void* data, std::size_t bytes)
|
||||
{
|
||||
assert(!isOpen());
|
||||
data_ = std::span<std::uint8_t>(const_cast<std::uint8_t*>(data.data()), data.size()); // NOLINT(cppcoreguidelines-pro-type-const-cast) we'll be fine
|
||||
data_ = std::span<std::uint8_t>(const_cast<std::uint8_t*>(static_cast<const std::uint8_t*>(data)), bytes); // NOLINT(cppcoreguidelines-pro-type-const-cast) we'll be fine
|
||||
pos_ = 0;
|
||||
canWrite_ = false;
|
||||
}
|
||||
|
@ -137,14 +137,25 @@ public:
|
||||
return writeSpan(std::span(std::forward<TItBegin>(begin), std::forward<TItEnd>(end)));
|
||||
}
|
||||
|
||||
StreamError readString(std::string& outString);
|
||||
StreamError writeString(std::string_view str);
|
||||
StreamError readBinaryString(std::string& outString);
|
||||
StreamError writeBinaryString(std::string_view str);
|
||||
|
||||
[[deprecated("Use readBinaryString() or readAsString() instead.")]]
|
||||
inline StreamError readString(std::string& outString) { return readBinaryString(outString); }
|
||||
|
||||
[[deprecated("Use writeBinaryString() or writeText() instead.")]]
|
||||
inline StreamError writeString(std::string_view str) { return writeBinaryString(str); }
|
||||
|
||||
StreamError getTotalLength(std::size_t& outLength);
|
||||
StreamError readRest(TypelessBuffer& outBuffer);
|
||||
|
||||
template<typename TChar = char>
|
||||
StreamError readAsString(std::basic_string<TChar>& outString);
|
||||
|
||||
inline StreamError writeText(std::string_view str)
|
||||
{
|
||||
return writeSpan(str);
|
||||
}
|
||||
};
|
||||
|
||||
class FileStream : public Stream
|
||||
@ -181,7 +192,14 @@ private:
|
||||
bool canWrite_ = false;
|
||||
public:
|
||||
void openRW(std::span<std::uint8_t> data);
|
||||
void openRO(std::span<const std::uint8_t> data);
|
||||
template<typename T>
|
||||
void openRO(std::span<T> data) {
|
||||
openROImpl(data.data(), data.size_bytes());
|
||||
}
|
||||
template<typename TChar>
|
||||
inline void openRO(std::basic_string_view<TChar> stringView) {
|
||||
openROImpl(stringView.data(), stringView.size() * sizeof(TChar));
|
||||
}
|
||||
void close();
|
||||
[[nodiscard]] inline bool isOpen() const { return data_.data() != nullptr; }
|
||||
[[nodiscard]] inline std::size_t availableBytes() const {
|
||||
@ -196,6 +214,8 @@ public:
|
||||
StreamError seek(std::intptr_t pos, SeekMode seekMode = SeekMode::ABSOLUTE) override;
|
||||
bool isAtEnd() override;
|
||||
StreamFeatures getFeatures() override;
|
||||
private:
|
||||
void openROImpl(const void* data, std::size_t bytes);
|
||||
};
|
||||
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user