dtopen.c revision 3e14f97f673e8a630f076077de35afdd43dc1587
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1985-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* http://www.opensource.org/licenses/cpl1.0.txt *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "dthdr.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic char* Version = "\n@(#)$Id: cdt (AT&T Research) 2005-04-20 $\0\n";
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Make a new dictionary
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin**
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo (5/25/96)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDt_t* dtopen(Dtdisc_t* disc, Dtmethod_t* meth)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDt_t* dtopen(disc, meth)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDtdisc_t* disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDtmethod_t* meth;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Dt_t* dt = (Dt_t*)Version; /* shut-up unuse warning */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg int e;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Dtdata_t* data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!disc || !meth)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* allocate space for dictionary */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(dt = (Dt_t*) malloc(sizeof(Dt_t))))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* initialize all absolutely private data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->searchf = NIL(Dtsearch_f);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->meth = NIL(Dtmethod_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->disc = NIL(Dtdisc_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dtdisc(dt,disc,0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->type = DT_MALLOC;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->nview = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->view = dt->walk = NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->user = NIL(Void_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(disc->eventf)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* if shared/persistent dictionary, get existing data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data = NIL(Dtdata_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((e = (*disc->eventf)(dt,DT_OPEN,(Void_t*)(&data),disc)) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto err_open;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(e > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(data)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(data->type&meth->type)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto done;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else goto err_open;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!disc->memoryf)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto err_open;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free((Void_t*)dt);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(dt = (*disc->memoryf)(0, 0, sizeof(Dt_t), disc)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->searchf = NIL(Dtsearch_f);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->meth = NIL(Dtmethod_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->disc = NIL(Dtdisc_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dtdisc(dt,disc,0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->type = DT_MEMORYF;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->nview = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->view = dt->walk = NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* allocate sharable data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(data = (Dtdata_t*)(dt->memoryf)(dt,NIL(Void_t*),sizeof(Dtdata_t),disc)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { err_open:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free((Void_t*)dt);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Dt_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data->type = meth->type;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data->here = NIL(Dtlink_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data->htab = NIL(Dtlink_t**);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data->ntab = data->size = data->loop = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin data->minp = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->data = data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->searchf = meth->searchf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dt->meth = meth;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(disc->eventf)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (*disc->eventf)(dt, DT_ENDOPEN, (Void_t*)dt, disc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return dt;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}