input.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #pragma once
  2. #include <util/generic/fwd.h>
  3. #include <util/generic/noncopyable.h>
  4. #include <util/system/defaults.h>
  5. class IOutputStream;
  6. /**
  7. * @addtogroup Streams_Base
  8. * @{
  9. */
  10. /**
  11. * Abstract input stream.
  12. */
  13. class IInputStream: public TNonCopyable {
  14. public:
  15. IInputStream() noexcept;
  16. virtual ~IInputStream();
  17. IInputStream(IInputStream&&) noexcept {
  18. }
  19. IInputStream& operator=(IInputStream&&) noexcept {
  20. return *this;
  21. }
  22. /**
  23. * Reads some data from the stream. Note that this function might read less
  24. * data than what was requested. Use `Load` function if you want to read as
  25. * much data as possible.
  26. *
  27. * @param buf Buffer to read into.
  28. * @param len Number of bytes to read.
  29. * @returns Number of bytes that were actually read.
  30. * A return value of zero signals end of stream.
  31. */
  32. inline size_t Read(void* buf, size_t len) {
  33. if (len == 0) {
  34. return 0;
  35. }
  36. return DoRead(buf, len);
  37. }
  38. /**
  39. * Reads one character from the stream.
  40. *
  41. * @param[out] c Character to read.
  42. * @returns Whether the character was read.
  43. * A return value of false signals the end
  44. * of stream.
  45. */
  46. inline bool ReadChar(char& c) {
  47. return DoRead(&c, 1) > 0;
  48. }
  49. /**
  50. * Reads all characters from the stream until the given character is
  51. * encountered, and stores them into the given string. The character itself
  52. * is read from the stream, but not stored in the string.
  53. *
  54. * @param[out] st String to read into.
  55. * @param ch Character to stop at.
  56. * @returns Total number of characters read from the stream.
  57. * A return value of zero signals end of stream.
  58. */
  59. inline size_t ReadTo(TString& st, char ch) {
  60. return DoReadTo(st, ch);
  61. }
  62. /**
  63. * Reads the requested amount of data from the stream. Unlike `Read`, this
  64. * function stops only when the requested amount of data is read, or when
  65. * end of stream is reached.
  66. *
  67. * @param buf Buffer to read into.
  68. * @param len Number of bytes to read.
  69. * @returns Number of bytes that were actually read.
  70. * A return value different from `len`
  71. * signals end of stream.
  72. */
  73. size_t Load(void* buf, size_t len);
  74. /**
  75. * Reads the requested amount of data from the stream, or fails with an
  76. * exception if unable to do so.
  77. *
  78. * @param buf Buffer to read into.
  79. * @param len Number of bytes to read.
  80. * @see Load
  81. */
  82. void LoadOrFail(void* buf, size_t len);
  83. /**
  84. * Reads all data from this stream and returns it as a string.
  85. *
  86. * @returns Contents of this stream as a string.
  87. */
  88. TString ReadAll();
  89. /**
  90. * Reads all data from this stream and writes it into a provided output
  91. * stream.
  92. *
  93. * @param out Output stream to use.
  94. * @returns Total number of characters read from the stream.
  95. */
  96. ui64 ReadAll(IOutputStream& out);
  97. /**
  98. * Reads all data from the stream until the first occurrence of '\n'. Also
  99. * handles Windows line breaks correctly.
  100. *
  101. * @returns Next line read from this stream,
  102. * excluding the line terminator.
  103. * @throws yexception If no data could be read from a stream
  104. * because end of stream has already been
  105. * reached.
  106. */
  107. TString ReadLine();
  108. /**
  109. * Reads all characters from the stream until the given character is
  110. * encountered and returns them as a string. The character itself is read
  111. * from the stream, but not stored in the string.
  112. *
  113. * @param ch Character to stop at.
  114. * @returns String containing all the characters read.
  115. * @throws yexception If no data could be read from a stream
  116. * because end of stream has already been
  117. * reached.
  118. */
  119. TString ReadTo(char ch);
  120. /**
  121. * Reads all data from the stream until the first occurrence of '\n' and
  122. * stores it into provided string. Also handles Windows line breaks correctly.
  123. *
  124. * @param[out] st String to store read characters into,
  125. * excluding the line terminator.
  126. * @returns Total number of characters read from the stream.
  127. * A return value of zero signals end of stream.
  128. */
  129. size_t ReadLine(TString& st);
  130. /**
  131. * Reads UTF8 encoded characters from the stream the first occurrence of '\n',
  132. * converts them into wide ones, and stores into provided string. Also handles
  133. * Windows line breaks correctly.
  134. *
  135. * @param[out] w Wide string to store read characters into,
  136. * excluding the line terminator.
  137. * @returns Total number of characters read from the stream.
  138. * A return value of zero signals end of stream.
  139. */
  140. size_t ReadLine(TUtf16String& w);
  141. /**
  142. * Skips some data from the stream without reading / copying it. Note that
  143. * this function might skip less data than what was requested.
  144. *
  145. * @param len Number of bytes to skip.
  146. * @returns Number of bytes that were actually skipped.
  147. * A return value of zero signals end of stream.
  148. */
  149. size_t Skip(size_t len);
  150. protected:
  151. /**
  152. * Reads some data from the stream. Might read less data than what was
  153. * requested.
  154. *
  155. * @param buf Buffer to read into.
  156. * @param len Number of bytes to read.
  157. * @returns Number of bytes that were actually read.
  158. * A return value of zero signals end of stream.
  159. * @throws yexception If IO error occurs.
  160. */
  161. virtual size_t DoRead(void* buf, size_t len) = 0;
  162. /**
  163. * Skips some data from the stream. Might skip less data than what was
  164. * requested.
  165. *
  166. * @param len Number of bytes to skip.
  167. * @returns Number of bytes that were actually skipped.
  168. * A return value of zero signals end of stream.
  169. * @throws yexception If IO error occurs.
  170. */
  171. virtual size_t DoSkip(size_t len);
  172. /**
  173. * Reads all characters from the stream until the given character is
  174. * encountered, and stores them into the given string. The character itself
  175. * is read from the stream, but not stored in the string.
  176. *
  177. * Provided string is cleared only if there is data in the stream.
  178. *
  179. * @param[out] st String to read into.
  180. * @param ch Character to stop at.
  181. * @returns Total number of characters read from the stream.
  182. * A return value of zero signals end of stream.
  183. * @throws yexception If IO error occurs.
  184. */
  185. virtual size_t DoReadTo(TString& st, char ch);
  186. /**
  187. * Reads all data from this stream and writes it into a provided output
  188. * stream.
  189. *
  190. * @param out Output stream to use.
  191. * @returns Total number of characters read from
  192. * this stream.
  193. * @throws yexception If IO error occurs.
  194. */
  195. virtual ui64 DoReadAll(IOutputStream& out);
  196. };
  197. /**
  198. * Transfers all data from the given input stream into the given output stream.
  199. *
  200. * @param in Input stream.
  201. * @param out Output stream.
  202. */
  203. ui64 TransferData(IInputStream* in, IOutputStream* out);
  204. /**
  205. * `operator>>` for `IInputStream` by default delegates to this function.
  206. *
  207. * Note that while `operator>>` uses overloading (and thus argument-dependent
  208. * lookup), `In` uses template specializations. This makes it possible to
  209. * have a single `In` declaration, and then just provide specializations in
  210. * cpp files, letting the linker figure everything else out. This approach
  211. * reduces compilation times.
  212. *
  213. * However, if the flexibility of overload resolution is needed, then one should
  214. * just overload `operator>>`.
  215. *
  216. * @param in Input stream to read from.
  217. * @param[out] value Value to read.
  218. * @throws `yexception` on invalid input or end of stream.
  219. * @see Out(IOutputStream&, T&)
  220. */
  221. template <typename T>
  222. void In(IInputStream& in, T& value);
  223. /**
  224. * Reads a value from the stream.
  225. *
  226. * @param in Input stream to read from.
  227. * @param[out] value Value to read.
  228. * @returns Input stream.
  229. * @throws `yexception` on invalid input or end of stream.
  230. * @see operator<<(IOutputStream&, T&)
  231. */
  232. template <typename T>
  233. inline IInputStream& operator>>(IInputStream& in, T& value) {
  234. In<T>(in, value);
  235. return in;
  236. }
  237. namespace NPrivate {
  238. IInputStream& StdInStream() noexcept;
  239. } // namespace NPrivate
  240. /**
  241. * Standard input stream.
  242. */
  243. #define Cin (::NPrivate::StdInStream())
  244. /** @} */