diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 79a19c17..1e200298 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -10,6 +10,7 @@ #include // string, char_traits #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#include //FILE * #include @@ -45,6 +46,27 @@ struct input_adapter_protocol /// a type to simplify interfaces using input_adapter_t = std::shared_ptr; +class file_input_adapter : public input_adapter_protocol +{ +public: + explicit file_input_adapter(const FILE *file) noexcept + : file(file) + {} + + std::char_traits::int_type get_character() noexcept override + { + auto res = fgetc(const_cast(file)); + if(res == EOF) + return std::char_traits::eof(); + else + return static_cast::int_type>(res); + } +private: + /// the file pointer to read from + const FILE * file; +}; + + /*! Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at beginning of input. Does not support changing the underlying std::streambuf @@ -287,7 +309,8 @@ class input_adapter { public: // native support - + input_adapter(FILE * file) + : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) : ia(std::make_shared(i)) {}