rendered paste body#include <iostream>#include <string>#include <stack>#include <fstream>bool testNumeral(char *str, int n){ const char num[17] = "0123456789absdef"; char num_n[17]; strncpy(num_n, num, n); num_n[n]='\0'; //std::cout<<num_n<<endl; if (strspn(str, num_n) == strlen(str)) return true; else return false;}bool testNumber(char *str){ char c[] = {'@', '#', '$'}; int n[] = { 2 , 10, 16}; int i, count=3; for (i=0; i<count; i++) if (c[i]==str[0]) break; str++; if (i<count) return testNumeral(str, n[i]); else return false;}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_tag(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: no open tag!"<<std::endl; tags.pop(); } else { std::cout<<"tags error: wrong close tag!"<<std::endl; } } } } if (!tags.empty()) { std::cout<<"tags error: no close tag!"<<std::endl; } std::cout<<"comment: "<<comment<<std::endl; } void validate_ABC(const text_buffer & text) { while (!feof(pfile)) { c = getc(pfile); if (!strchr(An, c)) printf("Error ABC \n", c); } } void validate_num(const text_buffer & text) { } };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_ABC(text); val.validate_tag(text); val.validate_num(text); return 0;}