• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BOOST_CONVERT_PRINTF_HPP
6 #define BOOST_CONVERT_PRINTF_HPP
7 
8 #include <boost/convert/base.hpp>
9 #include <boost/make_default.hpp>
10 #include <boost/mpl/vector.hpp>
11 #include <boost/mpl/find.hpp>
12 #include <boost/range/as_literal.hpp>
13 #include <string>
14 #include <cstdio>
15 
16 namespace boost { namespace cnv
17 {
18     struct printf;
19 }}
20 
21 struct boost::cnv::printf : boost::cnv::cnvbase<boost::cnv::printf>
22 {
23     using this_type = boost::cnv::printf;
24     using base_type = boost::cnv::cnvbase<this_type>;
25 
26     using base_type::operator();
27 
28     template<typename in_type>
29     cnv::range<char*>
to_strboost::cnv::printf30     to_str(in_type value_in, char* buf) const
31     {
32         char const*     fmt = pformat(pos<in_type>());
33         int const num_chars = snprintf(buf, bufsize_, fmt, precision_, value_in);
34         bool const  success = num_chars < bufsize_;
35 
36         return cnv::range<char*>(buf, success ? (buf + num_chars) : buf);
37     }
38     template<typename string_type, typename out_type>
39     void
str_toboost::cnv::printf40     str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
41     {
42         out_type    result = boost::make_default<out_type>();
43         int const num_read = sscanf(&*range.begin(), format(pos<out_type>()), &result);
44 
45         if (num_read == 1)
46             result_out = result;
47     }
48 
49     private:
50 
posboost::cnv::printf51     template<typename Type> int pos() const
52     {
53         using managed_types = boost::mpl::vector<double, float,
54                                   int, unsigned int,
55                                   short int, unsigned short int,
56                                   long int, unsigned long int>;
57         using type_iterator = typename boost::mpl::find<managed_types, Type>::type;
58         using      type_pos = typename type_iterator::pos;
59 
60         return type_pos::value;
61     }
62 
pformatboost::cnv::printf63     char const* pformat(int pos) const
64     {
65         static char const* d_fmt[] = { "%.*f", "%.*f", "%.*d", "%.*u", "%.*hd", "%.*hu", "%.*ld", "%.*lu" }; // Must match managed_types
66         static char const* x_fmt[] = { "%.*f", "%.*f", "%.*x", "%.*x", "%.*hx", "%.*hx", "%.*lx", "%.*lx" }; // Must match managed_types
67         static char const* o_fmt[] = { "%.*f", "%.*f", "%.*o", "%.*o", "%.*ho", "%.*ho", "%.*lo", "%.*lo" }; // Must match managed_types
68         char const*            fmt = base_ == boost::cnv::base::dec ? d_fmt[pos]
69                                    : base_ == boost::cnv::base::hex ? x_fmt[pos]
70                                    : base_ == boost::cnv::base::oct ? o_fmt[pos]
71                                    : (BOOST_ASSERT(0), nullptr);
72         return fmt;
73     }
formatboost::cnv::printf74     char const* format(int pos) const
75     {
76         static char const* d_fmt[] = { "%f", "%f", "%d", "%u", "%hd", "%hu", "%ld", "%lu" }; // Must match managed_types
77         static char const* x_fmt[] = { "%f", "%f", "%x", "%x", "%hx", "%hx", "%lx", "%lx" }; // Must match managed_types
78         static char const* o_fmt[] = { "%f", "%f", "%o", "%o", "%ho", "%ho", "%lo", "%lo" }; // Must match managed_types
79         char const*            fmt = base_ == boost::cnv::base::dec ? d_fmt[pos]
80                                    : base_ == boost::cnv::base::hex ? x_fmt[pos]
81                                    : base_ == boost::cnv::base::oct ? o_fmt[pos]
82                                    : (BOOST_ASSERT(0), nullptr);
83         return fmt;
84     }
85 };
86 
87 #endif // BOOST_CONVERT_PRINTF_HPP
88