Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, October 16, 2026

Diff for libplaton/platon/html/escape.c between version 1.1 and 1.2

version 1.1, 2003/04/21 23:47:39 version 1.2, 2003/04/22 14:21:34
Line 1 
Line 1 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <ctype.h>  
   
 #include <platon/html/escape.h>  #include <platon/html/escape.h>
   
 struct html_escape_table html_escape_space[] = {  #define HTML_ESCAPE_TABLE_TERMINATOR { '\0', NULL }
   
   static struct html_escape_table html_escape_space[] = { /* {{{ */
         { ' ', "&nbsp;" },          { ' ', "&nbsp;" },
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table html_escape_special_chars[] = {  static struct html_escape_table html_escape_special_chars[] = { /* {{{ */
         { '\x3C', "&lt;"  },          { '\x3C', "&lt;"  },
         { '\x3E', "&gt;"  },          { '\x3E', "&gt;"  },
         { '\x26', "&amp;" },          { '\x26', "&amp;" },
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table html_escape_quotes[] = {  static struct html_escape_table html_escape_quotes[] = { /* {{{ */
         { '\x22', "&quot;" },          { '\"', "&quot;" },
         { '\'',   "&#039;" },          { '\'', "&#039;" },
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table html_escape_iso8859_1[] = {  static struct html_escape_table html_escape_iso8859_1[] = { /* {{{ */
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table html_escape_iso8859_2[] = {  static struct html_escape_table html_escape_iso8859_2[] = { /* {{{ */
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table html_escape_iso8859_15[] = {  static struct html_escape_table html_escape_iso8859_15[] = { /* {{{ */
         { '\xE4', "&auml;"  },          { '\xE4', "&auml;"  },
         { '\xC4', "&Auml;"  },          { '\xC4', "&Auml;"  },
         { '\xF6', "&ouml;"  },          { '\xF6', "&ouml;"  },
Line 39  struct html_escape_table html_escape_iso
Line 40  struct html_escape_table html_escape_iso
         { '\xDC', "&Uuml;"  },          { '\xDC', "&Uuml;"  },
         { '\xDF', "&szlig;" },          { '\xDF', "&szlig;" },
         { '\xA4', "&euro;"  },          { '\xA4', "&euro;"  },
         { '\0', NULL }          HTML_ESCAPE_TABLE_TERMINATOR
 };  }; /* }}} */
   
 struct html_escape_table *html_escape_tables[] = {  static struct html_escape_table *html_escape_tables[] = { /* {{{ */
         html_escape_space,          html_escape_space,
         html_escape_special_chars,          html_escape_special_chars,
         html_escape_quotes,          html_escape_quotes,
Line 50  struct html_escape_table *html_escape_ta
Line 51  struct html_escape_table *html_escape_ta
         html_escape_iso8859_2,          html_escape_iso8859_2,
         html_escape_iso8859_15,          html_escape_iso8859_15,
         NULL          NULL
 };  }; /* }}} */
   
         char *          char *
 PLATON_FUNC(html_escape)(str, types)  PLATON_FUNC(html_escape)(str, types)
         const char *str;          const char *str;
         const int types;          const int types;
 {  { /* {{{ */
         register int i, ii, j, len = 1;          register int i, ii, j, len = 1;
         struct html_escape_table *current_table;          struct html_escape_table *current_table;
         char *html;          char *html;
Line 85  PLATON_FUNC(html_escape)(str, types)
Line 86  PLATON_FUNC(html_escape)(str, types)
   
                                 /* Substitute character with appropriate entity */                                  /* Substitute character with appropriate entity */
                                 len += strlen(current_table[j].html);                                  len += strlen(current_table[j].html);
                                 if ((html = realloc(html, len)) == NULL)                                  if ((html = (char *) realloc(html, len)) == NULL)
                                         return NULL;                                          return NULL;
                                 strcat(html, current_table[j].html);                                  strcat(html, current_table[j].html);
                                 break;                                  break;
Line 99  PLATON_FUNC(html_escape)(str, types)
Line 100  PLATON_FUNC(html_escape)(str, types)
                         continue;                          continue;
   
                 /* Just copy character */                  /* Just copy character */
                 if ((html = realloc(html, len + 1)) == NULL)  
                         return NULL;  
                 html[len - 1] = *str;  
                 len++;                  len++;
                   if ((html = (char *) realloc(html, len)) == NULL)
                           return NULL;
                   html[len - 2] = *str;
                   html[len - 1] = '\0';
         }          }
         html[len - 1] = '\0';  
   
         return html;          return html;
 }  } /* }}} */
   
           char *
   PLATON_FUNC(html_unescape)(html, types)
           const char *html;
           const int types;
   { /* {{{ */
           register int i, ii, j, len;
           struct html_escape_table *current_table;
           char *str, *ptr;
   
           /* String duplication */
           if ((str = strdup(html)) == NULL)
                   return NULL;
   
           /* Loop through every HTML escape table */
           for (i = 0, ii = 1; ; i++, ii *= 2) { /* ii == 2^i */
                   if ((current_table = html_escape_tables[i]) == NULL)
                           break;
   
 #if defined(SELF) || defined(SELFTEST) || defined(SELF_HTML_ESCAPE)                  /* Check if table is selected */
                   if (! types & ii)
                           continue;
   
                   /* Loop though every entity of HTML escape table */
                   for (j = 0; current_table[j].html != NULL; j++) {
   
                           while ((ptr = strstr(str, current_table[j].html)) != NULL) {
                                   len  = strlen(current_table[j].html);
                                   if (len <= 0)
                                           break;
                                   *ptr = current_table[j].code;
                                   memmove((void *) (ptr + 1), (void *) (ptr + len),
                                                   strlen(ptr + len) + 1);
                           }
   
                   }
           }
   
           return str;
   } /* }}} */
   
   #if defined(SELF) || defined(SELFTEST) || defined(SELF_HTML_ESCAPE) /* {{{ */
   
 #include <stdio.h>  #include <stdio.h>
   
 #define TEST_htmlentities       "[<--//-->]"  #define TEST_STRING             "<<--'//'-->>"
   
         int          int
 main(argc, argv)  main(argc, argv)
         int argc;          int argc;
         char **argv;          char **argv;
 {  {
         register char *s1;          register char *s1, *s2;
   
         s1 = htmlentities(TEST_htmlentities);          s1 = htmlspecialchars(TEST_STRING);
         printf("htmlentities(\"%s\") = \"%s\"\n", TEST_htmlentities, s1);          printf("htmlspecialchars(\"%s\") = \"%s\"\n", TEST_STRING, s1);
           s2 = html_unescape(s1, HTML_ESCAPE_HTMLSPECIALCHARS);
           printf("html_unescape(\"%s\", HTML_ESCAPE_HTMLSPECIALCHARS) = \"%s\"\n", s1, s2);
           free(s2);
           free(s1);
   
           s1 = htmlentities(TEST_STRING);
           printf("htmlentities(\"%s\") = \"%s\"\n", TEST_STRING, s1);
           s2 = html_unescape(s1, HTML_ESCAPE_HTMLENTITIES);
           printf("html_unescape(\"%s\", HTML_ESCAPE_HTMLENTITIES) = \"%s\"\n", s1, s2);
           free(s2);
         free(s1);          free(s1);
   
         return 0;          return 0;
 }  }
   
 #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRCGI) */  #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRCGI) */ /* }}} */
   
   /* Modeline for ViM {{{
    * vim: set ts=4:
    * vim600: fdm=marker fdl=0 fdc=0:
    * }}} */
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top