Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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