Annotation of libwww/Library/src/HTAtom.c, revision 2.6
1.1 timbl 1: /* Atoms: Names to numbers HTAtom.c
2: ** =======================
3: **
4: ** Atoms are names which are given representative pointer values
5: ** so that they can be stored more efficiently, and comparisons
6: ** for equality done more efficiently.
7: **
8: ** Atoms are kept in a hash table consisting of an array of linked lists.
9: **
10: ** Authors:
11: ** TBL Tim Berners-Lee, WorldWideWeb project, CERN
12: ** (c) Copyright CERN 1991 - See Copyright.html
13: **
14: */
15: #define HASH_SIZE 101 /* Tunable */
16: #include "HTAtom.h"
17:
18: #include <stdio.h> /* joe@athena, TBL 921019 */
2.6 ! luotonen 19: #include <string.h>
! 20:
1.1 timbl 21: #include "HTUtils.h"
22:
23: PRIVATE HTAtom * hash_table[HASH_SIZE];
24: PRIVATE BOOL initialised = NO;
25:
2.6 ! luotonen 26: PUBLIC HTAtom * HTAtom_for ARGS1(CONST char *, string)
1.1 timbl 27: {
28: int hash;
29: CONST char * p;
30: HTAtom * a;
31:
32: /* First time around, clear hash table
33: */
34: if (!initialised) {
35: int i;
36: for (i=0; i<HASH_SIZE; i++)
37: hash_table[i] = (HTAtom *) 0;
38: initialised = YES;
39: }
40:
41: /* Generate hash function
42: */
43: for(p=string, hash=0; *p; p++) {
44: hash = (hash * 3 + *p) % HASH_SIZE;
45: }
46:
47: /* Search for the string in the list
48: */
49: for (a=hash_table[hash]; a; a=a->next) {
50: if (0==strcmp(a->name, string)) {
1.2 timbl 51: /* if (TRACE) fprintf(stderr,
52: "HTAtom: Old atom %p for `%s'\n", a, string); */
1.1 timbl 53: return a; /* Found: return it */
54: }
55: }
56:
57: /* Generate a new entry
58: */
59: a = (HTAtom *)malloc(sizeof(*a));
60: if (a == NULL) outofmem(__FILE__, "HTAtom_for");
61: a->name = (char *)malloc(strlen(string)+1);
62: if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
63: strcpy(a->name, string);
64: a->next = hash_table[hash]; /* Put onto the head of list */
65: hash_table[hash] = a;
1.2 timbl 66: /* if (TRACE) fprintf(stderr, "HTAtom: New atom %p for `%s'\n", a, string); */
1.1 timbl 67: return a;
68: }
69:
2.6 ! luotonen 70:
! 71: PRIVATE BOOL mime_match ARGS2(CONST char *, name,
! 72: CONST char *, templ)
! 73: {
! 74: if (name && templ) {
! 75: static char *n1 = NULL;
! 76: static char *t1 = NULL;
! 77: char *n2;
! 78: char *t2;
! 79:
! 80: StrAllocCopy(n1, name); /* These also free the ones */
! 81: StrAllocCopy(t1, templ); /* from previous call. */
! 82:
! 83: if (!(n2 = strchr(n1, '/')) || !(t2 = strchr(t1, '/')))
! 84: return NO;
! 85:
! 86: *(n2++) = (char)0;
! 87: *(t2++) = (char)0;
! 88:
! 89: if ((0==strcmp(t1, "*") || 0==strcmp(t1, n1)) &&
! 90: (0==strcmp(t2, "*") || 0==strcmp(t2, n2)))
! 91: return YES;
! 92: }
! 93: return NO;
! 94: }
! 95:
! 96:
! 97: PUBLIC HTList *HTAtom_templateMatches ARGS1(CONST char *, templ)
! 98: {
! 99: HTList *matches = HTList_new();
! 100:
! 101: if (initialised && templ) {
! 102: int i;
! 103: HTAtom *cur;
! 104:
! 105: for (i=0; i<HASH_SIZE; i++) {
! 106: for (cur = hash_table[i]; cur; cur=cur->next) {
! 107: if (mime_match(cur->name, templ))
! 108: HTList_addObject(matches, (void*)cur);
! 109: }
! 110: }
! 111: }
! 112: return matches;
! 113: }
1.1 timbl 114:
Webmaster