/home/dko/projects/mobilec/trunk/src/mxml-2.2.2/mxml-entity.c

Go to the documentation of this file.
00001 /*
00002  * "$Id: mxml-entity.c,v 1.1 2007/05/23 20:43:27 david_ko Exp $"
00003  *
00004  * Character entity support code for Mini-XML, a small XML-like
00005  * file parsing library.
00006  *
00007  * Copyright 2003-2005 by Michael Sweet.
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * Contents:
00020  *
00021  *   mxmlEntityAddCallback()    - Add a callback to convert entities to
00022  *                                Unicode.
00023  *   mxmlEntityGetName()        - Get the name that corresponds to the
00024  *                                character value.
00025  *   mxmlEntityGetValue()       - Get the character corresponding to a named
00026  *                                entity.
00027  *   mxmlEntityRemoveCallback() - Remove a callback.
00028  *   default_callback()         - Lookup standard (X)HTML entities.
00029  */
00030 
00031 /*
00032  * Include necessary headers...
00033  */
00034 
00035 #include "config.h"
00036 #include "mxml.h"
00037 
00038 
00039 /*
00040  * Local functions...
00041  */
00042 
00043 static int      default_callback(const char *name);
00044 
00045 
00046 /*
00047  * Callback array...
00048  */
00049 
00050 static int      num_callbacks = 1;
00051 static int      (*callbacks[100])(const char *name) =
00052                 {
00053                   default_callback
00054                 };
00055 
00056 
00057 /*
00058  * 'mxmlEntityAddCallback()' - Add a callback to convert entities to Unicode.
00059  */
00060 
00061 int                                     /* O - 0 on success, -1 on failure */
00062 mxmlEntityAddCallback(int (*cb)(const char *name))
00063                                         /* I - Callback function to add */
00064 {
00065   if (num_callbacks < (int)(sizeof(callbacks) / sizeof(callbacks[0])))
00066   {
00067     callbacks[num_callbacks] = cb;
00068     num_callbacks ++;
00069 
00070     return (0);
00071   }
00072   else
00073   {
00074     mxml_error("Unable to add entity callback!");
00075 
00076     return (-1);
00077   }
00078 }
00079 
00080 
00081 /*
00082  * 'mxmlEntityGetName()' - Get the name that corresponds to the character value.
00083  *
00084  * If val does not need to be represented by a named entity, NULL is returned.
00085  */
00086 
00087 const char *                            /* O - Entity name or NULL */
00088 mxmlEntityGetName(int val)              /* I - Character value */
00089 {
00090   switch (val)
00091   {
00092     case '&' :
00093         return ("amp");
00094 
00095     case '<' :
00096         return ("lt");
00097 
00098     case '>' :
00099         return ("gt");
00100 
00101     case '\"' :
00102         return ("quot");
00103 
00104     default :
00105         return (NULL);
00106   }
00107 }
00108 
00109 
00110 /*
00111  * 'mxmlEntityGetValue()' - Get the character corresponding to a named entity.
00112  *
00113  * The entity name can also be a numeric constant. -1 is returned if the
00114  * name is not known.
00115  */
00116 
00117 int                                     /* O - Character value or -1 on error */
00118 mxmlEntityGetValue(const char *name)    /* I - Entity name */
00119 {
00120   int   i;                              /* Looping var */
00121   int   ch;                             /* Character value */
00122 
00123 
00124   for (i = 0; i < num_callbacks; i ++)
00125     if ((ch = (callbacks[i])(name)) >= 0)
00126       return (ch);
00127 
00128   return (-1);
00129 }
00130 
00131 
00132 /*
00133  * 'mxmlEntityRemoveCallback()' - Remove a callback.
00134  */
00135 
00136 void
00137 mxmlEntityRemoveCallback(int (*cb)(const char *name))
00138                                         /* I - Callback function to remove */
00139 {
00140   int   i;                              /* Looping var */
00141 
00142 
00143   for (i = 0; i < num_callbacks; i ++)
00144     if (cb == callbacks[i])
00145     {
00146      /*
00147       * Remove the callback...
00148       */
00149 
00150       num_callbacks --;
00151 
00152       if (i < num_callbacks)
00153         memmove(callbacks + i, callbacks + i + 1,
00154                 (num_callbacks - i) * sizeof(callbacks[0]));
00155 
00156       return;
00157     }
00158 }
00159 
00160 
00161 /*
00162  * 'default_callback()' - Lookup standard (X)HTML entities.
00163  */
00164 
00165 static int                              /* O - Unicode value or -1 */
00166 default_callback(const char *name)      /* I - Entity name */
00167 {
00168   int   diff,                           /* Difference between names */
00169         current,                        /* Current entity in search */
00170         first,                          /* First entity in search */
00171         last;                           /* Last entity in search */
00172   static const struct
00173   {
00174     const char  *name;                  /* Entity name */
00175     int         val;                    /* Character value */
00176   }     entities[] =
00177   {
00178     { "AElig",          198 },
00179     { "Aacute",         193 },
00180     { "Acirc",          194 },
00181     { "Agrave",         192 },
00182     { "Alpha",          913 },
00183     { "Aring",          197 },
00184     { "Atilde",         195 },
00185     { "Auml",           196 },
00186     { "Beta",           914 },
00187     { "Ccedil",         199 },
00188     { "Chi",            935 },
00189     { "Dagger",         8225 },
00190     { "Delta",          916 },
00191     { "Dstrok",         208 },
00192     { "ETH",            208 },
00193     { "Eacute",         201 },
00194     { "Ecirc",          202 },
00195     { "Egrave",         200 },
00196     { "Epsilon",        917 },
00197     { "Eta",            919 },
00198     { "Euml",           203 },
00199     { "Gamma",          915 },
00200     { "Iacute",         205 },
00201     { "Icirc",          206 },
00202     { "Igrave",         204 },
00203     { "Iota",           921 },
00204     { "Iuml",           207 },
00205     { "Kappa",          922 },
00206     { "Lambda",         923 },
00207     { "Mu",             924 },
00208     { "Ntilde",         209 },
00209     { "Nu",             925 },
00210     { "OElig",          338 },
00211     { "Oacute",         211 },
00212     { "Ocirc",          212 },
00213     { "Ograve",         210 },
00214     { "Omega",          937 },
00215     { "Omicron",        927 },
00216     { "Oslash",         216 },
00217     { "Otilde",         213 },
00218     { "Ouml",           214 },
00219     { "Phi",            934 },
00220     { "Pi",             928 },
00221     { "Prime",          8243 },
00222     { "Psi",            936 },
00223     { "Rho",            929 },
00224     { "Scaron",         352 },
00225     { "Sigma",          931 },
00226     { "THORN",          222 },
00227     { "Tau",            932 },
00228     { "Theta",          920 },
00229     { "Uacute",         218 },
00230     { "Ucirc",          219 },
00231     { "Ugrave",         217 },
00232     { "Upsilon",        933 },
00233     { "Uuml",           220 },
00234     { "Xi",             926 },
00235     { "Yacute",         221 },
00236     { "Yuml",           376 },
00237     { "Zeta",           918 },
00238     { "aacute",         225 },
00239     { "acirc",          226 },
00240     { "acute",          180 },
00241     { "aelig",          230 },
00242     { "agrave",         224 },
00243     { "alefsym",        8501 },
00244     { "alpha",          945 },
00245     { "amp",            '&' },
00246     { "and",            8743 },
00247     { "ang",            8736 },
00248     { "aring",          229 },
00249     { "asymp",          8776 },
00250     { "atilde",         227 },
00251     { "auml",           228 },
00252     { "bdquo",          8222 },
00253     { "beta",           946 },
00254     { "brkbar",         166 },
00255     { "brvbar",         166 },
00256     { "bull",           8226 },
00257     { "cap",            8745 },
00258     { "ccedil",         231 },
00259     { "cedil",          184 },
00260     { "cent",           162 },
00261     { "chi",            967 },
00262     { "circ",           710 },
00263     { "clubs",          9827 },
00264     { "cong",           8773 },
00265     { "copy",           169 },
00266     { "crarr",          8629 },
00267     { "cup",            8746 },
00268     { "curren",         164 },
00269     { "dArr",           8659 },
00270     { "dagger",         8224 },
00271     { "darr",           8595 },
00272     { "deg",            176 },
00273     { "delta",          948 },
00274     { "diams",          9830 },
00275     { "die",            168 },
00276     { "divide",         247 },
00277     { "eacute",         233 },
00278     { "ecirc",          234 },
00279     { "egrave",         232 },
00280     { "empty",          8709 },
00281     { "emsp",           8195 },
00282     { "ensp",           8194 },
00283     { "epsilon",        949 },
00284     { "equiv",          8801 },
00285     { "eta",            951 },
00286     { "eth",            240 },
00287     { "euml",           235 },
00288     { "euro",           8364 },
00289     { "exist",          8707 },
00290     { "fnof",           402 },
00291     { "forall",         8704 },
00292     { "frac12",         189 },
00293     { "frac14",         188 },
00294     { "frac34",         190 },
00295     { "frasl",          8260 },
00296     { "gamma",          947 },
00297     { "ge",             8805 },
00298     { "gt",             '>' },
00299     { "hArr",           8660 },
00300     { "harr",           8596 },
00301     { "hearts",         9829 },
00302     { "hellip",         8230 },
00303     { "hibar",          175 },
00304     { "iacute",         237 },
00305     { "icirc",          238 },
00306     { "iexcl",          161 },
00307     { "igrave",         236 },
00308     { "image",          8465 },
00309     { "infin",          8734 },
00310     { "int",            8747 },
00311     { "iota",           953 },
00312     { "iquest",         191 },
00313     { "isin",           8712 },
00314     { "iuml",           239 },
00315     { "kappa",          954 },
00316     { "lArr",           8656 },
00317     { "lambda",         955 },
00318     { "lang",           9001 },
00319     { "laquo",          171 },
00320     { "larr",           8592 },
00321     { "lceil",          8968 },
00322     { "ldquo",          8220 },
00323     { "le",             8804 },
00324     { "lfloor",         8970 },
00325     { "lowast",         8727 },
00326     { "loz",            9674 },
00327     { "lrm",            8206 },
00328     { "lsaquo",         8249 },
00329     { "lsquo",          8216 },
00330     { "lt",             '<' },
00331     { "macr",           175 },
00332     { "mdash",          8212 },
00333     { "micro",          181 },
00334     { "middot",         183 },
00335     { "minus",          8722 },
00336     { "mu",             956 },
00337     { "nabla",          8711 },
00338     { "nbsp",           160 },
00339     { "ndash",          8211 },
00340     { "ne",             8800 },
00341     { "ni",             8715 },
00342     { "not",            172 },
00343     { "notin",          8713 },
00344     { "nsub",           8836 },
00345     { "ntilde",         241 },
00346     { "nu",             957 },
00347     { "oacute",         243 },
00348     { "ocirc",          244 },
00349     { "oelig",          339 },
00350     { "ograve",         242 },
00351     { "oline",          8254 },
00352     { "omega",          969 },
00353     { "omicron",        959 },
00354     { "oplus",          8853 },
00355     { "or",             8744 },
00356     { "ordf",           170 },
00357     { "ordm",           186 },
00358     { "oslash",         248 },
00359     { "otilde",         245 },
00360     { "otimes",         8855 },
00361     { "ouml",           246 },
00362     { "para",           182 },
00363     { "part",           8706 },
00364     { "permil",         8240 },
00365     { "perp",           8869 },
00366     { "phi",            966 },
00367     { "pi",             960 },
00368     { "piv",            982 },
00369     { "plusmn",         177 },
00370     { "pound",          163 },
00371     { "prime",          8242 },
00372     { "prod",           8719 },
00373     { "prop",           8733 },
00374     { "psi",            968 },
00375     { "quot",           '\"' },
00376     { "rArr",           8658 },
00377     { "radic",          8730 },
00378     { "rang",           9002 },
00379     { "raquo",          187 },
00380     { "rarr",           8594 },
00381     { "rceil",          8969 },
00382     { "rdquo",          8221 },
00383     { "real",           8476 },
00384     { "reg",            174 },
00385     { "rfloor",         8971 },
00386     { "rho",            961 },
00387     { "rlm",            8207 },
00388     { "rsaquo",         8250 },
00389     { "rsquo",          8217 },
00390     { "sbquo",          8218 },
00391     { "scaron",         353 },
00392     { "sdot",           8901 },
00393     { "sect",           167 },
00394     { "shy",            173 },
00395     { "sigma",          963 },
00396     { "sigmaf",         962 },
00397     { "sim",            8764 },
00398     { "spades",         9824 },
00399     { "sub",            8834 },
00400     { "sube",           8838 },
00401     { "sum",            8721 },
00402     { "sup",            8835 },
00403     { "sup1",           185 },
00404     { "sup2",           178 },
00405     { "sup3",           179 },
00406     { "supe",           8839 },
00407     { "szlig",          223 },
00408     { "tau",            964 },
00409     { "there4",         8756 },
00410     { "theta",          952 },
00411     { "thetasym",       977 },
00412     { "thinsp",         8201 },
00413     { "thorn",          254 },
00414     { "tilde",          732 },
00415     { "times",          215 },
00416     { "trade",          8482 },
00417     { "uArr",           8657 },
00418     { "uacute",         250 },
00419     { "uarr",           8593 },
00420     { "ucirc",          251 },
00421     { "ugrave",         249 },
00422     { "uml",            168 },
00423     { "upsih",          978 },
00424     { "upsilon",        965 },
00425     { "uuml",           252 },
00426     { "weierp",         8472 },
00427     { "xi",             958 },
00428     { "yacute",         253 },
00429     { "yen",            165 },
00430     { "yuml",           255 },
00431     { "zeta",           950 },
00432     { "zwj",            8205 },
00433     { "zwnj",           8204 }
00434   };
00435 
00436 
00437  /*
00438   * Do a binary search for the named entity...
00439   */
00440 
00441   first = 0;
00442   last  = (int)(sizeof(entities) / sizeof(entities[0]) - 1);
00443 
00444   while ((last - first) > 1)
00445   {
00446     current = (first + last) / 2;
00447 
00448     if ((diff = strcmp(name, entities[current].name)) == 0)
00449       return (entities[current].val);
00450     else if (diff < 0)
00451       last = current;
00452     else
00453       first = current;
00454   }
00455 
00456  /*
00457   * If we get here, there is a small chance that there is still
00458   * a match; check first and last...
00459   */
00460 
00461   if (!strcmp(name, entities[first].name))
00462     return (entities[first].val);
00463   else if (!strcmp(name, entities[last].name))
00464     return (entities[last].val);
00465   else
00466     return (-1);
00467 }
00468 
00469 
00470 /*
00471  * End of "$Id: mxml-entity.c,v 1.1 2007/05/23 20:43:27 david_ko Exp $".
00472  */

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4