123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #pragma once
- #include <util/generic/fwd.h>
- #include <util/generic/noncopyable.h>
- #include <util/system/defaults.h>
- class IOutputStream;
- /**
- * @addtogroup Streams_Base
- * @{
- */
- /**
- * Abstract input stream.
- */
- class IInputStream: public TNonCopyable {
- public:
- IInputStream() noexcept;
- virtual ~IInputStream();
- IInputStream(IInputStream&&) noexcept {
- }
- IInputStream& operator=(IInputStream&&) noexcept {
- return *this;
- }
- /**
- * Reads some data from the stream. Note that this function might read less
- * data than what was requested. Use `Load` function if you want to read as
- * much data as possible.
- *
- * @param buf Buffer to read into.
- * @param len Number of bytes to read.
- * @returns Number of bytes that were actually read.
- * A return value of zero signals end of stream.
- */
- inline size_t Read(void* buf, size_t len) {
- if (len == 0) {
- return 0;
- }
- return DoRead(buf, len);
- }
- /**
- * Reads one character from the stream.
- *
- * @param[out] c Character to read.
- * @returns Whether the character was read.
- * A return value of false signals the end
- * of stream.
- */
- inline bool ReadChar(char& c) {
- return DoRead(&c, 1) > 0;
- }
- /**
- * Reads all characters from the stream until the given character is
- * encountered, and stores them into the given string. The character itself
- * is read from the stream, but not stored in the string.
- *
- * @param[out] st String to read into.
- * @param ch Character to stop at.
- * @returns Total number of characters read from the stream.
- * A return value of zero signals end of stream.
- */
- inline size_t ReadTo(TString& st, char ch) {
- return DoReadTo(st, ch);
- }
- /**
- * Reads the requested amount of data from the stream. Unlike `Read`, this
- * function stops only when the requested amount of data is read, or when
- * end of stream is reached.
- *
- * @param buf Buffer to read into.
- * @param len Number of bytes to read.
- * @returns Number of bytes that were actually read.
- * A return value different from `len`
- * signals end of stream.
- */
- size_t Load(void* buf, size_t len);
- /**
- * Reads the requested amount of data from the stream, or fails with an
- * exception if unable to do so.
- *
- * @param buf Buffer to read into.
- * @param len Number of bytes to read.
- * @see Load
- */
- void LoadOrFail(void* buf, size_t len);
- /**
- * Reads all data from this stream and returns it as a string.
- *
- * @returns Contents of this stream as a string.
- */
- TString ReadAll();
- /**
- * Reads all data from this stream and writes it into a provided output
- * stream.
- *
- * @param out Output stream to use.
- * @returns Total number of characters read from the stream.
- */
- ui64 ReadAll(IOutputStream& out);
- /**
- * Reads all data from the stream until the first occurrence of '\n'. Also
- * handles Windows line breaks correctly.
- *
- * @returns Next line read from this stream,
- * excluding the line terminator.
- * @throws yexception If no data could be read from a stream
- * because end of stream has already been
- * reached.
- */
- TString ReadLine();
- /**
- * Reads all characters from the stream until the given character is
- * encountered and returns them as a string. The character itself is read
- * from the stream, but not stored in the string.
- *
- * @param ch Character to stop at.
- * @returns String containing all the characters read.
- * @throws yexception If no data could be read from a stream
- * because end of stream has already been
- * reached.
- */
- TString ReadTo(char ch);
- /**
- * Reads all data from the stream until the first occurrence of '\n' and
- * stores it into provided string. Also handles Windows line breaks correctly.
- *
- * @param[out] st String to store read characters into,
- * excluding the line terminator.
- * @returns Total number of characters read from the stream.
- * A return value of zero signals end of stream.
- */
- size_t ReadLine(TString& st);
- /**
- * Reads UTF8 encoded characters from the stream the first occurrence of '\n',
- * converts them into wide ones, and stores into provided string. Also handles
- * Windows line breaks correctly.
- *
- * @param[out] w Wide string to store read characters into,
- * excluding the line terminator.
- * @returns Total number of characters read from the stream.
- * A return value of zero signals end of stream.
- */
- size_t ReadLine(TUtf16String& w);
- /**
- * Skips some data from the stream without reading / copying it. Note that
- * this function might skip less data than what was requested.
- *
- * @param len Number of bytes to skip.
- * @returns Number of bytes that were actually skipped.
- * A return value of zero signals end of stream.
- */
- size_t Skip(size_t len);
- protected:
- /**
- * Reads some data from the stream. Might read less data than what was
- * requested.
- *
- * @param buf Buffer to read into.
- * @param len Number of bytes to read.
- * @returns Number of bytes that were actually read.
- * A return value of zero signals end of stream.
- * @throws yexception If IO error occurs.
- */
- virtual size_t DoRead(void* buf, size_t len) = 0;
- /**
- * Skips some data from the stream. Might skip less data than what was
- * requested.
- *
- * @param len Number of bytes to skip.
- * @returns Number of bytes that were actually skipped.
- * A return value of zero signals end of stream.
- * @throws yexception If IO error occurs.
- */
- virtual size_t DoSkip(size_t len);
- /**
- * Reads all characters from the stream until the given character is
- * encountered, and stores them into the given string. The character itself
- * is read from the stream, but not stored in the string.
- *
- * Provided string is cleared only if there is data in the stream.
- *
- * @param[out] st String to read into.
- * @param ch Character to stop at.
- * @returns Total number of characters read from the stream.
- * A return value of zero signals end of stream.
- * @throws yexception If IO error occurs.
- */
- virtual size_t DoReadTo(TString& st, char ch);
- /**
- * Reads all data from this stream and writes it into a provided output
- * stream.
- *
- * @param out Output stream to use.
- * @returns Total number of characters read from
- * this stream.
- * @throws yexception If IO error occurs.
- */
- virtual ui64 DoReadAll(IOutputStream& out);
- };
- /**
- * Transfers all data from the given input stream into the given output stream.
- *
- * @param in Input stream.
- * @param out Output stream.
- */
- ui64 TransferData(IInputStream* in, IOutputStream* out);
- /**
- * `operator>>` for `IInputStream` by default delegates to this function.
- *
- * Note that while `operator>>` uses overloading (and thus argument-dependent
- * lookup), `In` uses template specializations. This makes it possible to
- * have a single `In` declaration, and then just provide specializations in
- * cpp files, letting the linker figure everything else out. This approach
- * reduces compilation times.
- *
- * However, if the flexibility of overload resolution is needed, then one should
- * just overload `operator>>`.
- *
- * @param in Input stream to read from.
- * @param[out] value Value to read.
- * @throws `yexception` on invalid input or end of stream.
- * @see Out(IOutputStream&, T&)
- */
- template <typename T>
- void In(IInputStream& in, T& value);
- /**
- * Reads a value from the stream.
- *
- * @param in Input stream to read from.
- * @param[out] value Value to read.
- * @returns Input stream.
- * @throws `yexception` on invalid input or end of stream.
- * @see operator<<(IOutputStream&, T&)
- */
- template <typename T>
- inline IInputStream& operator>>(IInputStream& in, T& value) {
- In<T>(in, value);
- return in;
- }
- namespace NPrivate {
- IInputStream& StdInStream() noexcept;
- } // namespace NPrivate
- /**
- * Standard input stream.
- */
- #define Cin (::NPrivate::StdInStream())
- /** @} */
|