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