config_file.hpp 5.8 KB

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