printf.hpp 3.4 KB

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