Subversion Repositories Kolibri OS

Rev

Rev 8445 | Rev 8500 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. struct _tag
  3. {
  4.         char name[32];
  5.         char prior[32];
  6.         bool opened;
  7.         collection attributes;
  8.         collection values;
  9.         dword value;
  10.         bool is();
  11.         bool parse();
  12.         void debug_tag();
  13.         dword get_next_param();
  14.         dword get_value_of();
  15. } tag=0;
  16.  
  17. bool _tag::is(dword _text)
  18. {
  19.         if ( !strcmp(#name, _text) ) {
  20.                 return true;
  21.         } else {
  22.                 return false;
  23.         }
  24. }
  25.  
  26. bool _tag::parse(dword _bufpos, bufend)
  27. {
  28.         bool retok = true;
  29.         dword bufpos = ESDWORD[_bufpos];
  30.         dword params, paramsend;
  31.  
  32.         dword closepos;
  33.         dword whitepos;
  34.         dword openpos;
  35.  
  36.         if (name) strcpy(#prior, #name); else prior = '\0';
  37.         name = '\0';
  38.         attributes.drop();
  39.         values.drop();         
  40.  
  41.         if (!strncmp(bufpos,"!--",3))
  42.         {
  43.                 bufpos+=3;
  44.                 //STRSTR
  45.                 while (strncmp(bufpos,"-->",3)!=0) && (bufpos < bufend)
  46.                 {
  47.                         bufpos++;
  48.                 }
  49.                 bufpos+=2;
  50.                 retok = false;
  51.                 goto _RET;
  52.         }
  53.  
  54.         if (ESBYTE[bufpos] == '/') {
  55.                 opened = false;
  56.                 bufpos++;
  57.         } else {
  58.                 opened = true;
  59.         }
  60.  
  61.         closepos = strchr(bufpos, '>');
  62.         whitepos = strchrw(bufpos, bufend-bufpos);
  63.  
  64.         if (debug_mode) {
  65.                 if (!closepos) debugln("null closepos");
  66.                 if (!whitepos) debugln("null whitepos");
  67.         }
  68.  
  69.         if (!whitepos) || (whitepos > closepos) {
  70.                 //no param
  71.                 strncpy(#name, bufpos, math.min(closepos - bufpos, sizeof(tag.name)));
  72.                 debug_tag();
  73.                 bufpos = closepos;
  74.         } else {
  75.                 //we have param
  76.                 while (chrlnum(whitepos, '\"', closepos - whitepos)%2) { //alt="Next>>"
  77.                         /*
  78.                         openpos = strchr(closepos+1, '<');
  79.                         closepos = strchr(closepos+1, '>');
  80.                         if (openpos) && (openpos < closepos) {
  81.                                 closepos = openpos - 1;
  82.                                 break;
  83.                         }
  84.                         */
  85.                         if (!openpos = strchr(closepos+1, '<')) break;
  86.                         if (openpos < strchr(closepos+1, '>')) break;
  87.                         if (!closepos = EAX) {closepos = bufend;break;}
  88.                 }
  89.                 strncpy(#name, bufpos, math.min(whitepos - bufpos, sizeof(tag.name)));
  90.                 debug_tag();
  91.                 bufpos = closepos;
  92.  
  93.                 params = malloc(closepos - whitepos + 1);
  94.                 strncpy(params, whitepos, closepos - whitepos);
  95.                 if (debug_mode) { debug("params: "); debugln(params+1); }
  96.                 paramsend = params + closepos - whitepos;
  97.                 while (paramsend = get_next_param(params, paramsend-1));
  98.                 free(params);
  99.         }
  100.  
  101.         if (name) {
  102.                 strlwr(#name);
  103.                 // ignore text inside the next tags
  104.                 if (is("script")) || (is("style")) || (is("binary")) || (is("select")) {
  105.                         strcpy(#prior, #name);
  106.                         sprintf(#name, "</%s>", #prior);
  107.                         if (strstri(bufpos, #name)) bufpos = EAX-1;
  108.                         retok = false;
  109.                 } else {
  110.                         if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for <br/>
  111.                 }
  112.         } else {
  113.                 retok = false;
  114.         }
  115.  
  116. _RET:
  117.         ESDWORD[_bufpos] = bufpos;
  118.         return retok;
  119. }
  120.  
  121. void _tag::debug_tag()
  122. {
  123.         if (debug_mode) {
  124.                 debugch('<');
  125.                 if (!opened) debugch('/');
  126.                 debug(#name);
  127.                 debugln(">");
  128.         }
  129. }
  130.  
  131. dword _tag::get_next_param(dword ps, pe)
  132. {
  133.         // "ps" - param start
  134.         // "pe" - param end
  135.         // "q"  - quote char
  136.         char q = NULL;
  137.         dword fixeq;
  138.         dword val;
  139.         dword attr;
  140.        
  141.         if (ESBYTE[pe] == '/') pe--;
  142.         while (pe>ps) && (__isWhite(ESBYTE[pe])) pe--;
  143.  
  144.         if (ESBYTE[pe] == '"') || (ESBYTE[pe] == '\'')
  145.         {
  146.                 //remove quote
  147.                 q = ESBYTE[pe];
  148.                 ESBYTE[pe] = '\0';
  149.                 pe--;
  150.  
  151.                 //find VAL start and copy
  152.                 pe = strrchr(ps, q) + ps;
  153.                 val = pe;
  154.                 pe--;
  155.                 ESBYTE[pe] = '\0';
  156.  
  157.                 //find ATTR end
  158.                 while (pe > ps) && (ESBYTE[pe] != '=') pe--;
  159.                 ESBYTE[pe] = '\0';
  160.         }
  161.         else
  162.         {
  163.                 //find VAL start and copy
  164.                 while (pe > ps) && (ESBYTE[pe] != '=') pe--;
  165.                 val = pe+1;
  166.                 ESBYTE[pe] = '\0';
  167.                 //already have ATTR end
  168.         }
  169.  
  170.         //find ATTR start and copy
  171.         while (pe>ps) && (!__isWhite(ESBYTE[pe])) pe--;
  172.         attr = pe + 1;
  173.         ESBYTE[pe] = '\0';
  174.  
  175.         // Fix case: src=./images/logo?sid=e8ece8b38b
  176.         // Exchange '=' and '\0' position.
  177.         // attr: src=./images/logo?sid   =>   src
  178.         // val:  e8ece8b38b              =>   ./images/logo?sid=e8ece8b38b
  179.         fixeq = strchr(attr,'=');
  180.         if (!q) && (fixeq) {
  181.                 ESBYTE[val-1] >< ESBYTE[fixeq];
  182.                 val = fixeq+1;
  183.         }
  184.         strlwr(attr);
  185.         strrtrim(val);
  186.  
  187.         attributes.add(attr);
  188.         values.add(val);
  189.  
  190.         if (debug_mode) {
  191.                 debug("atr: "); debugln(attr);
  192.                 debug("val: "); debugln(val);
  193.                 debugch('\n');
  194.         }
  195.  
  196.         if (pe==ps) return NULL;
  197.         return pe;
  198. }
  199.  
  200. dword _tag::get_value_of(dword _attr_name)
  201. {
  202.         int pos = attributes.get_pos_by_name(_attr_name);
  203.         if (pos == -1) {
  204.                 value = 0;
  205.         } else {
  206.                 value = values.get(pos);
  207.         }
  208.         return value;
  209. }
  210.