stdio_file.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include "buffered.h"
  3. #include <library/cpp/yson_pull/detail/macros.h>
  4. #include <library/cpp/yson_pull/exceptions.h>
  5. #include <library/cpp/yson_pull/input.h>
  6. #include <cstdio>
  7. #include <memory>
  8. namespace NYsonPull {
  9. namespace NDetail {
  10. namespace NInput {
  11. class TStdioFile: public TBuffered {
  12. FILE* file_;
  13. public:
  14. TStdioFile(FILE* file, size_t buffer_size)
  15. : TBuffered(buffer_size)
  16. , file_{file} {
  17. }
  18. protected:
  19. result do_fill_buffer() override {
  20. auto nread = ::fread(buffer_data(), 1, buffer_size(), file_);
  21. if (Y_UNLIKELY(nread == 0)) {
  22. if (ferror(file_)) {
  23. throw NException::TSystemError();
  24. }
  25. if (feof(file_)) {
  26. return result::at_end;
  27. }
  28. }
  29. buffer().reset(buffer_data(), buffer_data() + nread);
  30. return result::have_more_data;
  31. }
  32. };
  33. }
  34. } // namespace NDetail
  35. }