Annotation of XML/entities.c, revision 1.5
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);
1.5 ! veillard 106: table = (xmlEntitiesTablePtr) doc->entities;
1.2 httpng 107: for (i = 0;i < table->nb_entities;i++) {
108: cur = &table->table[i];
109: if (!strcmp(cur->id, id)) return(cur->value);
110: }
111: return(0);
1.1 httpng 112: }
113:
114: /*
115: * xmlSubstituteEntities : do a global entities lookup on a input string
116: * and returns a duplicate after the entities substitution.
117: */
1.3 httpng 118: const CHAR *xmlReadEntity(xmlDocPtr doc, const CHAR *input, CHAR **value) {
119: static CHAR entity[100];
120: int i;
121:
122: *value = NULL;
123: if (*input == '&') {
124: input++;
125: if (*input == '#') {
126: } else {
127: for (i = 0;i < 99;i++) {
1.4 veillard 128: /* !!!!!!! */
1.3 httpng 129: }
130: }
131: }
132: return(input);
133: }
134:
135: /*
136: * xmlSubstituteEntities : do a global entities lookup on a input string
137: * and returns a duplicate after the entities substitution.
138: */
1.1 httpng 139: CHAR *xmlSubstituteEntities(xmlDocPtr doc, const CHAR *input) {
1.3 httpng 140: CHAR *cur = input;
141: CHAR *out = buffer;
142: int i;
143:
144: if (buffer == NULL) {
145: buffer_size = 1000;
146: buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
147: if (buffer == NULL) {
148: perror("malloc failed");
149: exit(1);
150: }
151: out = buffer;
152: }
153: for (i = 0;*cur != 0;cur++) {
154: if (*cur == '&') {
155: CHAR *entity;
156:
157: cur = xmlReadEntity(doc, cur, &entity);
158: if (entity != NULL)
159: while (*entity != 0) {
160: *out++ = *entity++;
161: i++;
162: if (i + 10 > buffer_size) growBuffer();
163: }
164: } else if (*cur == '%') {
165: } else {
166: *out++ == *cur;
167: i++;
168: }
169:
170: if (i + 10 > buffer_size) growBuffer();
171: }
172: *out++ == 0;
173: return(buffer);
1.1 httpng 174: }
175:
176: /*
1.2 httpng 177: * xmlEncodeEntities : do a global encoding of a string, replacing the
178: * basic values with their entyties form.
1.1 httpng 179: */
1.2 httpng 180: CHAR *xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) {
1.3 httpng 181: CHAR *cur = input;
182: CHAR *out = buffer;
183:
1.5 ! veillard 184: /* !!!!!!!!!!!!!! */
1.3 httpng 185: if (buffer == NULL) {
186: buffer_size = 1000;
187: buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR));
188: if (buffer == NULL) {
189: perror("malloc failed");
190: exit(1);
191: }
192: out = buffer;
193: }
1.5 ! veillard 194: return(out);
1.2 httpng 195: }
196:
197: /*
198: * xmlCreateEntitiesTable : create and initialize an enmpty hash table
199: */
200: xmlEntitiesTablePtr xmlCreateEntitiesTable(void) {
201: xmlEntitiesTablePtr ret;
1.1 httpng 202:
1.2 httpng 203: ret = (xmlEntitiesTablePtr)
204: malloc(sizeof(xmlEntitiesTable));
1.1 httpng 205: if (ret == NULL) {
1.2 httpng 206: fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
207: sizeof(xmlEntitiesTable));
208: return(NULL);
209: }
210: ret->max_entities = XML_MIN_ENTITIES_TABLE;
211: ret->nb_entities = 0;
212: ret->table = (xmlEntityPtr )
213: malloc(ret->max_entities * sizeof(xmlEntity));
214: if (ret == NULL) {
215: fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
216: ret->max_entities * sizeof(xmlEntity));
217: free(ret);
1.1 httpng 218: return(NULL);
219: }
220: return(ret);
221: }
222:
223: /*
1.2 httpng 224: * xmlFreeEntitiesTable : clean up and free an entities hash table.
1.1 httpng 225: */
1.2 httpng 226: void xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
1.1 httpng 227: int i;
228:
229: if (table == NULL) return;
230:
1.2 httpng 231: for (i = 0;i < table->nb_entities;i++) {
232: xmlFreeEntity(&table->table[i]);
1.1 httpng 233: }
1.2 httpng 234: free(table->table);
1.1 httpng 235: free(table);
236: }
237:
Webmaster