csv_decorator.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_
  16. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_
  17. #include <boost/range/iterator_range_core.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/expressions/formatters/char_decorator.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. namespace expressions {
  27. namespace aux {
  28. template< typename >
  29. struct csv_decorator_traits;
  30. #ifdef BOOST_LOG_USE_CHAR
  31. template< >
  32. struct csv_decorator_traits< char >
  33. {
  34. static boost::iterator_range< const char* const* > get_patterns()
  35. {
  36. static const char* const patterns[] =
  37. {
  38. "\""
  39. };
  40. return boost::make_iterator_range(patterns);
  41. }
  42. static boost::iterator_range< const char* const* > get_replacements()
  43. {
  44. static const char* const replacements[] =
  45. {
  46. "\"\""
  47. };
  48. return boost::make_iterator_range(replacements);
  49. }
  50. };
  51. #endif // BOOST_LOG_USE_CHAR
  52. #ifdef BOOST_LOG_USE_WCHAR_T
  53. template< >
  54. struct csv_decorator_traits< wchar_t >
  55. {
  56. static boost::iterator_range< const wchar_t* const* > get_patterns()
  57. {
  58. static const wchar_t* const patterns[] =
  59. {
  60. L"\""
  61. };
  62. return boost::make_iterator_range(patterns);
  63. }
  64. static boost::iterator_range< const wchar_t* const* > get_replacements()
  65. {
  66. static const wchar_t* const replacements[] =
  67. {
  68. L"\"\""
  69. };
  70. return boost::make_iterator_range(replacements);
  71. }
  72. };
  73. #endif // BOOST_LOG_USE_WCHAR_T
  74. template< typename CharT >
  75. struct csv_decorator_gen
  76. {
  77. typedef CharT char_type;
  78. template< typename SubactorT >
  79. BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
  80. {
  81. typedef csv_decorator_traits< char_type > traits_type;
  82. typedef pattern_replacer< char_type > replacer_type;
  83. typedef char_decorator_actor< SubactorT, replacer_type > result_type;
  84. typedef typename result_type::terminal_type terminal_type;
  85. typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }};
  86. return result_type(act);
  87. }
  88. };
  89. } // namespace aux
  90. /*!
  91. * CSV-style decorator generator object. The decorator doubles double quotes that may be found
  92. * in the output. See http://en.wikipedia.org/wiki/Comma-separated_values for more information on
  93. * the CSV format. The generator provides <tt>operator[]</tt> that can be used to construct
  94. * the actual decorator. For example:
  95. *
  96. * <code>
  97. * csv_decor[ stream << attr< std::string >("MyAttr") ]
  98. * </code>
  99. *
  100. * For wide-character formatting there is the similar \c wcsv_decor decorator generator object.
  101. */
  102. #ifdef BOOST_LOG_USE_CHAR
  103. const aux::csv_decorator_gen< char > csv_decor = {};
  104. #endif
  105. #ifdef BOOST_LOG_USE_WCHAR_T
  106. const aux::csv_decorator_gen< wchar_t > wcsv_decor = {};
  107. #endif
  108. /*!
  109. * The function creates an CSV-style decorator generator for arbitrary character type.
  110. */
  111. template< typename CharT >
  112. BOOST_FORCEINLINE aux::csv_decorator_gen< CharT > make_csv_decor()
  113. {
  114. return aux::csv_decorator_gen< CharT >();
  115. }
  116. } // namespace expressions
  117. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  118. } // namespace boost
  119. #include <boost/log/detail/footer.hpp>
  120. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_