snprintf.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  32. // MSVC snprintfs are not conforming but they are good enough for our cases.
  33. // MinGW32, at least the older versions up until gcc 4.7, also provide the non-conforming interface.
  34. inline int vsnprintf(char* buf, std::size_t size, const char* format, std::va_list args)
  35. {
  36. int n = _vsnprintf(buf, size, format, args);
  37. if (static_cast< unsigned int >(n) >= size)
  38. {
  39. n = static_cast< int >(size);
  40. buf[size - 1] = '\0';
  41. }
  42. return n;
  43. }
  44. # ifdef BOOST_LOG_USE_WCHAR_T
  45. inline int vswprintf(wchar_t* buf, std::size_t size, const wchar_t* format, std::va_list args)
  46. {
  47. int n = _vsnwprintf(buf, size, format, args);
  48. if (static_cast< unsigned int >(n) >= size)
  49. {
  50. n = static_cast< int >(size);
  51. buf[size - 1] = L'\0';
  52. }
  53. return n;
  54. }
  55. # endif // BOOST_LOG_USE_WCHAR_T
  56. inline int snprintf(char* buf, std::size_t size, const char* format, ...)
  57. {
  58. std::va_list args;
  59. va_start(args, format);
  60. int n = vsnprintf(buf, size, format, args);
  61. va_end(args);
  62. return n;
  63. }
  64. # ifdef BOOST_LOG_USE_WCHAR_T
  65. inline int swprintf(wchar_t* buf, std::size_t size, const wchar_t* format, ...)
  66. {
  67. std::va_list args;
  68. va_start(args, format);
  69. int n = vswprintf(buf, size, format, args);
  70. va_end(args);
  71. return n;
  72. }
  73. # endif // BOOST_LOG_USE_WCHAR_T
  74. #else
  75. // Standard-conforming compilers already have the correct snprintfs
  76. using ::snprintf;
  77. using ::vsnprintf;
  78. # ifdef BOOST_LOG_USE_WCHAR_T
  79. using ::swprintf;
  80. using ::vswprintf;
  81. # endif // BOOST_LOG_USE_WCHAR_T
  82. #endif
  83. } // namespace aux
  84. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  85. } // namespace boost
  86. #include <boost/log/detail/footer.hpp>
  87. #endif // BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_