buffer_fill.hpp 869 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. Copyright 2019-2020 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_IO_DETAIL_BUFFER_FILL_HPP
  8. #define BOOST_IO_DETAIL_BUFFER_FILL_HPP
  9. #include <iosfwd>
  10. #include <cstddef>
  11. namespace boost {
  12. namespace io {
  13. namespace detail {
  14. template<class charT, class traits>
  15. inline bool
  16. buffer_fill(std::basic_streambuf<charT, traits>& buf, charT ch,
  17. std::size_t size)
  18. {
  19. charT fill[] = { ch, ch, ch, ch, ch, ch, ch, ch };
  20. enum {
  21. chunk = sizeof fill / sizeof(charT)
  22. };
  23. for (; size > chunk; size -= chunk) {
  24. if (static_cast<std::size_t>(buf.sputn(fill, chunk)) != chunk) {
  25. return false;
  26. }
  27. }
  28. return static_cast<std::size_t>(buf.sputn(fill, size)) == size;
  29. }
  30. } /* detail */
  31. } /* io */
  32. } /* boost */
  33. #endif