]>
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 | ||
b24c88b3 RC |
18 | #if 0 |
19 | static const char *cvsid = | |
20 | "\n%%% $Id$\n"; | |
21 | #endif | |
946fc045 | 22 | |
4a83b7b0 DD |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
25 | ||
26 | #include "hash.h" | |
27 | ||
b24c88b3 RC |
28 | class hash_internals |
29 | { | |
4a83b7b0 DD |
30 | public: |
31 | char **keys; | |
32 | int numkeys; | |
33 | int maxkeys; | |
34 | int prev_index; | |
35 | }; | |
36 | ||
37 | hash::hash () | |
38 | { | |
39 | h = new hash_internals; | |
40 | h->numkeys = 0; | |
41 | h->maxkeys = 10; | |
42 | h->keys = (char **) malloc (h->maxkeys * sizeof (char *)); | |
43 | h->prev_index = 0; | |
44 | } | |
45 | ||
46 | hash::~hash () | |
47 | { | |
48 | free (h->keys); | |
5e0464a1 | 49 | delete h; |
4a83b7b0 DD |
50 | } |
51 | ||
52 | ||
53 | void | |
fa0c0d10 | 54 | hash::add (char const *string) |
4a83b7b0 DD |
55 | { |
56 | int i; | |
b24c88b3 | 57 | for (i = 0; i < h->numkeys; i++) |
4a83b7b0 DD |
58 | if (strcmp (h->keys[i], string) == 0) |
59 | return; | |
60 | if (h->numkeys >= h->maxkeys) | |
61 | { | |
62 | h->maxkeys += 10; | |
63 | h->keys = (char **) realloc (h->keys, h->maxkeys * sizeof (char *)); | |
64 | } | |
65 | ||
5e0464a1 RC |
66 | h->keys[h->numkeys] = new char[strlen(string) + 1]; |
67 | strcpy (h->keys[h->numkeys], string); | |
b24c88b3 | 68 | h->numkeys++; |
4a83b7b0 DD |
69 | } |
70 | ||
71 | ||
72 | int | |
fa0c0d10 | 73 | hash::has (char const *string) |
4a83b7b0 DD |
74 | { |
75 | int i; | |
b24c88b3 | 76 | for (i = 0; i < h->numkeys; i++) |
4a83b7b0 DD |
77 | if (strcmp (h->keys[i], string) == 0) |
78 | return 1; | |
79 | return 0; | |
80 | } | |
81 | ||
82 | char * | |
fa0c0d10 | 83 | hash::enumerate (char const *prev) |
4a83b7b0 DD |
84 | { |
85 | if (prev == 0) | |
86 | h->prev_index = -1; | |
b24c88b3 | 87 | h->prev_index++; |
4a83b7b0 DD |
88 | if (h->prev_index >= h->numkeys) |
89 | return 0; | |
90 | return h->keys[h->prev_index]; | |
91 | } | |
92 | ||
93 | static int | |
94 | rev_len (const void *va, const void *vb) | |
95 | { | |
b24c88b3 RC |
96 | char *a = *(char **) va; |
97 | char *b = *(char **) vb; | |
4a83b7b0 DD |
98 | return strcmp (b, a); |
99 | } | |
100 | ||
101 | void | |
102 | hash::reverse_sort () | |
103 | { | |
104 | qsort (h->keys, h->numkeys, sizeof (h->keys[0]), rev_len); | |
105 | } |