snprintf.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED
  3. // Copyright 2018, 2020, 2021 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See library home page at http://www.boost.org/libs/system
  9. #include <boost/config.hpp>
  10. #include <cstdio>
  11. #include <cstdarg>
  12. //
  13. namespace boost
  14. {
  15. namespace system
  16. {
  17. namespace detail
  18. {
  19. #if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
  20. inline void snprintf( char * buffer, std::size_t len, char const * format, ... )
  21. {
  22. # if defined( BOOST_MSVC )
  23. # pragma warning( push )
  24. # pragma warning( disable: 4996 )
  25. # endif
  26. if( len == 0 ) return;
  27. va_list args;
  28. va_start( args, format );
  29. _vsnprintf( buffer, len - 1, format, args );
  30. buffer[ len - 1 ] = 0;
  31. va_end( args );
  32. # if defined( BOOST_MSVC )
  33. # pragma warning( pop )
  34. # endif
  35. }
  36. #else
  37. #if defined(__GNUC__) && __GNUC__ >= 3
  38. __attribute__((__format__ (__printf__, 3, 4)))
  39. #endif
  40. inline void snprintf( char * buffer, std::size_t len, char const * format, ... )
  41. {
  42. va_list args;
  43. va_start( args, format );
  44. std::vsnprintf( buffer, len, format, args );
  45. va_end( args );
  46. }
  47. #endif
  48. } // namespace detail
  49. } // namespace system
  50. } // namespace boost
  51. #endif // #ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED