vector_streambuf.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2016-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #pragma once
  24. #include <algorithm>
  25. #include <iostream>
  26. #include <vector>
  27. #include <xlnt/xlnt_config.hpp>
  28. namespace xlnt {
  29. namespace detail {
  30. /// <summary>
  31. /// Allows a std::vector to be read through a std::istream.
  32. /// </summary>
  33. class XLNT_API vector_istreambuf : public std::streambuf
  34. {
  35. using int_type = std::streambuf::int_type;
  36. public:
  37. explicit vector_istreambuf(const std::vector<std::uint8_t> &data);
  38. vector_istreambuf(const vector_istreambuf &) = delete;
  39. vector_istreambuf &operator=(const vector_istreambuf &) = delete;
  40. private:
  41. int_type underflow() override;
  42. int_type uflow() override;
  43. std::streamsize showmanyc() override;
  44. std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode) override;
  45. std::streampos seekpos(std::streampos sp, std::ios_base::openmode) override;
  46. private:
  47. const std::vector<std::uint8_t> &data_;
  48. std::size_t position_;
  49. };
  50. /// <summary>
  51. /// Allows a std::vector to be written through a std::ostream.
  52. /// </summary>
  53. class XLNT_API vector_ostreambuf : public std::streambuf
  54. {
  55. using int_type = std::streambuf::int_type;
  56. public:
  57. explicit vector_ostreambuf(std::vector<std::uint8_t> &data);
  58. vector_ostreambuf(const vector_ostreambuf &) = delete;
  59. vector_ostreambuf &operator=(const vector_ostreambuf &) = delete;
  60. private:
  61. int_type overflow(int_type c) override;
  62. std::streamsize xsputn(const char *s, std::streamsize n) override;
  63. std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode) override;
  64. std::streampos seekpos(std::streampos sp, std::ios_base::openmode) override;
  65. private:
  66. std::vector<std::uint8_t> &data_;
  67. std::size_t position_;
  68. };
  69. //TODO: detail headers shouldn't be exporting such functions
  70. /// <summary>
  71. /// Helper function to read all data from in_stream and store them in a vector.
  72. /// </summary>
  73. XLNT_API std::vector<std::uint8_t> to_vector(std::istream &in_stream);
  74. /// <summary>
  75. /// Helper function to write all data from bytes into out_stream.
  76. /// </summary>
  77. XLNT_API void to_stream(const std::vector<std::uint8_t> &bytes, std::ostream &out_stream);
  78. /// <summary>
  79. /// Shortcut function to stream a vector of bytes into a std::ostream.
  80. /// </summary>
  81. XLNT_API std::ostream &operator<<(std::ostream &out_stream, const std::vector<std::uint8_t> &bytes);
  82. } // namespace detail
  83. } // namespace xlnt