diff --git a/include/nlohmann/detail/view/input.hpp b/include/nlohmann/detail/view/input.hpp index d028e81e2..b8db12ad6 100644 --- a/include/nlohmann/detail/view/input.hpp +++ b/include/nlohmann/detail/view/input.hpp @@ -11,7 +11,15 @@ #include // max #include // size_t #ifndef JSON_NO_IO - #include // FILE, fread + #include // FILE, fread, ftell + #if defined(_WIN32) + #include // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers) + #include // (for sys/stat.h) + #include // _fstat64, _S_IFMT, _S_IFREG + #elif defined(__unix__) || defined(__APPLE__) + #include // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers) + #include // fstat, S_ISREG + #endif #endif #include // istream, streambuf #include // basic_string, char_traits, string @@ -142,6 +150,16 @@ inline void append_stream(std::string& buf, std::istream& is) { return; } + // room for the rest when the stream buffer knows how much is left (for a + // file with libstdc++, the rest of the file), and for the read that finds + // the end: the input is then read in one block, without a reallocation and + // copy at each doubling of the string (only a hint: the loop below reads + // what is there) + const std::streamsize rest = sb->in_avail(); + if (rest > 0 && static_cast(rest) < buf.max_size() - buf.size() - 4096) + { + buf.reserve(buf.size() + static_cast(rest) + 4096); + } for (;;) { const std::size_t used = buf.size(); @@ -168,11 +186,51 @@ struct is_c_stream {}; #ifndef JSON_NO_IO +/// the number of bytes left in a C stream that is a regular file (from its +/// size and position), 0 for anything else and on other systems: a hint for +/// the room to reserve; f is not null +inline unsigned long long file_rest(std::FILE* f) noexcept +{ +#if defined(_WIN32) + struct _stat64 st {}; + if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG) + { + const long long here = ::_ftelli64(f); + if (here >= 0 && st.st_size > here) + { + return static_cast(st.st_size - here); + } + } +#elif defined(__unix__) || defined(__APPLE__) + struct stat st {}; + if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode)) + { + const long here = std::ftell(f); + if (here >= 0 && st.st_size > here) + { + return static_cast(st.st_size - here); + } + } +#else + static_cast(f); +#endif + return 0; +} + /// append the rest of a C stream: in blocks with fread, not character by -/// character (the library's file adapter reads with fgetc); -/// f is not null +/// character (the library's file adapter reads with fgetc), and in one block +/// when it is a regular file of known size; f is not null inline void append_file(std::string& buf, std::FILE* f) { + // room for the rest of a regular file and the byte that shows its end: + // the file is then read in one block, without a reallocation and copy at + // each doubling of the string (only a hint: the loop below reads what is + // there) + const unsigned long long rest = file_rest(f); + if (rest > 0 && rest < buf.max_size() - buf.size() - 1) + { + buf.reserve(buf.size() + static_cast(rest) + 1); + } for (;;) { const std::size_t used = buf.size(); diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index d56c63272..7f804856f 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -7957,7 +7957,15 @@ NLOHMANN_JSON_NAMESPACE_END #include // max #include // size_t #ifndef JSON_NO_IO - #include // FILE, fread + #include // FILE, fread, ftell + #if defined(_WIN32) + #include // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers) + #include // (for sys/stat.h) + #include // _fstat64, _S_IFMT, _S_IFREG + #elif defined(__unix__) || defined(__APPLE__) + #include // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers) + #include // fstat, S_ISREG + #endif #endif #include // istream, streambuf #include // basic_string, char_traits, string @@ -8089,6 +8097,16 @@ inline void append_stream(std::string& buf, std::istream& is) { return; } + // room for the rest when the stream buffer knows how much is left (for a + // file with libstdc++, the rest of the file), and for the read that finds + // the end: the input is then read in one block, without a reallocation and + // copy at each doubling of the string (only a hint: the loop below reads + // what is there) + const std::streamsize rest = sb->in_avail(); + if (rest > 0 && static_cast(rest) < buf.max_size() - buf.size() - 4096) + { + buf.reserve(buf.size() + static_cast(rest) + 4096); + } for (;;) { const std::size_t used = buf.size(); @@ -8115,11 +8133,51 @@ struct is_c_stream {}; #ifndef JSON_NO_IO +/// the number of bytes left in a C stream that is a regular file (from its +/// size and position), 0 for anything else and on other systems: a hint for +/// the room to reserve; f is not null +inline unsigned long long file_rest(std::FILE* f) noexcept +{ +#if defined(_WIN32) + struct _stat64 st {}; + if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG) + { + const long long here = ::_ftelli64(f); + if (here >= 0 && st.st_size > here) + { + return static_cast(st.st_size - here); + } + } +#elif defined(__unix__) || defined(__APPLE__) + struct stat st {}; + if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode)) + { + const long here = std::ftell(f); + if (here >= 0 && st.st_size > here) + { + return static_cast(st.st_size - here); + } + } +#else + static_cast(f); +#endif + return 0; +} + /// append the rest of a C stream: in blocks with fread, not character by -/// character (the library's file adapter reads with fgetc); -/// f is not null +/// character (the library's file adapter reads with fgetc), and in one block +/// when it is a regular file of known size; f is not null inline void append_file(std::string& buf, std::FILE* f) { + // room for the rest of a regular file and the byte that shows its end: + // the file is then read in one block, without a reallocation and copy at + // each doubling of the string (only a hint: the loop below reads what is + // there) + const unsigned long long rest = file_rest(f); + if (rest > 0 && rest < buf.max_size() - buf.size() - 1) + { + buf.reserve(buf.size() + static_cast(rest) + 1); + } for (;;) { const std::size_t used = buf.size(); diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index 65180ef55..6a28c37cb 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -535,6 +535,33 @@ TEST_CASE("json_view") CHECK_THROWS_AS(from_null = json_document::parse(static_cast(nullptr)), json::parse_error&); } + // a regular file is read in one block, from its position to its end + std::FILE* tail_file = std::tmpfile(); // NOLINT(cppcoreguidelines-owning-memory) + if (tail_file != nullptr) + { + const std::string prefix = "garbage "; + CHECK(std::fwrite(prefix.data(), 1, prefix.size(), tail_file) == prefix.size()); + CHECK(std::fwrite(text.data(), 1, text.size(), tail_file) == text.size()); + CHECK(std::fseek(tail_file, static_cast(prefix.size()), SEEK_SET) == 0); +#if defined(_WIN32) || defined(__unix__) || defined(__APPLE__) + CHECK(nlohmann::detail::view::file_rest(tail_file) == text.size()); +#endif + const json_document from_tail = json_document::parse(tail_file); + CHECK(from_tail.source() == text); + CHECK(from_tail.root().materialize() == json::parse(text)); + CHECK(nlohmann::detail::view::file_rest(tail_file) == 0); + CHECK(std::fclose(tail_file) == 0); // NOLINT(cppcoreguidelines-owning-memory) + } + + // a stream is read from its position to its end as well, in one block + // when its buffer knows how much is left + std::istringstream tail_stream("garbage " + text); + tail_stream.seekg(8); + const json_document from_tail_stream = json_document::parse(tail_stream); + CHECK(from_tail_stream.source() == text); + CHECK(from_tail_stream.root().materialize() == json::parse(text)); + CHECK(tail_stream.eof()); + // a stream without a buffer is an empty input std::istream no_buffer(nullptr); CHECK(json_document::parse(no_buffer, false).is_discarded());