snprintf.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file snprintf.hpp
  9. * \author Andrey Semashev
  10. * \date 20.02.2009
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_
  17. #include <stdio.h>
  18. #include <cstddef>
  19. #include <cstdarg>
  20. #include <boost/log/detail/config.hpp>
  21. #ifdef BOOST_LOG_USE_WCHAR_T
  22. #include <wchar.h>
  23. #endif // BOOST_LOG_USE_WCHAR_T
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. namespace aux {
  31. #if !defined(_MSC_VER)
  32. // Standard-conforming compilers already have the correct snprintfs
  33. using ::snprintf;
  34. using ::vsnprintf;
  35. # ifdef BOOST_LOG_USE_WCHAR_T
  36. using ::swprintf;
  37. using ::vswprintf;
  38. # endif // BOOST_LOG_USE_WCHAR_T
  39. #else // !defined(_MSC_VER)
  40. // MSVC snprintfs are not conforming but they are good enough for our cases
  41. inline int vsnprintf(char* buf, std::size_t size, const char* format, std::va_list args)
  42. {
  43. int n = _vsnprintf(buf, size, format, args);
  44. if (static_cast< unsigned int >(n) >= size)
  45. {
  46. n = static_cast< int >(size);
  47. buf[size - 1] = '\0';
  48. }
  49. return n;
  50. }
  51. # ifdef BOOST_LOG_USE_WCHAR_T
  52. inline int vswprintf(wchar_t* buf, std::size_t size, const wchar_t* format, std::va_list args)
  53. {
  54. int n = _vsnwprintf(buf, size, format, args);
  55. if (static_cast< unsigned int >(n) >= size)
  56. {
  57. n = static_cast< int >(size);
  58. buf[size - 1] = L'\0';
  59. }
  60. return n;
  61. }
  62. # endif // BOOST_LOG_USE_WCHAR_T
  63. inline int snprintf(char* buf, std::size_t size, const char* format, ...)
  64. {
  65. std::va_list args;
  66. va_start(args, format);
  67. int n = vsnprintf(buf, size, format, args);
  68. va_end(args);
  69. return n;
  70. }
  71. # ifdef BOOST_LOG_USE_WCHAR_T
  72. inline int swprintf(wchar_t* buf, std::size_t size, const wchar_t* format, ...)
  73. {
  74. std::va_list args;
  75. va_start(args, format);
  76. int n = vswprintf(buf, size, format, args);
  77. va_end(args);
  78. return n;
  79. }
  80. # endif // BOOST_LOG_USE_WCHAR_T
  81. #endif // !defined(_MSC_VER)
  82. } // namespace aux
  83. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  84. } // namespace boost
  85. #include <boost/log/detail/footer.hpp>
  86. #endif // BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_