home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / tidy26jul99 / entities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-08  |  7.5 KB  |  393 lines

  1. /* entities.c -- recognize HTML ISO entities
  2.  
  3.   (c) 1998 (W3C) MIT, INRIA, Keio University
  4.   See tidy.c for the copyright notice.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "platform.h"
  9. #include "html.h"
  10.  
  11. #define HASHSIZE 731
  12.  
  13. struct nlist
  14. {
  15.     struct nlist *next;
  16.     char *name;
  17.     unsigned code;
  18. };
  19.  
  20. static struct nlist *hashtab[HASHSIZE];
  21.  
  22. struct entity
  23. {
  24.     char *name;
  25.     uint code;
  26. } entities[] =
  27. {
  28.     "nbsp",   160,
  29.     "iexcl",  161,
  30.     "cent",   162,
  31.     "pound",  163,
  32.     "curren", 164,
  33.     "yen",    165,
  34.     "brvbar", 166,
  35.     "sect",   167,
  36.     "uml",    168,
  37.     "copy",   169,
  38.     "ordf",   170,
  39.     "laquo",  171,
  40.     "not",    172,
  41.     "shy",    173,
  42.     "reg",    174,
  43.     "macr",   175,
  44.     "deg",    176,
  45.     "plusmn", 177,
  46.     "sup2",   178,
  47.     "sup3",   179,
  48.     "acute",  180,
  49.     "micro",  181,
  50.     "para",   182,
  51.     "middot", 183,
  52.     "cedil",  184,
  53.     "sup1",   185,
  54.     "ordm",   186,
  55.     "raquo",  187,
  56.     "frac14", 188,
  57.     "frac12", 189,
  58.     "frac34", 190,
  59.     "iquest", 191,
  60.     "Agrave", 192,
  61.     "Aacute", 193,
  62.     "Acirc",  194,
  63.     "Atilde", 195,
  64.     "Auml",   196,
  65.     "Aring",  197,
  66.     "AElig",  198,
  67.     "Ccedil", 199,
  68.     "Egrave", 200,
  69.     "Eacute", 201,
  70.     "Ecirc",  202,
  71.     "Euml",   203,
  72.     "Igrave", 204,
  73.     "Iacute", 205,
  74.     "Icirc",  206,
  75.     "Iuml",   207,
  76.     "ETH",    208,
  77.     "Ntilde", 209,
  78.     "Ograve", 210,
  79.     "Oacute", 211,
  80.     "Ocirc",  212,
  81.     "Otilde", 213,
  82.     "Ouml",   214,
  83.     "times",  215,
  84.     "Oslash", 216,
  85.     "Ugrave", 217,
  86.     "Uacute", 218,
  87.     "Ucirc",  219,
  88.     "Uuml",   220,
  89.     "Yacute", 221,
  90.     "THORN",  222,
  91.     "szlig",  223,
  92.     "agrave", 224,
  93.     "aacute", 225,
  94.     "acirc",  226,
  95.     "atilde", 227,
  96.     "auml",   228,
  97.     "aring",  229,
  98.     "aelig",  230,
  99.     "ccedil", 231,
  100.     "egrave", 232,
  101.     "eacute", 233,
  102.     "ecirc",  234,
  103.     "euml",   235,
  104.     "igrave", 236,
  105.     "iacute", 237,
  106.     "icirc",  238,
  107.     "iuml",   239,
  108.     "eth",    240,
  109.     "ntilde", 241,
  110.     "ograve", 242,
  111.     "oacute", 243,
  112.     "ocirc",  244,
  113.     "otilde", 245,
  114.     "ouml",   246,
  115.     "divide", 247,
  116.     "oslash", 248,
  117.     "ugrave", 249,
  118.     "uacute", 250,
  119.     "ucirc",  251,
  120.     "uuml",   252,
  121.     "yacute", 253,
  122.     "thorn",  254,
  123.     "yuml",   255,
  124.     "fnof",     402,
  125.     "Alpha",    913,
  126.     "Beta",     914,
  127.     "Gamma",    915,
  128.     "Delta",    916,
  129.     "Epsilon",  917,
  130.     "Zeta",     918,
  131.     "Eta",      919,
  132.     "Theta",    920,
  133.     "Iota",     921,
  134.     "Kappa",    922,
  135.     "Lambda",   923,
  136.     "Mu",       924,
  137.     "Nu",       925,
  138.     "Xi",       926,
  139.     "Omicron",  927,
  140.     "Pi",       928,
  141.     "Rho",      929,
  142.     "Sigma",    931,
  143.     "Tau",      932,
  144.     "Upsilon",  933,
  145.     "Phi",      934,
  146.     "Chi",      935,
  147.     "Psi",      936,
  148.     "Omega",    937,
  149.     "alpha",    945,
  150.     "beta",     946,
  151.     "gamma",    947,
  152.     "delta",    948,
  153.     "epsilon",  949,
  154.     "zeta",     950,
  155.     "eta",      951,
  156.     "theta",    952,
  157.     "iota",     953,
  158.     "kappa",    954,
  159.     "lambda",   955,
  160.     "mu",       956,
  161.     "nu",       957,
  162.     "xi",       958,
  163.     "omicron",  959,
  164.     "pi",       960,
  165.     "rho",      961,
  166.     "sigmaf",   962,
  167.     "sigma",    963,
  168.     "tau",      964,
  169.     "upsilon",  965,
  170.     "phi",      966,
  171.     "chi",      967,
  172.     "psi",      968,
  173.     "omega",    969,
  174.     "thetasym", 977,
  175.     "upsih",    978,
  176.     "piv",      982,
  177.     "bull",     8226,
  178.     "hellip",   8230,
  179.     "prime",    8242,
  180.     "Prime",    8243,
  181.     "oline",    8254,
  182.     "frasl",    8260,
  183.     "weierp",   8472,
  184.     "image",    8465,
  185.     "real",     8476,
  186.     "trade",    8482,
  187.     "alefsym",  8501,
  188.     "larr",     8592,
  189.     "uarr",     8593,
  190.     "rarr",     8594,
  191.     "darr",     8595,
  192.     "harr",     8596,
  193.     "crarr",    8629,
  194.     "lArr",     8656,
  195.     "uArr",     8657,
  196.     "rArr",     8658,
  197.     "dArr",     8659,
  198.     "hArr",     8660,
  199.     "forall",   8704,
  200.     "part",     8706,
  201.     "exist",    8707,
  202.     "empty",    8709,
  203.     "nabla",    8711,
  204.     "isin",     8712,
  205.     "notin",    8713,
  206.     "ni",       8715,
  207.     "prod",     8719,
  208.     "sum",      8721,
  209.     "minus",    8722,
  210.     "lowast",   8727,
  211.     "radic",    8730,
  212.     "prop",     8733,
  213.     "infin",    8734,
  214.     "ang",      8736,
  215.     "and",      8743,
  216.     "or",       8744,
  217.     "cap",      8745,
  218.     "cup",      8746,
  219.     "int",      8747,
  220.     "there4",   8756,
  221.     "sim",      8764,
  222.     "cong",     8773,
  223.     "asymp",    8776,
  224.     "ne",       8800,
  225.     "equiv",    8801,
  226.     "le",       8804,
  227.     "ge",       8805,
  228.     "sub",      8834,
  229.     "sup",      8835,
  230.     "nsub",     8836,
  231.     "sube",     8838,
  232.     "supe",     8839,
  233.     "oplus",    8853,
  234.     "otimes",   8855,
  235.     "perp",     8869,
  236.     "sdot",     8901,
  237.     "lceil",    8968,
  238.     "rceil",    8969,
  239.     "lfloor",   8970,
  240.     "rfloor",   8971,
  241.     "lang",     9001,
  242.     "rang",     9002,
  243.     "loz",      9674,
  244.     "spades",   9824,
  245.     "clubs",    9827,
  246.     "hearts",   9829,
  247.     "diams",    9830,
  248.     "quot",    34,
  249.     "amp",     38,
  250.     "lt",      60,
  251.     "gt",      62,
  252.     "OElig",   338,
  253.     "oelig",   339,
  254.     "Scaron",  352,
  255.     "scaron",  353,
  256.     "Yuml",    376,
  257.     "circ",    710,
  258.     "tilde",   732,
  259.     "ensp",    8194,
  260.     "emsp",    8195,
  261.     "thinsp",  8201,
  262.     "zwnj",    8204,
  263.     "zwj",     8205,
  264.     "lrm",     8206,
  265.     "rlm",     8207,
  266.     "ndash",   8211,
  267.     "mdash",   8212,
  268.     "lsquo",   8216,
  269.     "rsquo",   8217,
  270.     "sbquo",   8218,
  271.     "ldquo",   8220,
  272.     "rdquo",   8221,
  273.     "bdquo",   8222,
  274.     "dagger",  8224,
  275.     "Dagger",  8225,
  276.     "permil",  8240,
  277.     "lsaquo",  8249,
  278.     "rsaquo",  8250,
  279.     "euro",    8364,
  280.      null,      0
  281. };
  282.  
  283. static unsigned hash(char *s)
  284. {
  285.     uint hashval;
  286.  
  287.     for (hashval = 0; *s != '\0'; s++)
  288.         hashval = *s + 31*hashval;
  289.  
  290.     return hashval % HASHSIZE;
  291. }
  292.  
  293. static struct nlist *lookup(char *s)
  294. {
  295.     struct nlist *np;
  296.  
  297.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  298.         if (wstrcmp(s, np->name) == 0)
  299.             return np;
  300.     return NULL;
  301. }
  302.  
  303. static struct nlist *install(char *name, uint code)
  304. {
  305.     struct nlist *np;
  306.     uint hashval;
  307.  
  308.     if ((np = lookup(name)) == NULL)
  309.     {
  310.         np = (struct nlist *)MemAlloc(sizeof(*np));
  311.  
  312.         if (np == NULL || (np->name = wstrdup(name)) == NULL)
  313.             return NULL;
  314.  
  315.         hashval = hash(name);
  316.         np->next = hashtab[hashval];
  317.         hashtab[hashval] = np;
  318.     }
  319.  
  320.     np->code = code;
  321.     return np;
  322. }
  323.  
  324.  
  325. /* entity starting with "&" returns zero on error */
  326. uint EntityCode(char *name)
  327. {
  328.     int c;
  329.     struct nlist *np;
  330.  
  331.     /* numeric entitity: name = "&#" followed by number */
  332.     if (name[1] == '#')
  333.     {
  334.         c = 0;  /* zero on missing/bad number */
  335.  
  336.         /* 'x' prefix denotes hexadecimal number format */
  337.         if (name[2] == 'x')
  338.             sscanf(name+3, "%x", &c);
  339.         else
  340.             sscanf(name+2, "%d", &c);
  341.  
  342.         return c;
  343.     }
  344.  
  345.    /* Named entity: name ="&" followed by a name */
  346.     if ((np = lookup(name+1)))
  347.         return np->code;
  348.  
  349.     return 0;   /* zero signifies unknown entity name */
  350. }
  351.  
  352. void InitEntities(void)
  353. {
  354.     struct entity *ep;
  355.     
  356.     for(ep = entities; ep->name != null; ++ep)
  357.         install(ep->name, ep->code);
  358. }
  359.  
  360. void FreeEntities(void)
  361. {
  362.     struct nlist *prev, *next;
  363.     int i;
  364.  
  365.     for (i = 0; i < HASHSIZE; ++i)
  366.     {
  367.         prev = null;
  368.         next = hashtab[i];
  369.  
  370.         while(next)
  371.         {
  372.             prev = next->next;
  373.             MemFree(next->name);
  374.             MemFree(next);
  375.             next = prev;
  376.         }
  377.     }
  378. }
  379.  
  380.  
  381. char *EntityName(uint n)
  382. {
  383.     struct entity *ep;
  384.     
  385.     for(ep = entities; ep->name != null; ++ep)
  386.     {
  387.         if (ep->code == n)
  388.             return ep->name;
  389.     }
  390.  
  391.     return null;
  392. }
  393.