In this example:
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <sstream>
struct tag {
bool isOpening;
std::string name;
};
void print(const std::deque<tag> &deq){
for(const auto &i : deq){
std::cout << i.name << std::endl;
}
}
int main() {
// std::ifstream infile("a.html");
// std::stringstream buffer;
// buffer << infile.rdbuf();
// std::string buf = std::move(buffer.str());
std::deque<tag> deq;
std::string buf = "<html>\n"
"\n"
"<body>\n"
" <h1>Hello <b>world</b></h1>\n"
"</body>\n"
"\n"
"</html>";
size_t pos = 0;
while ((pos = buf.find('<', pos)) != std::string::npos) {
bool isOpening = true;
if(buf[pos+1] == '/'){
isOpening = false;
pos++;
}
std::string token = buf.substr(pos, buf.find('>', pos));
deq.push_back({isOpening, token});
pos++;
}
print(deq);
}
I would like to parse for each tag its name and whether it is opening or not. But the output is:
<html
<body>
<h
<h1>Hello <b>world</b>
<b>world</b></h1>
</body>
</ht
/b></h1>
</body>
</html>
/h1>
</body>
</html>
/body>
</html>
/html>
which is of course not correct. So what am I doing wrong?