aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/dict/roget.c
blob: 16bfeae5758356ab62bac79ae6ca490199a03174 (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
#include "dict.h"

/* Roget's Thesaurus from project Gutenberg */

/* static long Last = 0; */

void
rogetprintentry(Entry e, int cmd)
{
	int spc;
	char c, *p;

	spc = 0;
	p = e.start;

	if(cmd == 'h'){
		while(!isspace(*p) && p < e.end)
			p++;
		while(strncmp(p, " -- ", 4) != 0 && p < e.end){
			while(isspace(*p) && p < e.end)
				p++;
			if (*p == '[' || *p == '{'){	
				c = (*p == '[')? ']': '}';
				while(*p != c && p < e.end)
					p++;
				p++;
				continue;
			}
			if (isdigit(*p) || ispunct(*p)){
				while(!isspace(*p) && p < e.end)
					p++;
				continue;
			}


			if (isspace(*p))
				spc = 1;
			else
			if (spc){
				outchar(' ');
				spc = 0;
			}

			while(!isspace(*p) && p < e.end)
				outchar(*p++);
		}
		return;
	}	

	while(p < e.end && !isspace(*p))
		p++;
	while(p < e.end && isspace(*p))
		p++;

	while (p < e.end){
		if (p < e.end -4 && strncmp(p, " -- ", 4) == 0){	/* first line */
			outnl(2);
			p += 4;
			spc = 0;
		}
			
		if (p < e.end -2 && strncmp(p, "[ ", 4) == 0){		/* twiddle layout */
			outchars(" [");
			continue;
		}

		if (p < e.end -4 && strncmp(p, "&c (", 4) == 0){	/* usefull xref */
			if (spc)
				outchar(' ');
			outchar('/');
			while(p < e.end && *p != '(')
				p++;
			p++;
			while(p < e.end && *p != ')')
				outchar(*p++);
			p++;
			while(p < e.end && isspace(*p))
				p++;
			while(p < e.end && isdigit(*p))
				p++;
			outchar('/');
			continue;
		}

		if (p < e.end -3 && strncmp(p, "&c ", 3) == 0){		/* less usefull xref */
			while(p < e.end && !isdigit(*p))
				p++;
			while(p < e.end && isdigit(*p))
				p++;
			continue;
		}

		if (*p == '\n' && p < (e.end -1)){			/* their newlines */
			spc = 0;
			p++;
			if (isspace(*p)){				/* their continuation line */
				while (isspace(*p))
					p++;
				p--;
			}
			else{
				outnl(2);
			}
		}
		if (spc && *p != ';' && *p != '.' &&
		    *p != ',' && !isspace(*p)){				/* drop spaces before punct */
			spc = 0;
			outchar(' ');
		}
		if (isspace(*p))
			spc = 1;
		else
			outchar(*p);
		p++;
	}
	outnl(0);
}

long
rogetnextoff(long fromoff)
{
	int i;
	vlong l;
	char *p;

	Bseek(bdict, fromoff, 0);
	Brdline(bdict, '\n');
	while ((p = Brdline(bdict, '\n')) != nil){
		l = Blinelen(bdict);
		if (!isdigit(*p))
			continue;
		for (i = 0; i < l-4; i++)
			if (strncmp(p+i, " -- ", 4) == 0)
				return Boffset(bdict)-l;
	}
	return Boffset(bdict);
}

void
rogetprintkey(void)
{
	Bprint(bout, "No pronunciation key.\n");
}