Subversion Repositories Kolibri OS

Rev

Rev 8396 | Rev 8440 | 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.         char params[6000];
  7.         bool opened;
  8.         collection attributes;
  9.         collection values;
  10.         dword value;
  11.         bool is();
  12.         bool parse_tag();
  13.         void debug_tag();
  14.         bool get_next_param();
  15.         dword get_value_of();
  16. } tag=0;
  17.  
  18. bool _tag::is(dword _text)
  19. {
  20.         if ( !strcmp(#name, _text) ) {
  21.                 return true;
  22.         } else {
  23.                 return false;
  24.         }
  25. }
  26.  
  27. bool _tag::parse_tag(dword _bufpos, bufend)
  28. {
  29.         bool retok = true;
  30.         dword bufpos = ESDWORD[_bufpos];
  31.  
  32.         dword closepos;
  33.         dword whitepos;
  34.  
  35.         if (name) strcpy(#prior, #name); else prior = '\0';
  36.         name = '\0';
  37.         params = '\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.                 goto _RET;
  51.         }
  52.  
  53.         if (ESBYTE[bufpos] == '/') {
  54.                 opened = false;
  55.                 bufpos++;
  56.         } else {
  57.                 opened = true;
  58.         }
  59.  
  60.         closepos = strchr(bufpos, '>');
  61.         whitepos = strchrw(bufpos, bufend-bufpos);
  62.         if (whitepos > closepos) {
  63.                 //no param
  64.                 strncpy(#name, bufpos, math.min(closepos - bufpos, sizeof(tag.name)));
  65.                 debug_tag();
  66.                 params = '\0';
  67.                 bufpos = closepos;
  68.         } else {
  69.                 //we have param
  70.                 strncpy(#name, bufpos, math.min(whitepos - bufpos, sizeof(tag.name)));
  71.                 strncpy(#params, whitepos, math.min(closepos - whitepos, sizeof(tag.params)));
  72.                 debug_tag();
  73.                 bufpos = closepos;
  74.                 while (get_next_param());
  75.         }
  76.  
  77.         if (!name) {
  78.                 retok = false;
  79.                 goto _RET;
  80.         }
  81.  
  82.         strlwr(#name);
  83.  
  84.         // ignore text inside the next tags
  85.         if (is("script")) || (is("style")) || (is("binary")) || (is("select")) {
  86.                 strcpy(#prior, #name);
  87.                 sprintf(#name, "</%s>", #prior);
  88.                 if (strstri(bufpos, #name)) bufpos = EAX-1;
  89.                 retok = false;
  90.                 goto _RET;
  91.         }
  92.  
  93.         if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for <br/>
  94.  
  95. _RET:
  96.         ESDWORD[_bufpos] = bufpos;
  97.         return retok;
  98. }
  99.  
  100. void _tag::debug_tag()
  101. {
  102.         if (debug_mode) {
  103.                 debugch('<');
  104.                 if (!opened) debugch('/');
  105.                 debug(#name);
  106.                 debugln(">");
  107.                 if (params) {
  108.                         debug("params: ");
  109.                         debugln(#params+1);
  110.                 }
  111.         }
  112. }
  113.  
  114. bool _tag::get_next_param()
  115. {
  116.         byte  quotes = NULL;
  117.         int   i;
  118.         unsigned char  val[6000];
  119.         unsigned char attr[6000];
  120.  
  121.         if (!params) return false;
  122.        
  123.         i = strlen(#params) - 1;
  124.         if (params[i] == '/') i--;
  125.         while (i>0) && (__isWhite(params[i])) i--;
  126.  
  127.         if (params[i] == '"') || (params[i] == '\'')
  128.         {
  129.                 //remove quotes
  130.                 quotes = params[i];
  131.                 params[i] = '\0';
  132.                 i--;
  133.  
  134.                 //find VAL start and copy
  135.                 i = strrchr(#params, quotes);
  136.                 strlcpy(#val, #params + i, sizeof(val)-1);
  137.                 params[i] = '\0';
  138.                 i--;
  139.  
  140.                 //find ATTR end
  141.                 while (i > 0) && (params[i] != '=') i--;
  142.                 params[i+1] = '\0';
  143.         }
  144.         else
  145.         {
  146.                 //find VAL start and copy
  147.                 while (i > 0) && (params[i] != '=') i--;
  148.                 i++;
  149.                 strlcpy(#val, #params + i, sizeof(val)-1);
  150.  
  151.                 //already have ATTR end
  152.         }
  153.  
  154.         //find ATTR start and copy
  155.         while (i>0) && (!__isWhite(params[i])) i--;
  156.         strlcpy(#attr, #params + i + 1, sizeof(attr)-1);
  157.         params[i] = '\0';
  158.  
  159.         //fix case: src=./images/KolibriOS_logo2.jpg?sid=e8ece8b38b
  160.         i = strchr(#attr,'=');
  161.         if (!quotes) && (i) {
  162.                 strlcpy(#val, i+1, sizeof(val)-1);
  163.                 ESBYTE[i+1] = '\0';
  164.         }
  165.         strlwr(#attr);
  166.         strrtrim(#val);
  167.  
  168.         attributes.add(#attr);
  169.         values.add(#val);
  170.  
  171.         if (debug_mode) {
  172.                 debug("atr: "); debugln(#attr);
  173.                 debug("val: "); debugln(#val);
  174.                 debugch('\n');
  175.         }
  176.  
  177.         return true;
  178. }
  179.  
  180. dword _tag::get_value_of(dword _attr_name)
  181. {
  182.         int pos = attributes.get_pos_by_name(_attr_name);
  183.         if (pos == -1) {
  184.                 value = 0;
  185.         } else {
  186.                 value = values.get(pos);
  187.         }
  188.         return value;
  189. }
  190.