00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "config.h"
00036 #include "mxml.h"
00037
00038
00039
00040
00041
00042
00043 static int default_callback(const char *name);
00044
00045
00046
00047
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
00059
00060
00061 int
00062 mxmlEntityAddCallback(int (*cb)(const char *name))
00063
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
00083
00084
00085
00086
00087 const char *
00088 mxmlEntityGetName(int val)
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
00112
00113
00114
00115
00116
00117 int
00118 mxmlEntityGetValue(const char *name)
00119 {
00120 int i;
00121 int ch;
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
00134
00135
00136 void
00137 mxmlEntityRemoveCallback(int (*cb)(const char *name))
00138
00139 {
00140 int i;
00141
00142
00143 for (i = 0; i < num_callbacks; i ++)
00144 if (cb == callbacks[i])
00145 {
00146
00147
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
00163
00164
00165 static int
00166 default_callback(const char *name)
00167 {
00168 int diff,
00169 current,
00170 first,
00171 last;
00172 static const struct
00173 {
00174 const char *name;
00175 int val;
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
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
00458
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
00472