1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "dthdr.h"
23da2e3ebdSchin static char* Version = "\n@(#)$Id: cdt (AT&T Research) 2005-04-20 $\0\n";
24da2e3ebdSchin
25da2e3ebdSchin /* Make a new dictionary
26da2e3ebdSchin **
27da2e3ebdSchin ** Written by Kiem-Phong Vo (5/25/96)
28da2e3ebdSchin */
29da2e3ebdSchin
30da2e3ebdSchin #if __STD_C
dtopen(Dtdisc_t * disc,Dtmethod_t * meth)31da2e3ebdSchin Dt_t* dtopen(Dtdisc_t* disc, Dtmethod_t* meth)
32da2e3ebdSchin #else
33da2e3ebdSchin Dt_t* dtopen(disc, meth)
34da2e3ebdSchin Dtdisc_t* disc;
35da2e3ebdSchin Dtmethod_t* meth;
36da2e3ebdSchin #endif
37da2e3ebdSchin {
38da2e3ebdSchin Dt_t* dt = (Dt_t*)Version; /* shut-up unuse warning */
39da2e3ebdSchin reg int e;
40da2e3ebdSchin Dtdata_t* data;
41da2e3ebdSchin
42da2e3ebdSchin if(!disc || !meth)
43da2e3ebdSchin return NIL(Dt_t*);
44da2e3ebdSchin
45da2e3ebdSchin /* allocate space for dictionary */
46da2e3ebdSchin if(!(dt = (Dt_t*) malloc(sizeof(Dt_t))))
47da2e3ebdSchin return NIL(Dt_t*);
48da2e3ebdSchin
49da2e3ebdSchin /* initialize all absolutely private data */
50da2e3ebdSchin dt->searchf = NIL(Dtsearch_f);
51da2e3ebdSchin dt->meth = NIL(Dtmethod_t*);
52da2e3ebdSchin dt->disc = NIL(Dtdisc_t*);
53da2e3ebdSchin dtdisc(dt,disc,0);
54da2e3ebdSchin dt->type = DT_MALLOC;
55da2e3ebdSchin dt->nview = 0;
56da2e3ebdSchin dt->view = dt->walk = NIL(Dt_t*);
57da2e3ebdSchin dt->user = NIL(Void_t*);
58da2e3ebdSchin
59da2e3ebdSchin if(disc->eventf)
60da2e3ebdSchin { /* if shared/persistent dictionary, get existing data */
61da2e3ebdSchin data = NIL(Dtdata_t*);
62da2e3ebdSchin if((e = (*disc->eventf)(dt,DT_OPEN,(Void_t*)(&data),disc)) < 0)
63da2e3ebdSchin goto err_open;
64da2e3ebdSchin else if(e > 0)
65da2e3ebdSchin { if(data)
66da2e3ebdSchin { if(data->type&meth->type)
67da2e3ebdSchin goto done;
68da2e3ebdSchin else goto err_open;
69da2e3ebdSchin }
70da2e3ebdSchin
71da2e3ebdSchin if(!disc->memoryf)
72da2e3ebdSchin goto err_open;
73da2e3ebdSchin
74da2e3ebdSchin free((Void_t*)dt);
75da2e3ebdSchin if(!(dt = (*disc->memoryf)(0, 0, sizeof(Dt_t), disc)) )
76da2e3ebdSchin return NIL(Dt_t*);
77da2e3ebdSchin dt->searchf = NIL(Dtsearch_f);
78da2e3ebdSchin dt->meth = NIL(Dtmethod_t*);
79da2e3ebdSchin dt->disc = NIL(Dtdisc_t*);
80da2e3ebdSchin dtdisc(dt,disc,0);
81da2e3ebdSchin dt->type = DT_MEMORYF;
82da2e3ebdSchin dt->nview = 0;
83da2e3ebdSchin dt->view = dt->walk = NIL(Dt_t*);
84da2e3ebdSchin }
85da2e3ebdSchin }
86da2e3ebdSchin
87da2e3ebdSchin /* allocate sharable data */
88da2e3ebdSchin if(!(data = (Dtdata_t*)(dt->memoryf)(dt,NIL(Void_t*),sizeof(Dtdata_t),disc)) )
89da2e3ebdSchin { err_open:
90da2e3ebdSchin free((Void_t*)dt);
91da2e3ebdSchin return NIL(Dt_t*);
92da2e3ebdSchin }
93da2e3ebdSchin
94da2e3ebdSchin data->type = meth->type;
95da2e3ebdSchin data->here = NIL(Dtlink_t*);
96da2e3ebdSchin data->htab = NIL(Dtlink_t**);
97da2e3ebdSchin data->ntab = data->size = data->loop = 0;
98da2e3ebdSchin data->minp = 0;
99da2e3ebdSchin
100da2e3ebdSchin done:
101da2e3ebdSchin dt->data = data;
102da2e3ebdSchin dt->searchf = meth->searchf;
103da2e3ebdSchin dt->meth = meth;
104da2e3ebdSchin
105da2e3ebdSchin if(disc->eventf)
106da2e3ebdSchin (*disc->eventf)(dt, DT_ENDOPEN, (Void_t*)dt, disc);
107da2e3ebdSchin
108da2e3ebdSchin return dt;
109da2e3ebdSchin }
110