xref: /onnv-gate/usr/src/lib/libast/common/cdt/dtrestore.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #include	"dthdr.h"
23*4887Schin 
24*4887Schin /*	Restore dictionary from given tree or list of elements.
25*4887Schin **	There are two cases. If called from within, list is nil.
26*4887Schin **	From without, list is not nil and data->size must be 0.
27*4887Schin **
28*4887Schin **	Written by Kiem-Phong Vo (5/25/96)
29*4887Schin */
30*4887Schin 
31*4887Schin #if __STD_C
32*4887Schin int dtrestore(reg Dt_t* dt, reg Dtlink_t* list)
33*4887Schin #else
34*4887Schin int dtrestore(dt, list)
35*4887Schin reg Dt_t*	dt;
36*4887Schin reg Dtlink_t*	list;
37*4887Schin #endif
38*4887Schin {
39*4887Schin 	reg Dtlink_t	*t, **s, **ends;
40*4887Schin 	reg int		type;
41*4887Schin 	reg Dtsearch_f	searchf = dt->meth->searchf;
42*4887Schin 
43*4887Schin 	type = dt->data->type&DT_FLATTEN;
44*4887Schin 	if(!list) /* restoring a flattened dictionary */
45*4887Schin 	{	if(!type)
46*4887Schin 			return -1;
47*4887Schin 		list = dt->data->here;
48*4887Schin 	}
49*4887Schin 	else	/* restoring an extracted list of elements */
50*4887Schin 	{	if(dt->data->size != 0)
51*4887Schin 			return -1;
52*4887Schin 		type = 0;
53*4887Schin 	}
54*4887Schin 	dt->data->type &= ~DT_FLATTEN;
55*4887Schin 
56*4887Schin 	if(dt->data->type&(DT_SET|DT_BAG))
57*4887Schin 	{	dt->data->here = NIL(Dtlink_t*);
58*4887Schin 		if(type) /* restoring a flattened dictionary */
59*4887Schin 		{	for(ends = (s = dt->data->htab) + dt->data->ntab; s < ends; ++s)
60*4887Schin 			{	if((t = *s) )
61*4887Schin 				{	*s = list;
62*4887Schin 					list = t->right;
63*4887Schin 					t->right = NIL(Dtlink_t*);
64*4887Schin 				}
65*4887Schin 			}
66*4887Schin 		}
67*4887Schin 		else	/* restoring an extracted list of elements */
68*4887Schin 		{	dt->data->size = 0;
69*4887Schin 			while(list)
70*4887Schin 			{	t = list->right;
71*4887Schin 				(*searchf)(dt,(Void_t*)list,DT_RENEW);
72*4887Schin 				list = t;
73*4887Schin 			}
74*4887Schin 		}
75*4887Schin 	}
76*4887Schin 	else
77*4887Schin 	{	if(dt->data->type&(DT_OSET|DT_OBAG))
78*4887Schin 			dt->data->here = list;
79*4887Schin 		else /*if(dt->data->type&(DT_LIST|DT_STACK|DT_QUEUE))*/
80*4887Schin 		{	dt->data->here = NIL(Dtlink_t*);
81*4887Schin 			dt->data->head = list;
82*4887Schin 		}
83*4887Schin 		if(!type)
84*4887Schin 			dt->data->size = -1;
85*4887Schin 	}
86*4887Schin 
87*4887Schin 	return 0;
88*4887Schin }
89