config_file.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef CONFIG_FILE_HPP
  2. #define CONFIG_FILE_HPP
  3. // Config Class
  4. // Author: Charles Gruenwald III
  5. /*! \file config_file.hpp
  6. * This file contains the class responsible for 'flat file' configurations.
  7. * It accepts files very similar to the windows .reg file format, as in sub-sections
  8. * are collapsed like the following:
  9. * \verbatim
  10. ---------------------
  11. [main]
  12. value_in_main = 1
  13. [main/subsection]
  14. value_in_sub = "asdf"
  15. "value with spaces" = 4
  16. --------------------- \endverbatim
  17. *
  18. * It uses boost::regex to perform the parsing. Note that comments are not preserved
  19. * across saves.
  20. */
  21. #include "config.hpp"
  22. #include "config_file_grammar.hpp"
  23. #include <fstream>
  24. // #define BOOST_SPIRIT_DEBUG // define this for debug output
  25. namespace config
  26. {
  27. #if (BOOST_VERSION==103500)
  28. using namespace boost::spirit;
  29. #else
  30. using namespace boost::spirit::classic;
  31. #endif
  32. /*! \brief ConfigFile: A flat-file interface for the Config Class.
  33. * This file contains the class that is used to interface a flat
  34. * file for config input / output. It uses boost::spirit for the
  35. * config grammar.
  36. *
  37. * The grammar is as follows: \verbatim
  38. config = key* >> section* >> end
  39. section = section_name >> key*
  40. key = key_name >> '=' >> value
  41. key_name = string!
  42. value = real | int | string
  43. section_name = '[' >> *lex_escape_ch_p >> ']'
  44. string = '"' >> *lex_escape_ch_p >> '"' | +(alnum | punct) >> *(alnum | punct ) \endverbatim
  45. */
  46. class ConfigFile : public config::Config
  47. {
  48. typedef tree_parse_info<config_parser::iterator_t, config_parser::factory_t> parse_info_t;
  49. typedef tree_match<config_parser::iterator_t, config_parser::factory_t> tree_match_t;
  50. typedef tree_match_t::node_t node_t;
  51. typedef tree_match_t::const_tree_iterator tree_iter_t;
  52. public:
  53. ConfigFile(bool case_sensitive = false);
  54. ConfigFile(const Section & root, bool case_sensitive = false);
  55. ~ConfigFile(){}
  56. void saveAs(const String &path);
  57. void loadConfigFromString(const String & cfg);
  58. private:
  59. void SaveTreeAs(std::ofstream &out, const Section &current);
  60. /*! loadConfig() Function which loads the Config tree with the entries extracted from the
  61. * file located at the m_path member variable.
  62. */
  63. void loadConfig();
  64. //! loadFileToString() Function that reads a given filename into a String
  65. void loadFileToString(String &s, const String &filename);
  66. //! parse() The function that is responsible for recursively building the tree
  67. void parse(const String &source, Section & current);
  68. void evalTree(Section & current, tree_iter_t const& node, int depth = 0);
  69. void showParseTree(tree_iter_t const& node, int depth = 0);
  70. // This function is used to turn \" into " and \\ into \ and the like
  71. void unEscapeText(const String & source, String & dest);
  72. void escapeText(const String & source, String & dest);
  73. String getNodeValue(tree_iter_t const& node);
  74. RuleID getNodeID(tree_iter_t const& node);
  75. //Hooks for creating appropriate objects when they are parse
  76. void createStringKey(Section & current, const String key_name, const String & value);
  77. void createIntKey(Section & current, const String key_name, SInt64 value);
  78. void createFloatKey(Section & current, const String key_name, double value);
  79. //The following code is commented out until full thread protection is provided
  80. //in the get/set functions as well as block writing, preferably through a shared
  81. //mutex.
  82. // boost::mutex m_fileWriterMux;
  83. };
  84. }
  85. #endif