Subversion Repositories Kolibri OS

Rev

Rev 9089 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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