Subversion Repositories Kolibri OS

Rev

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

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