2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (c) 2016-2020 Thomas Fussell
|
2016-10-31 08:28:40 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2017-04-21 02:03:03 +08:00
|
|
|
|
2016-10-31 08:28:40 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <algorithm>
|
2016-10-31 08:28:40 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <xlnt/xlnt_config.hpp>
|
|
|
|
|
2016-10-31 08:28:40 +08:00
|
|
|
namespace xlnt {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Allows a std::vector to be read through a std::istream.
|
|
|
|
/// </summary>
|
2017-05-01 11:25:57 +08:00
|
|
|
class XLNT_API vector_istreambuf : public std::streambuf
|
2016-10-31 08:28:40 +08:00
|
|
|
{
|
|
|
|
using int_type = std::streambuf::int_type;
|
|
|
|
|
|
|
|
public:
|
2017-04-30 00:29:41 +08:00
|
|
|
vector_istreambuf(const std::vector<std::uint8_t> &data);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
|
|
|
vector_istreambuf(const vector_istreambuf &) = delete;
|
|
|
|
vector_istreambuf &operator=(const vector_istreambuf &) = delete;
|
|
|
|
|
|
|
|
private:
|
2017-04-30 00:29:41 +08:00
|
|
|
int_type underflow();
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
int_type uflow();
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streamsize showmanyc();
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streampos seekpos(std::streampos sp, std::ios_base::openmode);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const std::vector<std::uint8_t> &data_;
|
2016-11-01 08:48:43 +08:00
|
|
|
std::size_t position_;
|
2016-10-31 08:28:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Allows a std::vector to be written through a std::ostream.
|
|
|
|
/// </summary>
|
2017-05-01 11:25:57 +08:00
|
|
|
class XLNT_API vector_ostreambuf : public std::streambuf
|
2016-10-31 08:28:40 +08:00
|
|
|
{
|
|
|
|
using int_type = std::streambuf::int_type;
|
|
|
|
|
|
|
|
public:
|
2017-04-30 00:29:41 +08:00
|
|
|
vector_ostreambuf(std::vector<std::uint8_t> &data);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
|
|
|
vector_ostreambuf(const vector_ostreambuf &) = delete;
|
|
|
|
vector_ostreambuf &operator=(const vector_ostreambuf &) = delete;
|
|
|
|
|
|
|
|
private:
|
2017-04-30 00:29:41 +08:00
|
|
|
int_type overflow(int_type c = traits_type::eof());
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streamsize xsputn(const char *s, std::streamsize n);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
2017-04-30 00:29:41 +08:00
|
|
|
std::streampos seekpos(std::streampos sp, std::ios_base::openmode);
|
2016-10-31 08:28:40 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::uint8_t> &data_;
|
2016-11-01 08:48:43 +08:00
|
|
|
std::size_t position_;
|
2016-10-31 08:28:40 +08:00
|
|
|
};
|
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
//TODO: detail headers shouldn't be exporting such functions
|
|
|
|
|
2017-01-05 08:02:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Helper function to read all data from in_stream and store them in a vector.
|
|
|
|
/// </summary>
|
2017-04-30 00:29:41 +08:00
|
|
|
XLNT_API std::vector<std::uint8_t> to_vector(std::istream &in_stream);
|
2017-01-05 08:02:31 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Helper function to write all data from bytes into out_stream.
|
|
|
|
/// </summary>
|
2017-04-30 00:29:41 +08:00
|
|
|
XLNT_API void to_stream(const std::vector<std::uint8_t> &bytes, std::ostream &out_stream);
|
2017-01-05 08:02:31 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Shortcut function to stream a vector of bytes into a std::ostream.
|
|
|
|
/// </summary>
|
2017-04-30 00:29:41 +08:00
|
|
|
XLNT_API std::ostream &operator<<(std::ostream &out_stream, const std::vector<std::uint8_t> &bytes);
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-10-31 08:28:40 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|