Make io::read_full return util::blob

This commit is contained in:
Nikita Lisitsa 2023-02-10 21:15:19 +03:00
parent 40c62d621a
commit 5e6cd060d7
7 changed files with 28 additions and 19 deletions

View file

@ -4,6 +4,7 @@
#include <psemek/gfx/drawable.hpp>
#include <psemek/gfx/texture.hpp>
#include <psemek/util/span.hpp>
#include <psemek/util/blob.hpp>
#include <functional>
@ -25,6 +26,6 @@ namespace psemek::gfx
virtual ~gltf_mesh() {}
};
std::unique_ptr<gltf_mesh> make_gltf_mesh(gltf_asset const & asset, std::function<std::vector<char>(std::string const &)> uri_loader);
std::unique_ptr<gltf_mesh> make_gltf_mesh(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader);
}

View file

@ -31,7 +31,7 @@ namespace psemek::gfx
struct gltf_mesh_impl
: gltf_mesh
{
gltf_mesh_impl(gltf_asset const & asset, std::function<std::vector<char>(std::string const &)> uri_loader);
gltf_mesh_impl(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader);
gltf_asset::material const & material(std::size_t index) const override
{
@ -58,7 +58,7 @@ namespace psemek::gfx
std::unordered_map<util::hstring, std::vector<primitive>> meshes_;
};
gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function<std::vector<char>(std::string const &)> uri_loader)
gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function<util::blob(std::string const &)> 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<gltf_mesh> make_gltf_mesh(gltf_asset const & asset, std::function<std::vector<char>(std::string const &)> uri_loader)
std::unique_ptr<gltf_mesh> make_gltf_mesh(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader)
{
return std::make_unique<gltf_mesh_impl>(asset, std::move(uri_loader));
}

View file

@ -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());

View file

@ -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)

View file

@ -1,12 +1,11 @@
#pragma once
#include <psemek/io/error.hpp>
#include <psemek/util/blob.hpp>
#include <cstddef>
#include <memory>
#include <vector>
namespace psemek::io
{
@ -31,6 +30,6 @@ namespace psemek::io
std::unique_ptr<ostream> std_out();
std::unique_ptr<ostream> std_err();
std::vector<char> read_full(istream && stream);
util::blob read_full(istream && stream);
}

View file

@ -46,22 +46,32 @@ namespace psemek::io
return std::make_unique<file_ostream>(stderr);
}
std::vector<char> read_full(istream && stream)
util::blob read_full(istream && stream)
{
std::size_t allocated = 0;
std::size_t size = 0;
std::vector<char> result(1024);
std::unique_ptr<char[]> 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<char[]> 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));
}
}

View file

@ -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());