1
// Copyright (C) 2000 - 2002 Hewlett-Packard Company
3
// This program is free software; you can redistribute it and/or modify it
4
// under the term of the GNU Lesser General Public License as published by the
5
// Free Software Foundation; either version 2 of the License, or (at your
6
// option) any later version.
8
// This program is distributed in the hope that it will be useful, but WITHOUT
9
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13
// You should have received a copy of the GNU Lesser General Public License
14
// along with this program; if not, write to the Free Software Foundation,
15
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// @(#) $Revision: 4.5 $ $Source: /judy/src/apps/demo/interSL.c $
20
// INTERACTIVE JUDYSL DEMO PROGRAM
22
// This program is a very simple interactive demonstration of JudySL. Text
23
// keywords are entered. The program uses that text as a key into a JudySL
24
// array and increments a usage count.
26
// Keys can be single keywords like, "mykey", or multiple words like, "now is
27
// the time for the quick brown fox to take the bull by the horns".
29
// The program recognizes the following keywords as special:
31
// CMD_LIST lists all the currently entered keys and their counts
32
// CMD_QUIT terminates the program
34
// This program demonstrates:
39
// how to access a JudySL value
41
// Note: Using JudySL gives you fast lookups as with hashing but without
42
// having to define a hash function, and without having to predetermine the
43
// hash table size. It also gives you a sorted list at the same time.
51
// Commands recognized by the program:
53
#define CMD_LIST "list"
54
#define CMD_QUIT "quit"
56
#define PLURAL(count) ((count == 1) ? "" : "s")
60
char Index [BUFSIZ]; // value from user.
61
void ** PPValue; // associated with Index.
62
void * PArray = (Pvoid_t) NULL; // JudySL array.
63
JError_t JError; // Judy error structure
64
char * Pc; // place in string.
68
(void) puts ("JudySL interactive demonstration:");
69
(void) puts ("When asked for input, enter some text or:");
70
(void) printf ("- \"%s\" to list previously entered text or\n", CMD_LIST);
71
(void) printf ("- \"%s\" (or EOF) to quit the program\n\n", CMD_QUIT);
78
(void) printf ("\nEnter key/list/quit: ");
79
(void) fflush (stdout);
81
if (fgets (Index, BUFSIZ, stdin) == (char *) NULL)
84
if ((Pc = strchr (Index, '\n')) != (char *) NULL)
85
*Pc = '\0'; // strip trailing newline.
89
if (! strcmp (Index, CMD_QUIT)) break;
92
// LIST ALL INPUT IN ALPHABETICAL ORDER:
94
if (! strcmp (Index, CMD_LIST))
98
for (PPValue = JudySLFirst (PArray, Index, 0);
99
PPValue != (PPvoid_t) NULL;
100
PPValue = JudySLNext (PArray, Index, 0))
102
(void) printf (" \"%s\" stored %lu time%s\n",
103
Index, *((PWord_t) PPValue),
104
PLURAL (*((PWord_t) PPValue)));
111
// ALL OTHER VALUES ARE KEYS:
113
// Insert Index into the array. If Index already exists, JudySLIns() returns a
114
// pointer to the old value. If Index doesn't already exist, then a slot is
115
// created and a pointer to this slot (initialized to zero) is returned.
117
if ((PPValue = JudySLIns (& PArray, Index, &JError)) == PPJERR)
118
{ // assume out of memory.
119
(void) printf ("Error %d, cannot insert \"%s\" in array.\n",
120
JU_ERRNO(&JError), Index);
124
++(*((PWord_t) PPValue)); // increment usage count.
126
(void) printf (" \"%s\" stored %ld time%s\n",
127
Index, *((PWord_t) PPValue),
128
PLURAL (*((PWord_t) PPValue)));
130
} // while 1 (continuous loop until user quits)