File: dict.c

package info (click to toggle)
rsynth 2.0-6
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 720 kB
  • ctags: 544
  • sloc: ansic: 5,535; sh: 1,249; makefile: 117
file content (129 lines) | stat: -rw-r--r-- 2,091 bytes parent folder | download | duplicates (3)
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
#include <config.h>
#include "proto.h"
#include <stdio.h>
#include "phones.h"

void *dict;
char **dialect = ph_am;

#ifdef HAVE_LIBGDBM
#include <useconfig.h>
#include <ctype.h>
#include <gdbm.h>

#include "dict.h"
#include "getargs.h"

#ifndef DICT_DIR
#define DICT_DIR "/usr/local/lib"
#endif

char *dict_path = "b";


unsigned char *
dict_find(s, n)
char *s;
unsigned n;
{
 if (!n)
  n = strlen(s);
 if (dict)
  {
   datum key;
   datum data;
   int i;
   key.dptr = malloc(n);
   key.dsize = n;
   for (i = 0; i < n; i++)
    {
     key.dptr[i] = (islower(s[i])) ? toupper(s[i]) : s[i];
    }
   data = gdbm_fetch((GDBM_FILE) dict, key);
   free(key.dptr);
   if (data.dptr)
    {
     unsigned char *w = (unsigned char *) malloc(data.dsize + 1);
     memcpy(w, data.dptr, data.dsize);
     w[data.dsize] = 0;
     free(data.dptr);
     return w;
    }
  }
 return NULL;
}

static void choose_dialect PROTO((void));

static void
choose_dialect()
{
 unsigned char *word = dict_find("schedule", 0);
 if (word)
  {
   if (word[0] == SH)
    dialect = ph_br;
   else if (word[0] == S && word[1] == K)
    dialect = ph_am;
   free(word);
  }
}

int
dict_init(argc, argv)
int argc;
char *argv[];
{
 char *buf = NULL;
 argc = getargs("Dictionary", argc, argv, "d", "", &dict_path, "Which dictionary [b|a]", NULL);
 if (!help_only)
  {
   buf = malloc(strlen(DICT_DIR) + strlen("Dict.db") + strlen(dict_path) + 2);
   sprintf(buf, "%sDict.db", dict_path);
   if (!(dict = gdbm_open(buf, GDBM_READER, 0, 0666, NULL)))
    {                
     sprintf(buf, "%s/%sDict.db", DICT_DIR, dict_path);
     dict = gdbm_open(buf, GDBM_READER, 0, 0666, NULL);
    }                
   if (dict)         
    {                
     dict_path = realloc(buf, strlen(buf) + 1);
    }                
   if (dict)         
    choose_dialect();
  }
 return argc;
}

void
dict_term()
{
 if (dict)
  gdbm_close((GDBM_FILE) dict);
}

#else

unsigned char *
dict_find(s, n)
char *s;
unsigned n;
{
 return NULL;
}

int
dict_init(argc, argv)
int argc;
char *argv[];
{
 return argc;
}

void
dict_term()
{

}

#endif