diff --git a/AK/DeprecatedMemoryStream.h b/AK/DeprecatedMemoryStream.h deleted file mode 100644 index cf0f26ed35..0000000000 --- a/AK/DeprecatedMemoryStream.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2020, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include - -namespace AK { - -class DeprecatedInputMemoryStream final : public DeprecatedInputStream { -public: - explicit DeprecatedInputMemoryStream(ReadonlyBytes bytes) - : m_bytes(bytes) - { - } - - bool unreliable_eof() const override { return eof(); } - bool eof() const { return m_offset >= m_bytes.size(); } - - size_t read(Bytes bytes) override - { - if (has_any_error()) - return 0; - - auto const count = min(bytes.size(), remaining()); - __builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, count); - m_offset += count; - return count; - } - - bool read_or_error(Bytes bytes) override - { - if (remaining() < bytes.size()) { - set_recoverable_error(); - return false; - } - - __builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, bytes.size()); - m_offset += bytes.size(); - return true; - } - - bool discard_or_error(size_t count) override - { - if (remaining() < count) { - set_recoverable_error(); - return false; - } - - m_offset += count; - return true; - } - - void seek(size_t offset) - { - VERIFY(offset < m_bytes.size()); - m_offset = offset; - } - - u8 peek_or_error() const - { - if (remaining() == 0) { - set_recoverable_error(); - return 0; - } - - return m_bytes[m_offset]; - } - - ReadonlyBytes bytes() const { return m_bytes; } - size_t offset() const { return m_offset; } - size_t remaining() const { return m_bytes.size() - m_offset; } - -private: - ReadonlyBytes m_bytes; - size_t m_offset { 0 }; -}; - -class DeprecatedOutputMemoryStream final : public DeprecatedOutputStream { -public: - explicit DeprecatedOutputMemoryStream(Bytes bytes) - : m_bytes(bytes) - { - } - - size_t write(ReadonlyBytes bytes) override - { - auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset)); - m_offset += nwritten; - return nwritten; - } - - bool write_or_error(ReadonlyBytes bytes) override - { - if (remaining() < bytes.size()) { - set_recoverable_error(); - return false; - } - - write(bytes); - return true; - } - - size_t fill_to_end(u8 value) - { - auto const nwritten = m_bytes.slice(m_offset).fill(value); - m_offset += nwritten; - return nwritten; - } - - bool is_end() const { return remaining() == 0; } - - ReadonlyBytes bytes() const { return { data(), size() }; } - Bytes bytes() { return { data(), size() }; } - - u8 const* data() const { return m_bytes.data(); } - u8* data() { return m_bytes.data(); } - - size_t size() const { return m_offset; } - size_t remaining() const { return m_bytes.size() - m_offset; } - void reset() { m_offset = 0; } - -private: - size_t m_offset { 0 }; - Bytes m_bytes; -}; - -} - -#if USING_AK_GLOBALLY -using AK::DeprecatedInputMemoryStream; -using AK::DeprecatedOutputMemoryStream; -#endif diff --git a/AK/DeprecatedStream.h b/AK/DeprecatedStream.h deleted file mode 100644 index 694ed16e3f..0000000000 --- a/AK/DeprecatedStream.h +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2020, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace AK::Detail { - -class DeprecatedStream { -public: - virtual ~DeprecatedStream() { VERIFY(!has_any_error()); } - - virtual bool has_recoverable_error() const { return m_recoverable_error; } - virtual bool has_fatal_error() const { return m_fatal_error; } - virtual bool has_any_error() const { return has_recoverable_error() || has_fatal_error(); } - - virtual bool handle_recoverable_error() - { - VERIFY(!has_fatal_error()); - return exchange(m_recoverable_error, false); - } - virtual bool handle_fatal_error() { return exchange(m_fatal_error, false); } - virtual bool handle_any_error() - { - if (has_any_error()) { - m_recoverable_error = false; - m_fatal_error = false; - - return true; - } - - return false; - } - - ErrorOr try_handle_any_error() - { - if (!handle_any_error()) - return {}; - return Error::from_string_literal("Stream error"); - } - - virtual void set_recoverable_error() const { m_recoverable_error = true; } - virtual void set_fatal_error() const { m_fatal_error = true; } - -private: - mutable bool m_recoverable_error { false }; - mutable bool m_fatal_error { false }; -}; - -} - -namespace AK { - -class DeprecatedInputStream : public virtual Detail::DeprecatedStream { -public: - // Reads at least one byte unless none are requested or none are available. Does nothing - // and returns zero if there is already an error. - virtual size_t read(Bytes) = 0; - - // If this function returns true, then no more data can be read. If read(Bytes) previously - // returned zero even though bytes were requested, then the inverse is true as well. - virtual bool unreliable_eof() const = 0; - - // Some streams additionally define a method with the signature: - // - // bool eof() const; - // - // This method has the same semantics as unreliable_eof() but returns true if and only if no - // more data can be read. (A failed read is not necessary.) - - virtual bool read_or_error(Bytes) = 0; - virtual bool discard_or_error(size_t count) = 0; -}; - -class DeprecatedOutputStream : public virtual Detail::DeprecatedStream { -public: - virtual size_t write(ReadonlyBytes) = 0; - virtual bool write_or_error(ReadonlyBytes) = 0; -}; - -class DeprecatedDuplexStream - : public DeprecatedInputStream - , public DeprecatedOutputStream { -}; - -inline DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, Bytes bytes) -{ - stream.read_or_error(bytes); - return stream; -} -inline DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, ReadonlyBytes bytes) -{ - stream.write_or_error(bytes); - return stream; -} - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, LittleEndian& value) -{ - return stream >> Bytes { &value.m_value, sizeof(value.m_value) }; -} -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, LittleEndian value) -{ - return stream << ReadonlyBytes { &value.m_value, sizeof(value.m_value) }; -} - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, BigEndian& value) -{ - return stream >> Bytes { &value.m_value, sizeof(value.m_value) }; -} -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, BigEndian value) -{ - return stream << ReadonlyBytes { &value.m_value, sizeof(value.m_value) }; -} - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, Optional& value) -{ - T temporary; - stream >> temporary; - value = temporary; - return stream; -} - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, I& value) -{ - stream.read_or_error({ &value, sizeof(value) }); - return stream; -} -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, I value) -{ - stream.write_or_error({ &value, sizeof(value) }); - return stream; -} - -#ifndef KERNEL - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, F& value) -{ - stream.read_or_error({ &value, sizeof(value) }); - return stream; -} -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, F value) -{ - stream.write_or_error({ &value, sizeof(value) }); - return stream; -} - -#endif - -} diff --git a/AK/DeprecatedString.cpp b/AK/DeprecatedString.cpp index cd5da89d83..5877bc7426 100644 --- a/AK/DeprecatedString.cpp +++ b/AK/DeprecatedString.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -416,29 +415,6 @@ bool DeprecatedString::operator==(char const* cstring) const return view() == cstring; } -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, DeprecatedString& string) -{ - StringBuilder builder; - - for (;;) { - char next_char; - stream >> next_char; - - if (stream.has_any_error()) { - stream.set_fatal_error(); - string = nullptr; - return stream; - } - - if (next_char) { - builder.append(next_char); - } else { - string = builder.to_deprecated_string(); - return stream; - } - } -} - DeprecatedString DeprecatedString::vformatted(StringView fmtstr, TypeErasedFormatParams& params) { StringBuilder builder; diff --git a/AK/DeprecatedString.h b/AK/DeprecatedString.h index 011dd45703..fd89efcb3a 100644 --- a/AK/DeprecatedString.h +++ b/AK/DeprecatedString.h @@ -338,8 +338,6 @@ struct CaseInsensitiveStringTraits : public Traits { DeprecatedString escape_html_entities(StringView html); -DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, DeprecatedString& string); - } #if USING_AK_GLOBALLY diff --git a/AK/Endian.h b/AK/Endian.h index 92bf739ceb..fa05afd7f6 100644 --- a/AK/Endian.h +++ b/AK/Endian.h @@ -76,21 +76,9 @@ ALWAYS_INLINE T convert_between_host_and_network_endian(T value) return convert_between_host_and_big_endian(value); } -template -class LittleEndian; - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream&, LittleEndian&); - -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream&, LittleEndian); - template class [[gnu::packed]] LittleEndian { public: - friend DeprecatedInputStream& operator>>(DeprecatedInputStream&, LittleEndian&); - friend DeprecatedOutputStream& operator<< (DeprecatedOutputStream&, LittleEndian); - constexpr LittleEndian() = default; constexpr LittleEndian(T value) @@ -108,21 +96,9 @@ private: T m_value { 0 }; }; -template -class BigEndian; - -template -DeprecatedInputStream& operator>>(DeprecatedInputStream&, BigEndian&); - -template -DeprecatedOutputStream& operator<<(DeprecatedOutputStream&, BigEndian); - template class [[gnu::packed]] BigEndian { public: - friend DeprecatedInputStream& operator>>(DeprecatedInputStream&, BigEndian&); - friend DeprecatedOutputStream& operator<< (DeprecatedOutputStream&, BigEndian); - constexpr BigEndian() = default; constexpr BigEndian(T value) diff --git a/AK/Forward.h b/AK/Forward.h index 3f67a9fd8a..b58158dd8c 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -22,10 +22,6 @@ class BigEndianInputBitStream; class BigEndianOutputBitStream; using ByteBuffer = Detail::ByteBuffer<32>; class CircularBuffer; -class DeprecatedInputStream; -class DeprecatedInputMemoryStream; -class DeprecatedOutputStream; -class DeprecatedOutputMemoryStream; class Error; class FlyString; class GenericLexer; @@ -167,10 +163,6 @@ using AK::Bytes; using AK::CircularBuffer; using AK::CircularQueue; using AK::DeprecatedFlyString; -using AK::DeprecatedInputMemoryStream; -using AK::DeprecatedInputStream; -using AK::DeprecatedOutputMemoryStream; -using AK::DeprecatedOutputStream; using AK::DeprecatedString; using AK::DeprecatedStringCodePointIterator; using AK::DoublyLinkedList; diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt index c59619642e..77a288d2ea 100644 --- a/Tests/AK/CMakeLists.txt +++ b/Tests/AK/CMakeLists.txt @@ -19,7 +19,6 @@ set(AK_TEST_SOURCES TestCircularDeque.cpp TestCircularQueue.cpp TestComplex.cpp - TestDeprecatedMemoryStream.cpp TestDeprecatedString.cpp TestDisjointChunks.cpp TestDistinctNumeric.cpp diff --git a/Tests/AK/TestDeprecatedMemoryStream.cpp b/Tests/AK/TestDeprecatedMemoryStream.cpp deleted file mode 100644 index 18dc3bb9f4..0000000000 --- a/Tests/AK/TestDeprecatedMemoryStream.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2020, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include - -#include -#include - -TEST_CASE(read_an_integer) -{ - u32 expected = 0x01020304, actual; - - DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } }; - stream >> actual; - - EXPECT(!stream.has_any_error() && stream.eof()); - EXPECT_EQ(expected, actual); -} - -TEST_CASE(read_a_bool) -{ - bool expected = true, actual; - - DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } }; - stream >> actual; - - EXPECT(!stream.has_any_error() && stream.eof()); - EXPECT_EQ(expected, actual); -} - -TEST_CASE(read_a_double) -{ - double expected = 3.141592653589793, actual; - - DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } }; - stream >> actual; - - EXPECT(!stream.has_any_error() && stream.eof()); - EXPECT_EQ(expected, actual); -} - -TEST_CASE(recoverable_error) -{ - u32 expected = 0x01020304, actual = 0; - u64 to_large_value = 0; - - DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } }; - - EXPECT(!stream.has_any_error() && !stream.eof()); - stream >> to_large_value; - EXPECT(stream.has_recoverable_error() && !stream.eof()); - - EXPECT(stream.handle_recoverable_error()); - EXPECT(!stream.has_any_error() && !stream.eof()); - - stream >> actual; - EXPECT(!stream.has_any_error() && stream.eof()); - EXPECT_EQ(expected, actual); -} - -TEST_CASE(chain_stream_operator) -{ - Array const expected { 0, 1, 2, 3 }; - Array actual; - - DeprecatedInputMemoryStream stream { expected }; - - stream >> actual[0] >> actual[1] >> actual[2] >> actual[3]; - EXPECT(!stream.has_any_error() && stream.eof()); - - EXPECT_EQ(expected, actual); -} - -TEST_CASE(seeking_slicing_offset) -{ - Array const input { 0, 1, 2, 3, 4, 5, 6, 7 }; - Array const expected0 { 0, 1, 2, 3 }; - Array const expected1 { 4, 5, 6, 7 }; - Array const expected2 { 1, 2, 3, 4 }; - - Array actual0 {}, actual1 {}, actual2 {}; - - DeprecatedInputMemoryStream stream { input }; - - stream >> actual0; - EXPECT(!stream.has_any_error() && !stream.eof()); - EXPECT_EQ(expected0, actual0); - - stream.seek(4); - stream >> actual1; - EXPECT(!stream.has_any_error() && stream.eof()); - EXPECT_EQ(expected1, actual1); - - stream.seek(1); - stream >> actual2; - EXPECT(!stream.has_any_error() && !stream.eof()); - EXPECT_EQ(expected2, actual2); -} - -TEST_CASE(read_endian_values) -{ - Array const input { 0, 1, 2, 3, 4, 5, 6, 7 }; - DeprecatedInputMemoryStream stream { input }; - - LittleEndian value1; - BigEndian value2; - stream >> value1 >> value2; - - EXPECT_EQ(value1, 0x03020100u); - EXPECT_EQ(value2, 0x04050607u); -} - -TEST_CASE(new_output_memory_stream) -{ - Array buffer; - DeprecatedOutputMemoryStream stream { buffer }; - - EXPECT_EQ(stream.size(), 0u); - EXPECT_EQ(stream.remaining(), 16u); - - stream << LittleEndian(0x12'87); - - EXPECT_EQ(stream.size(), 2u); - EXPECT_EQ(stream.remaining(), 14u); - - stream << buffer; - - EXPECT(stream.handle_recoverable_error()); - EXPECT_EQ(stream.size(), 2u); - EXPECT_EQ(stream.remaining(), 14u); - - EXPECT_EQ(stream.bytes().data(), buffer.data()); - EXPECT_EQ(stream.bytes().size(), 2u); -} diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index dc6d744a45..796b50f072 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -589,155 +589,4 @@ ErrorOr LocalSocket::release_fd() return fd; } -WrappedAKInputStream::WrappedAKInputStream(NonnullOwnPtr stream) - : m_stream(move(stream)) -{ -} - -ErrorOr WrappedAKInputStream::read(Bytes bytes) -{ - auto bytes_read = m_stream->read(bytes); - - if (m_stream->has_any_error()) - return Error::from_string_literal("Underlying InputStream indicated an error"); - - return bytes.slice(0, bytes_read); -} - -ErrorOr WrappedAKInputStream::discard(size_t discarded_bytes) -{ - if (!m_stream->discard_or_error(discarded_bytes)) - return Error::from_string_literal("Underlying InputStream indicated an error"); - - return {}; -} - -ErrorOr WrappedAKInputStream::write(ReadonlyBytes) -{ - return Error::from_errno(EBADF); -} - -bool WrappedAKInputStream::is_eof() const -{ - return m_stream->unreliable_eof(); -} - -bool WrappedAKInputStream::is_open() const -{ - return true; -} - -void WrappedAKInputStream::close() -{ -} - -WrappedAKOutputStream::WrappedAKOutputStream(NonnullOwnPtr stream) - : m_stream(move(stream)) -{ -} - -ErrorOr WrappedAKOutputStream::read(Bytes) -{ - return Error::from_errno(EBADF); -} - -ErrorOr WrappedAKOutputStream::write(ReadonlyBytes bytes) -{ - auto bytes_written = m_stream->write(bytes); - - if (m_stream->has_any_error()) - return Error::from_string_literal("Underlying OutputStream indicated an error"); - - return bytes_written; -} - -bool WrappedAKOutputStream::is_eof() const -{ - return true; -} - -bool WrappedAKOutputStream::is_open() const -{ - return true; -} - -void WrappedAKOutputStream::close() -{ -} - -WrapInAKInputStream::WrapInAKInputStream(AK::Stream& stream) - : m_stream(stream) -{ -} - -size_t WrapInAKInputStream::read(Bytes bytes) -{ - if (has_any_error()) - return 0; - - auto data_or_error = m_stream.read(bytes); - if (data_or_error.is_error()) { - set_fatal_error(); - return 0; - } - - return data_or_error.value().size(); -} - -bool WrapInAKInputStream::unreliable_eof() const -{ - return m_stream.is_eof(); -} - -bool WrapInAKInputStream::read_or_error(Bytes bytes) -{ - if (read(bytes) < bytes.size()) { - set_fatal_error(); - return false; - } - - return true; -} - -bool WrapInAKInputStream::discard_or_error(size_t count) -{ - auto maybe_error = m_stream.discard(count); - - if (maybe_error.is_error()) { - set_fatal_error(); - return false; - } - - return true; -} - -WrapInAKOutputStream::WrapInAKOutputStream(AK::Stream& stream) - : m_stream(stream) -{ -} - -size_t WrapInAKOutputStream::write(ReadonlyBytes bytes) -{ - if (has_any_error()) - return 0; - - auto length_or_error = m_stream.write(bytes); - if (length_or_error.is_error()) { - set_fatal_error(); - return 0; - } - - return length_or_error.value(); -} - -bool WrapInAKOutputStream::write_or_error(ReadonlyBytes bytes) -{ - if (write(bytes) < bytes.size()) { - set_fatal_error(); - return false; - } - - return true; -} - } diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index bc4df1775b..9c74acbbaf 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -608,57 +607,4 @@ private: using ReusableTCPSocket = BasicReusableSocket; using ReusableUDPSocket = BasicReusableSocket; -// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. -class WrappedAKInputStream final : public AK::Stream { -public: - WrappedAKInputStream(NonnullOwnPtr stream); - virtual ErrorOr read(Bytes) override; - virtual ErrorOr discard(size_t discarded_bytes) override; - virtual ErrorOr write(ReadonlyBytes) override; - virtual bool is_eof() const override; - virtual bool is_open() const override; - virtual void close() override; - -private: - NonnullOwnPtr m_stream; -}; - -// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. -class WrappedAKOutputStream final : public AK::Stream { -public: - WrappedAKOutputStream(NonnullOwnPtr stream); - virtual ErrorOr read(Bytes) override; - virtual ErrorOr write(ReadonlyBytes) override; - virtual bool is_eof() const override; - virtual bool is_open() const override; - virtual void close() override; - -private: - NonnullOwnPtr m_stream; -}; - -// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. -class WrapInAKInputStream final : public DeprecatedInputStream { -public: - WrapInAKInputStream(AK::Stream& stream); - virtual size_t read(Bytes) override; - virtual bool unreliable_eof() const override; - virtual bool read_or_error(Bytes) override; - virtual bool discard_or_error(size_t count) override; - -private: - AK::Stream& m_stream; -}; - -// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. -class WrapInAKOutputStream final : public DeprecatedOutputStream { -public: - WrapInAKOutputStream(AK::Stream& stream); - virtual size_t write(ReadonlyBytes) override; - virtual bool write_or_error(ReadonlyBytes) override; - -private: - AK::Stream& m_stream; -}; - } diff --git a/Userland/Libraries/LibGfx/GIFLoader.cpp b/Userland/Libraries/LibGfx/GIFLoader.cpp index dbf76239b1..26a5208380 100644 --- a/Userland/Libraries/LibGfx/GIFLoader.cpp +++ b/Userland/Libraries/LibGfx/GIFLoader.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include