1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <config.h>
#include "proto.h"
#include <stdio.h>
#include <ctype.h>
#include <useconfig.h>
#include <gdbm.h>
#include "trie.h"
#include "darray.h"
#include "phones.h"
/* Don't force block size let gdbm/os decide */
#define BLOCK_SIZE 0
#ifndef GDBM_FAST
/* Tolerate older versions of gdbm ... */
#define GDBM_FAST 0
#endif
trie_ptr phones;
static void enter_phones PROTO((void));
static void
enter_phones()
{
int i;
char *s;
for (i = 1; (s = ph_name[i]); i++)
trie_insert(&phones, s, (void *) i);
}
static void enter_words PROTO((GDBM_FILE db, FILE * f));
static void
enter_words(db, f)
GDBM_FILE db;
FILE *f;
{
char buf[4096];
while (fgets(buf, sizeof(buf), f))
{
char *s = buf;
char *h = strchr(s, '#');
if (h)
*h = '\0';
while (isspace(*s))
s++;
if (*s)
{
char *p = s;
while (isalpha(*p) || *p == '-' || *p == '\'')
{
if (islower(*p))
*p = toupper(*p);
p++;
}
if (isspace(*p))
{
char codes[4096];
char *d = codes;
int ok = 1;
datum key;
key.dptr = s;
key.dsize = p - s;
while (*p && ok)
{
unsigned code;
while (isspace(*p))
p++;
if (*p)
{
char *e = p;
while (isalpha(*e) || *e == '1' || *e == '2')
{
if (islower(*e))
*e = toupper(*e);
e++;
}
if (*e == '0')
*e++ = ' ';
if (e > p && (code = (unsigned) trie_lookup(&phones, &p)))
*d++ = code;
else
{
fprintf(stderr, "Bad code %.*s>%s", (int)(p - s), s, p);
ok = 0;
break;
}
}
}
if (ok)
{
datum data;
data.dptr = codes;
data.dsize = d - codes;
gdbm_store(db, key, data, GDBM_INSERT);
}
}
else
{
if (*p != '(')
fprintf(stderr, "Ignore (%c) %s", *p, s);
}
}
}
}
int main PROTO((int argc, char *argv[], char *env[]));
int
main(argc, argv, envp)
int argc;
char *argv[];
char *envp[];
{
if (argc == 3)
{
FILE *f = fopen(argv[1], "r");
if (f)
{
GDBM_FILE db = gdbm_open(argv[2], BLOCK_SIZE, GDBM_WRCREAT | GDBM_FAST, 0644, NULL);
if (db)
{
enter_phones();
enter_words(db, f);
gdbm_close(db);
}
else
perror(argv[2]);
fclose(f);
}
else
perror(argv[1]);
}
return 0;
}
|