1 | #include "dthdr.h" |
2 | static char* Version = "\n@(#)$Id$\0\n" ; |
3 | |
4 | /* Make a new dictionary |
5 | ** |
6 | ** Written by Kiem-Phong Vo (5/25/96) |
7 | */ |
8 | |
9 | Dt_t* dtopen(Dtdisc_t* disc, Dtmethod_t* meth) |
10 | { |
11 | Dt_t* dt = (Dt_t*)Version; /* shut-up unuse warning */ |
12 | reg int e; |
13 | Dtdata_t* data; |
14 | |
15 | if(!disc || !meth) |
16 | return NIL(Dt_t*); |
17 | |
18 | /* allocate space for dictionary */ |
19 | if(!(dt = (Dt_t*) malloc(sizeof(Dt_t)))) |
20 | return NIL(Dt_t*); |
21 | |
22 | /* initialize all absolutely private data */ |
23 | dt->searchf = NIL(Dtsearch_f); |
24 | dt->meth = NIL(Dtmethod_t*); |
25 | dt->disc = NIL(Dtdisc_t*); |
26 | dtdisc(dt,disc,0); |
27 | dt->type = DT_MALLOC; |
28 | dt->nview = 0; |
29 | dt->view = dt->walk = NIL(Dt_t*); |
30 | dt->user = NIL(void*); |
31 | |
32 | if(disc->eventf) |
33 | { /* if shared/persistent dictionary, get existing data */ |
34 | data = NIL(Dtdata_t*); |
35 | if((e = (*disc->eventf)(dt,DT_OPEN,(void*)(&data),disc)) < 0) |
36 | goto err_open; |
37 | else if(e > 0) |
38 | { if(data) |
39 | { if(data->type&meth->type) |
40 | goto done; |
41 | else goto err_open; |
42 | } |
43 | |
44 | if(!disc->memoryf) |
45 | goto err_open; |
46 | |
47 | free((void*)dt); |
48 | if(!(dt = (*disc->memoryf)(0, 0, sizeof(Dt_t), disc)) ) |
49 | return NIL(Dt_t*); |
50 | dt->searchf = NIL(Dtsearch_f); |
51 | dt->meth = NIL(Dtmethod_t*); |
52 | dt->disc = NIL(Dtdisc_t*); |
53 | dtdisc(dt,disc,0); |
54 | dt->type = DT_MEMORYF; |
55 | dt->nview = 0; |
56 | dt->view = dt->walk = NIL(Dt_t*); |
57 | } |
58 | } |
59 | |
60 | /* allocate sharable data */ |
61 | if(!(data = (Dtdata_t*)(dt->memoryf)(dt,NIL(void*),sizeof(Dtdata_t),disc)) ) |
62 | { err_open: |
63 | free((void*)dt); |
64 | return NIL(Dt_t*); |
65 | } |
66 | |
67 | data->type = meth->type; |
68 | data->here = NIL(Dtlink_t*); |
69 | data->htab = NIL(Dtlink_t**); |
70 | data->ntab = data->size = data->loop = 0; |
71 | data->minp = 0; |
72 | |
73 | done: |
74 | dt->data = data; |
75 | dt->searchf = meth->searchf; |
76 | dt->meth = meth; |
77 | |
78 | if(disc->eventf) |
79 | (*disc->eventf)(dt, DT_ENDOPEN, (void*)dt, disc); |
80 | |
81 | return dt; |
82 | } |
83 | |