xref: /onnv-gate/usr/src/lib/libast/common/cdt/dtrestore.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #include	"dthdr.h"
234887Schin 
244887Schin /*	Restore dictionary from given tree or list of elements.
254887Schin **	There are two cases. If called from within, list is nil.
264887Schin **	From without, list is not nil and data->size must be 0.
274887Schin **
284887Schin **	Written by Kiem-Phong Vo (5/25/96)
294887Schin */
304887Schin 
314887Schin #if __STD_C
dtrestore(reg Dt_t * dt,reg Dtlink_t * list)324887Schin int dtrestore(reg Dt_t* dt, reg Dtlink_t* list)
334887Schin #else
344887Schin int dtrestore(dt, list)
354887Schin reg Dt_t*	dt;
364887Schin reg Dtlink_t*	list;
374887Schin #endif
384887Schin {
394887Schin 	reg Dtlink_t	*t, **s, **ends;
404887Schin 	reg int		type;
414887Schin 	reg Dtsearch_f	searchf = dt->meth->searchf;
424887Schin 
434887Schin 	type = dt->data->type&DT_FLATTEN;
444887Schin 	if(!list) /* restoring a flattened dictionary */
454887Schin 	{	if(!type)
464887Schin 			return -1;
474887Schin 		list = dt->data->here;
484887Schin 	}
494887Schin 	else	/* restoring an extracted list of elements */
504887Schin 	{	if(dt->data->size != 0)
514887Schin 			return -1;
524887Schin 		type = 0;
534887Schin 	}
544887Schin 	dt->data->type &= ~DT_FLATTEN;
554887Schin 
564887Schin 	if(dt->data->type&(DT_SET|DT_BAG))
574887Schin 	{	dt->data->here = NIL(Dtlink_t*);
584887Schin 		if(type) /* restoring a flattened dictionary */
594887Schin 		{	for(ends = (s = dt->data->htab) + dt->data->ntab; s < ends; ++s)
604887Schin 			{	if((t = *s) )
614887Schin 				{	*s = list;
624887Schin 					list = t->right;
634887Schin 					t->right = NIL(Dtlink_t*);
644887Schin 				}
654887Schin 			}
664887Schin 		}
674887Schin 		else	/* restoring an extracted list of elements */
684887Schin 		{	dt->data->size = 0;
694887Schin 			while(list)
704887Schin 			{	t = list->right;
714887Schin 				(*searchf)(dt,(Void_t*)list,DT_RENEW);
724887Schin 				list = t;
734887Schin 			}
744887Schin 		}
754887Schin 	}
764887Schin 	else
774887Schin 	{	if(dt->data->type&(DT_OSET|DT_OBAG))
784887Schin 			dt->data->here = list;
794887Schin 		else /*if(dt->data->type&(DT_LIST|DT_STACK|DT_QUEUE))*/
804887Schin 		{	dt->data->here = NIL(Dtlink_t*);
814887Schin 			dt->data->head = list;
824887Schin 		}
834887Schin 		if(!type)
844887Schin 			dt->data->size = -1;
854887Schin 	}
864887Schin 
874887Schin 	return 0;
884887Schin }
89