-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (67 loc) · 1.6 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "latin.h"
#define PROMPT "?"
void trimstr(char *str, char *outbuffer, size_t buffer_len)
{
char *start = str;
char *end;
size_t trimmed_len;
size_t out_len;
while (isspace(*start)) {
start++;
}
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) {
end--;
}
trimmed_len = end - start + 1;
out_len = trimmed_len < buffer_len ? trimmed_len : buffer_len - 1;
memcpy(outbuffer, start, out_len);
outbuffer[out_len] = 0;
}
void process_query(char *word)
{
lat_res **lres;
lat_res **hres;
int i;
int j;
lres = latsearch(word);
if (lres[0]) {
for (i = 0; lres[i]; ++i) {
lat_res *r = lres[i];
printf("%s : %s\n", r->form, r->meaning);
for (j = 0; r->det[j]; ++j) {
printf(" %s\n", r->det[j]);
}
}
}
hres = husearch(word);
if (hres[0]) {
for (i = 0; hres[i]; ++i) {
lat_res *r = hres[i];
printf("%s %s\n", r->form, r->meaning);
}
}
}
int main(int argc, char **argv)
{
char *input_line;
size_t input_len;
char trimmed_line[1024];
while (1) {
printf("%s ", PROMPT);
ssize_t input_size = getline(&input_line, &input_len, stdin);
if (input_size <= 0) {
break;
}
trimstr(input_line, trimmed_line, sizeof(trimmed_line));
if (strlen(trimmed_line) == 0) {
break;
}
process_query(trimmed_line);
}
return 0;
}