printf.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2009-2020 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_PRINTF_HPP
  5. #define BOOST_CONVERT_PRINTF_HPP
  6. #include <boost/convert/base.hpp>
  7. #include <boost/make_default.hpp>
  8. #include <boost/mpl/vector.hpp>
  9. #include <boost/mpl/find.hpp>
  10. #include <string>
  11. #include <cstdio>
  12. namespace boost { namespace cnv
  13. {
  14. struct printf;
  15. }}
  16. struct boost::cnv::printf : boost::cnv::cnvbase<boost::cnv::printf>
  17. {
  18. using this_type = boost::cnv::printf;
  19. using base_type = boost::cnv::cnvbase<this_type>;
  20. using base_type::operator();
  21. template<typename in_type>
  22. cnv::range<char*>
  23. to_str(in_type value_in, char* buf) const
  24. {
  25. char const* fmt = pformat(pos<in_type>());
  26. int const num_chars = snprintf(buf, bufsize_, fmt, precision_, value_in);
  27. bool const success = num_chars < bufsize_;
  28. return cnv::range<char*>(buf, success ? (buf + num_chars) : buf);
  29. }
  30. template<typename string_type, typename out_type>
  31. void
  32. str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
  33. {
  34. out_type result = boost::make_default<out_type>();
  35. int const num_read = sscanf(&*range.begin(), format(pos<out_type>()), &result);
  36. if (num_read == 1)
  37. result_out = result;
  38. }
  39. private:
  40. template<typename Type> int pos() const
  41. {
  42. using managed_types = boost::mpl::vector<double, float,
  43. int, unsigned int,
  44. short int, unsigned short int,
  45. long int, unsigned long int>;
  46. using type_iterator = typename boost::mpl::find<managed_types, Type>::type;
  47. using type_pos = typename type_iterator::pos;
  48. return type_pos::value;
  49. }
  50. char const* pformat(int pos) const
  51. {
  52. static char const* d_fmt[] = { "%.*f", "%.*f", "%.*d", "%.*u", "%.*hd", "%.*hu", "%.*ld", "%.*lu" }; // Must match managed_types
  53. static char const* x_fmt[] = { "%.*f", "%.*f", "%.*x", "%.*x", "%.*hx", "%.*hx", "%.*lx", "%.*lx" }; // Must match managed_types
  54. static char const* o_fmt[] = { "%.*f", "%.*f", "%.*o", "%.*o", "%.*ho", "%.*ho", "%.*lo", "%.*lo" }; // Must match managed_types
  55. char const* fmt = base_ == boost::cnv::base::dec ? d_fmt[pos]
  56. : base_ == boost::cnv::base::hex ? x_fmt[pos]
  57. : base_ == boost::cnv::base::oct ? o_fmt[pos]
  58. : (BOOST_ASSERT(0), nullptr);
  59. return fmt;
  60. }
  61. char const* format(int pos) const
  62. {
  63. static char const* d_fmt[] = { "%f", "%f", "%d", "%u", "%hd", "%hu", "%ld", "%lu" }; // Must match managed_types
  64. static char const* x_fmt[] = { "%f", "%f", "%x", "%x", "%hx", "%hx", "%lx", "%lx" }; // Must match managed_types
  65. static char const* o_fmt[] = { "%f", "%f", "%o", "%o", "%ho", "%ho", "%lo", "%lo" }; // Must match managed_types
  66. char const* fmt = base_ == boost::cnv::base::dec ? d_fmt[pos]
  67. : base_ == boost::cnv::base::hex ? x_fmt[pos]
  68. : base_ == boost::cnv::base::oct ? o_fmt[pos]
  69. : (BOOST_ASSERT(0), nullptr);
  70. return fmt;
  71. }
  72. };
  73. #endif // BOOST_CONVERT_PRINTF_HPP