~ubuntu-branches/ubuntu/breezy/judy/breezy

« back to all changes in this revision

Viewing changes to src/apps/demo/interSL.c

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Y. Ts'o
  • Date: 2004-01-17 00:04:53 UTC
  • Revision ID: james.westby@ubuntu.com-20040117000453-d5sj6uoon2v1g4gf
Tags: upstream-0.0.4
ImportĀ upstreamĀ versionĀ 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000 - 2002 Hewlett-Packard Company
 
2
//
 
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.
 
7
//
 
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
 
11
// for more details.
 
12
//
 
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
 
16
// _________________
 
17
 
 
18
// @(#) $Revision: 4.5 $ $Source: /judy/src/apps/demo/interSL.c $
 
19
//
 
20
// INTERACTIVE JUDYSL DEMO PROGRAM
 
21
//
 
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.
 
25
//
 
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".
 
28
//
 
29
// The program recognizes the following keywords as special:
 
30
//
 
31
//    CMD_LIST  lists all the currently entered keys and their counts
 
32
//    CMD_QUIT  terminates the program
 
33
//
 
34
// This program demonstrates:
 
35
//
 
36
//    JudySLIns
 
37
//    JudySLFirst
 
38
//    JudySLNext
 
39
//    how to access a JudySL value
 
40
//
 
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.
 
44
 
 
45
#include <stdio.h>
 
46
#include <string.h>
 
47
#include <errno.h>
 
48
 
 
49
#include "Judy.h"
 
50
 
 
51
// Commands recognized by the program:
 
52
 
 
53
#define CMD_LIST "list"
 
54
#define CMD_QUIT "quit"
 
55
 
 
56
#define PLURAL(count) ((count == 1) ? "" : "s")
 
57
 
 
58
main()
 
59
{
 
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.
 
65
 
 
66
// EMIT INSTRUCTIONS:
 
67
 
 
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);
 
72
 
 
73
 
 
74
// ACCEPT COMMANDS:
 
75
 
 
76
    while (1)
 
77
    {
 
78
        (void) printf ("\nEnter key/list/quit: ");
 
79
        (void) fflush (stdout);
 
80
 
 
81
        if (fgets (Index, BUFSIZ, stdin) == (char *) NULL)
 
82
            break;
 
83
 
 
84
        if ((Pc = strchr (Index, '\n')) != (char *) NULL)
 
85
            *Pc = '\0';                 // strip trailing newline.
 
86
 
 
87
// QUIT:
 
88
 
 
89
        if (! strcmp (Index, CMD_QUIT)) break;
 
90
 
 
91
 
 
92
// LIST ALL INPUT IN ALPHABETICAL ORDER:
 
93
 
 
94
        if (! strcmp (Index, CMD_LIST))
 
95
        {
 
96
             Index[0] = '\0';
 
97
 
 
98
             for (PPValue  = JudySLFirst (PArray, Index, 0);
 
99
                  PPValue != (PPvoid_t) NULL;
 
100
                  PPValue  = JudySLNext  (PArray, Index, 0))
 
101
             {
 
102
                 (void) printf ("  \"%s\" stored %lu time%s\n",
 
103
                                Index,  *((PWord_t) PPValue),
 
104
                                PLURAL (*((PWord_t) PPValue)));
 
105
             }
 
106
 
 
107
             continue;
 
108
        }
 
109
 
 
110
 
 
111
// ALL OTHER VALUES ARE KEYS:
 
112
//
 
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.
 
116
 
 
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);
 
121
            exit (1);
 
122
        }
 
123
 
 
124
        ++(*((PWord_t) PPValue));               // increment usage count.
 
125
 
 
126
        (void) printf ("  \"%s\" stored %ld time%s\n",
 
127
                       Index,  *((PWord_t) PPValue),
 
128
                       PLURAL (*((PWord_t) PPValue)));
 
129
 
 
130
    } // while 1 (continuous loop until user quits)
 
131
 
 
132
    exit (0);
 
133
    /*NOTREACHED*/
 
134
 
 
135
} // main()