6 static int _verbose = 1;
7 static const char * dictfn = "/usr/share/dict/words";
8 static poptBits dictbits = NULL;
15 static int loadDict(const char * fn, poptBits * ap)
18 size_t nb = sizeof(b);
19 FILE * fp = fopen(fn, "r");
23 if (fp == NULL || ferror(fp)) goto exit;
26 while ((t = fgets(b, nb, fp)) != NULL) {
27 while (*t && isspace(*t)) t++;
28 if (*t == '#') continue;
30 while (te-- > t && isspace(*te)) *te = '\0';
31 if (*t == '\0') continue;
34 fprintf(stderr, "==> poptSaveBits(%p, \"%s\")\n", *ap, t);
35 (void) poptSaveBits(ap, 0, t);
40 if (fp) (void) fclose(fp);
44 static struct poptOption options[] = {
45 { "debug", 'd', POPT_BIT_SET|POPT_ARGFLAG_TOGGLE, &_debug, 1,
46 "Set debugging.", NULL },
47 { "verbose", 'v', POPT_BIT_SET|POPT_ARGFLAG_TOGGLE, &_verbose, 1,
48 "Set verbosity.", NULL },
55 int main(int argc, const char ** argv)
57 poptContext optCon = NULL;
58 const char ** av = NULL;
59 poptBits avbits = NULL;
60 int ec = 2; /* assume failure */
63 #if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
64 mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
67 /* XXX Scale the Bloom filters in popt. */
68 if ((rc = loadDict(dictfn, NULL)) <= 0)
72 _poptBitsN = _poptBitsK * rc;
74 optCon = poptGetContext("tdict", argc, argv, options, 0);
76 /* Read all the options (if any). */
77 while ((rc = poptGetNextOpt(optCon)) > 0) {
78 char * optArg = poptGetOptArg(optCon);
79 if (optArg) free(optArg);
81 default: goto exit; break;
85 fprintf(stderr, "tdict: %s: %s\n",
86 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
91 if ((rc = loadDict(dictfn, &dictbits)) <= 0)
94 av = poptGetArgs(optCon);
95 if ((rc = poptBitsArgs(optCon, &avbits)) != 0)
98 poptBits Ibits = NULL;
99 (void) poptBitsUnion(&Ibits, dictbits);
100 rc = poptBitsIntersect(&Ibits, avbits);
101 fprintf(stdout, "===== %s words are in %s\n", (rc ? "Some" : "No"), dictfn);
102 if (Ibits) free(Ibits);
106 rc = poptBitsChk(dictbits, *av);
107 if (rc < 0) goto exit;
111 fprintf(stdout, "%s:\tYES\n", *av);
115 fprintf(stdout, "%s:\tNO\n", *av);
124 fprintf(stdout, "===== poptBits N:%u M:%u K:%u (%uKb) total(%u) = hits(%u) + misses(%u)\n",
125 _poptBitsN, _poptBitsM, _poptBitsK, (((_poptBitsM/8)+1)+1023)/1024, e.total, e.hits, e.misses);
126 if (avbits) free(avbits);
127 optCon = poptFreeContext(optCon);
128 #if defined(HAVE_MCHECK_H) && defined(HAVE_MTRACE)
129 muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */