1 /*  objacces.c
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name:  objacces.c
27 *
28 * Author:  James Ostell
29 *
30 * Version Creation Date: 1/1/91
31 *
32 * $Revision: 6.2 $
33 *
34 * File Description:  Object manager for module NCBI-Access
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * Date	   Name        Description of modification
39 * -------  ----------  -----------------------------------------------------
40 * 05-13-93 Schuler     All public functions are now declared LIBCALL.
41 *
42 * $Log: objacces.c,v $
43 * Revision 6.2  2015/10/23 00:04:24  kans
44 * NOIJRA Clear av DataVal variable on AsnWrite, needed for supporting Int8 integers in ASN.1
45 *
46 * Revision 6.1  2004/04/01 13:43:07  lavr
47 * Spell "occurred", "occurrence", and "occurring"
48 *
49 * Revision 6.0  1997/08/25 18:49:05  madden
50 * Revision changed to 6.0
51 *
52 * Revision 4.1  1997/06/19 18:40:33  vakatov
53 * [WIN32,MSVC++]  Adopted for the "NCBIOBJ.LIB" DLL'ization
54 *
55 * Revision 4.0  1995/07/26 13:48:06  ostell
56 * force revision to 4.0
57 *
58  * Revision 3.1  1995/05/15  21:22:00  ostell
59  * added Log line
60  *
61 *
62 *
63 * ==========================================================================
64 */
65 #include <asnacces.h>        /* the AsnTool header */
66 #include <objacces.h>		   /* the general objects interface */
67 
68 static Boolean loaded = FALSE;
69 
70 /*****************************************************************************
71 *
72 *   AccessAsnLoad()
73 *
74 *****************************************************************************/
AccessAsnLoad(void)75 NLM_EXTERN Boolean LIBCALL AccessAsnLoad (void)
76 {
77     if (loaded)
78         return TRUE;
79 
80     if (AsnLoad())
81         loaded = TRUE;
82     return loaded;
83 }
84 
85 /*****************************************************************************
86 *
87 *   LinkSet Routines
88 *
89 *****************************************************************************/
90 
91 
92 /*****************************************************************************
93 *
94 *   LinkSetNew()
95 *
96 *****************************************************************************/
LinkSetNew(void)97 NLM_EXTERN LinkSetPtr LIBCALL LinkSetNew (void)
98 {
99 	return (LinkSetPtr)MemNew(sizeof(LinkSet));
100 }
101 /*****************************************************************************
102 *
103 *   LinkSetFree(lsp)
104 *
105 *****************************************************************************/
LinkSetFree(LinkSetPtr lsp)106 NLM_EXTERN LinkSetPtr LIBCALL LinkSetFree (LinkSetPtr lsp)
107 {
108 	if (lsp == NULL)
109 		return (LinkSetPtr)NULL;
110 	MemFree(lsp->uids);
111 	MemFree(lsp->weights);
112 	return (LinkSetPtr)MemFree(lsp);
113 }
114 
115 /*****************************************************************************
116 *
117 *   LinkSetAsnWrite(lsp, aip, atp)
118 *   	atp is the current type (if identifier of a parent struct)
119 *       if atp == NULL, then assumes it stands alone (LinkSet ::=)
120 *
121 *****************************************************************************/
LinkSetAsnWrite(LinkSetPtr lsp,AsnIoPtr aip,AsnTypePtr orig)122 NLM_EXTERN Boolean LIBCALL LinkSetAsnWrite (LinkSetPtr lsp, AsnIoPtr aip, AsnTypePtr orig)
123 {
124 	DataVal av;
125 	AsnTypePtr atp;
126     Int4 num, i;
127     Int4Ptr ptr;
128     Boolean retval = FALSE;
129 
130 	if (! loaded)
131 	{
132 		if (! AccessAsnLoad())
133 			return FALSE;
134 	}
135 
136 	if (aip == NULL)
137 		return FALSE;
138 
139 	atp = AsnLinkType(orig, LINK_SET);   /* link local tree */
140     if (atp == NULL)
141         return FALSE;
142 
143 	if (lsp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
144 
145     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
146 
147     if (! AsnOpenStruct(aip, atp, (Pointer)lsp))
148         goto erret;
149     num = lsp->num;
150     av.intvalue = num;
151     if (! AsnWrite(aip, LINK_SET_num, &av))
152         goto erret;
153     ptr = lsp->uids;
154     if (! AsnOpenStruct(aip, LINK_SET_uids, (Pointer)lsp->uids))
155         goto erret;
156     for (i = 0; i < num; i++)
157     {
158         av.intvalue = ptr[i];
159         if (! AsnWrite(aip, LINK_SET_uids_E, &av))
160             goto erret;
161     }
162     if (! AsnCloseStruct(aip, LINK_SET_uids, (Pointer)lsp->uids))
163         goto erret;
164     if (lsp->weights != NULL)
165     {
166         ptr = lsp->weights;
167         if (! AsnOpenStruct(aip, LINK_SET_weights, (Pointer)lsp->weights))
168             goto erret;
169         for (i = 0; i < num; i++)
170         {
171             av.intvalue = ptr[i];
172             if (! AsnWrite(aip, LINK_SET_weights_E, &av))
173                 goto erret;
174         }
175         if (! AsnCloseStruct(aip, LINK_SET_weights, (Pointer)lsp->weights))
176             goto erret;
177     }
178     if (! AsnCloseStruct(aip, atp, (Pointer)lsp))
179         goto erret;
180     retval = TRUE;
181 erret:
182 	AsnUnlinkType(orig);       /* unlink local tree */
183 	return retval;
184 }
185 
186 /*****************************************************************************
187 *
188 *   LinkSetAsnRead(aip, atp)
189 *   	atp is the current type (if identifier of a parent struct)
190 *            assumption is readIdent has occurred
191 *       if atp == NULL, then assumes it stands alone and read ident
192 *            has not occurred.
193 *
194 *****************************************************************************/
LinkSetAsnRead(AsnIoPtr aip,AsnTypePtr orig)195 NLM_EXTERN LinkSetPtr LIBCALL LinkSetAsnRead (AsnIoPtr aip, AsnTypePtr orig)
196 {
197 	DataVal av;
198 	AsnTypePtr atp;
199     LinkSetPtr lsp=NULL;
200     Int4 num, i;
201     Int4Ptr ptr;
202 
203 	if (! loaded)
204 	{
205 		if (! AccessAsnLoad())
206 			return lsp;
207 	}
208 
209 	if (aip == NULL)
210 		return lsp;
211 
212 	if (orig == NULL)           /* LinkSet ::= (self contained) */
213 		atp = AsnReadId(aip, amp, LINK_SET);
214 	else
215 		atp = AsnLinkType(orig, LINK_SET);    /* link in local tree */
216     if (atp == NULL)
217         return lsp;
218 
219 	lsp = LinkSetNew();
220     if (lsp == NULL)
221         goto erret;
222 
223 	if (AsnReadVal(aip, atp, &av) <= 0)    /* read the start struct */
224         goto erret;
225 	atp = AsnReadId(aip, amp, atp);  /* find the num */
226     if (atp == NULL)
227         goto erret;
228 	if (AsnReadVal(aip, atp, &av) <= 0)     /* get the num */
229         goto erret;
230     num = av.intvalue;
231     lsp->num = num;
232 
233     atp = AsnReadId(aip, amp, atp);  /* start seq of uids */
234     if (atp == NULL)
235         goto erret;
236     if (AsnReadVal(aip, atp, &av) <= 0)
237         goto erret;
238     ptr = (Int4Ptr)MemNew((size_t)(sizeof(Int4) * (num + 1)));  /* 0 sentinel at end */
239     if (ptr == NULL)
240         goto erret;
241     lsp->uids = ptr;
242     i = 0;
243     while ((atp = AsnReadId(aip, amp, atp)) == LINK_SET_uids_E)
244     {
245         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
246         ptr[i] = av.intvalue;
247         i++;
248         if (i > num)
249             break;
250     }
251     if (atp == NULL)
252         goto erret;
253     if (i != num)
254     {
255         ErrPost(CTX_NCBIOBJ, 0, "Incorrect number of uids in Link-set. line %ld",
256             aip->linenumber);
257         goto erret;
258     }
259     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end seq of */
260 
261     atp = AsnReadId(aip, amp, atp);
262     if (atp == NULL)
263         goto erret;
264     if (atp == LINK_SET_weights)
265     {
266         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
267         ptr = (Int4Ptr)MemNew((size_t)(sizeof(Int4) * (num + 1)));  /* 0 sentinel at end */
268         if (ptr == NULL)
269             goto erret;
270         lsp->weights = ptr;
271         i = 0;
272         while ((atp = AsnReadId(aip, amp, atp)) == LINK_SET_weights_E)
273         {
274             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
275             ptr[i] = av.intvalue;
276             i++;
277             if (i > num)
278                 break;
279         }
280         if (atp == NULL)
281             goto erret;
282         if (i != num)
283         {
284             ErrPost(CTX_NCBIOBJ, 0, "Incorrect number of weights in Link-set. line %ld",
285                 aip->linenumber);
286             goto erret;
287         }
288         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end seq of */
289 
290 	    if ((atp = AsnReadId(aip, amp, atp)) == NULL)
291 	        goto erret;
292     }
293 
294     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;  /* end struct */
295 ret:
296 	AsnUnlinkType(orig);       /* unlink local tree */
297 	return lsp;
298 erret:
299     lsp = LinkSetFree(lsp);
300     goto ret;
301 }
302 
303