file.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 TFile& file);
  22. TUnbufferedFileInput(const TString& path);
  23. private:
  24. size_t DoRead(void* buf, size_t len) override;
  25. size_t DoSkip(size_t len) override;
  26. private:
  27. TFile File_;
  28. };
  29. /**
  30. * Memory-mapped file input stream.
  31. */
  32. class TMappedFileInput: public TMemoryInput {
  33. public:
  34. TMappedFileInput(const TFile& file);
  35. TMappedFileInput(const TString& path);
  36. ~TMappedFileInput() override;
  37. private:
  38. class TImpl;
  39. THolder<TImpl> Impl_;
  40. };
  41. /**
  42. * File output stream.
  43. *
  44. * Note that the output is unbuffered, thus writing in many small chunks is
  45. * likely to be quite slow.
  46. */
  47. class TUnbufferedFileOutput: public IOutputStream {
  48. public:
  49. TUnbufferedFileOutput(const TString& path);
  50. TUnbufferedFileOutput(const TFile& file);
  51. ~TUnbufferedFileOutput() override;
  52. TUnbufferedFileOutput(TUnbufferedFileOutput&&) noexcept = default;
  53. TUnbufferedFileOutput& operator=(TUnbufferedFileOutput&&) noexcept = default;
  54. private:
  55. void DoWrite(const void* buf, size_t len) override;
  56. void DoFlush() override;
  57. private:
  58. TFile File_;
  59. };
  60. /**
  61. * Buffered file input stream.
  62. *
  63. * @see TBuffered
  64. */
  65. class TFileInput: public TBuffered<TUnbufferedFileInput> {
  66. public:
  67. template <class T>
  68. inline TFileInput(T&& t, size_t buf = 1 << 13)
  69. : TBuffered<TUnbufferedFileInput>(buf, std::forward<T>(t))
  70. {
  71. }
  72. ~TFileInput() override = default;
  73. };
  74. /**
  75. * Buffered file output stream.
  76. *
  77. * Currently deprecated, please use TFileOutput in new code.
  78. *
  79. * @deprecated
  80. * @see TBuffered
  81. */
  82. class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> {
  83. public:
  84. template <class T>
  85. inline TFixedBufferFileOutput(T&& t, size_t buf = 1 << 13)
  86. : TBuffered<TUnbufferedFileOutput>(buf, std::forward<T>(t))
  87. {
  88. }
  89. ~TFixedBufferFileOutput() override = default;
  90. };
  91. /** @} */