From 5e6cd060d7534dd4c111bc834ab9fcbdec0de5a6 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 10 Feb 2023 21:15:19 +0300 Subject: [PATCH] Make io::read_full return util::blob --- libs/gfx/include/psemek/gfx/gltf_mesh.hpp | 3 ++- libs/gfx/source/gltf_mesh.cpp | 8 ++++---- libs/gfx/source/gltf_parser.cpp | 3 +-- libs/io/CMakeLists.txt | 1 + libs/io/include/psemek/io/stream.hpp | 5 ++--- libs/io/source/stream.cpp | 24 ++++++++++++++++------- libs/ui/source/bmfont.cpp | 3 +-- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/libs/gfx/include/psemek/gfx/gltf_mesh.hpp b/libs/gfx/include/psemek/gfx/gltf_mesh.hpp index a9e379b8..5cabbc04 100644 --- a/libs/gfx/include/psemek/gfx/gltf_mesh.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_mesh.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -25,6 +26,6 @@ namespace psemek::gfx virtual ~gltf_mesh() {} }; - std::unique_ptr make_gltf_mesh(gltf_asset const & asset, std::function(std::string const &)> uri_loader); + std::unique_ptr make_gltf_mesh(gltf_asset const & asset, std::function uri_loader); } diff --git a/libs/gfx/source/gltf_mesh.cpp b/libs/gfx/source/gltf_mesh.cpp index b0ddf769..b2625df1 100644 --- a/libs/gfx/source/gltf_mesh.cpp +++ b/libs/gfx/source/gltf_mesh.cpp @@ -31,7 +31,7 @@ namespace psemek::gfx struct gltf_mesh_impl : gltf_mesh { - gltf_mesh_impl(gltf_asset const & asset, std::function(std::string const &)> uri_loader); + gltf_mesh_impl(gltf_asset const & asset, std::function uri_loader); gltf_asset::material const & material(std::size_t index) const override { @@ -58,7 +58,7 @@ namespace psemek::gfx std::unordered_map> meshes_; }; - gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function(std::string const &)> uri_loader) + gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function uri_loader) { materials_ = asset.materials; @@ -72,7 +72,7 @@ namespace psemek::gfx { auto data = uri_loader(texture.uri); auto & target = textures_.emplace_back(); - target.load_srgb(gfx::read_png(io::memory_istream(data.data(), data.data() + data.size()))); + target.load_srgb(gfx::read_png(io::memory_istream(data.string_view()))); target.linear_mipmap_filter(); target.anisotropy(); target.generate_mipmap(); @@ -130,7 +130,7 @@ namespace psemek::gfx } - std::unique_ptr make_gltf_mesh(gltf_asset const & asset, std::function(std::string const &)> uri_loader) + std::unique_ptr make_gltf_mesh(gltf_asset const & asset, std::function uri_loader) { return std::make_unique(asset, std::move(uri_loader)); } diff --git a/libs/gfx/source/gltf_parser.cpp b/libs/gfx/source/gltf_parser.cpp index eeaa24eb..04dc6387 100644 --- a/libs/gfx/source/gltf_parser.cpp +++ b/libs/gfx/source/gltf_parser.cpp @@ -60,8 +60,7 @@ namespace psemek::gfx gltf_asset parse_gltf(io::istream && stream) { - auto description_str = io::read_full(std::move(stream)); - description_str.push_back(0); + auto description_str = io::read_full(std::move(stream)).string(); rapidjson::Document document; document.ParseInsitu(description_str.data()); diff --git a/libs/io/CMakeLists.txt b/libs/io/CMakeLists.txt index dc515d92..3d0a1b83 100644 --- a/libs/io/CMakeLists.txt +++ b/libs/io/CMakeLists.txt @@ -3,5 +3,6 @@ file(GLOB_RECURSE PSEMEK_IO_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "sour psemek_add_library(psemek-io ${PSEMEK_IO_HEADERS} ${PSEMEK_IO_SOURCES}) target_include_directories(psemek-io PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_link_libraries(psemek-io PUBLIC psemek-util) psemek_glob_tests(psemek-io tests) diff --git a/libs/io/include/psemek/io/stream.hpp b/libs/io/include/psemek/io/stream.hpp index 36128b5c..3c838a0d 100644 --- a/libs/io/include/psemek/io/stream.hpp +++ b/libs/io/include/psemek/io/stream.hpp @@ -1,12 +1,11 @@ #pragma once #include +#include #include #include -#include - namespace psemek::io { @@ -31,6 +30,6 @@ namespace psemek::io std::unique_ptr std_out(); std::unique_ptr std_err(); - std::vector read_full(istream && stream); + util::blob read_full(istream && stream); } diff --git a/libs/io/source/stream.cpp b/libs/io/source/stream.cpp index 11247f6e..06163c89 100644 --- a/libs/io/source/stream.cpp +++ b/libs/io/source/stream.cpp @@ -46,22 +46,32 @@ namespace psemek::io return std::make_unique(stderr); } - std::vector read_full(istream && stream) + util::blob read_full(istream && stream) { + std::size_t allocated = 0; std::size_t size = 0; - std::vector result(1024); + std::unique_ptr data; while (true) { - if (result.size() == size) - result.resize(result.size() * 2); + if (!data) + { + allocated = 16; + data.reset(new char[allocated]); + } + else if (size == allocated) + { + allocated *= 2; + std::unique_ptr new_data(new char[allocated]); + std::copy(data.get(), data.get() + size, new_data.get()); + data = std::move(new_data); + } - std::size_t count = stream.read(result.data() + size, result.size() - size); + std::size_t count = stream.read(data.get() + size, allocated - size); if (count == 0) break; size += count; } - result.resize(size); - return result; + return util::blob(size, std::move(data)); } } diff --git a/libs/ui/source/bmfont.cpp b/libs/ui/source/bmfont.cpp index 9e6b55e7..a48248a3 100644 --- a/libs/ui/source/bmfont.cpp +++ b/libs/ui/source/bmfont.cpp @@ -47,8 +47,7 @@ namespace psemek::ui bmfont_data bmfont_data::parse(io::istream && stream) { - auto description_str = io::read_full(std::move(stream)); - description_str.push_back(0); + auto description_str = io::read_full(std::move(stream)).string(); rapidjson::Document document; document.ParseInsitu(description_str.data());