generic_category_message.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_MESSAGE_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_MESSAGE_HPP_INCLUDED
  3. // Implementation of generic_error_category_message
  4. //
  5. // Copyright 2018 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See library home page at http://www.boost.org/libs/system
  11. #include <boost/config.hpp>
  12. #include <string>
  13. #include <cstring>
  14. namespace boost
  15. {
  16. namespace system
  17. {
  18. namespace detail
  19. {
  20. #if defined(__GLIBC__)
  21. // glibc has two incompatible strerror_r definitions
  22. inline char const * strerror_r_helper( char const * r, char const * ) noexcept
  23. {
  24. return r;
  25. }
  26. inline char const * strerror_r_helper( int r, char const * buffer ) noexcept
  27. {
  28. return r == 0? buffer: "Unknown error";
  29. }
  30. inline char const * generic_error_category_message( int ev, char * buffer, std::size_t len ) noexcept
  31. {
  32. return strerror_r_helper( strerror_r( ev, buffer, len ), buffer );
  33. }
  34. inline std::string generic_error_category_message( int ev )
  35. {
  36. char buffer[ 128 ];
  37. return generic_error_category_message( ev, buffer, sizeof( buffer ) );
  38. }
  39. #else // #if defined(__GLIBC__)
  40. // std::strerror is thread-safe on everything else, incl. Windows
  41. # if defined( BOOST_MSVC )
  42. # pragma warning( push )
  43. # pragma warning( disable: 4996 )
  44. # elif defined(__clang__) && defined(__has_warning)
  45. # pragma clang diagnostic push
  46. # if __has_warning("-Wdeprecated-declarations")
  47. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  48. # endif
  49. # endif
  50. inline std::string generic_error_category_message( int ev )
  51. {
  52. char const * m = std::strerror( ev );
  53. return m? m: "Unknown error";
  54. }
  55. inline char const * generic_error_category_message( int ev, char * buffer, std::size_t len ) noexcept
  56. {
  57. if( len == 0 )
  58. {
  59. return buffer;
  60. }
  61. if( len == 1 )
  62. {
  63. buffer[0] = 0;
  64. return buffer;
  65. }
  66. char const * m = std::strerror( ev );
  67. if( m == 0 ) return "Unknown error";
  68. std::strncpy( buffer, m, len - 1 );
  69. buffer[ len-1 ] = 0;
  70. return buffer;
  71. }
  72. # if defined( BOOST_MSVC )
  73. # pragma warning( pop )
  74. # elif defined(__clang__) && defined(__has_warning)
  75. # pragma clang diagnostic pop
  76. # endif
  77. #endif // #if defined(__GLIBC__)
  78. } // namespace detail
  79. } // namespace system
  80. } // namespace boost
  81. #endif // #ifndef BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_MESSAGE_HPP_INCLUDED