rendered paste body#include <iostream>#include <string>#include <stack>#include <fstream>class text_buffer{ std::string m_text;public: typedef std::string::const_iterator const_iterator; const_iterator begin() const {return m_text.begin();} const_iterator end() const {return m_text.end();} bool read_from_file(const std::string & file_name) { m_text.clear(); std::ifstream istr(file_name.c_str(),std::ios::binary); if(!istr) { std::cerr << "can't open input file:" << file_name << std::endl; return false; } int n = 0; char buf[1024]; while(n = istr.readsome(buf,1024)) m_text.append(buf,buf+n); return true; } };class validator{ std::string tgOpen; std::string tgClose;public: bool init(const std::string & tag_file) { std::ifstream istr(tag_file.c_str()); if(!istr) { std::cerr << "can't open tag's file " << std::endl; return false; } std::getline(istr,tgOpen); std::getline(istr,tgClose); if(tgOpen.size() != tgClose.size()) { std::cerr << "tag's file has incorrect format" << std::endl; return false; } return true; } void validate(const text_buffer & text) { bool comment = false; std::stack<char> tags; text_buffer::const_iterator cur = text.begin(), end = text.end(); for (;cur != end;++cur) { if (*cur == '!') comment=!comment; if (!comment) { if( tgOpen.find(*cur) != std::string::npos) { std::cout << *cur << std::endl; tags.push(*cur); continue; } int n; if( (n = tgClose.find(*cur)) != std::string::npos) { std::cout << *cur << std::endl; if (!tags.empty()) { if (tgOpen[n] != tags.top()) std::cout<<"tags error 1!"<<std::endl; tags.pop(); } else { std::cout<<"tags error 2!"<<std::endl; } } } } if (!tags.empty()) { std::cout<<"tags error 3!"<<std::endl; } std::cout<<"comment: "<<comment<<std::endl; } };int main(int argc, char ** argv){ if(argc != 3) { std::cerr <<"Usage: validator <text_file> <tag_file>" << std::endl; return 1; } text_buffer text; if(!text.read_from_file(argv[1])) { return 1; } validator val; if(!val.init(argv[2])) { return 1; } val.validate(text); return 0;}