123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #pragma once
- #include "fwd.h"
- #include "input.h"
- #include "output.h"
- #include "buffered.h"
- #include "mem.h"
- #include <util/system/file.h>
- #include <utility>
- /**
- * @addtogroup Streams_Files
- * @{
- */
- /**
- * Unbuffered file input stream.
- *
- * Note that the input is not buffered, which means that `ReadLine` calls will
- * be _very_ slow.
- */
- class TUnbufferedFileInput: public IInputStream {
- public:
- TUnbufferedFileInput(const char* path);
- TUnbufferedFileInput(const TString& path);
- TUnbufferedFileInput(const std::filesystem::path& path);
- TUnbufferedFileInput(const TFile& file);
- private:
- static constexpr EOpenMode OPEN_MODE = OpenExisting | RdOnly | Seq;
- size_t DoRead(void* buf, size_t len) override;
- size_t DoSkip(size_t len) override;
- private:
- TFile File_;
- };
- /**
- * Memory-mapped file input stream.
- */
- class TMappedFileInput: public TMemoryInput {
- public:
- TMappedFileInput(const TFile& file);
- TMappedFileInput(const TString& path);
- ~TMappedFileInput() override;
- private:
- class TImpl;
- THolder<TImpl> Impl_;
- };
- /**
- * File output stream.
- *
- * Note that the output is unbuffered, thus writing in many small chunks is
- * likely to be quite slow.
- */
- class TUnbufferedFileOutput: public IOutputStream {
- public:
- TUnbufferedFileOutput(const char* path);
- TUnbufferedFileOutput(const TString& path);
- TUnbufferedFileOutput(const std::filesystem::path& path);
- TUnbufferedFileOutput(const TFile& file);
- ~TUnbufferedFileOutput() override;
- TUnbufferedFileOutput(TUnbufferedFileOutput&&) noexcept = default;
- TUnbufferedFileOutput& operator=(TUnbufferedFileOutput&&) noexcept = default;
- private:
- static constexpr EOpenMode OPEN_MODE = CreateAlways | WrOnly | Seq;
- void DoWrite(const void* buf, size_t len) override;
- void DoFlush() override;
- private:
- TFile File_;
- };
- /**
- * Buffered file input stream.
- *
- * @see TBuffered
- */
- class TFileInput: public TBuffered<TUnbufferedFileInput> {
- public:
- template <class T>
- inline TFileInput(T&& t, size_t buf = 1 << 13)
- : TBuffered<TUnbufferedFileInput>(buf, std::forward<T>(t))
- {
- }
- ~TFileInput() override = default;
- };
- /**
- * Buffered file output stream.
- *
- * Currently deprecated, please use TFileOutput in new code.
- *
- * @deprecated
- * @see TBuffered
- */
- class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> {
- public:
- template <class T>
- inline TFixedBufferFileOutput(T&& t, size_t buf = 1 << 13)
- : TBuffered<TUnbufferedFileOutput>(buf, std::forward<T>(t))
- {
- }
- ~TFixedBufferFileOutput() override = default;
- };
- /** @} */
|