Annotation of XML/entities.c, revision 1.2

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.2     ! httpng     11:  * xmlFreeEntity : clean-up an entity record.
1.1       httpng     12:  */
                     13: 
1.2     ! httpng     14: void xmlFreeEntity(xmlEntityPtr entity) {
        !            15:     if (entity == NULL) return;
        !            16: 
        !            17:     entity->value = (CHAR) -1;
        !            18:     if (entity->id != NULL)
        !            19:        free((char *) entity->id);
        !            20: }
1.1       httpng     21: 
                     22: /*
1.2     ! httpng     23:  * xmlAddDocEntity : register a new entity for an entities table.
1.1       httpng     24:  */
1.2     ! httpng     25: static void xmlAddEntity(xmlEntitiesTablePtr table, CHAR value, const CHAR *id) {
        !            26:     int i;
        !            27:     xmlEntityPtr cur;
1.1       httpng     28: 
1.2     ! httpng     29:     for (i = 0;i < table->nb_entities;i++) {
        !            30:         cur = &table->table[i];
        !            31:        if ((cur->value == value) &&
        !            32:            (!strcmp(cur->id, id))) return;
        !            33:     }
        !            34:     if (table->nb_entities >= table->max_entities) {
        !            35:         /*
        !            36:         * need more elements.
        !            37:         */
        !            38:        table->max_entities *= 2;
        !            39:        table->table = (xmlEntityPtr) 
        !            40:            realloc(table->table, table->max_entities * sizeof(xmlEntity));
        !            41:        if (table->table) {
        !            42:            perror("realloc failed");
        !            43:            exit(1);
        !            44:        }
        !            45:     }
        !            46:     cur = &table->table[table->nb_entities];
        !            47:     cur->value = value;
        !            48:     cur->id = strdup(id);
        !            49:     table->nb_entities++;
        !            50: }
1.1       httpng     51: 
                     52: 
                     53: /*
1.2     ! httpng     54:  * xmlAddDtdEntity : register a new entity for this document.
1.1       httpng     55:  */
1.2     ! httpng     56: void xmlAddDtdEntity(xmlDtdPtr dtd, CHAR value, const CHAR *id) {
        !            57:     xmlEntitiesTablePtr table;
1.1       httpng     58: 
1.2     ! httpng     59:     table = (xmlEntitiesTablePtr) dtd->entities;
        !            60:     if (table == NULL) {
        !            61:         table = xmlCreateEntitiesTable();
        !            62:        dtd->entities = table;
1.1       httpng     63:     }
1.2     ! httpng     64:     xmlAddEntity(table, value, id);
1.1       httpng     65: }
                     66: 
                     67: /*
1.2     ! httpng     68:  * xmlAddDocEntity : register a new entity for this document.
1.1       httpng     69:  */
1.2     ! httpng     70: void xmlAddDocEntity(xmlDocPtr doc, CHAR value, const CHAR *id) {
        !            71:     xmlEntitiesTablePtr table;
1.1       httpng     72: 
1.2     ! httpng     73:     table = (xmlEntitiesTablePtr) doc->entities;
        !            74:     if (table == NULL) {
        !            75:         table = xmlCreateEntitiesTable();
        !            76:        doc->entities = table;
        !            77:     }
        !            78:     xmlAddEntity(table, value, id);
1.1       httpng     79: }
                     80: 
                     81: /*
                     82:  * xmlGetEntity : do an entity lookup in the hash table and
                     83:  *       returns the corrsponding CHAR, if found, zero otherwise.
                     84:  */
                     85: CHAR xmlGetEntity(xmlDocPtr doc, const CHAR *id) {
1.2     ! httpng     86:     int i;
        !            87:     xmlEntityPtr cur;
        !            88:     xmlEntitiesTablePtr table;
        !            89: 
        !            90:     if (doc->entities == NULL) return(0);
        !            91:     for (i = 0;i < table->nb_entities;i++) {
        !            92:         cur = &table->table[i];
        !            93:        if (!strcmp(cur->id, id)) return(cur->value);
        !            94:     }
        !            95:     return(0);
1.1       httpng     96: }
                     97: 
                     98: /*
                     99:  * xmlSubstituteEntities : do a global entities lookup on a input string
                    100:  *        and returns a duplicate after the entities substitution.
                    101:  */
                    102: CHAR *xmlSubstituteEntities(xmlDocPtr doc, const CHAR *input) {
                    103: }
                    104: 
                    105: /*
1.2     ! httpng    106:  * xmlEncodeEntities : do a global encoding of a string, replacing the
        !           107:  *        basic values with their entyties form.
1.1       httpng    108:  */
1.2     ! httpng    109: CHAR *xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) {
        !           110: }
        !           111: 
        !           112: /*
        !           113:  * xmlCreateEntitiesTable : create and initialize an enmpty hash table
        !           114:  */
        !           115: xmlEntitiesTablePtr xmlCreateEntitiesTable(void) {
        !           116:     xmlEntitiesTablePtr ret;
1.1       httpng    117: 
1.2     ! httpng    118:     ret = (xmlEntitiesTablePtr) 
        !           119:          malloc(sizeof(xmlEntitiesTable));
1.1       httpng    120:     if (ret == NULL) {
1.2     ! httpng    121:         fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
        !           122:                sizeof(xmlEntitiesTable));
        !           123:         return(NULL);
        !           124:     }
        !           125:     ret->max_entities = XML_MIN_ENTITIES_TABLE;
        !           126:     ret->nb_entities = 0;
        !           127:     ret->table = (xmlEntityPtr ) 
        !           128:          malloc(ret->max_entities * sizeof(xmlEntity));
        !           129:     if (ret == NULL) {
        !           130:         fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n",
        !           131:                ret->max_entities * sizeof(xmlEntity));
        !           132:        free(ret);
1.1       httpng    133:         return(NULL);
                    134:     }
                    135:     return(ret);
                    136: }
                    137: 
                    138: /*
1.2     ! httpng    139:  * xmlFreeEntitiesTable : clean up and free an entities hash table.
1.1       httpng    140:  */
1.2     ! httpng    141: void xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
1.1       httpng    142:     int i;
                    143: 
                    144:     if (table == NULL) return;
                    145: 
1.2     ! httpng    146:     for (i = 0;i < table->nb_entities;i++) {
        !           147:         xmlFreeEntity(&table->table[i]);
1.1       httpng    148:     }
1.2     ! httpng    149:     free(table->table);
1.1       httpng    150:     free(table);
                    151: }
                    152: 

Webmaster