From 38a76bafce303d211043fd118119c9431cdbe932 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 16 Oct 2020 17:37:09 +0300 Subject: add a separate Buffer class --- include/winapi/buffer.hpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++ include/winapi/handle.hpp | 5 ++-- src/handle.cpp | 10 +++---- 3 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 include/winapi/buffer.hpp diff --git a/include/winapi/buffer.hpp b/include/winapi/buffer.hpp new file mode 100644 index 0000000..246822e --- /dev/null +++ b/include/winapi/buffer.hpp @@ -0,0 +1,69 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace winapi { + +class Buffer : public std::vector { +public: + typedef std::vector Parent; + + Buffer() = default; + + explicit Buffer(Parent&& src) : Parent(std::move(src)) {} + + template + explicit Buffer(const std::basic_string& src) { + set(src); + } + + explicit Buffer(const void* src, std::size_t nb) { set(src, nb); } + + template + void set(const std::basic_string& src) { + set(src.c_str(), src.length() * sizeof(std::basic_string::char_type)); + } + + void set(const void* src, std::size_t nb) { + resize(nb); + std::memcpy(data(), src, nb); + } + + std::string as_utf8() const { + const auto c_str = reinterpret_cast(data()); + const auto nb = size(); + const auto nch = nb; + return {c_str, nch}; + } + + std::wstring as_utf16() const { + const auto c_str = reinterpret_cast(data()); + const auto nb = size(); + if (nb % 2 != 0) { + std::ostringstream oss; + oss << "Buffer size invalid at " << nb << " bytes"; + throw std::runtime_error{oss.str()}; + } + const auto nch = nb / 2; + return {c_str, nch}; + } + + void add(const Buffer& src) { + const auto nb = size(); + resize(size() + src.size()); + std::memcpy(data() + nb, src.data(), src.size()); + } +}; + +} // namespace winapi diff --git a/include/winapi/handle.hpp b/include/winapi/handle.hpp index af37424..5f813bc 100644 --- a/include/winapi/handle.hpp +++ b/include/winapi/handle.hpp @@ -5,6 +5,8 @@ #pragma once +#include "buffer.hpp" + #include #include @@ -12,7 +14,6 @@ #include #include #include -#include namespace winapi { @@ -37,8 +38,6 @@ public: static Handle std_out(); static Handle std_err(); - typedef std::vector Buffer; - Buffer read() const; BOOST_STATIC_CONSTEXPR std::size_t max_chunk_size = 16 * 1024; diff --git a/src/handle.cpp b/src/handle.cpp index a6ab3a5..1147cf6 100644 --- a/src/handle.cpp +++ b/src/handle.cpp @@ -3,6 +3,7 @@ // For details, see https://github.com/egor-tensin/winapi-common. // Distributed under the MIT License. +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include namespace winapi { namespace { @@ -100,15 +102,13 @@ bool Handle::read_chunk(Buffer& buffer) const { } } -Handle::Buffer Handle::read() const { +Buffer Handle::read() const { Buffer buffer; - Buffer chunk; while (true) { + Buffer chunk; const auto next = read_chunk(chunk); - - buffer.reserve(buffer.size() + chunk.size()); - buffer.insert(buffer.cend(), chunk.cbegin(), chunk.cend()); + buffer.add(chunk); if (!next) { break; -- cgit v1.2.3