Subversion Repositories Kolibri OS

Rev

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