You are on page 1of 2

1 /*1) Create a tag struct, that contains:

2
3 i) name
4
5 ii) a map for tags
6
7 iii) a map for attributes/values
8
9 iv) pointer to parent tag
10 2) Create a master map, that contains all first level tags
11
12 3) Create a current tag so that we can nest into and close them out.
13
14 */
15
16 #include <cstdio>
17 #include <iostream>
18 #include <sstream>
19 #include <map>
20 using namespace std;
21
22 struct tagobj {
23 string name;
24 map<string, tagobj*> tmap;
25 map<string, string> map_att;
26 tagobj* prev = NULL;
27 };
28
29 void delete_tags(tagobj* tag) {
30 for (map<string, tagobj*>::iterator i = tag->tmap.begin() ; i != tag->tmap.end() ; i
++ ) {
31 delete_tags(i->second);
32 }
33 delete(tag);
34 }
35
36 int main(){
37 //Setup a vector holding all first level tags
38 map<string, tagobj*> main;
39 //Current depth so as to nest
40 tagobj* masterTag = NULL;
41
42 int tlines, qlines;
43 string ints;
44 getline(cin, ints);
45 stringstream ss(ints);
46 ss >> tlines;
47 ss >> qlines;
48
49 string line;
50 size_t del_tag;
51 //Save the information
52 for(int i=0; i<tlines; i++) {
53 //Get Tag from line
54 getline(cin, line);
55 del_tag = line.find_first_of(" >");
56 string tag = line.substr(1,del_tag-1);
57 //opening tags
58 if(tag[0] != '/') {
59 //Create Struct
60 tagobj* currTag = new tagobj();
61 currTag->prev = masterTag;
62 currTag->name = tag;
63 //check if we add to master vector or previous:
64 if(!masterTag) {
65 main[currTag->name] = currTag;
66 } else {
67 masterTag->tmap[currTag->name] = currTag;
68 }
69 masterTag = currTag;
70 //Add all attributes
71 size_t found = line.find("=", del_tag);
72 while(found != string::npos) {
73 //Get the attribute
74 string name = line.substr(del_tag + 1, found-del_tag-2);
75 //move past the =
76 del_tag = found + 2;
77 found = line.find("\"", del_tag + 1);
78 //Copy the value
79 string value = line.substr(del_tag + 1,found - del_tag - 1);
80 //add to map
81 currTag->map_att[name]=value;
82 //keep going
83 del_tag = line.find(" ", found);
84 found = line.find("=", found);
85 }
86 } else {
87 //If its a closing Tag return main tag to its previous
88 masterTag = masterTag->prev;
89 }
90 }
91
92 for(int i=0; i < qlines; i++) {
93 tagobj* masterq = NULL;
94 getline(cin,line);
95 size_t start = 0;
96 string val;
97 bool fnd;
98 while (true) {
99 size_t found = line.find_first_of(".~", start);
100 string buff = line.substr(start,found-start);
101 fnd = false;
102 //looking for attribute
103 if(start > 0 && line[start-1] == '~') {
104 //Attributes have a sentinal
105 //Check for bad input
106 if(masterq) {
107 val = masterq->map_att[buff];
108 if(!val.empty()){
109 fnd = true;
110 }
111 break;
112 }
113 } else {
114 //Looking for a tag, if found, continue looking
115 map<string, tagobj*> *bufobj = NULL;
116 //Make sure to look in right place
117 if(masterq) {
118 bufobj = &(masterq->tmap);
119 } else {
120 bufobj = &main;
121 }
122 tagobj* frommap = (*bufobj)[buff];
123 if(frommap) {
124 masterq = frommap;
125 } else {
126 break;
127 }
128 }
129 start = found + 1;
130 }
131 //Printing Time:
132 if(fnd) {
133 cout << val << endl;
134 } else {
135 cout << "Not Found!" << endl;
136 }
137 }
138 for (map<string, tagobj*>::iterator i = main.begin() ; i != main.end() ; i ++ ) {
139 delete_tags(i->second);
140 }
141 }
142

You might also like