1 // 2 // reversed_memory_stream.hpp 3 // 4 // Copyright © 2024 Apple Inc. All rights reserved. 5 // 6 // Please refer to the license found in the LICENSE file in the root directory of the source tree. 7 8 #pragma once 9 10 #include <istream> 11 #include <ostream> 12 13 #include "memory_buffer.hpp" 14 15 namespace inmemoryfs { 16 17 /// A class for reading an in-memory stream buffer in reverse. 18 class ReversedIMemoryStreamBuf: public std::streambuf { 19 public: 20 ~ReversedIMemoryStreamBuf() = default; 21 22 /// Constructs a `ReversedIMemoryStreamBuf` from a `MemoryBuffer`. 23 /// 24 /// @param buffer The memory buffer. 25 explicit ReversedIMemoryStreamBuf(std::shared_ptr<MemoryBuffer> buffer) noexcept; 26 27 protected: 28 /// Called by seekof if the openmode is input. 29 pos_type iseekoff(off_type off, std::ios_base::seekdir dir); 30 31 /// Called by other member functions to alter the stream position of the controlled input sequence. 32 pos_type seekpos(pos_type pos, std::ios_base::openmode which) override; 33 34 /// Called by other member functions to get an estimate on the number of characters available in controlled input sequence. 35 /// 36 /// Returns number of characters available in controlled input sequence. 37 std::streamsize showmanyc() override; 38 39 /// Called by other member functions to put a character back into the controlled input sequence and decrease the position indicator. 40 /// 41 /// Returns the value of the character put back, converted to a value of type int. 42 int_type pbackfail(int_type ch) override; 43 44 /// Called by other member functions to get the current character in the controlled input sequence without changing the current position. 45 /// 46 /// Returns the value of the current character, converted to a value of type int. 47 std::streambuf::int_type underflow() override; 48 49 /// Called by other member functions to get the current character in the controlled input sequence and advances the current position. 50 /// 51 /// Returns the value of the current character, converted to a value of type int. 52 std::streambuf::int_type uflow() override; 53 54 /// Retrieves characters from the controlled input sequence and stores them in the array pointed by s, 55 /// until either n characters have been extracted or the end of the sequence is reached. 56 /// 57 /// Returns the number of characters copied. 58 std::streamsize xsgetn(char *s, std::streamsize n) override; 59 60 private: 61 /// Reads the character at the specified position. 62 std::streambuf::int_type read(char *pos); 63 64 const std::shared_ptr<MemoryBuffer> buffer_; 65 char *start_; 66 char *current_; 67 char *end_; 68 }; 69 70 /// A class for reading an in-memory buffer in reverse. 71 class ReversedIMemoryStream final : public std::istream { 72 public: 73 74 /// Constructs a `ReversedIMemoryStream` from a `MemoryBuffer`. 75 /// 76 /// @param buffer The memory buffer. 77 ReversedIMemoryStream(const std::shared_ptr<MemoryBuffer>& buffer) noexcept; 78 79 ~ReversedIMemoryStream() = default; 80 81 private: 82 ReversedIMemoryStreamBuf streambuf; 83 }; 84 85 } 86