~ubuntu-dev/ubuntu/lucid/mutt/lucid-201002110857

« back to all changes in this revision

Viewing changes to hash.c

  • Committer: Bazaar Package Importer
  • Author(s): أحمد المحمودي (Ahmed El-Mahmoudy)
  • Date: 2009-06-17 17:17:28 UTC
  • mfrom: (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20090617171728-61dkl7w5fgn7ybdq
Tags: upstream-1.5.20
Import upstream version 1.5.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
 
2
 * Copyright (C) 1996-2009 Michael R. Elkins <me@mutt.org>
3
3
 *
4
4
 *     This program is free software; you can redistribute it and/or modify
5
5
 *     it under the terms of the GNU General Public License as published by
23
23
#include <stdlib.h>
24
24
#include <stdio.h>
25
25
#include <string.h>
 
26
#include <ctype.h>
26
27
 
27
28
#include "mutt.h"
28
29
 
29
30
#define SOMEPRIME 149711
30
31
 
31
 
unsigned int hash_string (const unsigned char *s, unsigned int n)
 
32
static unsigned int hash_string (const unsigned char *s, unsigned int n)
32
33
{
33
34
  unsigned int h = 0;
34
35
 
39
40
  return h;
40
41
}
41
42
 
42
 
HASH *hash_create (int nelem)
 
43
static unsigned int hash_case_string (const unsigned char *s, unsigned int n)
 
44
{
 
45
  unsigned int h = 0;
 
46
 
 
47
  while (*s)
 
48
    h += (h << 7) + tolower (*s++);
 
49
  h = (h * SOMEPRIME) % n;
 
50
 
 
51
  return h;
 
52
}
 
53
 
 
54
HASH *hash_create (int nelem, int lower)
43
55
{
44
56
  HASH *table = safe_malloc (sizeof (HASH));
45
57
  if (nelem == 0)
46
58
    nelem = 2;
47
59
  table->nelem = nelem;
48
60
  table->table = safe_calloc (nelem, sizeof (struct hash_elem *));
 
61
  if (lower)
 
62
  {
 
63
    table->hash_string = hash_case_string;
 
64
    table->cmp_string = mutt_strcasecmp;
 
65
  }
 
66
  else
 
67
  {
 
68
    table->hash_string = hash_string;
 
69
    table->cmp_string = mutt_strcmp;
 
70
  }
49
71
  return table;
50
72
}
51
73
 
57
79
int hash_insert (HASH * table, const char *key, void *data, int allow_dup)
58
80
{
59
81
  struct hash_elem *ptr;
60
 
  int h;
 
82
  unsigned int h;
61
83
 
62
84
  ptr = (struct hash_elem *) safe_malloc (sizeof (struct hash_elem));
63
 
  h = hash_string ((unsigned char *) key, table->nelem);
 
85
  h = table->hash_string ((unsigned char *) key, table->nelem);
64
86
  ptr->key = key;
65
87
  ptr->data = data;
66
88
 
76
98
 
77
99
    for (tmp = table->table[h], last = NULL; tmp; last = tmp, tmp = tmp->next)
78
100
    {
79
 
      r = mutt_strcmp (tmp->key, key);
 
101
      r = table->cmp_string (tmp->key, key);
80
102
      if (r == 0)
81
103
      {
82
104
        FREE (&ptr);
99
121
  struct hash_elem *ptr = table->table[hash];
100
122
  for (; ptr; ptr = ptr->next)
101
123
  {
102
 
    if (mutt_strcmp (key, ptr->key) == 0)
 
124
    if (table->cmp_string (key, ptr->key) == 0)
103
125
      return (ptr->data);
104
126
  }
105
127
  return NULL;
114
136
  while (ptr) 
115
137
  {
116
138
    if ((data == ptr->data || !data)
117
 
        && mutt_strcmp (ptr->key, key) == 0)
 
139
        && table->cmp_string (ptr->key, key) == 0)
118
140
    {
119
141
      *last = ptr->next;
120
142
      if (destroy)