sax.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "sax.h"
  2. using namespace NConfig;
  3. namespace {
  4. class TSax: public IConfig, public IConfig::IValue {
  5. public:
  6. inline TSax(const TConfig& cfg)
  7. : C_(cfg)
  8. {
  9. }
  10. void DoForEach(IFunc* func) override {
  11. if (C_.IsA<TArray>()) {
  12. const TArray& a = C_.Get<TArray>();
  13. for (size_t i = 0; i < a.size(); ++i) {
  14. TSax slave(a[i]);
  15. func->Consume(ToString(i), &slave);
  16. }
  17. } else if (C_.IsA<TDict>()) {
  18. const TDict& d = C_.Get<TDict>();
  19. for (const auto& it : d) {
  20. TSax slave(it.second);
  21. func->Consume(it.first, &slave);
  22. }
  23. }
  24. }
  25. TString AsString() override {
  26. if (C_.IsA<TArray>()) {
  27. TSax slave(C_.Get<TArray>()[0]);
  28. return slave.AsString();
  29. }
  30. return C_.As<TString>();
  31. }
  32. bool AsBool() override {
  33. return C_.As<bool>();
  34. }
  35. IConfig* AsSubConfig() override {
  36. return this;
  37. }
  38. bool IsContainer() const override {
  39. return C_.IsA<TArray>() || C_.IsA<TDict>();
  40. }
  41. void DumpJson(IOutputStream& stream) const override {
  42. C_.DumpJson(stream);
  43. }
  44. void DumpLua(IOutputStream& stream) const override {
  45. C_.DumpLua(stream);
  46. }
  47. private:
  48. TConfig C_;
  49. };
  50. }
  51. namespace NConfig {
  52. THolder<IConfig> ConfigParser(IInputStream& in, const TGlobals& globals) {
  53. return MakeHolder<TSax>(TConfig::FromStream(in, globals));
  54. }
  55. }