intmain( int argc, char* argv[] ) { try { if ( argc <= 0 ) { abort(); // For sticklers: don't try to access argv[0] if argc <= 0. }
auto args = span( argv, argc );
// The program takes two command-line arguments: the hostname and "path" part of the URL. // Print the usage message unless there are these two arguments (plus the program name // itself, so arg count = 3 in total). if ( argc != 3 ) { cerr << "Usage: " << args.front() << " HOST PATH\n"; cerr << "\tExample: " << args.front() << " stanford.edu /class/cs144\n"; return EXIT_FAILURE; }
// Get the command-line arguments. const string host { args[1] }; const string path { args[2] };
// unsigned int WriteOFF_0 // unsigned int Overflow_1 // unsigned int ReadOFF_2 this->errorsize++; // string WriteOFF="Unable to write because write has been turned off"; // string Overflow="Data exceeds the maximum limit"; // string ReadOFF="Unable to read because read has been turned off"; }
classByteStream { protected: unsignedint errorsize=0; uint64_t capacity_; uint64_t buffered=0; uint64_t pushed=0; uint64_t poped=0; uint64_t headroom_=capacity_; std::string buffer{}; size_t ByteStreamState=4; // Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces.
public: explicitByteStream( uint64_t capacity );
// Helper functions (provided) to access the ByteStream's Reader and Writer interfaces Reader& reader(); const Reader& reader()const; Writer& writer(); const Writer& writer()const; };
classWriter : public ByteStream { public: voidpush( std::string data ); // Push data to stream, but only as much as available capacity allows.
voidclose(); // Signal that the stream has reached its ending. Nothing more will be written. voidset_error(); // Signal that the stream suffered an error.
boolis_closed()const; // Has the stream been closed? uint64_tavailable_capacity()const; // How many bytes can be pushed to the stream right now? uint64_tbytes_pushed()const; // Total number of bytes cumulatively pushed to the stream };
classReader : public ByteStream { public: std::string_view peek()const; // Peek at the next bytes in the buffer voidpop( uint64_t len ); // Remove `len` bytes from the buffer
boolis_finished()const; // Is the stream finished (closed and fully popped)? boolhas_error()const; // Has the stream had an error?
uint64_tbytes_buffered()const; // Number of bytes currently buffered (pushed and not popped) uint64_tbytes_popped()const; // Total number of bytes cumulatively popped from stream };
/* * read: A (provided) helper function thats peeks and pops up to `len` bytes * from a ByteStream Reader into a string; */ voidread( Reader& reader, uint64_t len, std::string& out );