PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/gator/daemon/mxml/mxml-entity.c

https://gitlab.com/pine64-android/linux-3.10
C | 449 lines | 352 code | 42 blank | 55 comment | 16 complexity | 7743da070d0a8745ac0a96d37aee5774 MD5 | raw file
  1. /*
  2. * "$Id: mxml-entity.c 451 2014-01-04 21:50:06Z msweet $"
  3. *
  4. * Character entity support code for Mini-XML, a small XML-like
  5. * file parsing library.
  6. *
  7. * Copyright 2003-2014 by Michael R Sweet.
  8. *
  9. * These coded instructions, statements, and computer programs are the
  10. * property of Michael R Sweet and are protected by Federal copyright
  11. * law. Distribution and use rights are outlined in the file "COPYING"
  12. * which should have been included with this file. If this file is
  13. * missing or damaged, see the license at:
  14. *
  15. * http://www.msweet.org/projects.php/Mini-XML
  16. */
  17. /*
  18. * Include necessary headers...
  19. */
  20. #include "mxml-private.h"
  21. /*
  22. * 'mxmlEntityAddCallback()' - Add a callback to convert entities to Unicode.
  23. */
  24. int /* O - 0 on success, -1 on failure */
  25. mxmlEntityAddCallback(
  26. mxml_entity_cb_t cb) /* I - Callback function to add */
  27. {
  28. _mxml_global_t *global = _mxml_global();
  29. /* Global data */
  30. if (global->num_entity_cbs < (int)(sizeof(global->entity_cbs) / sizeof(global->entity_cbs[0])))
  31. {
  32. global->entity_cbs[global->num_entity_cbs] = cb;
  33. global->num_entity_cbs ++;
  34. return (0);
  35. }
  36. else
  37. {
  38. mxml_error("Unable to add entity callback!");
  39. return (-1);
  40. }
  41. }
  42. /*
  43. * 'mxmlEntityGetName()' - Get the name that corresponds to the character value.
  44. *
  45. * If val does not need to be represented by a named entity, NULL is returned.
  46. */
  47. const char * /* O - Entity name or NULL */
  48. mxmlEntityGetName(int val) /* I - Character value */
  49. {
  50. switch (val)
  51. {
  52. case '&' :
  53. return ("amp");
  54. case '<' :
  55. return ("lt");
  56. case '>' :
  57. return ("gt");
  58. case '\"' :
  59. return ("quot");
  60. default :
  61. return (NULL);
  62. }
  63. }
  64. /*
  65. * 'mxmlEntityGetValue()' - Get the character corresponding to a named entity.
  66. *
  67. * The entity name can also be a numeric constant. -1 is returned if the
  68. * name is not known.
  69. */
  70. int /* O - Character value or -1 on error */
  71. mxmlEntityGetValue(const char *name) /* I - Entity name */
  72. {
  73. int i; /* Looping var */
  74. int ch; /* Character value */
  75. _mxml_global_t *global = _mxml_global();
  76. /* Global data */
  77. for (i = 0; i < global->num_entity_cbs; i ++)
  78. if ((ch = (global->entity_cbs[i])(name)) >= 0)
  79. return (ch);
  80. return (-1);
  81. }
  82. /*
  83. * 'mxmlEntityRemoveCallback()' - Remove a callback.
  84. */
  85. void
  86. mxmlEntityRemoveCallback(
  87. mxml_entity_cb_t cb) /* I - Callback function to remove */
  88. {
  89. int i; /* Looping var */
  90. _mxml_global_t *global = _mxml_global();
  91. /* Global data */
  92. for (i = 0; i < global->num_entity_cbs; i ++)
  93. if (cb == global->entity_cbs[i])
  94. {
  95. /*
  96. * Remove the callback...
  97. */
  98. global->num_entity_cbs --;
  99. if (i < global->num_entity_cbs)
  100. memmove(global->entity_cbs + i, global->entity_cbs + i + 1,
  101. (global->num_entity_cbs - i) * sizeof(global->entity_cbs[0]));
  102. return;
  103. }
  104. }
  105. /*
  106. * '_mxml_entity_cb()' - Lookup standard (X)HTML entities.
  107. */
  108. int /* O - Unicode value or -1 */
  109. _mxml_entity_cb(const char *name) /* I - Entity name */
  110. {
  111. int diff, /* Difference between names */
  112. current, /* Current entity in search */
  113. first, /* First entity in search */
  114. last; /* Last entity in search */
  115. static const struct
  116. {
  117. const char *name; /* Entity name */
  118. int val; /* Character value */
  119. } entities[] =
  120. {
  121. { "AElig", 198 },
  122. { "Aacute", 193 },
  123. { "Acirc", 194 },
  124. { "Agrave", 192 },
  125. { "Alpha", 913 },
  126. { "Aring", 197 },
  127. { "Atilde", 195 },
  128. { "Auml", 196 },
  129. { "Beta", 914 },
  130. { "Ccedil", 199 },
  131. { "Chi", 935 },
  132. { "Dagger", 8225 },
  133. { "Delta", 916 },
  134. { "Dstrok", 208 },
  135. { "ETH", 208 },
  136. { "Eacute", 201 },
  137. { "Ecirc", 202 },
  138. { "Egrave", 200 },
  139. { "Epsilon", 917 },
  140. { "Eta", 919 },
  141. { "Euml", 203 },
  142. { "Gamma", 915 },
  143. { "Iacute", 205 },
  144. { "Icirc", 206 },
  145. { "Igrave", 204 },
  146. { "Iota", 921 },
  147. { "Iuml", 207 },
  148. { "Kappa", 922 },
  149. { "Lambda", 923 },
  150. { "Mu", 924 },
  151. { "Ntilde", 209 },
  152. { "Nu", 925 },
  153. { "OElig", 338 },
  154. { "Oacute", 211 },
  155. { "Ocirc", 212 },
  156. { "Ograve", 210 },
  157. { "Omega", 937 },
  158. { "Omicron", 927 },
  159. { "Oslash", 216 },
  160. { "Otilde", 213 },
  161. { "Ouml", 214 },
  162. { "Phi", 934 },
  163. { "Pi", 928 },
  164. { "Prime", 8243 },
  165. { "Psi", 936 },
  166. { "Rho", 929 },
  167. { "Scaron", 352 },
  168. { "Sigma", 931 },
  169. { "THORN", 222 },
  170. { "Tau", 932 },
  171. { "Theta", 920 },
  172. { "Uacute", 218 },
  173. { "Ucirc", 219 },
  174. { "Ugrave", 217 },
  175. { "Upsilon", 933 },
  176. { "Uuml", 220 },
  177. { "Xi", 926 },
  178. { "Yacute", 221 },
  179. { "Yuml", 376 },
  180. { "Zeta", 918 },
  181. { "aacute", 225 },
  182. { "acirc", 226 },
  183. { "acute", 180 },
  184. { "aelig", 230 },
  185. { "agrave", 224 },
  186. { "alefsym", 8501 },
  187. { "alpha", 945 },
  188. { "amp", '&' },
  189. { "and", 8743 },
  190. { "ang", 8736 },
  191. { "apos", '\'' },
  192. { "aring", 229 },
  193. { "asymp", 8776 },
  194. { "atilde", 227 },
  195. { "auml", 228 },
  196. { "bdquo", 8222 },
  197. { "beta", 946 },
  198. { "brkbar", 166 },
  199. { "brvbar", 166 },
  200. { "bull", 8226 },
  201. { "cap", 8745 },
  202. { "ccedil", 231 },
  203. { "cedil", 184 },
  204. { "cent", 162 },
  205. { "chi", 967 },
  206. { "circ", 710 },
  207. { "clubs", 9827 },
  208. { "cong", 8773 },
  209. { "copy", 169 },
  210. { "crarr", 8629 },
  211. { "cup", 8746 },
  212. { "curren", 164 },
  213. { "dArr", 8659 },
  214. { "dagger", 8224 },
  215. { "darr", 8595 },
  216. { "deg", 176 },
  217. { "delta", 948 },
  218. { "diams", 9830 },
  219. { "die", 168 },
  220. { "divide", 247 },
  221. { "eacute", 233 },
  222. { "ecirc", 234 },
  223. { "egrave", 232 },
  224. { "empty", 8709 },
  225. { "emsp", 8195 },
  226. { "ensp", 8194 },
  227. { "epsilon", 949 },
  228. { "equiv", 8801 },
  229. { "eta", 951 },
  230. { "eth", 240 },
  231. { "euml", 235 },
  232. { "euro", 8364 },
  233. { "exist", 8707 },
  234. { "fnof", 402 },
  235. { "forall", 8704 },
  236. { "frac12", 189 },
  237. { "frac14", 188 },
  238. { "frac34", 190 },
  239. { "frasl", 8260 },
  240. { "gamma", 947 },
  241. { "ge", 8805 },
  242. { "gt", '>' },
  243. { "hArr", 8660 },
  244. { "harr", 8596 },
  245. { "hearts", 9829 },
  246. { "hellip", 8230 },
  247. { "hibar", 175 },
  248. { "iacute", 237 },
  249. { "icirc", 238 },
  250. { "iexcl", 161 },
  251. { "igrave", 236 },
  252. { "image", 8465 },
  253. { "infin", 8734 },
  254. { "int", 8747 },
  255. { "iota", 953 },
  256. { "iquest", 191 },
  257. { "isin", 8712 },
  258. { "iuml", 239 },
  259. { "kappa", 954 },
  260. { "lArr", 8656 },
  261. { "lambda", 955 },
  262. { "lang", 9001 },
  263. { "laquo", 171 },
  264. { "larr", 8592 },
  265. { "lceil", 8968 },
  266. { "ldquo", 8220 },
  267. { "le", 8804 },
  268. { "lfloor", 8970 },
  269. { "lowast", 8727 },
  270. { "loz", 9674 },
  271. { "lrm", 8206 },
  272. { "lsaquo", 8249 },
  273. { "lsquo", 8216 },
  274. { "lt", '<' },
  275. { "macr", 175 },
  276. { "mdash", 8212 },
  277. { "micro", 181 },
  278. { "middot", 183 },
  279. { "minus", 8722 },
  280. { "mu", 956 },
  281. { "nabla", 8711 },
  282. { "nbsp", 160 },
  283. { "ndash", 8211 },
  284. { "ne", 8800 },
  285. { "ni", 8715 },
  286. { "not", 172 },
  287. { "notin", 8713 },
  288. { "nsub", 8836 },
  289. { "ntilde", 241 },
  290. { "nu", 957 },
  291. { "oacute", 243 },
  292. { "ocirc", 244 },
  293. { "oelig", 339 },
  294. { "ograve", 242 },
  295. { "oline", 8254 },
  296. { "omega", 969 },
  297. { "omicron", 959 },
  298. { "oplus", 8853 },
  299. { "or", 8744 },
  300. { "ordf", 170 },
  301. { "ordm", 186 },
  302. { "oslash", 248 },
  303. { "otilde", 245 },
  304. { "otimes", 8855 },
  305. { "ouml", 246 },
  306. { "para", 182 },
  307. { "part", 8706 },
  308. { "permil", 8240 },
  309. { "perp", 8869 },
  310. { "phi", 966 },
  311. { "pi", 960 },
  312. { "piv", 982 },
  313. { "plusmn", 177 },
  314. { "pound", 163 },
  315. { "prime", 8242 },
  316. { "prod", 8719 },
  317. { "prop", 8733 },
  318. { "psi", 968 },
  319. { "quot", '\"' },
  320. { "rArr", 8658 },
  321. { "radic", 8730 },
  322. { "rang", 9002 },
  323. { "raquo", 187 },
  324. { "rarr", 8594 },
  325. { "rceil", 8969 },
  326. { "rdquo", 8221 },
  327. { "real", 8476 },
  328. { "reg", 174 },
  329. { "rfloor", 8971 },
  330. { "rho", 961 },
  331. { "rlm", 8207 },
  332. { "rsaquo", 8250 },
  333. { "rsquo", 8217 },
  334. { "sbquo", 8218 },
  335. { "scaron", 353 },
  336. { "sdot", 8901 },
  337. { "sect", 167 },
  338. { "shy", 173 },
  339. { "sigma", 963 },
  340. { "sigmaf", 962 },
  341. { "sim", 8764 },
  342. { "spades", 9824 },
  343. { "sub", 8834 },
  344. { "sube", 8838 },
  345. { "sum", 8721 },
  346. { "sup", 8835 },
  347. { "sup1", 185 },
  348. { "sup2", 178 },
  349. { "sup3", 179 },
  350. { "supe", 8839 },
  351. { "szlig", 223 },
  352. { "tau", 964 },
  353. { "there4", 8756 },
  354. { "theta", 952 },
  355. { "thetasym", 977 },
  356. { "thinsp", 8201 },
  357. { "thorn", 254 },
  358. { "tilde", 732 },
  359. { "times", 215 },
  360. { "trade", 8482 },
  361. { "uArr", 8657 },
  362. { "uacute", 250 },
  363. { "uarr", 8593 },
  364. { "ucirc", 251 },
  365. { "ugrave", 249 },
  366. { "uml", 168 },
  367. { "upsih", 978 },
  368. { "upsilon", 965 },
  369. { "uuml", 252 },
  370. { "weierp", 8472 },
  371. { "xi", 958 },
  372. { "yacute", 253 },
  373. { "yen", 165 },
  374. { "yuml", 255 },
  375. { "zeta", 950 },
  376. { "zwj", 8205 },
  377. { "zwnj", 8204 }
  378. };
  379. /*
  380. * Do a binary search for the named entity...
  381. */
  382. first = 0;
  383. last = (int)(sizeof(entities) / sizeof(entities[0]) - 1);
  384. while ((last - first) > 1)
  385. {
  386. current = (first + last) / 2;
  387. if ((diff = strcmp(name, entities[current].name)) == 0)
  388. return (entities[current].val);
  389. else if (diff < 0)
  390. last = current;
  391. else
  392. first = current;
  393. }
  394. /*
  395. * If we get here, there is a small chance that there is still
  396. * a match; check first and last...
  397. */
  398. if (!strcmp(name, entities[first].name))
  399. return (entities[first].val);
  400. else if (!strcmp(name, entities[last].name))
  401. return (entities[last].val);
  402. else
  403. return (-1);
  404. }
  405. /*
  406. * End of "$Id: mxml-entity.c 451 2014-01-04 21:50:06Z msweet $".
  407. */