mirror of
https://github.com/boostorg/system.git
synced 2025-07-29 20:17:13 +02:00
Add detail/snprintf.hpp
This commit is contained in:
@ -13,6 +13,7 @@
|
||||
#include <boost/system/detail/error_category.hpp>
|
||||
#include <boost/system/detail/error_condition.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
#include <boost/system/detail/snprintf.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
@ -83,7 +84,8 @@ inline char const * error_category::message( int ev, char * buffer, std::size_t
|
||||
#if !defined(BOOST_NO_EXCEPTIONS)
|
||||
catch( ... )
|
||||
{
|
||||
return "Message text unavailable";
|
||||
detail::snprintf( buffer, len, "No message text available for error %d", ev );
|
||||
return buffer;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
70
include/boost/system/detail/snprintf.hpp
Normal file
70
include/boost/system/detail/snprintf.hpp
Normal file
@ -0,0 +1,70 @@
|
||||
#ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED
|
||||
#define BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED
|
||||
|
||||
// Copyright 2018, 2020, 2021 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See library home page at http://www.boost.org/libs/system
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
|
||||
//
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace system
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
|
||||
|
||||
inline void snprintf( char * buffer, std::size_t len, char const * format, ... )
|
||||
{
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( push )
|
||||
# pragma warning( disable: 4996 )
|
||||
# endif
|
||||
|
||||
if( len == 0 ) return;
|
||||
|
||||
va_list args;
|
||||
va_start( args, format );
|
||||
|
||||
_vsnprintf( buffer, len - 1, format, args );
|
||||
buffer[ len - 1 ] = 0;
|
||||
|
||||
va_end( args );
|
||||
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( pop )
|
||||
# endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline void snprintf( char * buffer, std::size_t len, char const * format, ... )
|
||||
{
|
||||
va_list args;
|
||||
va_start( args, format );
|
||||
|
||||
std::vsnprintf( buffer, len, format, args );
|
||||
|
||||
va_end( args );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace system
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_SYSTEM_DETAIL_SNPRINTF_HPP_INCLUDED
|
@ -10,6 +10,7 @@
|
||||
//
|
||||
// See library home page at http://www.boost.org/libs/system
|
||||
|
||||
#include <boost/system/detail/snprintf.hpp>
|
||||
#include <boost/winapi/error_handling.hpp>
|
||||
#include <boost/winapi/character_code_conversion.hpp>
|
||||
#include <boost/winapi/local_memory.hpp>
|
||||
@ -27,35 +28,12 @@ namespace system
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
|
||||
|
||||
inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
|
||||
{
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( push )
|
||||
# pragma warning( disable: 4996 )
|
||||
# endif
|
||||
|
||||
_snprintf( buffer, len - 1, "Unknown error (%d)", ev );
|
||||
|
||||
buffer[ len - 1 ] = 0;
|
||||
return buffer;
|
||||
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( pop )
|
||||
# endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
|
||||
{
|
||||
std::snprintf( buffer, len, "Unknown error (%d)", ev );
|
||||
detail::snprintf( buffer, len, "Unknown error (%d)", ev );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline boost::winapi::UINT_ message_cp_win32()
|
||||
{
|
||||
#if defined(BOOST_SYSTEM_USE_UTF8)
|
||||
|
@ -87,3 +87,5 @@ boost_test(TYPE run SOURCES cygwin_error_test.cpp)
|
||||
boost_test(TYPE run SOURCES linux_error_test.cpp)
|
||||
|
||||
boost_test(TYPE link SOURCES errc_test3.cpp)
|
||||
|
||||
boost_test(TYPE run SOURCES snprintf_test.cpp)
|
||||
|
@ -91,3 +91,5 @@ run cygwin_error_test.cpp ;
|
||||
run linux_error_test.cpp ;
|
||||
|
||||
link errc_test3.cpp ;
|
||||
|
||||
run snprintf_test.cpp ;
|
||||
|
60
test/snprintf_test.cpp
Normal file
60
test/snprintf_test.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright 2021 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/system/detail/snprintf.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
char buffer[ 64 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 64 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 15 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "...xy...151..." );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 14 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "...xy...151.." );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 5 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "...x" );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 1 ];
|
||||
boost::system::detail::snprintf( buffer, sizeof(buffer), "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_CSTR_EQ( buffer, "" );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 1 ] = { 'Q' };
|
||||
boost::system::detail::snprintf( buffer, 0, "...%s...%d...", "xy", 151 );
|
||||
|
||||
BOOST_TEST_EQ( buffer[0], 'Q' );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user