CS144 Lab0实验四
An in-memory reliable byte stream
byte stream.hh
#ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
#define SPONGE_LIBSPONGE_BYTE_STREAM_HH
#include <string>
//! \brief An in-order byte stream.
//! Bytes are written on the "input" side and read from the "output"
//! side. The byte stream is finite: the writer can end the input,
//! and then no more bytes can be written.
class ByteStream {
private:
// Your code here -- add private members as necessary.
std::string buffer="";
size_t cap=0;
size_t total_read=0;
size_t total_write=0;
bool is_eof=false;
// Hint: This doesn't need to be a sophisticated data structure at
// all, but if any of your tests are taking longer than a second,
// that's a sign that you probably want to keep exploring
// different approaches.
bool _error{}; //!< Flag indicating that the stream suffered an error.
public:
//! Construct a stream with room for `capacity` bytes.
ByteStream(const size_t capacity);
//! \name "Input" interface for the writer
//!@{
//! Write a string of bytes into the stream. Write as many
//! as will fit, and return how many were written.
//! \returns the number of bytes accepted into the stream
size_t write(const std::string &data);
//! \returns the number of additional bytes that the stream has space for
size_t remaining_capacity() const;
//! Signal that the byte stream has reached its ending
void end_input();
//! Indicate that the stream suffered an error.
void set_error() { _error = true; }
//!@}
//! \name "Output" interface for the reader
//!@{
//! Peek at next "len" bytes of the stream
//! \returns a string
std::string peek_output(const size_t len) const;
//! Remove bytes from the buffer
void pop_output(const size_t len);
//! Read (i.e., copy and then pop) the next "len" bytes of the stream
//! \returns a string
std::string read(const size_t len);
//! \returns `true` if the stream input has ended
bool input_ended() const;
//! \returns `true` if the stream has suffered an error
bool error() const { return _error; }
//! \returns the maximum amount that can currently be read from the stream
size_t buffer_size() const;
//! \returns `true` if the buffer is empty
bool buffer_empty() const;
//! \returns `true` if the output has reached the ending
bool eof() const;
//!@}
//! \name General accounting
//!@{
//! Total number of bytes written
size_t bytes_written() const;
//! Total number of bytes popped
size_t bytes_read() const;
//!@}
};
#endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH
byte stream.cc
#include "byte_stream.hh"
// Dummy implementation of a flow-controlled in-memory byte stream.
// For Lab 0, please replace with a real implementation that passes the
// automated checks run by `make check_lab0`.
// You will need to add private members to the class declaration in `byte_stream.hh`
using namespace std;
ByteStream::ByteStream(const size_t capacity){
cap=capacity;
}
size_t ByteStream::write(const string &data) {
if(data.size() == 0)return 0;
size_t remain =remaining_capacity();
size_t write_size =(remain > data.size())?data.size():remain;
buffer+=data.substr(0, write_size);
total_write+=write_size;
return write_size;
}
//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {
return buffer.substr(0, len);
}
//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) {
total_read += len;
buffer.erase(0, len);
}
//! Read (i.e., copy and then pop) the next "len" bytes of the stream
//! \param[in] len bytes will be popped and returned
//! \returns a string
std::string ByteStream::read(const size_t len) {
size_t size =(len>buffer_size())?buffer_size():len;
string str = peek_output(size);
pop_output(size);
return str;
return buffer.substr(0, len);
}
void ByteStream::end_input() { is_eof = true;}
bool ByteStream::input_ended() const { return is_eof; }
size_t ByteStream::buffer_size() const { return buffer.size(); }
bool ByteStream::buffer_empty() const { return buffer.size() == 0; }
bool ByteStream::eof() const { return is_eof && buffer_empty(); }
size_t ByteStream::bytes_written() const { return total_write; }
size_t ByteStream::bytes_read() const { return total_read; }
size_t ByteStream::remaining_capacity() const { return cap - buffer.size(); }
!!!测试前要make