aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/file.h
blob: 692a593d9732a472cbe3062652955f79c0dd9815 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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;
};

/** @} */