]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig-ng/root_parser.cpp
Merge branch 'naffanya-dev'
[stg.git] / projects / stargazer / plugins / configuration / sgconfig-ng / root_parser.cpp
1 #include <cassert>
2
3 #include "common.h"
4
5 // TODO: Fix this shit!
6 #include "../../../tariffs.h"
7 #include "../../../users.h"
8 #include "../../../admin.h"
9 #include "../../../settings.h"
10
11 #include "parser_info.h"
12 #include "parser_getuser.h"
13 #include "parser_getusers.h"
14 #include "root_parser.h"
15
16 ROOT_PARSER::ROOT_PARSER(const ADMIN * ca, TARIFFS * t, USERS * u, const SETTINGS * s)
17     : PARSER(),
18       tariffs(t),
19       users(u),
20       currAdmin(ca),
21       settings(s),
22       handler(NULL),
23       depth(0),
24       handlerResult("<error message=\"Not implemented yet\"/>")
25 {
26     // new, new, new...
27     handlers["GetServerInfo"] = new PARSER_GET_SERVER_INFO(settings,
28                                           tariffs->GetTariffsNum(),
29                                           users->GetUserNum());
30     handlers["GetUser"] = new PARSER_GET_USER(currAdmin, users);
31     handlers["GetUsers"] = new PARSER_GET_USERS(currAdmin, users);
32 }
33
34 ROOT_PARSER::~ROOT_PARSER()
35 {
36     std::map<std::string, PARSER *>::iterator it;
37
38     for (it = handlers.begin(); it != handlers.end(); ++it) {
39         delete it->second;
40     }
41 }
42
43 bool ROOT_PARSER::StartTag(const char * name, const char ** attr)
44 {
45     if (depth == 0) {
46         handlerResult = "<error message=\"Not implemented yet\"/>";
47         Dispatch(name);
48     }
49
50     ++depth;
51
52     //assert(handler != NULL);
53
54     if (handler != NULL)
55         return handler->StartTag(name, attr);
56     else
57         return false;
58 }
59
60 bool ROOT_PARSER::EndTag(const char * name)
61 {
62     assert(depth > 0);
63
64     bool res;
65     if (handler != NULL)
66        res = handler->EndTag(name);
67     else
68        res = false;
69
70     --depth;
71
72     if (depth == 0) {
73         if (handler != NULL) {
74             handlerResult = handler->GetResult();
75         }
76         handler = NULL;
77     }
78
79     return res;
80 }
81
82 bool ROOT_PARSER::Dispatch(const std::string & name)
83 {
84     HMAP_ITERATOR it(handlers.find(name));
85
86     if (it == handlers.end()) {
87         printfd(__FILE__, "ROOT_PARSER::Dispatch() Handler for '%s' not found.\n", name.c_str());
88         return false;
89     }
90
91     handler = it->second;
92
93     return true;
94 }