entities.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:6k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /* recognize HTML ISO entities */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define HASHSIZE 161
  7. struct nlist
  8. {
  9.     struct nlist *next;
  10.     char *name;
  11.     unsigned code;
  12. };
  13. static struct nlist *hashtab[HASHSIZE];
  14. struct entity
  15. {
  16.     char *name;
  17.     unsigned code;
  18. } entities[] = 
  19.   {
  20.     "nbsp",     32,      /* non breaking space */
  21.     "quot",     34,
  22.     "apos",     39,
  23.     "iexcl",    161,
  24.     "cent",     162,
  25.     "pound",    163,
  26.     "curren",   164,
  27.     "yen",      165,
  28.     "brvbar",   166,
  29.     "sect",     167,
  30.     "uml",      168,
  31.     "copy",     169,
  32.     "ordf",     170,
  33.     "laquo",    171,
  34.     "raquo",    187,
  35.     "not",      172,
  36.     "reg",      174,
  37.     "macr",     175,
  38.     "deg",      176,
  39.     "plusmn",   177,
  40.     "sup2",     178,
  41.     "sup3",     179,
  42.     "acute",    180,
  43.     "micro",    181,
  44.     "para",     182,
  45.     "middot",   183,
  46.     "cedil",    184,
  47.     "sup1",     185,
  48.     "ordm",     186,
  49.     "frac14",   188,
  50.     "frac12",   189,
  51.     "iquest",   191,
  52.     "frac34",   190,
  53.     "AElig",    198,
  54.     "Aacute",   193,
  55.     "Acirc",    194,
  56.     "Agrave",   192,
  57.     "Aring",    197,
  58.     "Atilde",   195,
  59.     "Auml",     196,
  60.     "Ccedil",   199,
  61.     "ETH",      208,
  62.     "Eacute",   201,
  63.     "Ecirc",    202,
  64.     "Egrave",   200,
  65.     "Euml",     203,
  66.     "Iacute",   205,
  67.     "Icirc",    206,
  68.     "Igrave",   204,
  69.     "Iuml",     207,
  70.     "Ntilde",   209,
  71.     "Oacute",   211,
  72.     "Ocirc",    212,
  73.     "Ograve",   210,
  74.     "Oslash",   216,
  75.     "Otilde",   213,
  76.     "Ouml",     214,
  77.     "times",    215,
  78.     "THORN",    222,
  79.     "Uacute",   218,
  80.     "Ucirc",    219,
  81.     "Ugrave",   217,
  82.     "Uuml",     220,
  83.     "Yacute",   221,
  84.     "aacute",   225,
  85.     "acirc",    226,
  86.     "aelig",    230,
  87.     "agrave",   224,
  88.     "amp",      38,
  89.     "aring",    229,
  90.     "atilde",   227,
  91.     "auml",     228,
  92.     "ccedil",   231,
  93.     "eacute",   233,
  94.     "ecirc",    234,
  95.     "egrave",   232,
  96.     "eth",      240,
  97.     "euml",     235,
  98.     "tagc",     62,
  99.     "gt",       62,
  100.     "iacute",   237,
  101.     "icirc",    238,
  102.     "igrave",   236,
  103.     "iuml",     239,
  104.     "stago",    60,
  105.     "lt",       60,
  106.     "ntilde",   241,
  107.     "oacute",   243,
  108.     "ocirc",    244,
  109.     "ograve",   242,
  110.     "oslash",   248,
  111.     "otilde",   245,
  112.     "ouml",     246,
  113.     "szlig",    223,
  114.     "thorn",    254,
  115.     "uacute",   250,
  116.     "ucirc",    251,
  117.     "ugrave",   249,
  118.     "uuml",     252,
  119.     "yacute",   253,
  120.     "yuml",     255,
  121. /* symbol characters used in maths start here */
  122. /* Added by Janne Saarela (janne.saarela@cern.ch) */
  123.     "divide",   247,
  124. /*    <!ENTITY % ISOgrk3 PUBLIC
  125.  *       "ISO 8879:1986//ENTITIES Greek Symbols//EN">
  126.  *     %ISOgrk3;
  127.  */
  128.     
  129.     "alpha",  97,
  130.     "beta",   98,
  131.     "gamma",  103,
  132.     "Gamma",  71,
  133.     "delta",  100,
  134.     "Delta",  68,
  135.     "epsi",   101,
  136.     "zeta",   122,
  137.     "eta",    104,
  138.     "Theta",  81,
  139.     "thetav", 74,
  140.     "theta",  113,
  141.     "thetas", 113,
  142.     "iota",   105,
  143.     "kappa",  107,
  144.     "lambda", 108,
  145.     "Lambda", 76,
  146.     "mu",     109,
  147.     "nu",     110,
  148.     "xi",     120,
  149.     "Xi",     88,
  150.     "pi",     112,
  151.     "piv",    118,
  152.     "Pi",     80,
  153.     "Psi",    121,
  154.     "rho",    114,
  155.     "sigma",  115,
  156.     "Sigma",  83,
  157.     "sigmav", 86,
  158.     "tau",    116,
  159.     "upsi",   117,
  160.     "Upsi",   85,
  161.     "phi",    102,
  162.     "phis",   102,
  163.     "Phi",    70,
  164.     "phiv",   106,
  165.     "chi",    99,
  166.     "psi",    121,
  167.     "omega",  119,
  168.     "Omega",  87,
  169. /*    <!ENTITY % ISOtech PUBLIC
  170.  *      "ISO 8879-1986//ENTITIES General Technical//EN">
  171.  *    %ISOtech;
  172.  */
  173.     "ap",     187,
  174.     "and",    217,
  175.     "cdot",   215, /* staalesc 28/07/95 */
  176.     "cir",    176,
  177.     "darr",   175,
  178.     "dArr",   223,
  179.     "empty",  198,
  180.     "equiv",  186,
  181.     "exist",  36,
  182.     "forall", 34,
  183.     "ge",     179,
  184.     "harr",   171,
  185.     "hArr",   219,
  186.     "iff",    219,
  187.     "inf",    165,
  188.     "isin",   206,
  189.     "lang",   225,
  190.     "larr",   172,
  191.     "lcub",   123,
  192.     "lArr",   220,
  193.     "le",     163,
  194.     "ne",     185,
  195.     "nabla",  209,
  196.     "or",     218,
  197.     "perp",   94,
  198.     "prop",   181,
  199.     "rang",   241,
  200.     "rarr",   174,
  201.     "rcub",   125,
  202.     "rArr",   222,
  203.     "sim",    126,
  204.     "sub",    204,
  205.     "sube",   205,
  206.     "sup",    201,
  207.     "supe",   202,
  208.     "surd",   214, /* staalesc 13/12/95 */
  209.     "shy",    173,
  210.     "uarr",   173,
  211.     "uArr",   221,
  212.     "pd",     182
  213.   };
  214. static unsigned hash(char *s)
  215. {
  216.     unsigned hashval;
  217.     for (hashval = 0; *s != ''; s++)
  218.         hashval = *s + 31*hashval;
  219.     return hashval % HASHSIZE;
  220. }
  221. static struct nlist *lookup(char *s)
  222. {
  223.     struct nlist *np;
  224.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  225.         if (strcmp(s, np->name) == 0)
  226.             return np;
  227.     return NULL;
  228. }
  229. static struct nlist *install(char *name, unsigned code)
  230. {
  231.     struct nlist *np;
  232.     unsigned hashval;
  233.     if ((np = lookup(name)) == NULL)
  234.     {
  235.         np = (struct nlist *)malloc(sizeof(*np));
  236.         if (np == NULL || (np->name = strdup(name)) == NULL)
  237.             return NULL;
  238.         hashval = hash(name);
  239.         np->next = hashtab[hashval];
  240.         hashtab[hashval] = np;
  241.     }
  242.     np->code = code;
  243.     return np;
  244. }
  245. int entity(char *name, int *len)
  246. {
  247.     int i, c, r;
  248.     char *p, buf[64];
  249.     struct nlist *np;
  250.     for (i = 2, p = buf; i < 65; ++i)
  251.     {
  252.         c = *name++;
  253.         if (c == ';' || isspace(c))  /* howcome 27/2/95: added test for space */
  254.         {
  255.             *p = '';
  256.             
  257.     if (isspace(c))
  258. *len = i - 1;
  259.     else
  260. *len = i;
  261.     if ((r = atoi(buf + 1)) > 0) {
  262. return r;
  263.     } 
  264.     else {
  265. np = lookup(buf);
  266. if (np)
  267.     return np->code;
  268.     }
  269.             break;
  270.         }
  271.         *p++ = c;
  272.     }
  273.     return 0;   /* signifies unknown entity name */
  274. }
  275. void InitEntities(void)
  276. {
  277.     struct entity *ep;
  278.     
  279.     ep = entities;
  280.     for(;;)
  281.     {
  282.         install(ep->name, ep->code);
  283.         if (strcmp(ep->name, "pd") == 0)
  284.             break;
  285.         ++ep;        
  286.     }