]>
Commit | Line | Data |
---|---|---|
4a83b7b0 DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* Simple hash class for install.cc */ | |
17 | ||
946fc045 DD |
18 | static char *cvsid = "\n%%% $Id$\n"; |
19 | ||
4a83b7b0 DD |
20 | #include <stdlib.h> |
21 | #include <string.h> | |
22 | ||
23 | #include "hash.h" | |
24 | ||
25 | class hash_internals { | |
26 | public: | |
27 | char **keys; | |
28 | int numkeys; | |
29 | int maxkeys; | |
30 | int prev_index; | |
31 | }; | |
32 | ||
33 | hash::hash () | |
34 | { | |
35 | h = new hash_internals; | |
36 | h->numkeys = 0; | |
37 | h->maxkeys = 10; | |
38 | h->keys = (char **) malloc (h->maxkeys * sizeof (char *)); | |
39 | h->prev_index = 0; | |
40 | } | |
41 | ||
42 | hash::~hash () | |
43 | { | |
44 | free (h->keys); | |
45 | free (h); | |
46 | } | |
47 | ||
48 | ||
49 | void | |
50 | hash::add (char *string) | |
51 | { | |
52 | int i; | |
53 | for (i=0; i<h->numkeys; i++) | |
54 | if (strcmp (h->keys[i], string) == 0) | |
55 | return; | |
56 | if (h->numkeys >= h->maxkeys) | |
57 | { | |
58 | h->maxkeys += 10; | |
59 | h->keys = (char **) realloc (h->keys, h->maxkeys * sizeof (char *)); | |
60 | } | |
61 | ||
62 | h->keys[h->numkeys] = _strdup (string); | |
63 | h->numkeys ++; | |
64 | } | |
65 | ||
66 | ||
67 | int | |
68 | hash::has (char *string) | |
69 | { | |
70 | int i; | |
71 | for (i=0; i<h->numkeys; i++) | |
72 | if (strcmp (h->keys[i], string) == 0) | |
73 | return 1; | |
74 | return 0; | |
75 | } | |
76 | ||
77 | char * | |
78 | hash::enumerate (char *prev) | |
79 | { | |
80 | if (prev == 0) | |
81 | h->prev_index = -1; | |
82 | h->prev_index ++; | |
83 | if (h->prev_index >= h->numkeys) | |
84 | return 0; | |
85 | return h->keys[h->prev_index]; | |
86 | } | |
87 | ||
88 | static int | |
89 | rev_len (const void *va, const void *vb) | |
90 | { | |
91 | char *a = *(char **)va; | |
92 | char *b = *(char **)vb; | |
93 | return strcmp (b, a); | |
94 | } | |
95 | ||
96 | void | |
97 | hash::reverse_sort () | |
98 | { | |
99 | qsort (h->keys, h->numkeys, sizeof (h->keys[0]), rev_len); | |
100 | } |