ostream_put.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright 2019 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_OSTREAM_PUT_HPP
  8. #define BOOST_IO_OSTREAM_PUT_HPP
  9. #include <boost/io/detail/buffer_fill.hpp>
  10. #include <boost/io/detail/ostream_guard.hpp>
  11. namespace boost {
  12. namespace io {
  13. template<class charT, class traits>
  14. inline std::basic_ostream<charT, traits>&
  15. ostream_put(std::basic_ostream<charT, traits>& os, const charT* data,
  16. std::size_t size)
  17. {
  18. typedef std::basic_ostream<charT, traits> stream;
  19. detail::ostream_guard<charT, traits> guard(os);
  20. typename stream::sentry entry(os);
  21. if (entry) {
  22. std::basic_streambuf<charT, traits>& buf = *os.rdbuf();
  23. std::size_t width = static_cast<std::size_t>(os.width());
  24. if (width <= size) {
  25. if (static_cast<std::size_t>(buf.sputn(data, size)) != size) {
  26. return os;
  27. }
  28. } else if ((os.flags() & stream::adjustfield) == stream::left) {
  29. if (static_cast<std::size_t>(buf.sputn(data, size)) != size ||
  30. !detail::buffer_fill(buf, os.fill(), width - size)) {
  31. return os;
  32. }
  33. } else if (!detail::buffer_fill(buf, os.fill(), width - size) ||
  34. static_cast<std::size_t>(buf.sputn(data, size)) != size) {
  35. return os;
  36. }
  37. os.width(0);
  38. }
  39. guard.release();
  40. return os;
  41. }
  42. } /* io */
  43. } /* boost */
  44. #endif