ostream_string.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. ([email protected])
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_UTILITY_OSTREAM_STRING_HPP
  8. #define BOOST_UTILITY_OSTREAM_STRING_HPP
  9. #include <boost/config.hpp>
  10. #include <iosfwd>
  11. #include <cstddef>
  12. namespace boost {
  13. namespace detail {
  14. template<class charT, class traits>
  15. inline std::size_t
  16. oss_put(std::basic_ostream<charT, traits>& os, const charT* data,
  17. std::size_t size)
  18. {
  19. return static_cast<std::size_t>(os.rdbuf()->sputn(data, size));
  20. }
  21. template<class charT, class traits>
  22. inline bool
  23. oss_fill(std::basic_ostream<charT, traits>& os, std::size_t size)
  24. {
  25. charT c = os.fill();
  26. charT fill[] = { c, c, c, c, c, c, c, c };
  27. enum {
  28. chunk = sizeof fill / sizeof(charT)
  29. };
  30. for (; size > chunk; size -= chunk) {
  31. if (boost::detail::oss_put(os, fill, chunk) != chunk) {
  32. return false;
  33. }
  34. }
  35. return boost::detail::oss_put(os, fill, size) == size;
  36. }
  37. template<class charT, class traits>
  38. class oss_guard {
  39. public:
  40. explicit oss_guard(std::basic_ostream<charT, traits>& os) BOOST_NOEXCEPT
  41. : os_(&os) { }
  42. ~oss_guard() BOOST_NOEXCEPT_IF(false) {
  43. if (os_) {
  44. os_->setstate(std::basic_ostream<charT, traits>::badbit);
  45. }
  46. }
  47. void release() BOOST_NOEXCEPT {
  48. os_ = 0;
  49. }
  50. private:
  51. oss_guard(const oss_guard&);
  52. oss_guard& operator=(const oss_guard&);
  53. std::basic_ostream<charT, traits>* os_;
  54. };
  55. } /* detail */
  56. template<class charT, class traits>
  57. inline std::basic_ostream<charT, traits>&
  58. ostream_string(std::basic_ostream<charT, traits>& os, const charT* data,
  59. std::size_t size)
  60. {
  61. typedef std::basic_ostream<charT, traits> stream;
  62. detail::oss_guard<charT, traits> guard(os);
  63. typename stream::sentry entry(os);
  64. if (entry) {
  65. std::size_t width = static_cast<std::size_t>(os.width());
  66. if (width <= size) {
  67. if (detail::oss_put(os, data, size) != size) {
  68. return os;
  69. }
  70. } else if ((os.flags() & stream::adjustfield) == stream::left) {
  71. if (detail::oss_put(os, data, size) != size ||
  72. !detail::oss_fill(os, width - size)) {
  73. return os;
  74. }
  75. } else if (!detail::oss_fill(os, width - size) ||
  76. detail::oss_put(os, data, size) != size) {
  77. return os;
  78. }
  79. os.width(0);
  80. }
  81. guard.release();
  82. return os;
  83. }
  84. } /* boost */
  85. #endif