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