• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   formatters/csv_decorator.hpp
9  * \author Andrey Semashev
10  * \date   18.11.2012
11  *
12  * The header contains implementation of a CSV-style character decorator.
13  * See: http://en.wikipedia.org/wiki/Comma-separated_values
14  */
15 
16 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_
17 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_
18 
19 #include <boost/range/iterator_range_core.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/expressions/formatters/char_decorator.hpp>
22 #include <boost/log/detail/header.hpp>
23 
24 #ifdef BOOST_HAS_PRAGMA_ONCE
25 #pragma once
26 #endif
27 
28 namespace boost {
29 
30 BOOST_LOG_OPEN_NAMESPACE
31 
32 namespace expressions {
33 
34 namespace aux {
35 
36 template< typename >
37 struct csv_decorator_traits;
38 
39 #ifdef BOOST_LOG_USE_CHAR
40 template< >
41 struct csv_decorator_traits< char >
42 {
get_patternsboost::expressions::aux::csv_decorator_traits43     static boost::iterator_range< const char* const* > get_patterns()
44     {
45         static const char* const patterns[] =
46         {
47             "\""
48         };
49         return boost::make_iterator_range(patterns);
50     }
get_replacementsboost::expressions::aux::csv_decorator_traits51     static boost::iterator_range< const char* const* > get_replacements()
52     {
53         static const char* const replacements[] =
54         {
55             "\"\""
56         };
57         return boost::make_iterator_range(replacements);
58     }
59 };
60 #endif // BOOST_LOG_USE_CHAR
61 
62 #ifdef BOOST_LOG_USE_WCHAR_T
63 template< >
64 struct csv_decorator_traits< wchar_t >
65 {
get_patternsboost::expressions::aux::csv_decorator_traits66     static boost::iterator_range< const wchar_t* const* > get_patterns()
67     {
68         static const wchar_t* const patterns[] =
69         {
70             L"\""
71         };
72         return boost::make_iterator_range(patterns);
73     }
get_replacementsboost::expressions::aux::csv_decorator_traits74     static boost::iterator_range< const wchar_t* const* > get_replacements()
75     {
76         static const wchar_t* const replacements[] =
77         {
78             L"\"\""
79         };
80         return boost::make_iterator_range(replacements);
81     }
82 };
83 #endif // BOOST_LOG_USE_WCHAR_T
84 
85 template< typename CharT >
86 struct csv_decorator_gen
87 {
88     typedef CharT char_type;
89 
90     template< typename SubactorT >
operator []boost::expressions::aux::csv_decorator_gen91     BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
92     {
93         typedef csv_decorator_traits< char_type > traits_type;
94         typedef pattern_replacer< char_type > replacer_type;
95         typedef char_decorator_actor< SubactorT, replacer_type > result_type;
96         typedef typename result_type::terminal_type terminal_type;
97         typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }};
98         return result_type(act);
99     }
100 };
101 
102 } // namespace aux
103 
104 /*!
105  * CSV-style decorator generator object. The decorator doubles double quotes that may be found
106  * in the output. See http://en.wikipedia.org/wiki/Comma-separated_values for more information on
107  * the CSV format. The generator provides <tt>operator[]</tt> that can be used to construct
108  * the actual decorator. For example:
109  *
110  * <code>
111  * csv_decor[ stream << attr< std::string >("MyAttr") ]
112  * </code>
113  *
114  * For wide-character formatting there is the similar \c wcsv_decor decorator generator object.
115  */
116 #ifdef BOOST_LOG_USE_CHAR
117 BOOST_INLINE_VARIABLE const aux::csv_decorator_gen< char > csv_decor = {};
118 #endif
119 #ifdef BOOST_LOG_USE_WCHAR_T
120 BOOST_INLINE_VARIABLE const aux::csv_decorator_gen< wchar_t > wcsv_decor = {};
121 #endif
122 
123 /*!
124  * The function creates an CSV-style decorator generator for arbitrary character type.
125  */
126 template< typename CharT >
make_csv_decor()127 BOOST_FORCEINLINE aux::csv_decorator_gen< CharT > make_csv_decor()
128 {
129     return aux::csv_decorator_gen< CharT >();
130 }
131 
132 } // namespace expressions
133 
134 BOOST_LOG_CLOSE_NAMESPACE // namespace log
135 
136 } // namespace boost
137 
138 #include <boost/log/detail/footer.hpp>
139 
140 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_
141