CConfigHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //#define BOOST_SPIRIT_DEBUG
  2. #include "CConfigHandler.h"
  3. #include <boost/bind.hpp>
  4. #include <boost/spirit.hpp>
  5. #include <fstream>
  6. using namespace config;
  7. using namespace boost::spirit;
  8. CConfigHandler conf;
  9. struct lerror
  10. {
  11. std::string txt;
  12. lerror(const std::string & TXT):txt(TXT){};
  13. void operator()() const
  14. {
  15. tlog1 << txt << std::endl;
  16. }
  17. template<typename IteratorT>
  18. void operator()(IteratorT t1, IteratorT t2) const
  19. {
  20. tlog1 << txt << std::endl;
  21. }
  22. };
  23. struct lerror2
  24. {
  25. std::string txt;
  26. lerror2(const std::string & TXT):txt(TXT){};
  27. template<typename IteratorT>
  28. void operator()(IteratorT t1, IteratorT t2) const
  29. {
  30. std::string txt2(t1,t2);
  31. tlog1 << txt << txt2 << std::endl;
  32. }
  33. };
  34. //template <typename T, typename U>
  35. //struct AssignInAll
  36. //{
  37. // std::vector<T> &items;
  38. // U T::*pointer;
  39. // AssignInAll(std::vector<T> &Items, U T::*Pointer)
  40. // :items(Items),pointer(Pointer)
  41. // {}
  42. // void operator()(const U &as)
  43. // {
  44. // for(int i=0; i<items.size(); i++)
  45. // items[i].*pointer = U;
  46. // }
  47. //};
  48. struct SettingsGrammar : public grammar<SettingsGrammar>
  49. {
  50. template <typename ScannerT>
  51. struct definition
  52. {
  53. rule<ScannerT> r, clientOption, clientOptionsSequence;
  54. rule<ScannerT> GUIOption, GUIOptionsSequence, AdvMapOptionsSequence, AdvMapOption;
  55. definition(SettingsGrammar const& self)
  56. {
  57. clientOption
  58. = str_p("resolution=") >> (uint_p[assign_a(conf.cc.resx)] >> 'x' >> uint_p[assign_a(conf.cc.resy)] | eps_p[lerror("Wrong resolution!")])
  59. | str_p("port=") >> (uint_p[assign_a(conf.cc.port)] | eps_p[lerror("Wrong port!")])
  60. | str_p("bpp=") >> (uint_p[assign_a(conf.cc.bpp)] | eps_p[lerror("Wrong bpp!")])
  61. | str_p("localInformation=") >> (uint_p[assign_a(conf.cc.localInformation)] | eps_p[lerror("Wrong localInformation!")])
  62. | str_p("fullscreen=") >> (uint_p[assign_a(conf.cc.fullscreen)] | eps_p[lerror("Wrong fullscreen!")])
  63. | str_p("server=") >> ( ( +digit_p >> *('.' >> +digit_p) )[assign_a(conf.cc.server)] | eps_p[lerror("Wrong server!")])
  64. | str_p("defaultAI=") >> ((+(anychar_p - ';'))[assign_a(conf.cc.defaultAI)] | eps_p[lerror("Wrong defaultAI!")])
  65. | (+(anychar_p - '}'))[lerror2("Unrecognized client option: ")]
  66. ;
  67. clientOptionsSequence = *(clientOption >> (';' | eps_p[lerror("Semicolon lacking!")]));
  68. AdvMapOption
  69. = str_p("Buttons") >> ((ch_p('{') >> '}') | eps_p[lerror("Wrong Buttons!")])
  70. | str_p("Minimap : ") >>
  71. *(
  72. ( "width=" >> uint_p[assign_a(conf.gc.ac.minimap.w)]
  73. | "height=" >> uint_p[assign_a(conf.gc.ac.minimap.h)]
  74. | "x=" >> uint_p[assign_a(conf.gc.ac.minimap.x)]
  75. | "y=" >> uint_p[assign_a(conf.gc.ac.minimap.y)]
  76. )
  77. >> *ch_p(',')
  78. );
  79. AdvMapOptionsSequence = *(AdvMapOption >> (';' | eps_p[lerror("Semicolon lacking!")]));
  80. GUIOption = str_p("AdventureMap") >> (('{' >> AdvMapOptionsSequence >> '}') | eps_p[lerror("Wrong AdventureMap!")]);
  81. GUIOptionsSequence = *(GUIOption >> (';' | eps_p[lerror("Semicolon after GUIOption lacking!")]));
  82. r = str_p("clientSettings") >> (('{' >> clientOptionsSequence >> '}') | eps_p[lerror("Wrong clientSettings!")])
  83. >> str_p("GUISettings") >> (('{' >> GUIOptionsSequence >> '}') | eps_p[lerror("Wrong GUISettings!")]);
  84. #ifdef BOOST_SPIRIT_DEBUG
  85. BOOST_SPIRIT_DEBUG_RULE(clientOption);
  86. BOOST_SPIRIT_DEBUG_RULE(clientOptionsSequence);
  87. BOOST_SPIRIT_DEBUG_RULE(AdvMapOption);
  88. BOOST_SPIRIT_DEBUG_RULE(AdvMapOptionsSequence);
  89. BOOST_SPIRIT_DEBUG_RULE(GUIOption);
  90. BOOST_SPIRIT_DEBUG_RULE(GUIOptionsSequence);
  91. BOOST_SPIRIT_DEBUG_RULE(r);
  92. #endif
  93. }
  94. rule<ScannerT> const& start() const { return r; }
  95. };
  96. };
  97. struct CommentsGrammar : public grammar<CommentsGrammar>
  98. {
  99. template <typename ScannerT>
  100. struct definition
  101. {
  102. rule<ScannerT> comment;
  103. definition(CommentsGrammar const& self)
  104. {
  105. comment = comment_p("//") | comment_p("/*","*/") | space_p;
  106. BOOST_SPIRIT_DEBUG_RULE(comment);
  107. }
  108. rule<ScannerT> const& start() const { return comment; }
  109. };
  110. };
  111. CConfigHandler::CConfigHandler(void)
  112. {
  113. }
  114. CConfigHandler::~CConfigHandler(void)
  115. {
  116. }
  117. void config::CConfigHandler::init()
  118. {
  119. std::vector<char> settings;
  120. std::ifstream ifs("config/settings.txt");
  121. if(!ifs)
  122. {
  123. tlog1 << "Cannot open config/settings.txt !\n";
  124. return;
  125. }
  126. ifs.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
  127. std::copy(std::istream_iterator<char>(ifs),std::istream_iterator<char>(),std::back_inserter(settings));
  128. std::vector<char>::const_iterator first = settings.begin(), last = settings.end();
  129. SettingsGrammar sg;
  130. BOOST_SPIRIT_DEBUG_NODE(sg);
  131. CommentsGrammar cg;
  132. BOOST_SPIRIT_DEBUG_NODE(cg);
  133. parse_info<std::vector<char>::const_iterator> info = parse(first,last,sg,cg);
  134. if(!info.hit)
  135. tlog1 << "Cannot parse config/settings.txt file!\n";
  136. else if(!info.full)
  137. tlog2 << "Not entire config/settings.txt parsed!\n";
  138. }