NCBI C Toolkit Cross Reference

C/object/objgen.c


  1 /*  objgen.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:  objgen.c
 27 *
 28 * Author:  James Ostell
 29 *   
 30 * Version Creation Date: 1/1/91
 31 *
 32 * $Revision: 6.17 $
 33 *
 34 * File Description:  Object manager for module NCBI-General
 35 *
 36 * Modifications:  
 37 * --------------------------------------------------------------------------
 38 * Date     Name        Description of modification
 39 * -------  ----------  -----------------------------------------------------
 40 * 05-13-93 Schuler     All public functions are now declared LIBCALL.
 41 * 06-21-93 Schuler     Made a static copy of NCBI_months[] in this file 
 42 *                      (because it cannot be exported from ncbitime.c when 
 43 *                      it is linked as a DLL).
 44 *
 45 * ==========================================================================
 46 */
 47 #include <asngen.h>        /* the AsnTool header */
 48 #include <objgen.h>                /* the general objects interface */
 49 
 50 
 51 static Boolean loaded = FALSE;
 52 
 53 /* ValNode equivalent functions that allocate ObjValNode size */
 54 
 55 NLM_EXTERN ValNodePtr LIBCALL SeqDescrNew (ValNodePtr vnp)
 56 
 57 {
 58         ValNodePtr newnode;
 59 
 60         newnode = (ValNodePtr) Nlm_MemNew(sizeof(ObjValNode));
 61         if (newnode == NULL) return NULL;
 62         newnode->extended = 1; /* extended flag indicates extra fields are present */
 63         if (vnp != NULL)
 64     {
 65         while (vnp->next != NULL)
 66             vnp = vnp->next;
 67                 vnp->next = newnode;
 68     }
 69         return newnode;
 70 }
 71 
 72 NLM_EXTERN ValNodePtr LIBCALL SeqDescrAdd (ValNodePtr PNTR head)
 73 
 74 {
 75         ValNodePtr newnode;
 76 
 77         if (head != NULL)
 78         {
 79                 newnode = SeqDescrNew (*head);
 80                 if (*head == NULL)
 81                         *head = newnode;
 82         }
 83         else
 84                 newnode = SeqDescrNew (NULL);
 85 
 86         return newnode;
 87 }
 88 
 89 NLM_EXTERN ValNodePtr LIBCALL SeqDescrAddPointer (ValNodePtr PNTR head, Int2 choice, VoidPtr value)
 90 
 91 {
 92         ValNodePtr newnode;
 93 
 94         newnode = SeqDescrAdd (head);
 95         if (newnode != NULL)
 96         {
 97                 newnode->choice = (Uint1) choice;
 98                 newnode->data.ptrvalue = value;
 99         }
100 
101         return newnode;
102 }
103 
104 /*****************************************************************************
105 *
106 *   GeneralAsnLoad()
107 *
108 *****************************************************************************/
109 NLM_EXTERN Boolean LIBCALL GeneralAsnLoad (void)
110 {
111     if (loaded)
112         return TRUE;
113 
114     if (AsnLoad())
115         loaded = TRUE;
116     return loaded;
117 }
118 
119 /*****************************************************************************
120 *
121 *   Date Routines
122 *
123 *****************************************************************************/
124 /*****************************************************************************
125 *
126 *   DateNew()
127 *
128 *****************************************************************************/
129 NLM_EXTERN NCBI_DatePtr LIBCALL DateNew (void)
130 {
131         NCBI_DatePtr dp = NULL;
132         
133         dp = (NCBI_DatePtr)MemNew(sizeof(NCBI_Date));
134         dp->data[4] = 255;  /* hour not set */
135         dp->data[5] = 255;  /* minute not set */
136         dp->data[6] = 255;  /* second not set */
137         return dp;
138 }
139 
140 /*****************************************************************************
141 *
142 *   DateFree(dp)
143 *
144 *****************************************************************************/
145 NLM_EXTERN NCBI_DatePtr LIBCALL DateFree (NCBI_DatePtr dp)
146 {
147     if (dp == NULL)
148         return dp;
149 
150         MemFree(dp->str);
151         return (NCBI_DatePtr)MemFree(dp);
152 }
153 
154 /*****************************************************************************
155 *
156 *   DateClean(dp)
157 *
158 *****************************************************************************/
159 NLM_EXTERN NCBI_DatePtr LIBCALL DateClean (NCBI_DatePtr dp)
160 {
161     if (dp == NULL)
162         return dp;
163 
164         memset(dp, 0, sizeof(NCBI_Date));
165         dp->data[4] = 255;  /* hour not set */
166         dp->data[5] = 255;  /* minute not set */
167         dp->data[6] = 255;  /* second not set */
168         return dp;
169 }
170 
171 /*****************************************************************************
172 *
173 *   DateWrite(dp, year, month, day, season)
174 *   if dp->data[0] not set
175 *       if year != 0, it's a std date
176 *       else it's a str date
177 *   if a std date, season = season
178 *   if a str date, season = str
179 *
180 *****************************************************************************/
181 NLM_EXTERN Boolean LIBCALL DateWrite (NCBI_DatePtr dp, Int2 year, Int2 month, Int2 day, CharPtr season)
182 {
183         if (dp == NULL)
184                 return FALSE;
185 
186         if (dp->str != NULL)    /* remove previous string if any */
187                 MemFree(dp->str);
188 
189         dp->data[4] = 255;   /* set time fields to not-set */
190         dp->data[5] = 255;
191         dp->data[6] = 255;
192 
193         if (year == 0)       /* date-str */
194         {
195                 if (season == NULL)     /* not optional */
196                         return FALSE;
197                 dp->data[0] = 0;        /* set type */
198                 dp->data[1] = 0;        /* clear the rest */
199                 dp->data[2] = 0;
200                 dp->data[3] = 0;
201                 dp->str = StringSave(season);
202         }
203         else
204         {                     /* date-std */
205                 dp->data[0] = 1;                 /* set type */
206                 year -= 1900;
207                 if ((year < 1) || (year > 255))   /* year not optional */
208                         return FALSE;
209                 if ((month < 0) || (month > 12))
210                         return FALSE;
211                 if ((day < 0) || (day > 31))
212                         return FALSE;
213                 dp->data[1] = (Uint1)year;
214                 dp->data[2] = (Uint1)month;
215                 dp->data[3] = (Uint1)day;
216                 dp->str = StringSave(season);
217         }
218         return TRUE;
219 }
220 
221 /*****************************************************************************
222 *
223 *   DateRead(dp, &year, &month, &day, season)
224 *       season is buffer to copy season/str into
225 *
226 *****************************************************************************/
227 NLM_EXTERN Boolean LIBCALL DateRead (NCBI_DatePtr dp, Int2Ptr yearptr, Int2Ptr monthptr, Int2Ptr dayptr, CharPtr season)
228 {
229         if (dp == NULL)
230                 return FALSE;
231 
232         if (dp->data[0] == 0)     /* str type */
233         {
234                 if (season == NULL)
235                         return FALSE;
236                 StringCpy(season, dp->str);
237         }
238         else                      /* std type */
239         {
240                 if (yearptr != NULL)
241                         *yearptr = (Int2)dp->data[1] + 1900;
242                 if (monthptr != NULL)
243                         *monthptr = (Int2)dp->data[2];
244                 if (dayptr != NULL)
245                         *dayptr = (Int2)dp->data[3];
246                 if (season != NULL)
247                 {
248                         if (dp->str == NULL)
249                                 *season = '\0';
250                         else
251                                 StringCpy(season, dp->str);
252                 }
253         }
254         return TRUE;
255 }
256 
257 
258 /*****************************************************************************
259 *
260 *   DatePrint(dp, buf)
261 *
262 *****************************************************************************/
263 NLM_EXTERN Boolean LIBCALL DatePrint (NCBI_DatePtr dp, CharPtr to)
264 
265 {
266         Char tbuf[10];
267         
268         *to = '\0';
269 
270         if (dp->data[0] == 0)    /* str type */
271         {
272                 StringCpy(to, dp->str);    /* just copy string */
273         }
274         else
275         {
276                 if (dp->data[2] > 0)
277                 {
278       if (dp->data[2] > 12) {
279         to = StringMove (to, "inv");
280       } else {
281                           to = StringMove(to, NCBI_months[dp->data[2] - 1]);
282       }
283                         *to = ' ';
284                         to++;
285                         *to = '\0';
286                 }
287                 if (dp->data[3] > 0)
288                 {
289                         sprintf(tbuf, "%d, ", dp->data[3]);   /* for MS windows sprintf */
290             to = StringMove(to, tbuf);
291                 }
292                 if (dp->data[1] == 0)
293                         to = StringMove(to, "????");
294                 else
295                 {
296                         sprintf(tbuf, "%d", (int)(dp->data[1] + 1900));
297                 to = StringMove(to, tbuf);
298                 }
299                 if (dp->str != NULL)
300             {
301                         *to = ';';
302                         to++;
303                         *to = ' ';
304                         to++;
305                         to = StringMove(to, dp->str);
306                 }
307         }
308         return TRUE;
309 }
310 
311 /*****************************************************************************
312 *
313 *   DateCurr()
314 *
315 *****************************************************************************/
316 NLM_EXTERN NCBI_DatePtr LIBCALL DateCurr (void)
317 {
318         NCBI_DatePtr dp;
319         Nlm_DayTime dt;
320 
321         dp = DateNew();
322         if (dp == NULL) return dp;
323         GetDayTime(&dt);
324         dp->data[0] = (Uint1)1;    /* date.std */
325         dp->data[1] = (Uint1)dt.tm_year;
326         dp->data[2] = (Uint1)(dt.tm_mon + 1);
327         dp->data[3] = (Uint1)dt.tm_mday;
328 
329         return dp;
330 }
331 
332 /*****************************************************************************
333 *
334 *   DateTimeCurr()
335 *
336 *****************************************************************************/
337 NLM_EXTERN NCBI_DatePtr LIBCALL DateTimeCurr (void)
338 {
339         NCBI_DatePtr dp;
340         Nlm_DayTime dt;
341 
342         dp = DateNew();
343         if (dp == NULL) return dp;
344         GetDayTime(&dt);
345         dp->data[0] = (Uint1)1;    /* date.std */
346         dp->data[1] = (Uint1)dt.tm_year;
347         dp->data[2] = (Uint1)(dt.tm_mon + 1);
348         dp->data[3] = (Uint1)dt.tm_mday;
349         dp->data[4] = (Uint1)(dt.tm_hour);
350         dp->data[5] = (Uint1)(dt.tm_min);
351         dp->data[6] = (Uint1)(dt.tm_sec);
352 
353         return dp;
354 }
355 
356 /*****************************************************************************
357 *
358 *   DateDup(dp)
359 *
360 *****************************************************************************/
361 NLM_EXTERN NCBI_DatePtr LIBCALL DateDup (NCBI_DatePtr dp)
362 {
363         NCBI_DatePtr np;
364 
365         if (dp == NULL)
366                 return dp;
367 
368         np = DateNew();
369         if (np == NULL) return np;
370         MemCopy(np, dp, sizeof(NCBI_Date));
371         if (dp->str != NULL)
372                 np->str = StringSave(dp->str);
373         return np;
374 }
375 
376 /*****************************************************************************
377 *
378 *   DateAsnWrite(dp, aip, atp)
379 *       atp is the current type (if identifier of a parent struct)
380 *       if atp == NULL, then assumes it stands alone (Date ::=)
381 *
382 *****************************************************************************/
383 NLM_EXTERN Boolean LIBCALL DateAsnWrite (NCBI_DatePtr dp, AsnIoPtr aip, AsnTypePtr orig)
384 {
385         DataVal av;
386         AsnTypePtr atp;
387     Boolean retval = FALSE;
388 
389         if (! loaded)
390         {
391                 if (! GeneralAsnLoad())
392                         return FALSE;
393         }
394 
395         if (aip == NULL)
396                 return FALSE;
397 
398         atp = AsnLinkType(orig, DATE);   /* link local tree */
399     if (atp == NULL)
400         return FALSE;
401 
402         if (dp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
403 
404         av.ptrvalue = (Pointer)dp;
405 
406         if (dp->data[0] == 0)      /* str type */
407         {
408                 if (! AsnWriteChoice(aip, atp, (Int2)0, &av)) goto erret;
409                 av.ptrvalue = dp->str;
410                 if (! AsnWrite(aip, DATE_str, &av)) goto erret;    /* write the choice */
411         }
412         else                       /* std type */
413         {
414                 if (! AsnWriteChoice(aip, atp, (Int2)1, &av)) goto erret;
415 
416                 if (! AsnOpenStruct(aip, DATE_std, (Pointer)dp))
417             goto erret;
418 
419                 if (dp->data[1] == 0)   /* not set properly */
420                         av.intvalue = 0;    /* carry it through */
421                 else
422                         av.intvalue = (Int4)dp->data[1] + 1900;
423                 if (! AsnWrite(aip, DATE_STD_year, &av)) goto erret;
424                 if (dp->data[2] != 0)
425                 {
426                         av.intvalue = dp->data[2];
427                         if (! AsnWrite(aip, DATE_STD_month, &av)) goto erret;
428                 }
429                 if (dp->data[3] != 0)
430                 {
431                         av.intvalue = dp->data[3];
432                         if (! AsnWrite(aip, DATE_STD_day, &av)) goto erret;
433                 }
434                 if (dp->str != NULL)
435                 {
436                         av.ptrvalue = dp->str;
437                         if (! AsnWrite(aip, DATE_STD_season, &av)) goto erret;
438                 }
439                 if (dp->data[4] < 24)
440                 {
441                         av.intvalue = dp->data[4];
442                         if (! AsnWrite(aip, DATE_STD_hour, &av)) goto erret;
443                 }
444                 if (dp->data[5] < 60)
445                 {
446                         av.intvalue = dp->data[5];
447                         if (! AsnWrite(aip, DATE_STD_minute, &av)) goto erret;
448                 }
449                 if (dp->data[6] < 60)
450                 {
451                         av.intvalue = dp->data[6];
452                         if (! AsnWrite(aip, DATE_STD_second, &av)) goto erret;
453                 }
454 
455                 if (! AsnCloseStruct(aip, DATE_std, (Pointer)dp))
456             goto erret;
457         }
458     retval = TRUE;
459 erret:
460         AsnUnlinkType(orig);       /* unlink local tree */
461         return retval;
462 }
463 
464 /*****************************************************************************
465 *
466 *   DateAsnRead(aip, atp)
467 *       atp is the current type (if identifier of a parent struct)
468 *            assumption is readIdent has occurred
469 *       if atp == NULL, then assumes it stands alone and read ident
470 *            has not occurred.
471 *
472 *****************************************************************************/
473 NLM_EXTERN NCBI_DatePtr LIBCALL DateAsnRead (AsnIoPtr aip, AsnTypePtr orig)
474 {
475         DataVal av;
476         AsnTypePtr atp;
477     NCBI_DatePtr dp=NULL;
478 
479         if (! loaded)
480         {
481                 if (! GeneralAsnLoad())
482                         return dp;
483         }
484 
485         if (aip == NULL)
486                 return dp;
487 
488         if (orig == NULL)           /* Date ::= (self contained) */
489                 atp = AsnReadId(aip, amp, DATE);
490         else
491                 atp = AsnLinkType(orig, DATE);    /* link in local tree */
492     if (atp == NULL)
493         return dp;
494 
495         dp = DateNew();
496     if (dp == NULL)
497         goto erret;
498 
499         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE value (nothing) */
500         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* find the choice */
501         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;     /* get the first value */
502 
503         if (atp == DATE_str)      /* str type */
504         {
505                 dp->str = (CharPtr)av.ptrvalue;         /* save the string */
506         }
507         else if (atp == DATE_std)    /* std type */
508         {
509                 dp->data[0] = 1;         /* set type */
510                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* get the year */
511                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
512                 if (av.intvalue == 0)   /* year not set */
513                         dp->data[1] = 0;    /* don't blow it */
514                 else
515                         dp->data[1] = (Uint1)(av.intvalue - 1900);
516                                                   /* check optionals */
517                 while ((atp = AsnReadId(aip, amp, atp)) != DATE_std)
518                 {
519             if (atp == NULL)
520                 goto erret;
521                         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
522                         if (atp == DATE_STD_month)
523                                 dp->data[2] = (Uint1)av.intvalue;
524                         else if (atp == DATE_STD_day)
525                                 dp->data[3] = (Uint1)av.intvalue;
526                         else if (atp == DATE_STD_season)
527                                 dp->str = (CharPtr)av.ptrvalue;
528                         else if (atp == DATE_STD_hour)
529                                 dp->data[4] = (Uint1)av.intvalue;
530                         else if (atp == DATE_STD_minute)
531                                 dp->data[5] = (Uint1)av.intvalue;
532                         else if (atp == DATE_STD_second)
533                                 dp->data[6] = (Uint1)av.intvalue;
534                 }
535         if (atp == NULL) goto erret;
536                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read last END_STRUCT */
537         }
538 ret:
539         AsnUnlinkType(orig);       /* unlink local tree */
540         return dp;
541 erret:
542     dp = DateFree(dp);
543     goto ret;
544 }
545 
546 /*****************************************************************************
547 *
548 *   Int2 DateMatch(a, b, all)
549 *       returns 0 = same
550 *       1, -1 = different (b is after a = -1)
551 *       2, -2 = couldn't match, arbitrary ordering
552 *       if (all) then all fields in one must be in the other
553 *               else, matches only fields found in both
554 *       string dates just come in alphabetical order
555 *
556 *     thus, for 0,1,-1 return values are like strcmp()
557 *
558 *****************************************************************************/
559 NLM_EXTERN Int2 LIBCALL DateMatch (DatePtr a, DatePtr b, Boolean all)
560 {
561         Int2 retval = 2;
562 
563         if ((a != NULL) && (b != NULL))
564         {
565                 if (a->data[0] == b->data[0])  /* same type */
566                 {
567                         if (a->data[0] == 0)   /* string date */
568                         {
569                                 retval = (Int2)StringICmp(a->str, b->str);
570                                 if (retval > 0)
571                                         return retval;
572                                 else if (retval < 0)
573                                         return (Int2) -1;
574                         }
575                         else                    /* std date */
576                         {
577                                 if (a->data[1] > b->data[1])  /* year */
578                                         return (Int2)1;
579                                 else if (a->data[1] < b->data[1])
580                                         return (Int2)-1;
581                                 if (((a->data[2] != 0) && (b->data[2] != 0)) || all)
582                                 {
583                                         if (a->data[2] > b->data[2])  /* month */
584                                                 return (Int2)1;
585                                         else if (a->data[2] < b->data[2])
586                                                 return (Int2)-1;
587                                 }
588                                 if (((a->data[3] != 0) && (b->data[3] != 0)) || all)
589                                 {
590                                         if (a->data[3] > b->data[3])  /* day */
591                                                 return (Int2)1;
592                                         else if (a->data[3] < b->data[3])
593                                                 return (Int2)-1;
594                                 }
595                                 if (((a->str != NULL) && (b->str != NULL)) || all)
596                                 {
597                                         if ((a->str == NULL) && (b->str != NULL))
598                                                 return (Int2) -1;
599                                         else if ((a->str != NULL) && (b->str == NULL))
600                                                 return (Int2) 1;
601                                         retval = StringICmp(a->str, b->str);
602                                         if (retval > 0)
603                                                 return (Int2)1;
604                                         else if (retval < 0)
605                                                 return (Int2)-1;
606                                 }
607                         }
608                         retval = 0;
609                 }
610                 else
611                 {
612                         if (a->data[0] == 1)
613                                 return (Int2) -2;
614                         else
615                                 return retval;
616                 }
617         }
618         else if (all)
619         {
620                 if (a == NULL)
621                         return (Int2) -2;
622                 else
623                         return retval;
624         }
625         return retval;
626 }
627 
628 /*****************************************************************************
629 *
630 *   DateCheck (dp)
631 *       Checks the date and month values in a date structure
632 *       returns:
633 *      -4 = NULL pointer passed in
634 *      -3 = string date, can't be checked
635 *      -2 = month not set (but otherwise ok)
636 *      -1 = day not set  (but otherwise ok)
637 *       0 = date ok, month,day,year all set
638 *       1 = day invalid
639 *       2 = month invalid
640 *       3 = year not set (required for date)
641 *
642 *****************************************************************************/
643 NLM_EXTERN Int2 LIBCALL DateCheck (DatePtr dp)
644 {
645         Int2    day, month, year, last;
646         static Uint1 days[12] = { 31, 28, 31, 30, 31,
647           30, 31, 31, 30, 31, 30, 31 };
648                 
649         if (dp == NULL) {
650                 return -4;
651         }
652         if (dp->data[0] == 1) {
653                 if (! dp->data[1])
654                         return 3;
655                 year = (Int2)dp->data[1] + 1900;
656                 month = dp->data[2];
657                 day = dp->data[3];
658                 if (! month)    /* month not set */
659                         return -2;
660                 if (month > 12)
661                         return 2;
662                 if (! day)
663                         return -1;
664                 last = days[month-1];
665                 if ((month == 2) && (year%4 == 0) && (year != 2000)) {
666                         last = 29;
667                 }
668                 if (day > last) {
669                         return 1;
670                 }
671         }
672         else
673                 return -3;
674 
675         return 0;
676 }
677 
678 NLM_EXTERN DatePtr LIBCALL DateParse (CharPtr str)
679 
680 {
681   Int4      day = -1, month = -1, year = -1;
682   DatePtr   dp;
683   CharPtr   ptr;
684   Char      tmp [64];
685   long int  val;
686 
687   if (StringHasNoText (str)) return NULL;
688 
689   StringNCpy_0 (tmp, str, sizeof (tmp));
690   ptr = StringChr (tmp, '/');
691   if (ptr == NULL) {
692     ptr = StringChr (tmp, '-');
693   }
694   if (ptr != NULL) {
695     *ptr = '\0';
696     ptr++;
697     if (sscanf (tmp, "%ld", &val) == 1) {
698       month = (Int4) val;
699     }
700     str = StringChr (ptr, '/');
701     if (str == NULL) {
702       str = StringChr (ptr, '-');
703     }
704     if (str != NULL) {
705       *str = '\0';
706       str++;
707       if (sscanf (ptr, "%ld", &val) == 1) {
708         day = (Int4) val;
709       }
710       if (sscanf (str, "%ld", &val) == 1) {
711         year = (Int4) val;
712      }
713     }
714   }
715 
716   if (month < 0 || day < 0 || year < 2000) return NULL;
717   if (month > 12 || day > 31 || year > 2099) return NULL;
718 
719   dp = DateNew ();
720   if (dp == NULL) return NULL;
721 
722   dp->data [0] = 1;
723   dp->data [1] = (Uint1) (year - 1900);
724   dp->data [2] = (Uint1) month;
725   dp->data [3] = (Uint1) day;
726 
727   return dp;
728 }
729 
730 /*****************************************************************************
731 *
732 *   DbtagNew()
733 *
734 *****************************************************************************/
735 NLM_EXTERN DbtagPtr LIBCALL DbtagNew (void)
736 {
737         DbtagPtr dbt;
738 
739         dbt = (DbtagPtr)MemNew(sizeof(Dbtag));
740         return dbt;
741 }
742 
743 /*****************************************************************************
744 *
745 *   DbtagFree()
746 *
747 *****************************************************************************/
748 NLM_EXTERN DbtagPtr LIBCALL DbtagFree (DbtagPtr dbt)
749 {
750     if (dbt == NULL)
751         return dbt;
752 
753         dbt->tag = ObjectIdFree(dbt->tag);
754         MemFree(dbt->db);
755         return (DbtagPtr)MemFree(dbt);
756 }
757 
758 /*****************************************************************************
759 *
760 *   DbtagAsnRead(aip, atp)
761 *
762 *****************************************************************************/
763 NLM_EXTERN DbtagPtr LIBCALL DbtagAsnRead (AsnIoPtr aip, AsnTypePtr orig)
764 {
765         DbtagPtr dbt=NULL;
766         DataVal av;
767         AsnTypePtr atp;
768 
769         if (! loaded)
770         {
771                 if (! GeneralAsnLoad())
772                         return dbt;
773         }
774 
775         if (aip == NULL)
776                 return dbt;
777 
778         if (orig == NULL)           /* Dbtag ::= */
779                 atp = AsnReadId(aip, amp, DBTAG);
780         else
781                 atp = AsnLinkType(orig, DBTAG);
782     if (atp == NULL)
783         return dbt;
784 
785         dbt = DbtagNew();
786     if (dbt == NULL)
787         goto erret;
788 
789         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the START_STRUCT */
790 
791         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* read the db */
792         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
793         dbt->db = (CharPtr)av.ptrvalue;
794 
795         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* read tag */
796         dbt->tag = ObjectIdAsnRead(aip, atp);
797     if (dbt->tag == NULL)
798         goto erret;
799 
800         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* END_STRUCT */
801         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
802 ret:
803         AsnUnlinkType(orig);
804         return dbt;
805 erret:
806     dbt = DbtagFree(dbt);
807     goto ret;
808 }
809 
810 /*****************************************************************************
811 *
812 *   DbtagAsnWrite(dbt, aip, atp)
813 *
814 *****************************************************************************/
815 NLM_EXTERN Boolean LIBCALL DbtagAsnWrite (DbtagPtr dbt, AsnIoPtr aip, AsnTypePtr orig)
816 {
817         DataVal av;
818         AsnTypePtr atp;
819     Boolean retval = FALSE;
820 
821         if (! loaded)
822         {
823                 if (! GeneralAsnLoad())
824                         return FALSE;
825         }
826 
827         if (aip == NULL)
828                 return FALSE;
829 
830         atp = AsnLinkType(orig, DBTAG);
831     if (atp == NULL)
832         return FALSE;
833 
834         if (dbt == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
835 
836         if (! AsnOpenStruct(aip, atp, (Pointer)dbt)) goto erret;
837 
838         av.ptrvalue = dbt->db;
839         if (! AsnWrite(aip, DBTAG_db, &av)) goto erret;
840         if (! ObjectIdAsnWrite(dbt->tag, aip, DBTAG_tag)) goto erret;
841 
842         if (! AsnCloseStruct(aip, atp, (Pointer)dbt)) goto erret;
843     retval = TRUE;
844 erret:
845         AsnUnlinkType(orig);
846         return retval;
847 }
848 
849 /*****************************************************************************
850 *
851 *   DbtagMatch(a, b)
852 *
853 *****************************************************************************/
854 NLM_EXTERN Boolean LIBCALL DbtagMatchEx (DbtagPtr a, DbtagPtr b, Boolean case_sensitive)
855 {
856         if (a == b)
857                 return TRUE;
858 
859         if ((a == NULL) || (b == NULL))
860                 return FALSE;
861 
862   if (case_sensitive) {
863     if (StringCmp (a->db, b->db) != 0) {
864       return FALSE;
865     }
866   } else if (StringICmp(a->db, b->db) != 0) {
867                 return FALSE;
868   }
869 
870         return ObjectIdMatchEx(a->tag, b->tag, case_sensitive);
871 }
872 
873 
874 NLM_EXTERN Boolean LIBCALL DbtagMatch (DbtagPtr a, DbtagPtr b)
875 {
876   return DbtagMatchEx (a, b, FALSE);
877 }
878 
879 
880 /*****************************************************************************
881 *
882 *   DbtagDup(oldtag)
883 *
884 *****************************************************************************/
885 NLM_EXTERN DbtagPtr LIBCALL DbtagDup (DbtagPtr oldtag)
886 {
887         DbtagPtr newtag;
888 
889         if (oldtag == NULL)
890                 return oldtag;
891 
892         newtag = DbtagNew();
893         if (newtag == NULL) return newtag;
894         newtag->db = StringSave(oldtag->db);
895         newtag->tag = ObjectIdDup(oldtag->tag);
896         return newtag;
897 }
898 
899 /*****************************************************************************
900 *
901 *   DbtagLabel(dbt, buf, buflen)
902 *
903 *****************************************************************************/
904 NLM_EXTERN Int2 LIBCALL DbtagLabel(DbtagPtr dbt, CharPtr buf, Int2 buflen)
905 {
906         Char numbuf[15];
907         CharPtr tp = NULL;
908 
909         if (dbt == NULL) return 0;
910 
911         if (dbt->tag != NULL)
912         {
913                 if (dbt->tag->str != NULL)
914                         tp = dbt->tag->str;
915                 else
916                 {
917                         sprintf(numbuf, "%ld", (long)(dbt->tag->id));
918                         tp = numbuf;
919                 }
920         }
921 
922         return LabelCopyExtra(buf, ": ", buflen, dbt->db, tp);
923 }
924 
925 /*****************************************************************************
926 *
927 *   ObjectIdNew()
928 *
929 *****************************************************************************/
930 NLM_EXTERN ObjectIdPtr LIBCALL ObjectIdNew (void)
931 {
932         ObjectIdPtr oid;
933 
934         oid = (ObjectIdPtr)MemNew(sizeof(ObjectId));
935         return oid;
936 }
937 
938 /*****************************************************************************
939 *
940 *   ObjectIdFree()
941 *
942 *****************************************************************************/
943 NLM_EXTERN ObjectIdPtr LIBCALL ObjectIdFree (ObjectIdPtr oid)
944 {
945     if (oid == NULL)
946         return oid;
947 
948         MemFree(oid->str);
949         return (ObjectIdPtr)MemFree(oid);
950 }
951 
952 /*****************************************************************************
953 *
954 *   ObjectIdAsnRead(aip, atp)
955 *
956 *****************************************************************************/
957 NLM_EXTERN ObjectIdPtr LIBCALL ObjectIdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
958 {
959         ObjectIdPtr oid = NULL;
960         DataVal av;
961         AsnTypePtr atp;
962 
963         if (! loaded)
964         {
965                 if (! GeneralAsnLoad())
966                         return oid;
967         }
968 
969         if (aip == NULL)
970                 return oid;
971 
972         if (orig == NULL)           /* ObjectId ::= */
973                 atp = AsnReadId(aip, amp, OBJECT_ID);
974         else
975                 atp = AsnLinkType(orig, OBJECT_ID);
976     if (atp == NULL)
977         return oid;
978 
979         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE value (nothing) */
980 
981         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* read the CHOICE type */
982         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;          /* and the value */
983         
984         oid = ObjectIdNew();
985     if (oid == NULL)
986         goto erret;
987         if (atp == OBJECT_ID_id)
988                 oid->id = av.intvalue;
989         else
990                 oid->str = (CharPtr)av.ptrvalue;
991 ret:
992         AsnUnlinkType(orig);
993         return oid;
994 erret:
995     oid = ObjectIdFree(oid);
996     goto ret;
997 }
998 
999 /*****************************************************************************
1000 *
1001 *   ObjectIdAsnWrite(oid, aip, atp)
1002 *
1003 *****************************************************************************/
1004 NLM_EXTERN Boolean LIBCALL ObjectIdAsnWrite (ObjectIdPtr oid, AsnIoPtr aip, AsnTypePtr orig)
1005 {
1006         DataVal av;
1007         AsnTypePtr atp;
1008     Boolean retval = FALSE;
1009 
1010         if (! loaded)
1011         {
1012                 if (! GeneralAsnLoad())
1013                         return FALSE;
1014         }
1015 
1016         if (aip == NULL)
1017                 return FALSE;
1018 
1019         atp = AsnLinkType(orig, OBJECT_ID);
1020     if (atp == NULL)
1021         return FALSE;
1022 
1023         if (oid == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1024 
1025         if (! AsnWrite(aip, atp, &av)) goto erret;     /* write the CHOICE (no value) */
1026 
1027         if (oid->str == NULL)    /* id used */
1028         {
1029                 av.intvalue = oid->id;
1030                 atp = OBJECT_ID_id;
1031         }
1032         else                    /* str used */
1033         {
1034                 av.ptrvalue = oid->str;
1035                 atp = OBJECT_ID_str;
1036         }
1037 
1038         if (! AsnWrite(aip, atp, &av)) goto erret;
1039         retval = TRUE;
1040 erret:
1041         AsnUnlinkType(orig);
1042         return retval;
1043 }
1044 
1045 /*****************************************************************************
1046 *
1047 *   Boolean ObjectIdMatch(a, b)
1048 *
1049 *****************************************************************************/
1050 NLM_EXTERN Boolean LIBCALL ObjectIdMatchEx (ObjectIdPtr a, ObjectIdPtr b, Boolean case_sensitive)
1051 {
1052   Boolean rval = FALSE;
1053 
1054   if (a == b)
1055   {
1056                 rval = TRUE;
1057   }
1058 
1059   if ((a == NULL) || (b == NULL))   /* only one is null */
1060   {
1061     rval = FALSE;
1062   }
1063 
1064         if ((a->str != NULL) && (b->str != NULL))  /* same type */
1065         {
1066     if (case_sensitive)
1067     {
1068       if (StringCmp (a->str, b->str) == 0) 
1069       {
1070         rval = TRUE;
1071       }
1072       else
1073       {
1074         rval = FALSE;
1075       }
1076     }
1077     else 
1078     {
1079             if (StringICmp(a->str, b->str) == 0)
1080       {
1081         rval = TRUE;
1082       }
1083       else
1084       {
1085         rval = FALSE;
1086       }
1087     }
1088         }
1089   else if ((a->str == NULL) && (b->str == NULL))  /* must be same kind */
1090   {
1091     if (a->id == b->id) 
1092     {
1093       rval = TRUE;
1094     }
1095     else
1096     {
1097       rval = FALSE;
1098     }
1099   }
1100   else                   /* different kinds */
1101   {
1102     rval = FALSE;
1103   }
1104   return rval;
1105 }
1106 
1107 NLM_EXTERN Boolean LIBCALL ObjectIdMatch (ObjectIdPtr a, ObjectIdPtr b)
1108 {
1109   return ObjectIdMatchEx (a, b, FALSE);
1110 }
1111 
1112 
1113 /*****************************************************************************
1114 *
1115 *   ObjectIdDup(oldid)
1116 *
1117 *****************************************************************************/
1118 NLM_EXTERN ObjectIdPtr LIBCALL ObjectIdDup (ObjectIdPtr oldid)
1119 {
1120         ObjectIdPtr newid;
1121 
1122         if (oldid == NULL)
1123                 return oldid;
1124         
1125         newid = ObjectIdNew();
1126         newid->id = oldid->id;
1127         newid->str = StringSave(oldid->str);
1128         return newid;
1129 }
1130 
1131 /*****************************************************************************
1132 *
1133 *   NameStdNew()
1134 *
1135 *****************************************************************************/
1136 NLM_EXTERN NameStdPtr LIBCALL NameStdNew (void)
1137 {
1138         NameStdPtr nsp;
1139 
1140         nsp = (NameStdPtr)MemNew(sizeof(NameStd));
1141         return nsp;
1142 }
1143 
1144 /*****************************************************************************
1145 *
1146 *   NameStdFree()
1147 *
1148 *****************************************************************************/
1149 NLM_EXTERN NameStdPtr LIBCALL NameStdFree (NameStdPtr nsp)
1150 {
1151         Int2 i;
1152 
1153     if (nsp == NULL)
1154         return nsp;
1155 
1156         for (i = 0; i < 7; i++)
1157                 MemFree(nsp->names[i]);
1158         return (NameStdPtr)MemFree(nsp);
1159 }
1160 
1161 /*****************************************************************************
1162 *
1163 *   NameStdAsnRead(aip, atp)
1164 *
1165 *****************************************************************************/
1166 NLM_EXTERN NameStdPtr LIBCALL NameStdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
1167 {
1168         NameStdPtr nsp=NULL;
1169         DataVal av;
1170         AsnTypePtr atp, start;
1171         Int2 i;
1172 
1173         if (! loaded)
1174         {
1175                 if (! GeneralAsnLoad())
1176                         return nsp;
1177         }
1178 
1179         if (aip == NULL)
1180                 return nsp;
1181 
1182         if (orig == NULL)           /* NameStd ::= */
1183                 atp = AsnReadId(aip, amp, NAME_STD);
1184         else
1185                 atp = AsnLinkType(orig, NAME_STD);
1186         start = atp;
1187     if (atp == NULL)
1188         return nsp;
1189 
1190         nsp = NameStdNew();
1191     if (nsp == NULL)
1192         goto erret;
1193 
1194         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the START_STRUCT */
1195 
1196         do
1197         {
1198                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1199                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1200                 if (atp == NAME_STD_last)
1201                         i = 0;
1202                 else if (atp == NAME_STD_first)
1203                         i = 1;
1204                 else if (atp == NAME_STD_middle)
1205                         i = 2;
1206                 else if (atp == NAME_STD_full)
1207                         i = 3;
1208                 else if (atp == NAME_STD_initials)
1209                         i = 4;
1210                 else if (atp == NAME_STD_suffix)
1211                         i = 5;
1212                 else if (atp == NAME_STD_title)
1213                         i = 6;
1214                 else
1215                         i = 10;
1216 
1217                 if (i < 10)
1218                         nsp->names[i] = (CharPtr)av.ptrvalue;
1219         } while (atp != start);
1220 ret:
1221         AsnUnlinkType(orig);
1222         return nsp;
1223 erret:
1224     nsp = NameStdFree(nsp);
1225     goto ret;
1226 }
1227 
1228 /*****************************************************************************
1229 *
1230 *   NameStdAsnWrite(nsp, aip, atp)
1231 *
1232 *****************************************************************************/
1233 NLM_EXTERN Boolean LIBCALL NameStdAsnWrite (NameStdPtr nsp, AsnIoPtr aip, AsnTypePtr orig)
1234 {
1235         DataVal av;
1236         AsnTypePtr atp, ptr;
1237         Int2 i;
1238     Boolean retval = FALSE;
1239 
1240         if (! loaded)
1241         {
1242                 if (! GeneralAsnLoad())
1243                         return FALSE;
1244         }
1245 
1246         if (aip == NULL)
1247                 return FALSE;
1248 
1249         atp = AsnLinkType(orig, NAME_STD);
1250     if (atp == NULL)
1251         return FALSE;
1252 
1253         if (nsp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1254 
1255         if (! AsnOpenStruct(aip, atp, (Pointer)nsp)) goto erret;
1256 
1257         for (i = 0; i < 7; i++)
1258         {
1259                 if (nsp->names[i] != NULL)
1260                 {
1261                         av.ptrvalue = nsp->names[i];
1262                         switch (i)
1263                         {
1264                                 case 0:
1265                                         ptr = NAME_STD_last;
1266                                         break;
1267                                 case 1:
1268                                         ptr = NAME_STD_first;
1269                                         break;
1270                                 case 2:
1271                                         ptr = NAME_STD_middle;
1272                                         break;
1273                                 case 3:
1274                                         ptr = NAME_STD_full;
1275                                         break;
1276                                 case 4:
1277                                         ptr = NAME_STD_initials;
1278                                         break;
1279                                 case 5:
1280                                         ptr = NAME_STD_suffix;
1281                                         break;
1282                                 case 6:
1283                                         ptr = NAME_STD_title;
1284                                         break;
1285                         }
1286                         if (! AsnWrite(aip, ptr, &av)) goto erret;
1287                 }
1288         }
1289 
1290         if (! AsnCloseStruct(aip, atp, (Pointer)nsp)) goto erret;
1291     retval = TRUE;
1292 erret:
1293         AsnUnlinkType(orig);
1294         return retval;
1295 }
1296 
1297 /*****************************************************************************
1298 *
1299 *   NameStdMatch(nsp1, nsp2)
1300 *
1301 *****************************************************************************/
1302 NLM_EXTERN Boolean LIBCALL NameStdMatch (NameStdPtr nsp1, NameStdPtr nsp2)
1303 {
1304   Int4 j;
1305   
1306   if (nsp1 == NULL && nsp2 == NULL)
1307   {
1308     return TRUE;
1309   }
1310   else if (nsp1 == NULL || nsp2 == NULL)
1311   {
1312     return FALSE;
1313   }
1314   
1315   for (j = 0; j < 7; j++)
1316   {
1317     if (StringCmp (nsp1->names [j], nsp2->names[j]) != 0)
1318     {
1319       return FALSE;
1320     }
1321   }
1322   return TRUE;
1323 }
1324 
1325 /*****************************************************************************
1326 *
1327 *   PersonIdNew()
1328 *
1329 *****************************************************************************/
1330 NLM_EXTERN PersonIdPtr LIBCALL PersonIdNew (void)
1331 {
1332         PersonIdPtr pid;
1333 
1334         pid = (PersonIdPtr)MemNew(sizeof(PersonId));
1335         return pid;
1336 }
1337 
1338 /*****************************************************************************
1339 *
1340 *   PersonIdFree()
1341 *
1342 *****************************************************************************/
1343 NLM_EXTERN PersonIdPtr LIBCALL PersonIdFree (PersonIdPtr pid)
1344 {
1345     if (pid == NULL)
1346         return pid;
1347 
1348         if (pid->choice == 1)          /* Dbtag */
1349                 DbtagFree((DbtagPtr)pid->data);
1350         else if (pid->choice == 2)     /* Name-std */
1351                 NameStdFree((NameStdPtr)pid->data);
1352         else
1353                 MemFree(pid->data);
1354         return (PersonIdPtr)MemFree(pid);
1355 }
1356 
1357 /*****************************************************************************
1358 *
1359 *   PersonIdAsnRead(aip, atp)
1360 *
1361 *****************************************************************************/
1362 NLM_EXTERN PersonIdPtr LIBCALL PersonIdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
1363 {
1364         PersonIdPtr pid=NULL;
1365         DataVal av;
1366         AsnTypePtr atp;
1367 
1368         if (! loaded)
1369         {
1370                 if (! GeneralAsnLoad())
1371                         return pid;
1372         }
1373 
1374         if (aip == NULL)
1375                 return pid;
1376 
1377         if (orig == NULL)           /* PersonId ::= */
1378                 atp = AsnReadId(aip, amp, PERSON_ID);
1379         else
1380                 atp = AsnLinkType(orig, PERSON_ID);
1381     if (atp == NULL) return pid;
1382 
1383         pid = PersonIdNew();
1384     if (pid == NULL) goto erret;
1385 
1386         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE (nothing) */
1387 
1388         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* identify the CHOICE type */
1389         if (atp == PERSON_ID_dbtag)
1390         {
1391                 pid->choice = 1;
1392                 pid->data = DbtagAsnRead(aip, atp);
1393         }
1394         else if (atp == PERSON_ID_name)
1395         {
1396                 pid->choice = 2;
1397                 pid->data = NameStdAsnRead(aip, atp);
1398         }
1399         else
1400         {
1401                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;        /* a string type */
1402                 pid->data = av.ptrvalue;
1403                 if (atp == PERSON_ID_ml)
1404                         pid->choice = 3;
1405                 else if (atp == PERSON_ID_str)
1406                         pid->choice = 4;       /* str */
1407                 else if (atp == PERSON_ID_consortium)
1408                         pid->choice = 5;       /* consortium */
1409         }
1410     if (pid->data == NULL)
1411         goto erret;
1412 ret:
1413         AsnUnlinkType(orig);
1414         return pid;
1415 erret:
1416     pid = PersonIdFree(pid);
1417     goto ret;
1418 }
1419 
1420 /*****************************************************************************
1421 *
1422 *   PersonIdAsnWrite(pid, aip, atp)
1423 *
1424 *****************************************************************************/
1425 NLM_EXTERN Boolean LIBCALL PersonIdAsnWrite (PersonIdPtr pid, AsnIoPtr aip, AsnTypePtr orig)
1426 {
1427         DataVal av;
1428         AsnTypePtr atp;
1429     Boolean retval = FALSE;
1430 
1431         if (! loaded)
1432         {
1433                 if (! GeneralAsnLoad())
1434                         return FALSE;
1435         }
1436 
1437         if (aip == NULL)
1438                 return FALSE;
1439 
1440         atp = AsnLinkType(orig, PERSON_ID);
1441     if (atp == NULL)
1442         goto erret;
1443 
1444         if (pid == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1445 
1446         if (! AsnWrite(aip, atp, &av)) goto erret;          /* write the CHOICE, no value */
1447 
1448         av.ptrvalue = pid->data;
1449         switch (pid->choice)
1450         {
1451                 case 1:
1452                         if (! DbtagAsnWrite((DbtagPtr)(pid->data), aip, PERSON_ID_dbtag))
1453                 goto erret;
1454                         break;
1455                 case 2:
1456                         if (! NameStdAsnWrite((NameStdPtr)(pid->data), aip, PERSON_ID_name))
1457                 goto erret;
1458                         break;
1459                 case 3:
1460                         if (! AsnWrite(aip, PERSON_ID_ml, &av)) goto erret;
1461                         break;
1462                 case 4:
1463                         if (! AsnWrite(aip, PERSON_ID_str, &av)) goto erret;
1464                         break;
1465                 case 5:
1466                         if (! AsnWrite(aip, PERSON_ID_consortium, &av)) goto erret;
1467                         break;
1468         }
1469     retval = TRUE;
1470 erret:
1471         AsnUnlinkType(orig);
1472         return retval;
1473 }
1474 
1475 /*****************************************************************************
1476 *
1477 *   PersonIdLabel(pid, buf, buflen, format)
1478 *       Makes a short label, lastname then initials if it can
1479 *       format = PIDLABEL_GENBANK   last,initials
1480 *                  PIDLABEL_EMBL      last initials
1481 *
1482 *       Modeled from GBGetAuthNames in asn2ff
1483 *       returns number of bytes in buf
1484 *       buf MUST be at least (buflen + 1) long
1485 *
1486 *****************************************************************************/
1487 NLM_EXTERN Int2 LIBCALL PersonIdLabel (PersonIdPtr pid, CharPtr buf, Int2 buflen, Int2 format)
1488 {
1489         NameStdPtr nsp;
1490         Int2 len, diff;
1491         Char tbuf[2];
1492         CharPtr tmp;
1493 
1494         if ((pid == NULL) || (buflen < 0) || (buf == NULL))
1495                 return 0;
1496     
1497     if (buflen == 0)    /* on a boundary */
1498     {
1499         *(buf-1) = '>';
1500         return 0;
1501     }
1502     
1503         *buf = '?';
1504         *(buf+1) = '\0';
1505         len = buflen;
1506         if (format == PIDLABEL_GENBANK)
1507                 tbuf[0] = ',';
1508         else
1509                 tbuf[0] = ' ';
1510         tbuf[1] = '\0';
1511 
1512         if (pid->choice == 2)  /* structured name */
1513         {
1514                 nsp = (NameStdPtr)(pid->data);
1515                 if (nsp->names[0] != NULL)   /* last name */
1516                 {
1517                         diff = LabelCopy(buf, nsp->names[0], buflen);
1518                         buflen -= diff; buf += diff;
1519                         if (nsp->names[4])
1520                         {
1521                                 diff = LabelCopyExtra(buf, nsp->names[4], buflen, tbuf, NULL);
1522                                 buflen -= diff; buf += diff;
1523                         }
1524                         if (nsp->names[5]) /* suffix */
1525                         {
1526                                 tbuf[0] = ' ';
1527                                 diff = LabelCopyExtra(buf, nsp->names[5], buflen, tbuf, NULL);
1528                                 buflen -= diff; buf += diff;
1529                         }
1530                 }
1531                 else if (nsp->names[3] != NULL) /* full name */
1532                 {
1533                         diff = LabelCopy(buf, nsp->names[3], buflen);
1534                         buflen -= diff; buf += diff;
1535                 }
1536         }
1537         else if ((pid->choice == 3) || (pid->choice == 4) || (pid->choice == 5))
1538         {
1539                 diff = LabelCopy(buf, (CharPtr)(pid->data), buflen);
1540                 if (format == PIDLABEL_EMBL)
1541                 {
1542                         for (tmp = buf; *tmp != '\0'; tmp ++)
1543                         {
1544                                 if (*tmp == ',')
1545                                 {
1546                                         *tmp = ' ';
1547                                         break;
1548                                 }
1549                         }
1550                 }
1551                 buflen -= diff; buf += diff;
1552         }
1553         else
1554                 return LabelCopy(buf, "Unsupported PersonID", buflen);
1555         
1556         return (len - buflen);
1557 }
1558 
1559 /*****************************************************************************
1560 *
1561 *   PersonIdMatch(pip1, pip2)
1562 *
1563 *****************************************************************************/
1564 NLM_EXTERN Boolean LIBCALL PersonIdMatch (PersonIdPtr pip1, PersonIdPtr pip2)
1565 {
1566   Boolean does_match = TRUE;
1567   
1568   if (pip1 == NULL && pip2 == NULL)
1569   {
1570     return TRUE;
1571   }
1572   else if (pip1 == NULL || pip2 == NULL)
1573   {
1574     return FALSE;
1575   }
1576   else if (pip1->choice != pip2->choice)
1577   {
1578     return FALSE;
1579   }
1580   
1581   switch (pip1->choice)
1582   {
1583     case 0:
1584       /* not set */
1585       break;
1586     case 1:
1587       /* dbtag */
1588       does_match = DbtagMatch (pip1->data, pip2->data);
1589       break;
1590     case 2:
1591       /* name */
1592       does_match = NameStdMatch (pip1->data, pip2->data);
1593       break;
1594     case 3: /* ml */
1595     case 4: /* str */
1596     case 5: /* consortium */
1597       if (StringCmp (pip1->data, pip2->data) != 0)
1598       {
1599         does_match = FALSE;
1600       }
1601       break;
1602   }
1603   return does_match;
1604 }
1605 
1606 /*****************************************************************************
1607 *
1608 *   IntFuzzNew()
1609 *
1610 *****************************************************************************/
1611 NLM_EXTERN IntFuzzPtr LIBCALL IntFuzzNew (void)
1612 {
1613         IntFuzzPtr ifp;
1614 
1615         ifp = (IntFuzzPtr)MemNew(sizeof(IntFuzz));
1616         return ifp;
1617 }
1618 
1619 /*****************************************************************************
1620 *
1621 *   IntFuzzFree()
1622 *
1623 *****************************************************************************/
1624 NLM_EXTERN IntFuzzPtr LIBCALL IntFuzzFree (IntFuzzPtr ifp)
1625 {
1626         if (ifp == NULL)
1627                 return ifp;
1628         MemFree(ifp->alt);
1629         return (IntFuzzPtr)MemFree(ifp);
1630 }
1631 
1632 /*****************************************************************************
1633 *
1634 *   IntFuzzAsnRead(aip, atp)
1635 *
1636 *****************************************************************************/
1637 NLM_EXTERN IntFuzzPtr LIBCALL IntFuzzAsnRead (AsnIoPtr aip, AsnTypePtr orig)
1638 {
1639         IntFuzzPtr ifp=NULL;
1640         DataVal av;
1641         AsnTypePtr atp;
1642         Int4Ptr tmp;
1643 
1644         if (! loaded)
1645         {
1646                 if (! GeneralAsnLoad())
1647                         return ifp;
1648         }
1649 
1650         if (aip == NULL)
1651                 return ifp;
1652 
1653         if (orig == NULL)           /* IntFuzz ::= */
1654                 atp = AsnReadId(aip, amp, INT_FUZZ);
1655         else
1656                 atp = AsnLinkType(orig, INT_FUZZ);
1657     if (atp == NULL)
1658         return ifp;
1659 
1660         ifp = IntFuzzNew();
1661     if (ifp == NULL)
1662         goto erret;
1663 
1664         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE (nothing) */
1665 
1666         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* identify the CHOICE type */
1667         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;        /* read first value */
1668         ifp->a = av.intvalue;             /* ok in 3/4 of cases */
1669 
1670         if (atp == INT_FUZZ_p_m)   /* plus-minus */
1671         {
1672                 ifp->choice = 1;
1673         }
1674         else if (atp == INT_FUZZ_pct)
1675         {
1676                 ifp->choice = 3;
1677         }
1678         else if (atp == INT_FUZZ_lim)
1679         {
1680                 ifp->choice = 4;
1681         }
1682         else if (atp == INT_FUZZ_range)     /* range, read a SEQUENCE */
1683         {
1684                 ifp->choice = 2;
1685                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;        /* max */
1686                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1687                 ifp->a = av.intvalue;
1688                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;        /* min */
1689                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1690                 ifp->b = av.intvalue;
1691                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;        /* end of structure */
1692                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1693         }
1694         else if (atp == INT_FUZZ_alt)
1695         {
1696                 ifp->choice = 5;
1697                 ifp->a = 0;
1698                 ifp->b = 0;
1699                 while ((atp = AsnReadId(aip, amp, atp)) == INT_FUZZ_alt_E)
1700                 {
1701                         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1702                         if (ifp->a == ifp->b)   /* need more room */
1703                         {
1704                                 tmp = ifp->alt;
1705                                 ifp->alt = MemNew((size_t)(ifp->b + 10) * sizeof(Int4));
1706                                 if (ifp->alt == NULL) goto erret;
1707                                 MemMove(ifp->alt, tmp, (size_t)(ifp->b * sizeof(Int4)));
1708                                 MemFree(tmp);
1709                                 ifp->b += 10;
1710                         }
1711                         ifp->alt[ifp->a] = av.intvalue;
1712                         ifp->a++;
1713                         
1714                 }
1715                 if (atp == NULL) goto erret;
1716                 if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1717         }
1718 ret:
1719         AsnUnlinkType(orig);
1720         return ifp;
1721 erret:
1722     ifp = IntFuzzFree(ifp);
1723     goto ret;
1724 }
1725 
1726 /*****************************************************************************
1727 *
1728 *   IntFuzzAsnWrite(ifp, aip, atp)
1729 *
1730 *****************************************************************************/
1731 NLM_EXTERN Boolean LIBCALL IntFuzzAsnWrite (IntFuzzPtr ifp, AsnIoPtr aip, AsnTypePtr orig)
1732 {
1733         DataVal av;
1734         AsnTypePtr atp, ptr;
1735     Boolean retval = FALSE;
1736         Int4 i;
1737 
1738         if (! loaded)
1739         {
1740                 if (! GeneralAsnLoad())
1741                         return FALSE;
1742         }
1743 
1744         if (aip == NULL)
1745                 return FALSE;
1746 
1747         atp = AsnLinkType(orig, INT_FUZZ);
1748     if (atp == NULL) return FALSE;
1749 
1750         if (ifp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1751 
1752         av.ptrvalue = (Pointer)ifp;
1753         if (! AsnWriteChoice(aip, atp, (Int2)ifp->choice, &av)) goto erret;
1754 
1755         av.intvalue = ifp->a;          /* ok in 3/4 choices */
1756         ptr = NULL;
1757 
1758         switch (ifp->choice)
1759         {
1760                 case 1:
1761                         ptr = INT_FUZZ_p_m;
1762                         break;
1763                 case 2:
1764                         if (! AsnOpenStruct(aip, INT_FUZZ_range, (Pointer)ifp))
1765                 goto erret;
1766                         if (! AsnWrite(aip, INT_FUZZ_range_max, &av)) goto erret;
1767                         av.intvalue = ifp->b;
1768                         if (! AsnWrite(aip, INT_FUZZ_range_min, &av)) goto erret;
1769                         if (! AsnCloseStruct(aip, INT_FUZZ_range, (Pointer)ifp))
1770                 goto erret;
1771                         break;
1772                 case 3:
1773                         ptr = INT_FUZZ_pct;
1774                         break;
1775                 case 4:
1776                         ptr = INT_FUZZ_lim;
1777                         break;
1778                 case 5:
1779                         if (! AsnOpenStruct(aip, INT_FUZZ_alt, (Pointer)ifp))
1780                 goto erret;
1781                         for (i = 0; i < ifp->a; i++)
1782                         {
1783                                 av.intvalue = ifp->alt[i];
1784                                 if (! AsnWrite(aip, INT_FUZZ_alt_E, &av)) goto erret;
1785                         }
1786                         if (! AsnCloseStruct(aip, INT_FUZZ_alt, (Pointer)ifp))
1787                 goto erret;
1788                         break;
1789         }
1790 
1791         if (ptr != NULL)           /* 3/5 types */
1792                 if (! AsnWrite(aip, ptr, &av)) goto erret;
1793     retval = TRUE;
1794 erret:
1795         AsnUnlinkType(orig);
1796         return retval;
1797 }
1798 
1799 /*****************************************************************************
1800 *
1801 *   UserFieldNew()
1802 *
1803 *****************************************************************************/
1804 NLM_EXTERN UserFieldPtr LIBCALL UserFieldNew (void)
1805 {
1806         UserFieldPtr ufp;
1807 
1808         ufp = (UserFieldPtr)MemNew(sizeof(UserField));
1809         return ufp;
1810 }
1811 
1812 /*****************************************************************************
1813 *
1814 *   UserFieldFree()
1815 *
1816 *****************************************************************************/
1817 NLM_EXTERN UserFieldPtr LIBCALL UserFieldFree (UserFieldPtr ufp)
1818 {
1819     Int4 i, num;
1820     CharPtr PNTR cpp;
1821     ByteStorePtr PNTR bpp;
1822     UserFieldPtr ufpa, ufpb;
1823     UserObjectPtr uopa, uopb;
1824 
1825     if (ufp == NULL)
1826         return ufp;
1827 
1828     num = ufp->num;
1829 
1830         if (ufp->label != NULL)
1831                 ufp->label = ObjectIdFree(ufp->label);
1832                 
1833     switch (ufp->choice)
1834     {
1835         case 1:             /* str */
1836         case 8:             /* ints */
1837         case 9:             /* reals */
1838             MemFree(ufp->data.ptrvalue);
1839             break;
1840         case 5:             /* os */
1841             BSFree((ByteStorePtr) ufp->data.ptrvalue);
1842             break;
1843         case 6:             /* object */
1844             UserObjectFree((UserObjectPtr) ufp->data.ptrvalue);
1845             break;
1846         case 7:             /* strs */
1847             cpp = (CharPtr PNTR) ufp->data.ptrvalue;
1848             for (i = 0; i < num; i++)
1849                 MemFree(cpp[i]);
1850             MemFree(cpp);
1851             break;
1852         case 10:            /* oss */
1853             bpp = (ByteStorePtr PNTR) ufp->data.ptrvalue;
1854             for (i = 0; i < num; i++)
1855                 BSFree(bpp[i]);
1856             MemFree(bpp);
1857             break;
1858         case 11:            /* fields */
1859             ufpa = (UserFieldPtr) ufp->data.ptrvalue;
1860             while (ufpa != NULL)
1861             {
1862                 ufpb = ufpa->next;
1863                 UserFieldFree(ufpa);
1864                 ufpa = ufpb;
1865             }
1866             break;
1867         case 12:            /* objects */
1868             uopa = (UserObjectPtr) ufp->data.ptrvalue;
1869             while (uopa != NULL)
1870             {
1871                 uopb = uopa->next;
1872                 UserObjectFree(uopa);
1873                 uopa = uopb;
1874             }
1875             break;
1876 
1877     }
1878         return (UserFieldPtr)MemFree(ufp);
1879 }
1880 
1881 /*****************************************************************************
1882 *
1883 *   UserFieldAsnRead(aip, atp)
1884 *
1885 *****************************************************************************/
1886 NLM_EXTERN UserFieldPtr LIBCALL UserFieldAsnRead (AsnIoPtr aip, AsnTypePtr orig)
1887 {
1888         UserFieldPtr ufp=NULL;
1889         DataVal av;
1890         AsnTypePtr atp;
1891     Int4 num = 0, i = 0;
1892     CharPtr PNTR cpp;
1893     ByteStorePtr PNTR bpp;
1894     Int4Ptr ip;
1895     FloatHiPtr fp;
1896     UserFieldPtr ufpa, ufpb = NULL;
1897     UserObjectPtr uopa, uopb = NULL;
1898     ValNodePtr vnp, first = NULL, last = NULL;
1899     static char * emsg1 = "Too many %s in UserField. line %ld",
1900         * emsg2 = "Too few %s in UserField. line %ld";
1901 
1902         if (! loaded)
1903         {
1904                 if (! GeneralAsnLoad())
1905                         return ufp;
1906         }
1907 
1908         if (aip == NULL)
1909                 return ufp;
1910 
1911         if (orig == NULL)           /* UserField ::= */
1912                 atp = AsnReadId(aip, amp, USER_FIELD);
1913         else
1914                 atp = AsnLinkType(orig, USER_FIELD);
1915     if (atp == NULL)
1916         return ufp;
1917 
1918         ufp = UserFieldNew();
1919     if (ufp == NULL)
1920         goto erret;
1921 
1922     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read start struct */
1923 
1924     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* object id */
1925     ufp->label = ObjectIdAsnRead(aip, atp);
1926     if (ufp->label == NULL) goto erret;
1927 
1928     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* could be num */
1929     if (atp == USER_FIELD_num)
1930     {
1931         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1932         num = av.intvalue;
1933         ufp->num = num;
1934         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1935     }
1936 
1937         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE (nothing) */
1938 
1939         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* identify the CHOICE type */
1940 
1941     if (atp == USER_FIELD_data_object)
1942     {
1943         ufp->choice = 6;
1944         ufp->data.ptrvalue = (Pointer) UserObjectAsnRead(aip, atp);
1945         if (ufp->data.ptrvalue == NULL) goto erret;
1946     }
1947     else if (atp == USER_FIELD_data_strs)
1948     {
1949         ufp->choice = 7;
1950         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
1951 
1952         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_strs_E)
1953         {
1954             if (num > 0 && i >= num)
1955             {
1956                 ErrPost(CTX_NCBIOBJ,1, emsg1, "strs", aip->linenumber);
1957                 goto erret;
1958             }
1959             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1960             /*
1961             cpp[i] = (CharPtr)av.ptrvalue;
1962             */
1963             vnp = ValNodeAddStr (&last, 0, (CharPtr)av.ptrvalue);
1964             if (first == NULL) {
1965                 first = vnp;
1966             }
1967             last = vnp;
1968             i++;
1969         }
1970         if (atp == NULL) goto erret;
1971         if (num > 0 && i != num)
1972         {
1973             ErrPost(CTX_NCBIOBJ,1, emsg2, "strs", aip->linenumber);
1974             goto erret;
1975         }
1976         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
1977 
1978         num = i;
1979         ufp->num = num;
1980 
1981         ufp->data.ptrvalue = MemNew((size_t)(sizeof(CharPtr) * num));
1982         cpp = (CharPtr PNTR) ufp->data.ptrvalue;
1983         if (cpp == NULL) goto erret;
1984         for (vnp = first, i = 0; vnp != NULL && i < num; vnp = vnp->next, i++) {
1985             cpp [i] = (CharPtr) vnp->data.ptrvalue;
1986         }
1987 
1988         ValNodeFree (first);
1989     }
1990     else if (atp == USER_FIELD_data_ints)
1991     {
1992         ufp->choice = 8;
1993         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
1994 
1995         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_ints_E)
1996         {
1997             if (num > 0 && i >= num)
1998             {
1999                 ErrPost(CTX_NCBIOBJ,1, emsg1, "ints", aip->linenumber);
2000                 goto erret;
2001             }
2002             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2003             /*
2004             ip[i] = av.intvalue;
2005             */
2006             vnp = ValNodeAddInt (&last, 0, (Int4)av.intvalue);
2007             if (first == NULL) {
2008                 first = vnp;
2009             }
2010             last = vnp;
2011             i++;
2012         }
2013         if (atp == NULL) goto erret;
2014         if (num > 0 && i != num)
2015         {
2016             ErrPost(CTX_NCBIOBJ,1, emsg2, "ints", aip->linenumber);
2017             goto erret;
2018         }
2019         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2020 
2021         num = i;
2022         ufp->num = num;
2023 
2024         ufp->data.ptrvalue = MemNew((size_t)(sizeof(Int4) * num));
2025         ip = (Int4Ptr) ufp->data.ptrvalue;
2026         if (ip == NULL) goto erret;
2027         for (vnp = first, i = 0; vnp != NULL && i < num; vnp = vnp->next, i++) {
2028             ip [i] = (Int4) vnp->data.intvalue;
2029         }
2030 
2031         ValNodeFree (first);
2032     }
2033     else if (atp == USER_FIELD_data_reals)
2034     {
2035         ufp->choice = 9;
2036         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
2037 
2038         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_reals_E)
2039         {
2040             if (num > 0 && i >= num)
2041             {
2042                 ErrPost(CTX_NCBIOBJ,1, emsg1, "reals", aip->linenumber);
2043                 goto erret;
2044             }
2045             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2046             /*
2047             fp[i] = av.realvalue;
2048             */
2049             vnp = ValNodeAddFloat (&last, 0, (FloatHi)av.realvalue);
2050             if (first == NULL) {
2051                 first = vnp;
2052             }
2053             last = vnp;
2054             i++;
2055         }
2056         if (atp == NULL) goto erret;
2057         if (num > 0 && i != num)
2058         {
2059             ErrPost(CTX_NCBIOBJ,1, emsg2, "reals", aip->linenumber);
2060             goto erret;
2061         }
2062         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2063 
2064         num = i;
2065         ufp->num = num;
2066 
2067         ufp->data.ptrvalue = MemNew((size_t)(sizeof(FloatHi) * num));
2068         fp = (FloatHiPtr) ufp->data.ptrvalue;
2069         if (fp == NULL) goto erret;
2070         for (vnp = first, i = 0; vnp != NULL && i < num; vnp = vnp->next, i++) {
2071             fp [i] = (FloatHi) vnp->data.realvalue;
2072         }
2073 
2074         ValNodeFree (first);
2075     }
2076     else if (atp == USER_FIELD_data_oss)
2077     {
2078         ufp->choice = 10;
2079         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
2080 
2081         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_oss_E)
2082         {
2083             if (num > 0 && i >= num)
2084             {
2085                 ErrPost(CTX_NCBIOBJ,1, emsg1, "oss", aip->linenumber);
2086                 goto erret;
2087             }
2088             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2089             /*
2090             bpp[i] = (ByteStorePtr)av.ptrvalue;
2091             */
2092             vnp = ValNodeAddPointer (&last, 0, (Pointer)av.ptrvalue);
2093             if (first == NULL) {
2094                 first = vnp;
2095             }
2096             last = vnp;
2097             i++;
2098         }
2099         if (atp == NULL) goto erret;
2100         if (num > 0 && i != num)
2101         {
2102             ErrPost(CTX_NCBIOBJ,1, emsg2, "oss", aip->linenumber);
2103             goto erret;
2104         }
2105         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2106 
2107         num = i;
2108         ufp->num = num;
2109 
2110         ufp->data.ptrvalue = MemNew((size_t)(sizeof(ByteStorePtr) * num));
2111         bpp = (ByteStorePtr PNTR) ufp->data.ptrvalue;
2112         if (bpp == NULL) goto erret;
2113 
2114         for (vnp = first, i = 0; vnp != NULL && i < num; vnp = vnp->next, i++) {
2115             bpp [i] = (ByteStorePtr) vnp->data.ptrvalue;
2116         }
2117 
2118         ValNodeFree (first);
2119     }
2120     else if (atp == USER_FIELD_data_fields)
2121     {
2122         ufp->choice = 11;
2123         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
2124         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_fields_E)
2125         {
2126             ufpa = UserFieldAsnRead(aip, atp);
2127             if (ufpa == NULL) goto erret;
2128             if (ufpb == NULL)
2129                 ufp->data.ptrvalue = (Pointer) ufpa;
2130             else
2131                 ufpb->next = ufpa;
2132             ufpb = ufpa;
2133         }
2134         if (atp == NULL) goto erret;
2135         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2136     }
2137     else if (atp == USER_FIELD_data_objects)
2138     {
2139         ufp->choice = 12;
2140         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start SEQUENCE OF */
2141         while ((atp = AsnReadId(aip, amp, atp)) == USER_FIELD_data_objects_E)
2142         {
2143             uopa = UserObjectAsnRead(aip, atp);
2144             if (uopa == NULL) goto erret;
2145             if (uopb == NULL)
2146                 ufp->data.ptrvalue = (Pointer) uopa;
2147             else
2148                 uopb->next = uopa;
2149             uopb = uopa;
2150         }
2151         if (atp == NULL) goto erret;
2152         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2153     }
2154     else 
2155     {
2156         if (AsnReadVal(aip, atp, &ufp->data) <= 0) goto erret;
2157         if (atp == USER_FIELD_data_str)
2158             ufp->choice = 1;
2159         else if (atp == USER_FIELD_data_int)
2160             ufp->choice = 2;
2161         else if (atp == USER_FIELD_data_real)
2162             ufp->choice = 3;
2163         else if (atp == USER_FIELD_data_bool)
2164             ufp->choice = 4;
2165         else if (atp == USER_FIELD_data_os)
2166             ufp->choice = 5;
2167     }
2168 
2169     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* end struct */
2170     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2171 ret:
2172         AsnUnlinkType(orig);
2173         return ufp;
2174 erret:
2175     ufp = UserFieldFree(ufp);
2176     goto ret;
2177 }
2178 
2179 /*****************************************************************************
2180 *
2181 *   UserFieldAsnWrite(ufp, aip, atp)
2182 *
2183 *****************************************************************************/
2184 NLM_EXTERN Boolean LIBCALL UserFieldAsnWrite (UserFieldPtr ufp, AsnIoPtr aip, AsnTypePtr orig)
2185 {
2186         DataVal av;
2187         AsnTypePtr atp;
2188     Int4 num, i;
2189     CharPtr PNTR cpp;
2190     ByteStorePtr PNTR bpp;
2191     Int4Ptr ip;
2192     FloatHiPtr fp;
2193     UserFieldPtr ufpa;
2194     UserObjectPtr uopa;
2195     Boolean retval = FALSE;
2196 
2197         if (! loaded)
2198         {
2199                 if (! GeneralAsnLoad())
2200                         return FALSE;
2201         }
2202 
2203         if (aip == NULL)
2204                 return FALSE;
2205 
2206         atp = AsnLinkType(orig, USER_FIELD);
2207     if (atp == NULL) return FALSE;
2208 
2209         if (ufp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2210 
2211     if (! AsnOpenStruct(aip, atp, (Pointer)ufp)) goto erret;  /* start the struct */
2212     if (! ObjectIdAsnWrite(ufp->label, aip, USER_FIELD_label))
2213         goto erret;
2214     num = ufp->num;
2215 
2216     if (num > 0)
2217     {
2218         av.intvalue = num;
2219         if (! AsnWrite(aip, USER_FIELD_num, &av)) goto erret;
2220     }
2221     
2222         if (! AsnWriteChoice(aip, USER_FIELD_data, (Int2)ufp->choice, &ufp->data))
2223         goto erret;
2224 
2225     switch (ufp->choice)
2226     {
2227         case 1:
2228             if (! AsnWrite(aip, USER_FIELD_data_str, &ufp->data))
2229                 goto erret;
2230             break;
2231         case 2:
2232             if (! AsnWrite(aip, USER_FIELD_data_int, &ufp->data))
2233                 goto erret;
2234             break;
2235         case 3:
2236             if (! AsnWrite(aip, USER_FIELD_data_real, &ufp->data))
2237                 goto erret;
2238             break;
2239         case 4:
2240             if (! AsnWrite(aip, USER_FIELD_data_bool, &ufp->data))
2241                 goto erret;
2242             break;
2243         case 5:
2244             if (! AsnWrite(aip, USER_FIELD_data_os, &ufp->data))
2245                 goto erret;
2246             break;
2247         case 6:
2248             if (! UserObjectAsnWrite((UserObjectPtr)ufp->data.ptrvalue, aip, USER_FIELD_data_object))
2249                 goto erret;
2250             break;
2251         case 7:
2252             cpp = (CharPtr PNTR) ufp->data.ptrvalue;
2253             if (! AsnOpenStruct(aip, USER_FIELD_data_strs, (Pointer)cpp))
2254                 goto erret;
2255             for (i = 0; i < num; i++)
2256             {
2257                 av.ptrvalue = (Pointer) cpp[i];
2258                 if (! AsnWrite(aip, USER_FIELD_data_strs_E, &av)) goto erret;
2259             }
2260             if (! AsnCloseStruct(aip, USER_FIELD_data_strs, (Pointer)cpp))
2261                 goto erret;
2262             break;
2263         case 8:
2264             ip = (Int4Ptr) ufp->data.ptrvalue;
2265             if (! AsnOpenStruct(aip, USER_FIELD_data_ints, (Pointer)ip))
2266                 goto erret;
2267             for (i = 0; i < num; i++)
2268             {
2269                 av.intvalue = ip[i];
2270                 if (! AsnWrite(aip, USER_FIELD_data_ints_E, &av)) goto erret;
2271             }
2272             if (! AsnCloseStruct(aip, USER_FIELD_data_ints, (Pointer)ip))
2273                 goto erret;
2274             break;
2275         case 9:
2276             fp = (FloatHiPtr) ufp->data.ptrvalue;
2277             if (! AsnOpenStruct(aip, USER_FIELD_data_reals, (Pointer)fp))
2278                 goto erret;
2279             for (i = 0; i < num; i++)
2280             {
2281                 av.realvalue = fp[i];
2282                 if (! AsnWrite(aip, USER_FIELD_data_reals_E, &av)) goto erret;
2283             }
2284             if (! AsnCloseStruct(aip, USER_FIELD_data_reals, (Pointer)fp))
2285                 goto erret;
2286             break;
2287         case 10:
2288             bpp = (ByteStorePtr PNTR) ufp->data.ptrvalue;
2289             if (! AsnOpenStruct(aip, USER_FIELD_data_oss, (Pointer)bpp))
2290                 goto erret;
2291             for (i = 0; i < num; i++)
2292             {
2293                 av.ptrvalue = (Pointer) bpp[i];
2294                 if (! AsnWrite(aip, USER_FIELD_data_oss_E, &av)) goto erret;
2295             }
2296             if (! AsnCloseStruct(aip, USER_FIELD_data_oss, (Pointer)bpp))
2297                 goto erret;
2298             break;
2299         case 11:
2300             ufpa = (UserFieldPtr) ufp->data.ptrvalue;
2301             if (! AsnOpenStruct(aip, USER_FIELD_data_fields, ufp->data.ptrvalue))
2302                 goto erret;
2303             while (ufpa != NULL)
2304             {
2305                 if (! UserFieldAsnWrite(ufpa, aip, USER_FIELD_data_fields_E))
2306                     goto erret;
2307                 ufpa = ufpa->next;
2308             }
2309             if (! AsnCloseStruct(aip, USER_FIELD_data_fields, ufp->data.ptrvalue))
2310                 goto erret;
2311             break;
2312         case 12:
2313             uopa = (UserObjectPtr) ufp->data.ptrvalue;
2314             if (! AsnOpenStruct(aip, USER_FIELD_data_objects, ufp->data.ptrvalue))
2315                 goto erret;
2316             while (uopa != NULL)
2317             {
2318                 if (! UserObjectAsnWrite(uopa, aip, USER_FIELD_data_objects_E))
2319                     goto erret;
2320                 uopa = uopa->next;
2321             }
2322             if (! AsnCloseStruct(aip, USER_FIELD_data_objects, ufp->data.ptrvalue))
2323                 goto erret;
2324             break;
2325     }
2326     if (! AsnCloseStruct(aip, atp, (Pointer)ufp)) goto erret;
2327     retval = TRUE;
2328 erret:
2329         AsnUnlinkType(orig);
2330         return retval;
2331 }
2332 
2333 /*****************************************************************************
2334 *
2335 *   UserObjectNew()
2336 *
2337 *****************************************************************************/
2338 NLM_EXTERN UserObjectPtr LIBCALL UserObjectNew (void)
2339 {
2340         UserObjectPtr uop;
2341 
2342         uop = (UserObjectPtr)MemNew(sizeof(UserObject));
2343         return uop;
2344 }
2345 
2346 /*****************************************************************************
2347 *
2348 *   UserObjectFree()
2349 *
2350 *****************************************************************************/
2351 NLM_EXTERN UserObjectPtr LIBCALL UserObjectFree (UserObjectPtr uop)
2352 {
2353     UserFieldPtr ufpa, ufpb;
2354 
2355     if (uop == NULL)
2356         return uop;
2357 
2358     MemFree(uop->_class);
2359     ObjectIdFree(uop->type);
2360     ufpa = uop->data;
2361     while (ufpa != NULL)
2362     {
2363         ufpb = ufpa->next;
2364         UserFieldFree(ufpa);
2365         ufpa = ufpb;
2366     }
2367         return (UserObjectPtr)MemFree(uop);
2368 }
2369 
2370 /*****************************************************************************
2371 *
2372 *   UserObjectAsnRead(aip, atp)
2373 *
2374 *****************************************************************************/
2375 NLM_EXTERN UserObjectPtr LIBCALL UserObjectAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2376 {
2377         UserObjectPtr uop=NULL;
2378         DataVal av;
2379         AsnTypePtr atp;
2380     UserFieldPtr ufpa, ufpb = NULL;
2381 
2382         if (! loaded)
2383         {
2384                 if (! GeneralAsnLoad())
2385                         return uop;
2386         }
2387 
2388         if (aip == NULL)
2389                 return uop;
2390 
2391         if (orig == NULL)           /* UserObject ::= */
2392                 atp = AsnReadId(aip, amp, USER_OBJECT);
2393         else
2394                 atp = AsnLinkType(orig, USER_OBJECT);
2395     if (atp == NULL)
2396         return uop;
2397 
2398         uop = UserObjectNew();
2399     if (uop == NULL)
2400         goto erret;
2401 
2402         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the start struct */
2403 
2404         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* class? */
2405     if (atp == USER_OBJECT_class)
2406     {
2407         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2408         uop->_class = (CharPtr) av.ptrvalue;
2409         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
2410     }
2411 
2412     uop->type = ObjectIdAsnRead(aip, atp);
2413     if (uop->type == NULL) goto erret;
2414 
2415     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* start SEQUENCE OF */
2416     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2417     while ((atp = AsnReadId(aip, amp, atp)) == USER_OBJECT_data_E)
2418     {
2419         ufpa = UserFieldAsnRead(aip, atp);
2420         if (ufpa == NULL)
2421             goto erret;
2422         if (ufpb == NULL)
2423             uop->data = ufpa;
2424         else
2425             ufpb->next = ufpa;
2426         ufpb = ufpa;
2427     }
2428     if (atp == NULL) goto erret;
2429     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SEQUENCE OF */
2430     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* end SEQUENCE */
2431     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2432 ret:
2433         AsnUnlinkType(orig);
2434         return uop;
2435 erret:
2436     uop = UserObjectFree(uop);
2437     goto ret;
2438 }
2439 
2440 /*****************************************************************************
2441 *
2442 *   UserObjectAsnWrite(uop, aip, atp)
2443 *
2444 *****************************************************************************/
2445 NLM_EXTERN Boolean LIBCALL UserObjectAsnWrite (UserObjectPtr uop, AsnIoPtr aip, AsnTypePtr orig)
2446 {
2447         DataVal av;
2448         AsnTypePtr atp;
2449     UserFieldPtr ufp;
2450     Boolean retval = FALSE;
2451 
2452         if (! loaded)
2453         {
2454                 if (! GeneralAsnLoad())
2455                         return FALSE;
2456         }
2457 
2458         if (aip == NULL)
2459                 return FALSE;
2460 
2461         atp = AsnLinkType(orig, USER_OBJECT);
2462     if (atp == NULL) return FALSE;
2463 
2464         if (uop == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2465 
2466     if (! AsnOpenStruct(aip, atp, (Pointer)uop)) goto erret;
2467 
2468     if (uop->_class != NULL)
2469     {
2470         av.ptrvalue = (Pointer) uop->_class;
2471         if (! AsnWrite(aip, USER_OBJECT_class, &av)) goto erret;
2472     }
2473 
2474     if (! ObjectIdAsnWrite(uop->type, aip, USER_OBJECT_type))
2475         goto erret;
2476 
2477     if (! AsnOpenStruct(aip, USER_OBJECT_data, (Pointer)uop->data))
2478         goto erret;
2479     ufp = uop->data;
2480     while (ufp != NULL)
2481     {
2482         if (! UserFieldAsnWrite(ufp, aip, USER_OBJECT_data_E))
2483             goto erret;
2484         ufp = ufp->next;
2485     }
2486     if (! AsnCloseStruct(aip, USER_OBJECT_data, (Pointer)uop->data))
2487         goto erret;
2488     
2489     if (! AsnCloseStruct(aip, atp, (Pointer)uop)) goto erret;
2490     retval = TRUE;
2491 erret:
2492         AsnUnlinkType(orig);
2493         return retval;
2494 }
2495 
2496 
2497 
2498 
2499 
2500 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.