Riremito

html parser/Parser.cpp

Apr 14th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include<Windows.h>
  2. #include<stdio.h>
  3. #include"html.h"
  4.  
  5. HTMLParser::HTMLParser(char *src){
  6.     int length = strlen(src);
  7.     html = (char *)malloc(length + 1);
  8.     memcpy(html, src, length);
  9.     html[length] = 0;
  10.     htlist = NULL;
  11.     htp = NULL;
  12.     hti = NULL;
  13.     Search();
  14. }
  15.  
  16. HTMLParser::~HTMLParser(){
  17.     free(html);
  18.     HTMLTag *ntag = hti;
  19.     while(ntag){
  20.         hti = ntag;
  21.         ntag = ntag->GetNext();
  22.         delete hti;
  23.     }
  24.     hti = NULL;
  25. }
  26.  
  27. void HTMLParser::Search(int d){
  28.     char *pp, *pn;
  29.     pp = strstr(&html[d], "<");
  30.     if(!pp){
  31.         //printf("Search:Exit\n");
  32.         return;
  33.     }
  34.     else if(strncmp(pp, "<!--", 4) == 0){
  35.         pn = strstr(pp, "-->");
  36.         if(!pn){
  37.             //printf("Search:Error(Comment)\n");
  38.             return;
  39.         }
  40.         else{
  41.             return Search(pn + strlen("-->") - html);
  42.         }
  43.     }
  44.     else{
  45.         pn = strstr(pp, ">");
  46.         if(!pn){
  47.             //printf("Search:Error(Tag)\n");
  48.             return;
  49.         }
  50.         else{
  51.             pp++;
  52.             pn--;
  53.             HTMLTag *ht = new HTMLTag(pp, pn);
  54.             if(ht->GetTag()[0] != '/'){
  55.                 char *stag = (char *)malloc(strlen(ht->GetTag()) + 4);
  56.                 sprintf(stag, "</%s>", ht->GetTag());
  57.                 char *st = &pn[2], *et = strstr(pn, stag);
  58.                 if(et){
  59.                     ////////////////////////
  60.                     int i, j, length;
  61.                     bool btext = true;
  62.                     char *text;
  63.                     length = 0;
  64.                     for(i=0; &st[i] < et; i++){
  65.                         switch(st[i]){
  66.                         case '\t':
  67.                         case '\n':
  68.                         case '\r':
  69.                         case ' ':
  70.                             break;
  71.                         case '<':
  72.                             btext = false;
  73.                             break;
  74.                         case '>':
  75.                             btext = true;
  76.                             break;
  77.                         default:
  78.                             length++;
  79.                             break;
  80.                         }
  81.                     }
  82.                     text = (char *)malloc(length + 1);
  83.                     j = 0;
  84.                     for(i=0; &st[i] < et; i++){
  85.                         switch(st[i]){
  86.                         case '\t':
  87.                         case '\n':
  88.                         case '\r':
  89.                         case ' ':
  90.                             break;
  91.                         case '<':
  92.                             btext = false;
  93.                             break;
  94.                         case '>':
  95.                             btext = true;
  96.                             break;
  97.                         default:
  98.                             if(btext){
  99.                                 text[j++] = st[i];
  100.                             }
  101.                             break;
  102.                         }
  103.                     }
  104.                     text[j] = 0;
  105.                     if(j){
  106.                         ht->SetText(text);
  107.                     }
  108.                     free(text);
  109.                     //////////////////////////////
  110.                     if(!htlist){
  111.                         htlist = ht;
  112.                         hti = ht;
  113.                     }
  114.                     else{
  115.                         ht->SetParent(htp);
  116.                         htlist->SetNext(ht);
  117.                         htp = ht;
  118.                         htlist = ht;
  119.                     }
  120.                 }
  121.                 else{
  122.                     if(!htlist){
  123.                         htlist = ht;
  124.                         hti = ht;
  125.                     }
  126.                     else{
  127.                         ht->SetParent(htp);
  128.                         htlist->SetNext(ht);
  129.                         htlist = ht;
  130.                     }
  131.                 }
  132.                 free(stag);
  133.             }
  134.             else{
  135.                 if(htp){
  136.                     htp = htp->GetParent();
  137.                     ht->SetParent(htp);
  138.                     htlist->SetNext(ht);
  139.                     htlist = ht;
  140.                 }
  141.             }
  142.             return Search(pn + strlen("<") - html);
  143.         }
  144.     }
  145. }
  146.  
  147. void HTMLParser::ShowTree(){
  148.     HTMLTag *ht = hti;
  149.     while(ht){
  150.         htp = ht->GetParent();
  151.         while(htp){
  152.             printf("_");
  153.             htp = htp->GetParent();
  154.         }
  155.         printf("<%s>\n", ht->GetTag());
  156.         ht = ht->GetNext();
  157.     }
  158. }
  159. ///////////////////////////////////////////
  160. class CharPlus{
  161. private:
  162.     int count;
  163.     char *target[16];
  164. public:
  165.     CharPlus();
  166.     ~CharPlus();
  167.     void Add(char *s);
  168.     int GetCount();
  169.     char* GetChar(int d);
  170. };
  171.  
  172. CharPlus::CharPlus(){
  173.     count = 0;
  174. }
  175. CharPlus::~CharPlus(){
  176.     int i;
  177.     for(i=0; i<count; i++){
  178.         free(target[i]);
  179.     }
  180. }
  181. void CharPlus::Add(char *s){
  182.     int length = strlen(s);
  183.     target[count] = (char *)malloc(length + 1);
  184.     memcpy(target[count], s, length);
  185.     target[count++][length] = 0;
  186. }
  187. int CharPlus::GetCount(){
  188.     return count;
  189. }
  190. char* CharPlus::GetChar(int d){
  191.     return target[d];
  192. }
  193. ////////////////////////////////////////////
  194. HTMLTag* HTMLParser::Find(char *path, int d){
  195.     char *sp, *tp, *cpath;
  196.     CharPlus cp;
  197.     int i, count, tcount = 0;
  198.     HTMLTag *ht = hti;
  199.     cpath = (char *)malloc(strlen(path) + 1);
  200.     memcpy(cpath, path, strlen(path) + 1);
  201.     do{
  202.         sp = cpath;
  203.         do{
  204.             tp = strstr(sp, "->");
  205.             if(tp){
  206.                 sp = tp + strlen("->");
  207.             }
  208.         }while(tp);
  209.         cp.Add(sp);
  210.         if(sp != cpath){
  211.             sp[-strlen("->")] = 0;
  212.         }
  213.     }while(sp != cpath);
  214.     free(cpath);
  215.     count = cp.GetCount();
  216.     do{
  217.         HTMLTag *htp = ht;
  218.         for(i=0; i<count && htp; i++){
  219.             if(strstr(cp.GetChar(i), " ")?strstr(htp->GetFullTag(), cp.GetChar(i)):strcmp(htp->GetTag(), cp.GetChar(i)) == 0){
  220.                 if(i == count -1){
  221.                     if(tcount == d){
  222.                         return ht;
  223.                     }
  224.                     else{
  225.                         tcount++;
  226.                     }
  227.                 }
  228.                 htp = htp->GetParent();
  229.             }
  230.             else{
  231.                 break;
  232.             }
  233.         }
  234.         ht = ht->GetNext();
  235.     }while(ht);
  236.     return 0;
  237. }
  238.  
  239. HTMLTag * HTMLParser::GetTagList(){
  240.     return hti;
  241. }
Add Comment
Please, Sign In to add comment