config_file.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright Vladimir Prus 2002-2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_CONFIG_FILE_VP_2003_01_02
  6. #define BOOST_CONFIG_FILE_VP_2003_01_02
  7. #include <iosfwd>
  8. #include <string>
  9. #include <set>
  10. #include <boost/noncopyable.hpp>
  11. #include <boost/program_options/config.hpp>
  12. #include <boost/program_options/option.hpp>
  13. #include <boost/program_options/eof_iterator.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #include <boost/program_options/detail/convert.hpp>
  16. #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
  17. #include <istream> // std::getline
  18. #endif
  19. #include <boost/static_assert.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #ifdef BOOST_MSVC
  23. # pragma warning(push)
  24. # pragma warning(disable: 4251) // class XYZ needs to have dll-interface to be used by clients of class XYZ
  25. #endif
  26. namespace boost { namespace program_options { namespace detail {
  27. /** Standalone parser for config files in ini-line format.
  28. The parser is a model of single-pass lvalue iterator, and
  29. default constructor creates past-the-end-iterator. The typical usage is:
  30. config_file_iterator i(is, ... set of options ...), e;
  31. for(; i !=e; ++i) {
  32. *i;
  33. }
  34. Syntax conventions:
  35. - config file can not contain positional options
  36. - '#' is comment character: it is ignored together with
  37. the rest of the line.
  38. - variable assignments are in the form
  39. name '=' value.
  40. spaces around '=' are trimmed.
  41. - Section names are given in brackets.
  42. The actual option name is constructed by combining current section
  43. name and specified option name, with dot between. If section_name
  44. already contains dot at the end, new dot is not inserted. For example:
  45. @verbatim
  46. [gui.accessibility]
  47. visual_bell=yes
  48. @endverbatim
  49. will result in option "gui.accessibility.visual_bell" with value
  50. "yes" been returned.
  51. TODO: maybe, we should just accept a pointer to options_description
  52. class.
  53. */
  54. class BOOST_PROGRAM_OPTIONS_DECL common_config_file_iterator
  55. : public eof_iterator<common_config_file_iterator, option>
  56. {
  57. public:
  58. common_config_file_iterator() { found_eof(); }
  59. common_config_file_iterator(
  60. const std::set<std::string>& allowed_options,
  61. bool allow_unregistered = false);
  62. virtual ~common_config_file_iterator() {}
  63. public: // Method required by eof_iterator
  64. void get();
  65. #if BOOST_WORKAROUND(_MSC_VER, <= 1900)
  66. void decrement() {}
  67. void advance(difference_type) {}
  68. #endif
  69. protected: // Stubs for derived classes
  70. // Obtains next line from the config file
  71. // Note: really, this design is a bit ugly
  72. // The most clean thing would be to pass 'line_iterator' to
  73. // constructor of this class, but to avoid templating this class
  74. // we'd need polymorphic iterator, which does not exist yet.
  75. virtual bool getline(std::string&) { return false; }
  76. private:
  77. /** Adds another allowed option. If the 'name' ends with
  78. '*', then all options with the same prefix are
  79. allowed. For example, if 'name' is 'foo*', then 'foo1' and
  80. 'foo_bar' are allowed. */
  81. void add_option(const char* name);
  82. // Returns true if 's' is a registered option name.
  83. bool allowed_option(const std::string& s) const;
  84. // That's probably too much data for iterator, since
  85. // it will be copied, but let's not bother for now.
  86. std::set<std::string> allowed_options;
  87. // Invariant: no element is prefix of other element.
  88. std::set<std::string> allowed_prefixes;
  89. std::string m_prefix;
  90. bool m_allow_unregistered;
  91. };
  92. template<class charT>
  93. class basic_config_file_iterator : public common_config_file_iterator {
  94. public:
  95. basic_config_file_iterator()
  96. {
  97. found_eof();
  98. }
  99. /** Creates a config file parser for the specified stream.
  100. */
  101. basic_config_file_iterator(std::basic_istream<charT>& is,
  102. const std::set<std::string>& allowed_options,
  103. bool allow_unregistered = false);
  104. private: // base overrides
  105. bool getline(std::string&);
  106. private: // internal data
  107. shared_ptr<std::basic_istream<charT> > is;
  108. };
  109. typedef basic_config_file_iterator<char> config_file_iterator;
  110. typedef basic_config_file_iterator<wchar_t> wconfig_file_iterator;
  111. struct null_deleter
  112. {
  113. void operator()(void const *) const {}
  114. };
  115. template<class charT>
  116. basic_config_file_iterator<charT>::
  117. basic_config_file_iterator(std::basic_istream<charT>& is,
  118. const std::set<std::string>& allowed_options,
  119. bool allow_unregistered)
  120. : common_config_file_iterator(allowed_options, allow_unregistered)
  121. {
  122. this->is.reset(&is, null_deleter());
  123. get();
  124. }
  125. // Specializing this function for wchar_t causes problems on
  126. // borland and vc7, as well as on metrowerks. On the first two
  127. // I don't know a workaround, so make use of 'to_internal' to
  128. // avoid specialization.
  129. template<class charT>
  130. bool
  131. basic_config_file_iterator<charT>::getline(std::string& s)
  132. {
  133. std::basic_string<charT> in;
  134. if (std::getline(*is, in)) {
  135. s = to_internal(in);
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141. // Specialization is needed to workaround getline bug on Comeau.
  142. #if BOOST_WORKAROUND(__COMO_VERSION__, BOOST_TESTED_AT(4303)) || \
  143. (defined(__sgi) && BOOST_WORKAROUND(_COMPILER_VERSION, BOOST_TESTED_AT(741)))
  144. template<>
  145. bool
  146. basic_config_file_iterator<wchar_t>::getline(std::string& s);
  147. #endif
  148. }}}
  149. #ifdef BOOST_MSVC
  150. # pragma warning(pop)
  151. #endif
  152. #endif