aboutsummaryrefslogtreecommitdiffstats
path: root/spo256/getidx.c
blob: 70a730c1bd517cc265db6b37e1879f5a4bd4f750 (plain)
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
/*
 * Build word and abbreviation index in memory from .conf.
 * $Id: getidx.c 1.2 Mon, 24 Mar 1997 12:25:37 -0500 dyfet $
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions of distribution and reuse see product license.
 */

#include <other/string.h>
#include <other/config.h>
#include <std/files.h>
#include <std/process.h>
#include <other/memory.h>
#include "speak.h"

MEMPOOL	*mem;
IDX	**widx, **aidx;

static	int	key(uchar *str)
{
	unsigned	int	k = 0;
	int		len = strlen((char *)str);

	while(*str)
	{
		k = (k * 7) | (*str & 0x1f);
		++str;
	}
	
	k |= len;

	return (k % KEYSIZE);
}

void	getidx(CONFIG *cfg)
{
	int	i;
	char	*p, *q;
	IDX	*new;

	mem = mempool(16384, 16);
	
	widx = (IDX **)memreq(mem, sizeof(IDX *) * KEYSIZE);
	aidx = (IDX **)memreq(mem, sizeof(IDX *) * KEYSIZE);


	for(i = 0; i < KEYSIZE; ++i)
		widx[i] = aidx[i] = NULL;

	seek_config(cfg, "words");
	while(NULL != (p = read_config(cfg)))
	{
		q = strchr(p, '=');
		if(!q)
			continue;

		*(q++) = 0;
		p = strtrim(p, __SPACES);
		q = strtrim(q, __SPACES);
		i = key((uchar *)p);
		new = memlreq(mem, sizeof(IDX));
		new->word_link = widx[i];
		widx[i] = new;
		new->word_key = strreq(mem, p);
		new->word_spo = strreq(mem, q);
	}			
	seek_config(cfg, "abbrev");
	while(NULL != (p = read_config(cfg)))
	{
		q = strchr(p, '=');
		if(!q)
			continue;

		*(q++) = 0;
		p = strtrim(p, __SPACES);
		q = strtrim(q, __SPACES);
		i = key((uchar *)p);
		new = memlreq(mem, sizeof(IDX));
		new->word_link = aidx[i];
		aidx[i] = new;
		new->word_key = strreq(mem, p);
		new->word_spo = strreq(mem, q);
	}			
}

char	*find(IDX **table, char *str)
{
	IDX	*next = table[key((uchar *)str)];
	

	while(next)
	{
		if(!stricmp(str, next->word_key))
			return next->word_spo;

		next = next->word_link;
	}
	return NULL;
}