4d14a7ffae2c936ffe176bbc39f9e3c95c22e392
[com/gs-lite.git] / src / tools / xml_t.h
1 /* ------------------------------------------------\r
2 Copyright 2014 AT&T Intellectual Property\r
3    Licensed under the Apache License, Version 2.0 (the "License");\r
4    you may not use this file except in compliance with the License.\r
5    You may obtain a copy of the License at\r
6 \r
7      http://www.apache.org/licenses/LICENSE-2.0\r
8 \r
9    Unless required by applicable law or agreed to in writing, software\r
10    distributed under the License is distributed on an "AS IS" BASIS,\r
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12    See the License for the specific language governing permissions and\r
13    limitations under the License.\r
14  ------------------------------------------- */\r
15 \r
16 #ifndef __XML_T_DEFINED_\r
17 #define __XML_T_DEFINED_\r
18 \r
19 #include <stdio.h>\r
20 #include<vector>\r
21 #include<string>\r
22 #include<map>\r
23 #include<set>\r
24 #include<algorithm>\r
25 \r
26 \r
27 void xmlParser_setfileinput(FILE *f);\r
28 void xmlParser_setstringinput(char *s);\r
29 \r
30 \r
31 \r
32 struct name_val_pair_t{\r
33         std::string name;\r
34         std::string val;\r
35 \r
36         name_val_pair_t(const char *n, const char *v){\r
37                 name = n;\r
38                 val = v;\r
39         }\r
40 };\r
41 \r
42 struct name_val_list_t{\r
43         std::vector<name_val_pair_t *> nvp_list;\r
44 \r
45         name_val_list_t(){\r
46         }\r
47 \r
48         name_val_list_t(name_val_pair_t *nvp){\r
49                 nvp_list.push_back(nvp);\r
50         }\r
51 \r
52         name_val_list_t *append(name_val_pair_t *nvp){\r
53                 nvp_list.push_back(nvp);\r
54                 return this;\r
55         }\r
56 };\r
57 \r
58 struct tag_t{\r
59         std::string name;\r
60         std::vector<name_val_pair_t *> nvp_list;\r
61 \r
62         tag_t(const char *n, name_val_list_t *nvl){\r
63                 name=n;\r
64                 nvp_list = nvl->nvp_list;\r
65         }\r
66 };\r
67 \r
68 \r
69 struct xml_list_t;\r
70 \r
71 struct xml_t{\r
72         std::string name;\r
73         std::string end_tag_name;\r
74         std::vector<name_val_pair_t *> attribs;\r
75 \r
76         std::vector<xml_t *> subtrees;\r
77 \r
78         xml_t(){}\r
79 \r
80         xml_t(tag_t *s, xml_list_t *xl,const char *e);\r
81 \r
82         xml_t(tag_t *s, char *e){\r
83                 name=s->name;\r
84                 attribs = s->nvp_list;\r
85                 end_tag_name = e;\r
86         }\r
87 \r
88         xml_t(const char *n, name_val_list_t *nvl){\r
89                 name=n;\r
90                 attribs=nvl->nvp_list;\r
91         }\r
92 \r
93         std::string to_string(){\r
94                 return to_string("");\r
95         }\r
96         std::string to_string(std::string indent){\r
97                 std::string ret;\r
98                 std::string whitespace;\r
99                 int i;\r
100 \r
101                 ret += indent+"<"+name;\r
102                 for(i=0;i<attribs.size();++i)\r
103                         ret += " "+attribs[i]->name+"="+attribs[i]->val;\r
104 \r
105                 if(subtrees.size()>0){\r
106                         ret+=">\n";\r
107                         for(i=0;i<subtrees.size();++i){\r
108                                 ret += subtrees[i]->to_string(indent+"  ");\r
109                         }\r
110                         ret += indent + "</"+name+">\n";\r
111                 }else{\r
112                         ret+=">\n";\r
113                 }\r
114                 return ret;\r
115         }\r
116 \r
117         void get_roots(std::string type, std::vector<xml_t *> &ret);\r
118         void get_roots(std::set<std::string> type, std::vector<xml_t *> &ret);\r
119 \r
120         bool get_attrib_val(std::string name, std::string &val){\r
121                 val="";\r
122                 int i;\r
123                 for(i=0;i<attribs.size();++i){\r
124                         if(attribs[i]->name == name){\r
125                                 val = attribs[i]->val;\r
126                                 return true;\r
127                         }\r
128                 }\r
129                 return false;\r
130         }\r
131 \r
132 };\r
133 \r
134 struct xml_list_t{\r
135         std::vector<xml_t *> xlist;\r
136 \r
137         xml_list_t(xml_t *x){\r
138                 xlist.push_back(x);\r
139         }\r
140 \r
141         xml_list_t *append(xml_t *x){\r
142                 xlist.push_back(x);\r
143                 return this;\r
144         }\r
145 };\r
146 \r
147 #endif\r
148 \r