Annotation of XML/entities.c, revision 1.3
1.1 httpng 1: /*
2: * entities.c : implementation for the XML entities handking
3: */
4:
5: #include <stdio.h>
6: #include <malloc.h>
1.2 httpng 7: #include <strings.h>
1.1 httpng 8: #include "entities.h"
9:
10: /*
1.3 ! httpng 11: * A buffer used for converting entities to their equivalent and back.
! 12: */
! 13: CHAR *buffer = NULL;
! 14: static int buffer_size = 0;
! 15:
! 16: void growBuffer(void) {
! 17: buffer_size *= 2;
! 18: buffer = (CHAR *) realloc(buffer, buffer_size * sizeof(CHAR));
! 19: if (buffer == NULL) {
! 20: perror("realloc failed");
! 21: exit(1);
! 22: }
! 23: }
! 24:
! 25: /*
1.2 httpng 26: * xmlFreeEntity : clean-up an entity record.
1.1 httpng 27: */
28:
1.2 httpng 29: void xmlFreeEntity(xmlEntityPtr entity) {
30: if (entity == NULL) return;
31:
32: entity->value = (CHAR) -1;
33: if (entity->id != NULL)
34: free((char *) entity->id);
35: }
1.1 httpng 36:
37: /*
1.2 httpng 38: * xmlAddDocEntity : register a new entity for an entities table.
1.1 httpng 39: */
1.2 httpng 40: static void xmlAddEntity(xmlEntitiesTablePtr table, CHAR value, const CHAR *id) {
41: int i;
42: xmlEntityPtr cur;
1.1 httpng 43:
1.2 httpng 44: for (i = 0;i < table->nb_entities;i++) {
45: cur = &table->table[i];
46: if ((cur->value == value) &&
47: (!strcmp(cur->id, id))) return;
48: }
49: if (table->nb_entities >= table->max_entities) {
50: /*
51: * need more elements.
52: */
53: table->max_entities *= 2;
54: table->table = (xmlEntityPtr)
55: realloc(table->table, table->max_entities * sizeof(xmlEntity));
56: if (table->table) {
57: perror("realloc failed");
58: exit(1);
59: }
60: }
61: cur = &table->table[table->nb_entities];
62: cur->value = value;
63: cur->id = strdup(id);
64: table->nb_entities++;
65: }
1.1 httpng 66:
67:
68: /*
1.2 httpng 69: * xmlAddDtdEntity : register a new entity for this document.
1.1 httpng 70: */
1.2 httpng 71: void xmlAddDtdEntity(xmlDtdPtr dtd, CHAR value, const CHAR *id) {
72: xmlEntitiesTablePtr table;
1.1 httpng 73:
1.2 httpng 74: table = (xmlEntitiesTablePtr) dtd->entities;
75: if (table == NULL) {
76: table = xmlCreateEntitiesTable();
77: dtd->entities = table;
1.1 httpng 78: }
1.2 httpng 79: xmlAddEntity(table, value, id);
1.1 httpng 80: }
81:
82: /*
1.2 httpng 83: * xmlAddDocEntity : register a new entity for this document.
1.1 httpng 84: */
1.2 httpng 85: void xmlAddDocEntity(xmlDocPtr doc, CHAR value, const CHAR *id) {
86: xmlEntitiesTablePtr table;
1.1 httpng 87:
1.2 httpng 88: table = (xmlEntitiesTablePtr) doc->entities;
89: if (table == NULL) {
90: table = xmlCreateEntitiesTable();
91: doc->entities = table;
92: }
93: xmlAddEntity(table, value, id);
1.1 httpng 94: }
95:
96: /*
97: * xmlGetEntity : do an entity lookup in the hash table and
98: * returns the corrsponding CHAR, if found, zero otherwise.
99: */
100: CHAR xmlGetEntity(xmlDocPtr doc, const CHAR *id) {
1.2 httpng 101: int i;
102: xmlEntityPtr cur;
103: xmlEntitiesTablePtr table;
104:
105: if (doc->entities == NULL) return(0);
106: for (i = 0;i < table->nb_entities;i++) {
107: cur = &table->table[i];
108: if (!strcmp(cur->id, id)) return(cur->value);
109: }
110: return(0);
1.1 httpng 111: }
112:
113: /*
114: * xmlSubstituteEntities : do a global entities lookup on a input string
115: * and returns a duplicate after the entities substitution.
116: */
1.3 ! httpng 117: const CHAR *xmlReadEntity(xmlDocPtr doc, const CHAR *input, CHAR **value) {
! 118: static CHAR entity[100];
! 119: int i;
! 120:
! 121: *value = NULL;
! 122: if (*input == '&') {
! 123: input++;
! 124: if (*input == '#') {
! 125: } else {
! 126: for (i = 0;i < 99;i++) {
! 127: !!!!!!!
! 128: }
! 129: }
! 130: }
! 131: return(input);
! 132: }
! 133:
! 134: /*
! 135: * xmlSubstituteEntities : do a global entities lookup on a input string
! 136: * and returns a duplicate after the entities substitution.
! 137: */
1.1 httpng 138: CHAR *xmlSubstituteEntities(xmlDocPtr doc, const CHAR *input) {
1.3 ! httpng 139: CHAR *cur = input;
! 140: CHAR *out = buffer;
! 141: int i;
! 142:
! 143: if (buffer == NULL) {
! 144: buffer_size = 1000;
! 145: buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
! 146: if (buffer == NULL) {
! 147: perror("malloc failed");
! 148: exit(1);
! 149: }
! 150: out = buffer;
! 151: }
! 152: for (i = 0;*cur != 0;cur++) {
! 153: if (*cur == '&') {
! 154: CHAR *entity;
! 155:
! 156: cur = xmlReadEntity(doc, cur, &entity);
! 157: if (entity != NULL)
! 158: while (*entity != 0) {
! 159: *out++ = *entity++;
! 160: i++;
! 161: if (i + 10 > buffer_size) growBuffer();
! 162: }
! 163: } else if (*cur == '%') {
! 164: } else {
! 165: *out++ == *cur;
! 166: i++;
! 167: }
! 168:
! 169: if (i + 10 > buffer_size) growBuffer();
! 170: }
! 171: *out++ == 0;
! 172: return(buffer);
1.1 httpng 173: }
174:
175: /*
1.2 httpng 176: * xmlEncodeEntities : do a global encoding of a string, replacing the
177: * basic values with their entyties form.
1.1 httpng 178: */
1.2 httpng 179: CHAR *xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) {
1.3 ! httpng 180: CHAR *cur = input;
! 181: CHAR *out = buffer;
! 182:
! 183: if (buffer == NULL) {
! 184: buffer_size = 1000;
! 185: buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
! 186: if (buffer == NULL) {
! 187: perror("malloc failed");
! 188: exit(1);
! 189: }
! 190: out = buffer;
! 191: }
1.2 httpng 192: }
193:
194: /*
195: * xmlCreateEntitiesTable : create and initialize an enmpty hash table
196: */
197: xmlEntitiesTablePtr xmlCreateEntitiesTable(void) {
198: xmlEntitiesTablePtr ret;
1.1 httpng 199:
1.2 httpng 200: ret = (xmlEntitiesTablePtr)
201: malloc(sizeof(xmlEntitiesTable));
1.1 httpng 202: if (ret == NULL) {
1.2 httpng 203: fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
204: sizeof(xmlEntitiesTable));
205: return(NULL);
206: }
207: ret->max_entities = XML_MIN_ENTITIES_TABLE;
208: ret->nb_entities = 0;
209: ret->table = (xmlEntityPtr )
210: malloc(ret->max_entities * sizeof(xmlEntity));
211: if (ret == NULL) {
212: fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
213: ret->max_entities * sizeof(xmlEntity));
214: free(ret);
1.1 httpng 215: return(NULL);
216: }
217: return(ret);
218: }
219:
220: /*
1.2 httpng 221: * xmlFreeEntitiesTable : clean up and free an entities hash table.
1.1 httpng 222: */
1.2 httpng 223: void xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
1.1 httpng 224: int i;
225:
226: if (table == NULL) return;
227:
1.2 httpng 228: for (i = 0;i < table->nb_entities;i++) {
229: xmlFreeEntity(&table->table[i]);
1.1 httpng 230: }
1.2 httpng 231: free(table->table);
1.1 httpng 232: free(table);
233: }
234:
Webmaster