file.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include "fwd.h"
  3. #include "input.h"
  4. #include "output.h"
  5. #include "buffered.h"
  6. #include "mem.h"
  7. #include <util/system/file.h>
  8. #include <utility>
  9. /**
  10. * @addtogroup Streams_Files
  11. * @{
  12. */
  13. /**
  14. * Unbuffered file input stream.
  15. *
  16. * Note that the input is not buffered, which means that `ReadLine` calls will
  17. * be _very_ slow.
  18. */
  19. class TUnbufferedFileInput: public IInputStream {
  20. public:
  21. TUnbufferedFileInput(const char* path);
  22. TUnbufferedFileInput(const TString& path);
  23. TUnbufferedFileInput(const std::filesystem::path& path);
  24. TUnbufferedFileInput(const TFile& file);
  25. private:
  26. static constexpr EOpenMode OPEN_MODE = OpenExisting | RdOnly | Seq;
  27. size_t DoRead(void* buf, size_t len) override;
  28. size_t DoSkip(size_t len) override;
  29. private:
  30. TFile File_;
  31. };
  32. /**
  33. * Memory-mapped file input stream.
  34. */
  35. class TMappedFileInput: public TMemoryInput {
  36. public:
  37. TMappedFileInput(const TFile& file);
  38. TMappedFileInput(const TString& path);
  39. ~TMappedFileInput() override;
  40. private:
  41. class TImpl;
  42. THolder<TImpl> Impl_;
  43. };
  44. /**
  45. * File output stream.
  46. *
  47. * Note that the output is unbuffered, thus writing in many small chunks is
  48. * likely to be quite slow.
  49. */
  50. class TUnbufferedFileOutput: public IOutputStream {
  51. public:
  52. TUnbufferedFileOutput(const char* path);
  53. TUnbufferedFileOutput(const TString& path);
  54. TUnbufferedFileOutput(const std::filesystem::path& path);
  55. TUnbufferedFileOutput(const TFile& file);
  56. ~TUnbufferedFileOutput() override;
  57. TUnbufferedFileOutput(TUnbufferedFileOutput&&) noexcept = default;
  58. TUnbufferedFileOutput& operator=(TUnbufferedFileOutput&&) noexcept = default;
  59. private:
  60. static constexpr EOpenMode OPEN_MODE = CreateAlways | WrOnly | Seq;
  61. void DoWrite(const void* buf, size_t len) override;
  62. void DoFlush() override;
  63. private:
  64. TFile File_;
  65. };
  66. /**
  67. * Buffered file input stream.
  68. *
  69. * @see TBuffered
  70. */
  71. class TFileInput: public TBuffered<TUnbufferedFileInput> {
  72. public:
  73. template <class T>
  74. inline TFileInput(T&& t, size_t buf = 1 << 13)
  75. : TBuffered<TUnbufferedFileInput>(buf, std::forward<T>(t))
  76. {
  77. }
  78. ~TFileInput() override = default;
  79. };
  80. /**
  81. * Buffered file output stream.
  82. *
  83. * Currently deprecated, please use TFileOutput in new code.
  84. *
  85. * @deprecated
  86. * @see TBuffered
  87. */
  88. class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> {
  89. public:
  90. template <class T>
  91. inline TFixedBufferFileOutput(T&& t, size_t buf = 1 << 13)
  92. : TBuffered<TUnbufferedFileOutput>(buf, std::forward<T>(t))
  93. {
  94. }
  95. ~TFixedBufferFileOutput() override = default;
  96. };
  97. /** @} */