Annotation of libwww/Library/src/HTAtom.c, revision 2.11

2.8       frystyk     1: /*                                                                    HTAtom.c
                      2: **     ATOMS: STRINSGS TO NUMBERS
                      3: **
                      4: **     (c) COPYRIGHT CERN 1994.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: **     Atoms are names which are given representative pointer values
                      8: **     so that they can be stored more efficiently, and comparisons
                      9: **     for equality done more efficiently.
                     10: **
                     11: **     Atoms are kept in a hash table consisting of an array of linked lists.
                     12: **
                     13: ** Authors:
                     14: **     TBL     Tim Berners-Lee, WorldWideWeb project, CERN
                     15: **
                     16: */
2.10      roeber     17: 
2.11    ! frystyk    18: /* Library include files */
        !            19: #include "tcp.h"
        !            20: #include "HTUtils.h"
        !            21: #include "HTString.h"
        !            22: #include "HTList.h"
        !            23: #include "HTAtom.h"
2.10      roeber     24: 
1.1       timbl      25: #define HASH_SIZE      101             /* Tunable */
                     26: 
                     27: PRIVATE HTAtom * hash_table[HASH_SIZE];
                     28: PRIVATE BOOL initialised = NO;
                     29: 
2.9       frystyk    30: /*
                     31: **     Finds an atom representation for a string. The atom doesn't have to be
                     32: **     a new one but can be an already existing atom.
                     33: */
2.6       luotonen   34: PUBLIC HTAtom * HTAtom_for ARGS1(CONST char *, string)
1.1       timbl      35: {
                     36:     int hash;
                     37:     CONST char * p;
                     38:     HTAtom * a;
                     39:     
                     40:     /*         First time around, clear hash table
                     41:     */
                     42:     if (!initialised) {
                     43:         int i;
                     44:        for (i=0; i<HASH_SIZE; i++)
                     45:            hash_table[i] = (HTAtom *) 0;
                     46:        initialised = YES;
                     47:     }
                     48:     
                     49:     /*         Generate hash function
                     50:     */
                     51:     for(p=string, hash=0; *p; p++) {
                     52:         hash = (hash * 3 + *p) % HASH_SIZE;
                     53:     }
                     54:     
                     55:     /*         Search for the string in the list
                     56:     */
                     57:     for (a=hash_table[hash]; a; a=a->next) {
                     58:        if (0==strcmp(a->name, string)) {
2.11    ! frystyk    59:            /* if (TRACE) fprintf(TDEST,
1.2       timbl      60:                "HTAtom: Old atom %p for `%s'\n", a, string); */
1.1       timbl      61:            return a;                           /* Found: return it */
                     62:        }
                     63:     }
                     64:     
                     65:     /*         Generate a new entry
                     66:     */
                     67:     a = (HTAtom *)malloc(sizeof(*a));
                     68:     if (a == NULL) outofmem(__FILE__, "HTAtom_for");
                     69:     a->name = (char *)malloc(strlen(string)+1);
                     70:     if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
                     71:     strcpy(a->name, string);
                     72:     a->next = hash_table[hash];                /* Put onto the head of list */
                     73:     hash_table[hash] = a;
2.11    ! frystyk    74: /*    if (TRACE) fprintf(TDEST, "HTAtom: New atom %p for `%s'\n", a, string); */
1.1       timbl      75:     return a;
2.9       frystyk    76: }
                     77: 
                     78: 
                     79: /*
                     80: **     This function cleans up the memory used by atoms.
                     81: **     Written by Eric Sink, eric@spyglass.com
                     82: */
                     83: PUBLIC void HTAtom_deleteAll NOARGS
                     84: {
                     85:     int i;
                     86:     HTAtom *cur;
                     87:     HTAtom *next;
                     88:     
                     89:     for (i=0; i<HASH_SIZE; i++) {
                     90:        if (hash_table[i]) {
                     91:            cur = hash_table[i];
                     92:            while (cur) {
                     93:                next = cur->next;
                     94:                free(cur->name);
                     95:                free(cur);
                     96:                cur = next;     
                     97:            }   
                     98:        }
                     99:     }
1.1       timbl     100: }
                    101: 
2.6       luotonen  102: 
                    103: PRIVATE BOOL mime_match ARGS2(CONST char *, name,
                    104:                              CONST char *, templ)
                    105: {
                    106:     if (name && templ) {
                    107:        static char *n1 = NULL;
                    108:        static char *t1 = NULL;
                    109:        char *n2;
                    110:        char *t2;
                    111: 
                    112:        StrAllocCopy(n1, name);         /* These also free the ones     */
                    113:        StrAllocCopy(t1, templ);        /* from previous call.          */
                    114: 
                    115:        if (!(n2 = strchr(n1, '/'))  ||  !(t2 = strchr(t1, '/')))
                    116:            return NO;
                    117: 
                    118:        *(n2++) = (char)0;
                    119:        *(t2++) = (char)0;
                    120: 
                    121:        if ((0==strcmp(t1, "*") || 0==strcmp(t1, n1)) &&
                    122:            (0==strcmp(t2, "*") || 0==strcmp(t2, n2)))
                    123:            return YES;
                    124:     }
                    125:     return NO;
                    126: }
                    127:        
                    128: 
                    129: PUBLIC HTList *HTAtom_templateMatches ARGS1(CONST char *, templ)
                    130: {
                    131:     HTList *matches = HTList_new();
                    132: 
                    133:     if (initialised && templ) {
                    134:        int i;
                    135:        HTAtom *cur;
                    136: 
                    137:        for (i=0; i<HASH_SIZE; i++) {
                    138:            for (cur = hash_table[i];  cur;  cur=cur->next) {
                    139:                if (mime_match(cur->name, templ))
                    140:                    HTList_addObject(matches, (void*)cur);
                    141:            }
                    142:        }
                    143:     }
                    144:     return matches;
                    145: }
1.1       timbl     146: 

Webmaster