From 67b0daf27ba7659add80662945793d26e7b4a2b1 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Tue, 11 Dec 2018 09:33:30 -0500 Subject: [PATCH] Add the possibility of using FILE * from cstdio library to read a file. This enable the possibility of using low eand device with this library. --- single_include/nlohmann/json.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7038084d..36939b31 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -55,6 +55,7 @@ SOFTWARE. #include // allocator #include // string #include // vector +#include /*! @brief namespace for Niels Lohmann @@ -2285,6 +2286,25 @@ struct wide_string_input_helper } }; +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; +}; template class wide_string_input_adapter : public input_adapter_protocol { @@ -2354,6 +2374,9 @@ class input_adapter input_adapter(const std::u32string& ws) : ia(std::make_shared>(ws)) {} + input_adapter(const FILE *file) + : ia(std::make_shared(file)) {} + /// input adapter for buffer template