snprintf.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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. # if _MSC_VER >= 1400
  41. // MSVC 2005 and later provide the safe-CRT implementation of the conforming snprintf
  42. inline int vsnprintf(char* buf, std::size_t size, const char* format, std::va_list args)
  43. {
  44. return ::vsprintf_s(buf, size, format, args);
  45. }
  46. # ifdef BOOST_LOG_USE_WCHAR_T
  47. inline int vswprintf(wchar_t* buf, std::size_t size, const wchar_t* format, std::va_list args)
  48. {
  49. return ::vswprintf_s(buf, size, format, args);
  50. }
  51. # endif // BOOST_LOG_USE_WCHAR_T
  52. # else // _MSC_VER >= 1400
  53. // MSVC prior to 2005 had a non-conforming extension _vsnprintf, that sometimes did not put a terminating '\0'
  54. inline int vsnprintf(char* buf, std::size_t size, const char* format, std::va_list args)
  55. {
  56. register int n = _vsnprintf(buf, size - 1, format, args);
  57. buf[size - 1] = '\0';
  58. return n;
  59. }
  60. # ifdef BOOST_LOG_USE_WCHAR_T
  61. inline int vswprintf(wchar_t* buf, std::size_t size, const wchar_t* format, std::va_list args)
  62. {
  63. register int n = _vsnwprintf(buf, size - 1, format, args);
  64. buf[size - 1] = L'\0';
  65. return n;
  66. }
  67. # endif // BOOST_LOG_USE_WCHAR_T
  68. # endif // _MSC_VER >= 1400
  69. inline int snprintf(char* buf, std::size_t size, const char* format, ...)
  70. {
  71. std::va_list args;
  72. va_start(args, format);
  73. register int n = vsnprintf(buf, size, format, args);
  74. va_end(args);
  75. return n;
  76. }
  77. # ifdef BOOST_LOG_USE_WCHAR_T
  78. inline int swprintf(wchar_t* buf, std::size_t size, const wchar_t* format, ...)
  79. {
  80. std::va_list args;
  81. va_start(args, format);
  82. register int n = vswprintf(buf, size, format, args);
  83. va_end(args);
  84. return n;
  85. }
  86. # endif // BOOST_LOG_USE_WCHAR_T
  87. #endif // !defined(_MSC_VER)
  88. } // namespace aux
  89. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  90. } // namespace boost
  91. #include <boost/log/detail/footer.hpp>
  92. #endif // BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_