4 * Character entity support code for Mini-XML, a small XML-like
5 * file parsing library.
7 * Copyright 2003-2005 by Michael Sweet.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 * mxmlEntityAddCallback() - Add a callback to convert entities to
23 * mxmlEntityGetName() - Get the name that corresponds to the
25 * mxmlEntityGetValue() - Get the character corresponding to a named
27 * mxmlEntityRemoveCallback() - Remove a callback.
28 * default_callback() - Lookup standard (X)HTML entities.
32 * Include necessary headers...
43 static int default_callback(const char *name
);
50 static int num_callbacks
= 1;
51 static int (*callbacks
[100])(const char *name
) =
58 * 'mxmlEntityAddCallback()' - Add a callback to convert entities to Unicode.
61 int /* O - 0 on success, -1 on failure */
62 mxmlEntityAddCallback(int (*cb
)(const char *name
))
63 /* I - Callback function to add */
65 if (num_callbacks
< (int)(sizeof(callbacks
) / sizeof(callbacks
[0])))
67 callbacks
[num_callbacks
] = cb
;
74 mxml_error("Unable to add entity callback!");
82 * 'mxmlEntityGetName()' - Get the name that corresponds to the character value.
84 * If val does not need to be represented by a named entity, NULL is returned.
87 const char * /* O - Entity name or NULL */
88 mxmlEntityGetName(int val
) /* I - Character value */
111 * 'mxmlEntityGetValue()' - Get the character corresponding to a named entity.
113 * The entity name can also be a numeric constant. -1 is returned if the
117 int /* O - Character value or -1 on error */
118 mxmlEntityGetValue(const char *name
) /* I - Entity name */
120 int i
; /* Looping var */
121 int ch
; /* Character value */
124 for (i
= 0; i
< num_callbacks
; i
++)
125 if ((ch
= (callbacks
[i
])(name
)) >= 0)
133 * 'mxmlEntityRemoveCallback()' - Remove a callback.
137 mxmlEntityRemoveCallback(int (*cb
)(const char *name
))
138 /* I - Callback function to remove */
140 int i
; /* Looping var */
143 for (i
= 0; i
< num_callbacks
; i
++)
144 if (cb
== callbacks
[i
])
147 * Remove the callback...
152 if (i
< num_callbacks
)
153 memmove(callbacks
+ i
, callbacks
+ i
+ 1,
154 (num_callbacks
- i
) * sizeof(callbacks
[0]));
162 * 'default_callback()' - Lookup standard (X)HTML entities.
165 static int /* O - Unicode value or -1 */
166 default_callback(const char *name
) /* I - Entity name */
168 int diff
, /* Difference between names */
169 current
, /* Current entity in search */
170 first
, /* First entity in search */
171 last
; /* Last entity in search */
174 const char *name
; /* Entity name */
175 int val
; /* Character value */
438 * Do a binary search for the named entity...
442 last
= (int)(sizeof(entities
) / sizeof(entities
[0]) - 1);
444 while ((last
- first
) > 1)
446 current
= (first
+ last
) / 2;
448 if ((diff
= strcmp(name
, entities
[current
].name
)) == 0)
449 return (entities
[current
].val
);
457 * If we get here, there is a small chance that there is still
458 * a match; check first and last...
461 if (!strcmp(name
, entities
[first
].name
))
462 return (entities
[first
].val
);
463 else if (!strcmp(name
, entities
[last
].name
))
464 return (entities
[last
].val
);