aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/input/stdio_file.h
blob: c412b7e59b412531d3eff28aee2611c8c6eb0d0f (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
#pragma once

#include "buffered.h"

#include <library/cpp/yson_pull/detail/macros.h>

#include <library/cpp/yson_pull/exceptions.h>
#include <library/cpp/yson_pull/input.h>

#include <cstdio>
#include <memory>

namespace NYsonPull {
    namespace NDetail {
        namespace NInput {
            class TStdioFile: public TBuffered {
                FILE* file_;

            public:
                TStdioFile(FILE* file, size_t buffer_size)
                    : TBuffered(buffer_size)
                    , file_{file} {
                }

            protected:
                result do_fill_buffer() override {
                    auto nread = ::fread(buffer_data(), 1, buffer_size(), file_);
                    if (Y_UNLIKELY(nread == 0)) {
                        if (ferror(file_)) {
                            throw NException::TSystemError();
                        }
                        if (feof(file_)) {
                            return result::at_end;
                        }
                    }
                    buffer().reset(buffer_data(), buffer_data() + nread);
                    return result::have_more_data;
                }
            };
        }
    }     // namespace NDetail
}