1 /*
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:  medarch.c
27 *
28 * Author:  Epstein
29 *
30 * Version Creation Date:   03/31/93
31 *
32 * $Revision: 6.3 $
33 *
34 * File Description:
35 *       API for Medline Archive service
36 *
37 * Modifications:
38 * --------------------------------------------------------------------------
39 * Date     Name        Description of modification
40 * -------  ----------  -----------------------------------------------------
41 * 6/15/93  Epstein     Changed Message() to ErrPost() in init function
42 * 6/24/93  Epstein     Remember to load Pub object loaders in
43 *                      MedArchPubSetCreate().
44 * 8/04/93  Epstein     Fix Fini() logic to correctly disconnect the service,
45 *                      and fix erroneously-typed returned value.
46 * 2/28/96  Grisha      Add new function for MRI look up
47 * 5/08/96  Grisha      Add new functions for PubMed support
48 * 3/06/97  volodya     Has implemented global functions MedArchGetAccUids(),
49                        MedArchGetAccPmids()
50 *
51 * ==========================================================================
52 *
53 *
54 * RCS Modification History:
55 * $Log: medarch.c,v $
56 * Revision 6.3  2007/01/29 15:36:18  kans
57 * added commented-out calls to LaunchAsnTextViewer for future debugging
58 *
59 * Revision 6.2  2005/04/28 19:11:49  kans
60 * s_MedArchCitMatchPmId uses DEBUG_MED_ARCH_CIT_MATCH_PMID environment variable to save Mla-request to origmlarequest.txt file
61 *
62 * Revision 6.1  2004/04/01 13:43:07  lavr
63 * Spell "occurred", "occurrence", and "occurring"
64 *
65 * Revision 6.0  1997/08/25 18:35:40  madden
66 * Revision changed to 6.0
67 *
68 * Revision 5.6  1997/06/05 21:34:05  volodya
69 * volodya added more comments to MedArchGetAccPmids(), MedArchGetAccUids() description
70 *
71 * Revision 5.5  1997/06/03 22:07:08  volodya
72 * volodya has implemented MedArchGetAccPmids(), MedArchGetAccUids()
73 *
74 * Revision 5.4  1997/06/03 21:31:22  volodya
75 * volodyas has implemented MedArchGetAccUids(), MedArchGetAccPmids() functionality
76 *
77 * Revision 5.3  1997/04/18 18:27:19  grisha
78 * Fixed bug with ASN.1 I/O streams initializations
79 *
80  * Revision 5.2  1997/02/11  22:41:47  grisha
81  * Add code and prototypes for support MEDLARS entry
82  *
83  * Revision 5.1  1996/12/17  20:32:30  grisha
84  * update interface to support PubmedEntry
85  *
86  * Revision 5.0  1996/05/28  14:11:11  ostell
87  * Set to revision 5.0
88  *
89  * Revision 4.6  1996/05/10  16:17:15  grisha
90  * Fixed bug with check: MlaBack_error
91  *
92  * Revision 4.5  1996/05/08  22:09:27  grisha
93  * implement Medline Id, PubMed Id conversion functions
94  *
95  * Revision 4.4  1996/05/08  20:58:36  grisha
96  * implement new set of functions for PubMed support
97  *
98  * Revision 4.3  1996/02/28  22:13:51  kans
99  * trivial codewarrior warnings
100  *
101  * Revision 4.2  1996/02/28  21:41:46  grisha
102  * implement function MedArchGetMriUids(), now this function
103  * will call MedArch server for MRI lookup
104  *
105  * Revision 4.1  1996/01/05  21:40:44  grisha
106  * Add prototypes for new functions. MedArchGetMriUids for
107  * MRI lookup and MedArchGetAccUids for accession lookup
108  *
109  * Revision 4.0  1995/07/26  13:55:12  ostell
110  * force revision to 4.0
111  *
112  * Revision 1.15  1995/05/17  17:53:55  epstein
113  * add RCS log revision history
114  *
115 */
116 #ifndef __MEDARCHC__
117 #define __MEDARCHC__ medarchc
118 
119 /******************************************************************************/
120 /* INCLUDES */
121 /******************************************************************************/
122 #include <ncbinet.h>
123 #include <objmedli.h>
124 #include <objpub.h>
125 #include <objall.h>
126 #include "mapmla.h"
127 #include <objmla.h>
128 #include <medarch.h>
129 
130 /******************************************************************************/
131 /* DEFINES */
132 /******************************************************************************/
133 
134 /******************************************************************************/
135 /* GLOBAL VARIABLES */
136 /******************************************************************************/
137 MlaRequestPtr mlarp;
138 MlaBackPtr mlabp;
139 
140 /******************************************************************************/
141 /* LOCAL VARIABLES */
142 /******************************************************************************/
143 static NI_HandPtr svcp = NULL;
144 static AsnIoPtr   asnin = NULL;
145 static AsnIoPtr   asnout = NULL;
146 static Boolean num_attached = 0;
147 static Boolean reallyFinal = TRUE;
148 static NI_DispatcherPtr dispatcher;
149 static Boolean (*myNetInit) PROTO((void));
150 
151 /******************************************************************************/
152 /* LOCAL FUNCTIONS PROTOTYPES */
153 /******************************************************************************/
154 static MlaBackPtr      NetMedArchReadAsn PROTO((
155                            void
156                        ));
157 static Boolean         ReestablishNetMedArch PROTO((
158                            void
159                        ));
160 static Boolean         GenericReestablishNet PROTO((
161                            CharPtr,
162                            Boolean
163                        ));
164 static Boolean         NetInit PROTO((
165                            void
166                        ));
167 static Boolean         ForceNetInit PROTO((
168                            void
169                        ));
170 static Boolean         NetFini PROTO((
171                            void
172                        ));
173 static MedlineEntryPtr s_MedArchMedlineEntryGet PROTO((
174                            Int4    /* Medline Id */
175                        ));
176 static PubmedEntryPtr  s_MedArchPubmedEntryGetUid PROTO((
177                            Int4    /* PubMed Id */
178                        ));
179 static PubmedEntryPtr  s_MedArchPubmedEntryGetPmId PROTO((
180                            Int4    /* PubMed Id */
181                        ));
182 static MedlarsEntryPtr s_MedArchMedlarsEntryGetUid PROTO((
183                            Int4    /* PubMed Id */
184                        ));
185 static MedlarsEntryPtr s_MedArchMedlarsEntryGetPmId PROTO((
186                            Int4    /* PubMed Id */
187                        ));
188 static MedlineEntryPtr s_MedArchPubSetEntryGet PROTO((
189                            Int4,            /* Medline Id */
190                            MedlineEntryPtr, /* Last Medline entry */
191                            AsnIoPtr,        /* Client ASN.1 stream */
192                            AsnTypePtr       /* ASN.1 types */
193                        ));
194 static PubmedEntryPtr  s_MedArchPubSetEntryGetPmId PROTO((
195                            Int4,            /* PubMed Id */
196                            PubmedEntryPtr,  /* Last PubMed entry */
197                            AsnIoPtr,        /* Client ASN.1 stream */
198                            AsnTypePtr       /* ASN.1 types */
199                        ));
200 static PubmedEntryPtr  s_MedArchPubSetEntryGetUid PROTO((
201                            Int4,            /* PubMed Id */
202                            PubmedEntryPtr,  /* Last PubMed entry */
203                            AsnIoPtr,        /* Client ASN.1 stream */
204                            AsnTypePtr       /* ASN.1 types */
205                        ));
206 static ValNodePtr      s_MedArchGetPub PROTO((
207                            Int4    /* Medline Id */
208                        ));
209 static ValNodePtr      s_MedArchGetPubPmId PROTO((
210                            Int4    /* Medline Id */
211                        ));
212 static Int4            s_MedArchGetTitles PROTO((
213                            CharPtr PNTR,
214                            Int1Ptr,
215                            CharPtr,
216                            Int1,
217                            Int1,
218                            Int4
219                        ));
220 static Int4            s_MedArchCitMatch PROTO((
221                            ValNodePtr
222                        ));
223 static Int4            s_MedArchCitMatchPmId PROTO((
224                            ValNodePtr
225                        ));
226 static ValNodePtr      s_MedArchCitMatchList PROTO((
227                            ValNodePtr,
228                            Int4Ptr
229                        ));
230 static ValNodePtr      s_MedArchGetMriUids PROTO((
231                            Int4
232                        ));
233 static ValNodePtr      s_MedArchGetMriPmids PROTO((
234                            Int4
235                        ));
236 static ValNodePtr      s_MedArchGetAccPmids PROTO((
237                            Int4 iCode,
238                            CharPtr lpAccNum
239                        ));
240 static ValNodePtr      s_MedArchGetAccUids PROTO((
241                             Int4 iCode,
242                             CharPtr lpAccNum
243                        ));
244 static Int4            s_MedArchMu2Pm PROTO((
245                            Int4
246                        ));
247 static Int4            s_MedArchPm2Mu PROTO((
248                            Int4
249                        ));
250 
251 /******************************************************************************/
252 /* LOCAL FUNCTIONS */
253 /******************************************************************************/
254 
255 /******************************************************************************/
256 /*.doc NetMedArchReadAsn(internal) */
257 /*+
258 -*/
259 /******************************************************************************/
260 static MlaBackPtr
NetMedArchReadAsn(void)261 /*FCN*/NetMedArchReadAsn (void)
262 {
263     MlaBackPtr mlabp;
264     short erract;
265     ErrDesc err;
266 
267     ErrGetOpts(&erract, NULL);
268     ErrSetOpts(ERR_CONTINUE, 0);
269     ErrFetch(&err);                  /* clear any pending error */
270 
271     mlabp = MlaBackAsnRead(asnin, NULL);
272 
273     if ( mlabp == NULL ) {
274         ErrPost(CTX_UNKNOWN, 1, "Null message read from server");
275     }
276     ErrSetOpts(erract, 0);
277 
278     return mlabp;
279 }                                /* NetMedArchReadAsn() */
280 
281 /******************************************************************************/
282 /*.doc ReestablishNetMedArch(internal) */
283 /*+
284 -*/
285 /******************************************************************************/
286 static Boolean
ReestablishNetMedArch(void)287 /*FCN*/ReestablishNetMedArch (void)
288 {
289     return GenericReestablishNet("MedArch", TRUE);
290 }                                /* ReestablishNetMedArch() */
291 
292 /******************************************************************************/
293 /*.doc GenericReestablishNet(internal) */
294 /*+
295 -*/
296 /******************************************************************************/
297 static Boolean
GenericReestablishNet(CharPtr svcName,Boolean showErrs)298 /*FCN*/GenericReestablishNet (
299   CharPtr svcName,
300   Boolean showErrs
301 ){
302     Monitor *mon = NULL;
303     Boolean retval;
304     CharPtr buf;
305 
306     buf = (CharPtr)MemNew(2 * StrLen(svcName) + 60);
307 
308     if ( showErrs ) {
309         sprintf (buf, "Re-establishing %s Service", svcName);
310         mon = MonitorStrNew(buf, 40);
311         sprintf (buf, "Requesting %s service", svcName);
312         MonitorStrValue(mon, buf);
313     }
314     NetFini();
315     retval = TRUE;
316 
317     if ( !myNetInit() ) {
318         sprintf (buf, "%s get failed; re-contacting dispatcher", svcName);
319         MonitorStrValue(mon, buf);
320         retval = FALSE;
321         if ( ForceNetInit() ) {
322             /* successfully established contact w/dispatcher */
323             sprintf (
324                 buf,
325                 "%s get failed; re-requesting %s service",
326                 svcName,
327                 svcName
328             );
329             MonitorStrValue(mon, buf);
330             retval = myNetInit();
331         } else {
332             ErrPost(CTX_UNKNOWN, 1, "Unable to re-contact dispatcher");
333             if (showErrs) {
334                 ErrShow();
335             }
336         }
337     }
338 
339     MonitorFree(mon);
340 
341     if ( !retval ) {
342         sprintf (
343             buf,
344             "Unable to re-establish %s service",
345             svcName
346         );
347         ErrPost(CTX_UNKNOWN, 1, buf);
348         if ( showErrs ) {
349             ErrShow();
350         }
351     }
352 
353     MemFree((VoidPtr)buf);
354     return retval;
355 }                                       /* GenericReestablishNet() */
356 
357 /******************************************************************************/
358 /*.doc (internal) */
359 /*+
360 -*/
361 /******************************************************************************/
362 static Boolean
NetInit(void)363 /*FCN*/NetInit(void)
364 {
365     if ( num_attached++ > 0 ) {
366         return TRUE;
367     }
368 
369     if ( (dispatcher = NI_GenericInit (
370                            NULL,
371                            NULL,
372                            TRUE,
373                            NULL,
374                            0
375                        )) != NULL ) {
376         return TRUE;
377     }
378 
379     return FALSE;
380 }                                        /* NetInit() */
381 
382 /******************************************************************************/
383 /*.doc ForceNetInit(internal) */
384 /*+
385 -*/
386 /******************************************************************************/
387 static Boolean
ForceNetInit(void)388 /*FCN*/ForceNetInit (void)
389 {
390     Boolean retval;
391 
392     reallyFinal = FALSE;
393     num_attached = 0; /* force re-attempt to contact dispatcher */
394     retval = NetInit();
395     reallyFinal = TRUE;
396 
397     return retval;
398 }                                       /* ForceNetInit() */
399 
400 /******************************************************************************/
401 /*.doc NetFini(internal) */
402 /*+
403 -*/
404 /******************************************************************************/
405 static Boolean
NetFini(void)406 /*FCN*/NetFini (void)
407 {
408     if ( num_attached > 0 ) {
409         num_attached--;
410     }
411 
412     if ( num_attached == 0 ) {
413         NI_ServiceDisconnect (svcp);
414         svcp = NULL;
415         NI_EndServices (dispatcher);
416         dispatcher = NULL;
417     }
418 
419     return TRUE;
420 }                                        /* NetFini() */
421 
422 /******************************************************************************/
423 /*.doc s_MedArchFini(internal) */
424 /*+
425    Create FINI request and send to MedArch server, terminate
426    network connection.
427 -*/
428 /******************************************************************************/
429 static Boolean
s_MedArchFini(void)430 /*FCN*/s_MedArchFini (void)
431 {
432     Boolean retval = TRUE;
433 
434     if ( asnout != NULL && asnin != NULL ) {
435         mlarp = ValNodeNew(NULL);
436         mlarp->choice = MlaRequest_fini;
437         MlaRequestAsnWrite (mlarp, asnout, NULL);
438         AsnIoReset(asnout);
439         MlaRequestFree (mlarp);
440 
441         if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
442             retval = FALSE;
443         } else {
444             mlabp->data.ptrvalue = NULL;
445             MlaBackFree (mlabp);
446         }
447     }
448 
449     NetFini();
450 
451     return retval;
452 }                          /* s_MedArchFini() */
453 
454 /******************************************************************************/
455 /*.doc s_MedArchMedlineEntryGet(internal) */
456 /*+
457    Generate request: Get Medline Article using Medline Id, return
458    pointer to Medline entry. Return NULL if article not found or
459    some errors found.
460 -*/
461 /******************************************************************************/
462 static MedlineEntryPtr
s_MedArchMedlineEntryGet(Int4 uid)463 /*FCN*/s_MedArchMedlineEntryGet (
464   Int4 uid
465 ){
466     MlaRequestPtr mlarp;
467     MedlineEntryPtr mep;
468 
469     /* create request object */
470     mlarp = ValNodeNew(NULL);                 /* allocate node */
471     mlarp->choice = MlaRequest_getmle;        /* request type */
472     mlarp->data.intvalue = uid;               /* Medline id */
473     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
474     AsnIoReset(asnout);                       /* fush output stream */
475     MlaRequestFree (mlarp);                   /* free request */
476 
477     /* result processing */
478     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
479         return NULL;
480     }
481 
482     if ( mlabp->choice != MlaBack_getmle ) {
483         MlaBackFree (mlabp);
484         return NULL;
485     }
486 
487     mep = (MedlineEntryPtr) (mlabp->data.ptrvalue);
488     mlabp->data.ptrvalue = NULL; /* for clean free */
489     MlaBackFree (mlabp);
490 
491     return mep;
492 }                                    /* s_MedArchMedlineEntryGet() */
493 
494 /******************************************************************************/
495 /*.doc s_MedArchPubmedEntryGetPmId(internal) */
496 /*+
497    Generate request: Get Medline Article using PubMed Id, return
498    pointer to PubMed entry. Return NULL if article not found or
499    some errors found.
500 -*/
501 /******************************************************************************/
502 static PubmedEntryPtr
s_MedArchPubmedEntryGetPmId(Int4 pmid)503 /*FCN*/s_MedArchPubmedEntryGetPmId (
504   Int4 pmid
505 ){
506     MlaRequestPtr mlarp;
507     PubmedEntryPtr mep;
508 
509     /* create request object */
510     mlarp = ValNodeNew(NULL);                 /* allocate node */
511     mlarp->choice = MlaRequest_getmlepmid;    /* request type */
512     mlarp->data.intvalue = pmid;              /* PubMed id */
513     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
514     AsnIoReset(asnout);                       /* fush output stream */
515     MlaRequestFree (mlarp);                   /* free request */
516 
517     /* result processing */
518     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
519         return NULL;
520     }
521 
522     if ( mlabp->choice != MlaBack_getpme ) {
523         MlaBackFree (mlabp);
524         return NULL;
525     }
526 
527     mep = (PubmedEntryPtr) (mlabp->data.ptrvalue);
528     mlabp->data.ptrvalue = NULL; /* for clean free */
529     MlaBackFree (mlabp);
530 
531     return mep;
532 }                                    /* s_MedArchPubmedEntryGetPmId() */
533 
534 /******************************************************************************/
535 /*.doc s_MedArchPubmedEntryGetUid(internal) */
536 /*+
537    Generate request: Get Medline Article using PubMed Id, return
538    pointer to PubMed entry. Return NULL if article not found or
539    some errors found.
540 -*/
541 /******************************************************************************/
542 static PubmedEntryPtr
s_MedArchPubmedEntryGetUid(Int4 pmid)543 /*FCN*/s_MedArchPubmedEntryGetUid (
544   Int4 pmid
545 ){
546     MlaRequestPtr mlarp;
547     PubmedEntryPtr mep;
548 
549     /* create request object */
550     mlarp = ValNodeNew(NULL);                 /* allocate node */
551     mlarp->choice = MlaRequest_getmleuid;     /* request type */
552     mlarp->data.intvalue = pmid;              /* PubMed id */
553     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
554     AsnIoReset(asnout);                       /* fush output stream */
555     MlaRequestFree (mlarp);                   /* free request */
556 
557     /* result processing */
558     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
559         return NULL;
560     }
561 
562     if ( mlabp->choice != MlaBack_getpme ) {
563         MlaBackFree (mlabp);
564         return NULL;
565     }
566 
567     mep = (PubmedEntryPtr) (mlabp->data.ptrvalue);
568     mlabp->data.ptrvalue = NULL; /* for clean free */
569     MlaBackFree (mlabp);
570 
571     return mep;
572 }                                    /* s_MedArchPubmedEntryGetUid() */
573 
574 /******************************************************************************/
575 /*.doc s_MedArchMedlarsEntryGetPmId(internal) */
576 /*+
577    Generate request: Get MEDLARS entry using PubMed Id, return
578    pointer to Medlars entry. Return NULL if article not found or
579    some errors found.
580 -*/
581 /******************************************************************************/
582 static MedlarsEntryPtr
s_MedArchMedlarsEntryGetPmId(Int4 pmid)583 /*FCN*/s_MedArchMedlarsEntryGetPmId (
584   Int4 pmid
585 ){
586     MlaRequestPtr mlarp;
587     MedlarsEntryPtr mep;
588 
589     /* create request object */
590     mlarp = ValNodeNew(NULL);                 /* allocate node */
591     mlarp->choice = MlaRequest_getmlrpmid;    /* request type */
592     mlarp->data.intvalue = pmid;              /* PubMed id */
593     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
594     AsnIoReset(asnout);                       /* fush output stream */
595     MlaRequestFree (mlarp);                   /* free request */
596 
597     /* result processing */
598     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
599         return NULL;
600     }
601 
602     if ( mlabp->choice != MlaBack_getmlr ) {
603         MlaBackFree (mlabp);
604         return NULL;
605     }
606 
607     mep = (MedlarsEntryPtr) (mlabp->data.ptrvalue);
608     mlabp->data.ptrvalue = NULL; /* for clean free */
609     MlaBackFree (mlabp);
610 
611     return mep;
612 }                                    /* s_MedArchMedlarsEntryGetPmId() */
613 
614 /******************************************************************************/
615 /*.doc s_MedArchMedlarsEntryGetUid(internal) */
616 /*+
617    Generate request: Get MEDLARS entry using PubMed Id, return
618    pointer to Medlars entry. Return NULL if article not found or
619    some errors found.
620 -*/
621 /******************************************************************************/
622 static MedlarsEntryPtr
s_MedArchMedlarsEntryGetUid(Int4 pmid)623 /*FCN*/s_MedArchMedlarsEntryGetUid (
624   Int4 pmid
625 ){
626     MlaRequestPtr mlarp;
627     MedlarsEntryPtr mep;
628 
629     /* create request object */
630     mlarp = ValNodeNew(NULL);                 /* allocate node */
631     mlarp->choice = MlaRequest_getmlruid;     /* request type */
632     mlarp->data.intvalue = pmid;              /* PubMed id */
633     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
634     AsnIoReset(asnout);                       /* fush output stream */
635     MlaRequestFree (mlarp);                   /* free request */
636 
637     /* result processing */
638     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
639         return NULL;
640     }
641 
642     if ( mlabp->choice != MlaBack_getmlr ) {
643         MlaBackFree (mlabp);
644         return NULL;
645     }
646 
647     mep = (MedlarsEntryPtr) (mlabp->data.ptrvalue);
648     mlabp->data.ptrvalue = NULL; /* for clean free */
649     MlaBackFree (mlabp);
650 
651     return mep;
652 }                                    /* s_MedArchMedlarsEntryGetUid() */
653 
654 /******************************************************************************/
655 /*.doc s_MedArchPubSetEntryGet(internal) */
656 /*+
657    This function read Medline entry from MedArch and write
658    last entry while waiting for MedArch answer.
659 -*/
660 /******************************************************************************/
661 static MedlineEntryPtr
s_MedArchPubSetEntryGet(Int4 uid,MedlineEntryPtr lastmep,AsnIoPtr aip,AsnTypePtr atp)662 /*FCN*/s_MedArchPubSetEntryGet (
663   Int4 uid,
664   MedlineEntryPtr lastmep,
665   AsnIoPtr aip,
666   AsnTypePtr atp
667 ){
668     MlaRequestPtr mlarp;
669     MedlineEntryPtr mep;
670 
671     mlarp = ValNodeNew(NULL);
672     mlarp->choice = MlaRequest_getmle;
673     mlarp->data.intvalue = uid;
674     MlaRequestAsnWrite (mlarp, asnout, NULL);
675     AsnIoReset (asnout);
676     MlaRequestFree (mlarp);
677 
678     /* write the caller's last entry to a file while waiting
679        for the server to compute and send the answer for the
680        current entry
681     */
682     if ( lastmep != NULL && aip != NULL ) {
683         MedlineEntryAsnWrite (lastmep, aip, atp);
684     }
685 
686     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
687         return NULL;
688     }
689 
690     if ( mlabp->choice != MlaBack_getmle ) {
691         MlaBackFree (mlabp);
692         return NULL;
693     }
694 
695     mep = (MedlineEntryPtr) (mlabp->data.ptrvalue);
696     mlabp->data.ptrvalue = NULL; /* for clean free */
697     MlaBackFree (mlabp);
698 
699     return mep;
700 }                                 /* s_MedArchPubSetEntryGet() */
701 
702 /******************************************************************************/
703 /*.doc s_MedArchPubSetEntryGetPmId(internal) */
704 /*+
705    This function read Medline entry from MedArch and write
706    last entry while waiting for MedArch answer.
707 -*/
708 /******************************************************************************/
709 static PubmedEntryPtr
s_MedArchPubSetEntryGetPmId(Int4 pmid,PubmedEntryPtr lastmep,AsnIoPtr aip,AsnTypePtr atp)710 /*FCN*/s_MedArchPubSetEntryGetPmId (
711   Int4 pmid,
712   PubmedEntryPtr lastmep,
713   AsnIoPtr aip,
714   AsnTypePtr atp
715 ){
716     MlaRequestPtr mlarp;
717     PubmedEntryPtr mep;
718 
719     mlarp = ValNodeNew(NULL);
720     mlarp->choice = MlaRequest_getmlepmid;
721     mlarp->data.intvalue = pmid;
722     MlaRequestAsnWrite (mlarp, asnout, NULL);
723     AsnIoReset (asnout);
724     MlaRequestFree (mlarp);
725 
726     /* write the caller's last entry to a file while waiting
727        for the server to compute and send the answer for the
728        current entry
729     */
730     if ( lastmep != NULL && aip != NULL ) {
731         PubmedEntryAsnWrite (lastmep, aip, atp);
732     }
733 
734     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
735         return NULL;
736     }
737 
738     if ( mlabp->choice != MlaBack_getpme ) {
739         MlaBackFree (mlabp);
740         return NULL;
741     }
742 
743     mep = (PubmedEntryPtr) (mlabp->data.ptrvalue);
744     mlabp->data.ptrvalue = NULL; /* for clean free */
745     MlaBackFree (mlabp);
746 
747     return mep;
748 }                                /* s_MedArchPubSetEntryGetPmId() */
749 
750 /******************************************************************************/
751 /*.doc s_MedArchPubSetEntryGetUid(internal) */
752 /*+
753    This function read Medline entry from MedArch and write
754    last entry while waiting for MedArch answer.
755 -*/
756 /******************************************************************************/
757 static PubmedEntryPtr
s_MedArchPubSetEntryGetUid(Int4 pmid,PubmedEntryPtr lastmep,AsnIoPtr aip,AsnTypePtr atp)758 /*FCN*/s_MedArchPubSetEntryGetUid (
759   Int4 pmid,
760   PubmedEntryPtr lastmep,
761   AsnIoPtr aip,
762   AsnTypePtr atp
763 ){
764     MlaRequestPtr mlarp;
765     PubmedEntryPtr mep;
766 
767     mlarp = ValNodeNew(NULL);
768     mlarp->choice = MlaRequest_getmleuid;
769     mlarp->data.intvalue = pmid;
770     MlaRequestAsnWrite (mlarp, asnout, NULL);
771     AsnIoReset (asnout);
772     MlaRequestFree (mlarp);
773 
774     /* write the caller's last entry to a file while waiting
775        for the server to compute and send the answer for the
776        current entry
777     */
778     if ( lastmep != NULL && aip != NULL ) {
779         PubmedEntryAsnWrite (lastmep, aip, atp);
780     }
781 
782     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
783         return NULL;
784     }
785 
786     if ( mlabp->choice != MlaBack_getpme ) {
787         MlaBackFree (mlabp);
788         return NULL;
789     }
790 
791     mep = (PubmedEntryPtr) (mlabp->data.ptrvalue);
792     mlabp->data.ptrvalue = NULL; /* for clean free */
793     MlaBackFree (mlabp);
794 
795     return mep;
796 }                                /* s_MedArchPubSetEntryGetUid() */
797 
798 /******************************************************************************/
799 /*.doc s_MedArchGetPub(internal) */
800 /*+
801    This function read Citation definition for selected entry using
802    Medline Id.
803 -*/
804 /******************************************************************************/
805 static ValNodePtr
s_MedArchGetPub(Int4 uid)806 /*FCN*/s_MedArchGetPub (
807   Int4 uid                       /* Medline Id */
808 ){
809     MlaRequestPtr mlarp;
810     ValNodePtr pub;
811 
812     mlarp = ValNodeNew(NULL);
813     mlarp->choice = MlaRequest_getpub;
814     mlarp->data.intvalue = uid;
815     MlaRequestAsnWrite (mlarp, asnout, NULL);
816     AsnIoReset(asnout);
817     MlaRequestFree (mlarp);
818 
819     if ((mlabp = NetMedArchReadAsn()) == NULL)
820         return NULL;
821 
822     if (mlabp->choice != MlaBack_getpub)
823     {
824         MlaBackFree (mlabp);
825         return NULL;
826     }
827     pub = (ValNodePtr)mlabp->data.ptrvalue;
828     mlabp->data.ptrvalue = NULL; /* for clean free */
829     MlaBackFree (mlabp);
830 
831     return pub;
832 }                                /* s_MedArchGetPub() */
833 
834 /******************************************************************************/
835 /*.doc s_MedArchGetPubPmId(internal) */
836 /*+
837    This function read Citation definition for selected entry using
838    PubMed Id.
839 -*/
840 /******************************************************************************/
841 static ValNodePtr
s_MedArchGetPubPmId(Int4 pmid)842 /*FCN*/s_MedArchGetPubPmId (
843   Int4 pmid                       /* PubMed Id */
844 ){
845     MlaRequestPtr mlarp;
846     ValNodePtr pub;
847 
848     mlarp = ValNodeNew(NULL);
849     mlarp->choice = MlaRequest_getpubpmid;
850     mlarp->data.intvalue = pmid;
851     MlaRequestAsnWrite (mlarp, asnout, NULL);
852     AsnIoReset(asnout);
853     MlaRequestFree (mlarp);
854 
855     if ((mlabp = NetMedArchReadAsn()) == NULL)
856         return NULL;
857 
858     if (mlabp->choice != MlaBack_getpub)
859     {
860         MlaBackFree (mlabp);
861         return NULL;
862     }
863     pub = (ValNodePtr)mlabp->data.ptrvalue;
864     mlabp->data.ptrvalue = NULL; /* for clean free */
865     MlaBackFree (mlabp);
866 
867     return pub;
868 }                                /* s_MedArchGetPubPmId() */
869 
870 /******************************************************************************/
871 /*.doc s_MedArchGetTitles(internal) */
872 /*+
873 -*/
874 /******************************************************************************/
875 
876 /*
877 extern void LaunchAsnTextViewer (Pointer from, AsnWriteFunc writefunc, CharPtr title);
878 */
879 
880 static Int4
s_MedArchGetTitles(CharPtr PNTR titles_found,Int1Ptr title_types_found,CharPtr title_to_lookup,Int1 request_type,Int1 response_type,Int4 max_titles_to_find)881 /*FCN*/s_MedArchGetTitles (
882   CharPtr PNTR titles_found,
883   Int1Ptr title_types_found,
884   CharPtr title_to_lookup,
885   Int1 request_type,
886   Int1 response_type,
887   Int4 max_titles_to_find
888 ){
889     MlaRequestPtr mlarp;
890     TitleMsgPtr tmsgp;
891     TitleMsgListPtr tmlp;
892     Int4 i;
893 
894     mlarp = ValNodeNew(NULL);
895     mlarp->choice = MlaRequest_gettitle;
896     tmsgp = TitleMsgNew();
897     tmsgp->type = response_type;
898     tmsgp->title = ValNodeNew(NULL);
899     tmsgp->title->choice = request_type;
900     tmsgp->title->data.ptrvalue = (Pointer) StringSave(title_to_lookup);
901     mlarp->data.ptrvalue = (Pointer) tmsgp;
902     MlaRequestAsnWrite (mlarp, asnout, NULL);
903 
904 /*
905 LaunchAsnTextViewer ((Pointer) mlarp, (AsnWriteFunc) MlaRequestAsnWrite, "medarch request");
906 */
907 
908     AsnIoReset(asnout);
909     MlaRequestFree (mlarp);
910 
911     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
912         return 0;
913     }
914 
915 /*
916 LaunchAsnTextViewer ((Pointer) mlabp, (AsnWriteFunc) MlaBackAsnWrite, "medarch result");
917 */
918 
919     if ( mlabp->choice != MlaBack_gettitle ) {
920         MlaBackFree (mlabp);
921         return 0;
922     }
923 
924     if ( (tmlp = (TitleMsgListPtr)mlabp->data.ptrvalue) == NULL ) {
925         MlaBackFree (mlabp);
926         return 0;
927     }
928 
929     for ( i = 0, tmsgp = tmlp->titles;
930              i < max_titles_to_find
931           && tmlp != NULL
932           && i < tmlp->num;
933           i++, tmsgp = tmsgp->next ) {
934         titles_found[i] = (CharPtr) tmsgp->title->data.ptrvalue;
935         tmsgp->title->data.ptrvalue = NULL; /* for clean free */
936         title_types_found[i] = (Int1) tmsgp->type;
937     }
938     MlaBackFree (mlabp);
939 
940     return i;
941 }                                        /* s_MedArchGetTitles() */
942 
943 /******************************************************************************/
944 /*.doc s_MedArchCitMatch(internal) */
945 /*+
946 -*/
947 /******************************************************************************/
948 static Int4
s_MedArchCitMatch(ValNodePtr pub)949 /*FCN*/s_MedArchCitMatch (
950   ValNodePtr pub
951 ){
952     MlaRequestPtr mlarp;
953     Int4 uid;
954 
955     mlarp = ValNodeNew(NULL);
956     mlarp->choice = MlaRequest_citmatch;
957     mlarp->data.ptrvalue = pub;
958     MlaRequestAsnWrite (mlarp, asnout, NULL);
959     AsnIoReset(asnout);
960     mlarp->data.ptrvalue = NULL; /* for clean free */
961     MlaRequestFree (mlarp);
962 
963     if ((mlabp = NetMedArchReadAsn()) == NULL)
964         return 0;
965 
966     if (mlabp->choice != MlaBack_citmatch)
967     {
968         MlaBackFree (mlabp);
969         return 0;
970     }
971     uid = mlabp->data.intvalue;
972     MlaBackFree (mlabp);
973 
974     return uid;
975 }                                        /* s_MedArchCitMatch() */
976 
977 /******************************************************************************/
978 /*.doc s_MedArchCitMatchPmId(internal) */
979 /*+
980 -*/
981 /******************************************************************************/
982 static Boolean debug_med_arch_cit_match_pmid = FALSE;
983 static Boolean debug_med_arch_cit_match_pmid_set = FALSE;
984 
985 static Int4
s_MedArchCitMatchPmId(ValNodePtr pub)986 /*FCN*/s_MedArchCitMatchPmId (
987   ValNodePtr pub
988 ){
989     MlaRequestPtr mlarp;
990     Int4 pmid;
991 #ifdef OS_UNIX
992     AsnIoPtr  aip;
993     CharPtr   str;
994 #endif
995 
996     mlarp = ValNodeNew(NULL);
997     mlarp->choice = MlaRequest_citmatchpmid;
998     mlarp->data.ptrvalue = pub;
999 #ifdef OS_UNIX
1000     if (! debug_med_arch_cit_match_pmid_set) {
1001       str = (CharPtr) getenv ("DEBUG_MED_ARCH_CIT_MATCH_PMID");
1002       if (StringDoesHaveText (str)) {
1003         if (StringICmp (str, "TRUE") == 0) {
1004           debug_med_arch_cit_match_pmid = TRUE;
1005         }
1006       }
1007       debug_med_arch_cit_match_pmid_set = TRUE;
1008     }
1009 
1010     if (debug_med_arch_cit_match_pmid) {
1011       if (mlarp != NULL) {
1012         aip = AsnIoOpen ("origmlarequest.txt", "w");
1013         if (aip != NULL) {
1014           MlaRequestAsnWrite (mlarp, aip, NULL);
1015           AsnIoClose (aip);
1016         }
1017       }
1018     }
1019 #endif
1020     MlaRequestAsnWrite (mlarp, asnout, NULL);
1021     AsnIoReset(asnout);
1022     mlarp->data.ptrvalue = NULL; /* for clean free */
1023     MlaRequestFree (mlarp);
1024 
1025     if ((mlabp = NetMedArchReadAsn()) == NULL)
1026         return 0;
1027 
1028     if (mlabp->choice != MlaBack_citmatch)
1029     {
1030         MlaBackFree (mlabp);
1031         return 0;
1032     }
1033     pmid = mlabp->data.intvalue;
1034     MlaBackFree (mlabp);
1035 
1036     return pmid;
1037 }                                        /* s_MedArchCitMatchPmId() */
1038 
1039 /******************************************************************************/
1040 /*.doc s_MedArchCitMatchList(internal) */
1041 /*+
1042    This function send request '' to MedArch server and recive result
1043    list. It set up error code in case some error found.
1044 -*/
1045 /******************************************************************************/
1046 static ValNodePtr
s_MedArchCitMatchList(ValNodePtr pub,Int4Ptr pErrCode)1047 /*FCN*/s_MedArchCitMatchList (
1048   ValNodePtr pub,
1049   Int4Ptr    pErrCode
1050 ){
1051     MlaRequestPtr mlarp;
1052     register ValNodePtr pPmIdList = NULL; /* result PubMed ID list */
1053 
1054     mlarp = ValNodeNew(NULL);
1055     mlarp->choice = MlaRequest_citlstpmids;
1056     mlarp->data.ptrvalue = pub;
1057     MlaRequestAsnWrite (mlarp, asnout, NULL);
1058     AsnIoReset(asnout);
1059     mlarp->data.ptrvalue = NULL; /* for clean free */
1060     MlaRequestFree (mlarp);
1061 
1062     if ((mlabp = NetMedArchReadAsn()) == NULL) {
1063         *pErrCode = Error_val_operational_error;
1064         return NULL;
1065     }
1066 
1067     if ( mlabp->choice == MlaBack_getpmids ) { /* must be PubMed ID list */
1068         pPmIdList = (ValNodePtr)mlabp->data.ptrvalue; /* get Medline list  */
1069         mlabp->data.ptrvalue = NULL;                  /* for clean free */
1070         *pErrCode = 0;
1071     } else if ( mlabp->choice == MlaBack_error ) {
1072         *pErrCode = mlabp->data.intvalue;
1073     } else {
1074         *pErrCode = Error_val_operational_error;
1075     }
1076 
1077     MlaBackFree (mlabp);            /* free result */
1078 
1079     return pPmIdList;
1080 }                                /* s_MedArchCitMatchList() */
1081 
1082 /******************************************************************************/
1083 /*.doc s_MedArchGetMriUids(internal) */
1084 /*+
1085    This function call MedArch server and send MRI code, it recive
1086    list of Medline ID. Return NULL if where are no articles in
1087    selected MRI or if some error found.
1088 -*/
1089 /******************************************************************************/
1090 static
s_MedArchGetMriUids(Int4 mri)1091 ValNodePtr s_MedArchGetMriUids (
1092   Int4 mri                             /* Journal MRI code (I) */
1093 ){
1094     MlaRequestPtr mlarp;               /* to build request object */
1095     ValNodePtr pub = NULL;             /* result */
1096 
1097     mlarp = ValNodeNew(NULL);                 /* create request node */
1098     mlarp->choice = MlaRequest_getmriuids;    /* request code MRI lookup */
1099     mlarp->data.intvalue = mri;               /* journal MRI code */
1100     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to request server */
1101     AsnIoReset(asnout);                       /* reset ASN out buffer */
1102     MlaRequestFree (mlarp);                   /* do not need request node */
1103 
1104     if ( (mlabp = NetMedArchReadAsn()) == NULL ) { /* try to read result */
1105         return NULL;                               /* error */
1106     }
1107 
1108     if ( mlabp->choice != MlaBack_getuids ) { /* must be Medline ID list */
1109         MlaBackFree (mlabp);                  /* free result */
1110         return NULL;                          /* error */
1111     }
1112 
1113     pub = (ValNodePtr)mlabp->data.ptrvalue; /* get Medline list  */
1114     mlabp->data.ptrvalue = NULL;            /* for clean free */
1115     MlaBackFree (mlabp);                    /* free result */
1116 
1117     return pub;
1118 }                                   /* s_MedArchGetMriUids() */
1119 
1120 /******************************************************************************/
1121 /*.doc s_MedArchGetMriPmids(internal) */
1122 /*+
1123    This function call MedArch server and send MRI code, it recive
1124    list of Medline ID. Return NULL if where are no articles in
1125    selected MRI or if some error found.
1126 -*/
1127 /******************************************************************************/
1128 static
s_MedArchGetMriPmids(Int4 mri)1129 ValNodePtr s_MedArchGetMriPmids (
1130   Int4 mri                             /* Journal MRI code (I) */
1131 ){
1132     MlaRequestPtr mlarp;               /* to build request object */
1133     ValNodePtr pub = NULL;             /* result */
1134 
1135     mlarp = ValNodeNew(NULL);                 /* create request node */
1136     mlarp->choice = MlaRequest_getmripmids;   /* request code MRI lookup */
1137     mlarp->data.intvalue = mri;               /* journal MRI code */
1138     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to request server */
1139     AsnIoReset(asnout);                       /* reset ASN out buffer */
1140     MlaRequestFree (mlarp);                   /* do not need request node */
1141 
1142     if ( (mlabp = NetMedArchReadAsn()) == NULL ) { /* try to read result */
1143         return NULL;                               /* error */
1144     }
1145 
1146     if ( mlabp->choice != MlaBack_getuids ) { /* must be Medline ID list */
1147         MlaBackFree (mlabp);                  /* free result */
1148         return NULL;                          /* error */
1149     }
1150 
1151     pub = (ValNodePtr)mlabp->data.ptrvalue; /* get Medline list  */
1152     mlabp->data.ptrvalue = NULL;            /* for clean free */
1153     MlaBackFree (mlabp);                    /* free result */
1154 
1155     return pub;
1156 }                                   /* s_MedArchGetMriPmids() */
1157 
1158 /******************************************************************************/
1159 /*.doc s_MedArchGetAccUids(internal) */
1160 /*+
1161    This function call MedArch server and sends accession/xref code, it recives
1162    list of Medline IDs. plAccNum is an optional argument in accordance with
1163    Medline-si definition. Set it to NULL if accession code is unknown.
1164    It returns NULL if there are no entries found or if some error occurred.
1165 -*/
1166 /******************************************************************************/
1167 static
s_MedArchGetAccUids(Int4 iCode,CharPtr lpAccNum)1168 ValNodePtr s_MedArchGetAccUids (
1169   Int4 iCode,                  /* Type of xref (I) */
1170   CharPtr lpAccNum             /* The citation/accession number (I) */
1171 ){
1172     MlaRequestPtr mlarp;               /* to build request object */
1173     ValNodePtr pub = NULL;             /* result */
1174 
1175     mlarp = ValNodeNew(NULL);                 /* create request node */
1176     mlarp->choice = MlaRequest_getaccuids;    /* request code MRI lookup */
1177     mlarp->data.ptrvalue = ValNodeNew(NULL);  /* Medline-si structure */
1178     ((ValNodePtr)mlarp->data.ptrvalue)->choice = iCode;
1179     ((ValNodePtr)mlarp->data.ptrvalue)->data.ptrvalue =
1180                  (Nlm_VoidPtr)(lpAccNum == NULL ? NULL : StringSave(lpAccNum));
1181     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to request server */
1182     AsnIoReset(asnout);                       /* reset ASN out buffer */
1183     MlaRequestFree (mlarp);                   /* do not need request node */
1184 
1185     if ( (mlabp = NetMedArchReadAsn()) == NULL ) { /* try to read result */
1186         return NULL;                               /* error */
1187     }
1188 
1189     if ( mlabp->choice != MlaBack_getuids ) { /* must be Medline ID list */
1190         MlaBackFree (mlabp);                  /* free result */
1191         return NULL;                          /* error */
1192     }
1193 
1194     pub = (ValNodePtr)mlabp->data.ptrvalue; /* get Medline list  */
1195     mlabp->data.ptrvalue = NULL;            /* for clean free */
1196     MlaBackFree (mlabp);                    /* free result */
1197 
1198     return pub;
1199 }                                   /* s_MedArchGetAccUids() */
1200 
1201 /******************************************************************************/
1202 /*.doc s_MedArchGetAccPmids(internal) */
1203 /*+
1204    This function call MedArch server and sends accession/xref code, it recives
1205    list of PubMed IDs.
1206    plAccNum is an optional argument in accordance with
1207    Medline-si definition. Set it to NULL if accession code is unknown.
1208    It returns NULL if there are no entries found or if some error occurred.
1209  -*/
1210 /******************************************************************************/
1211 static
s_MedArchGetAccPmids(Int4 iCode,CharPtr lpAccNum)1212 ValNodePtr s_MedArchGetAccPmids (
1213   Int4 iCode,                  /* Type of xref (I) */
1214   CharPtr lpAccNum             /* The citation/accession number (I) */
1215 ){
1216     MlaRequestPtr mlarp;               /* to build request object */
1217     ValNodePtr pub = NULL;             /* result */
1218 
1219     mlarp = ValNodeNew(NULL);                 /* create request node */
1220     mlarp->choice = MlaRequest_getaccpmids;   /* request code PmId lookup */
1221     mlarp->data.ptrvalue = ValNodeNew(NULL);  /* Medline-si structure */
1222     ((ValNodePtr)mlarp->data.ptrvalue)->choice = iCode;
1223     ((ValNodePtr)mlarp->data.ptrvalue)->data.ptrvalue =
1224                  (Nlm_VoidPtr)(lpAccNum == NULL ? NULL : StringSave(lpAccNum));
1225     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to request server */
1226     AsnIoReset(asnout);                       /* reset ASN out buffer */
1227     MlaRequestFree (mlarp);                   /* do not need request node */
1228 
1229     if ( (mlabp = NetMedArchReadAsn()) == NULL ) { /* try to read result */
1230         return NULL;                               /* error */
1231     }
1232 
1233     if ( mlabp->choice != MlaBack_getpmids ) { /* must be Pm ID list */
1234         MlaBackFree (mlabp);                  /* free result */
1235         return NULL;                          /* error */
1236     }
1237 
1238     pub = (ValNodePtr)mlabp->data.ptrvalue; /* get Medline list  */
1239     mlabp->data.ptrvalue = NULL;            /* for clean free */
1240     MlaBackFree (mlabp);                    /* free result */
1241 
1242     return pub;
1243 }                                   /* s_MedArchGetAccPmids() */
1244 
1245 /******************************************************************************/
1246 /*.doc s_MedArchMu2Pm(internal) */
1247 /*+
1248    This function generate request to MedArch server for
1249    convert Medline Id to PubMed Id
1250 -*/
1251 /******************************************************************************/
1252 static Int4
s_MedArchMu2Pm(Int4 uid)1253 /*FCN*/s_MedArchMu2Pm (
1254   Int4 uid
1255 ){
1256     MlaRequestPtr mlarp;
1257     Int4 pmid;
1258 
1259     /* create request object */
1260     mlarp = ValNodeNew(NULL);                 /* allocate node */
1261     mlarp->choice = MlaRequest_uidtopmid;     /* request type */
1262     mlarp->data.intvalue = uid;               /* Medline id */
1263     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
1264     AsnIoReset(asnout);                       /* fush output stream */
1265     MlaRequestFree (mlarp);                   /* free request */
1266 
1267     /* result processing */
1268     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
1269         return 0;
1270     }
1271 
1272     if ( mlabp->choice != MlaBack_outpmid ) {
1273         MlaBackFree (mlabp);
1274         return 0;
1275     }
1276     pmid = mlabp->data.intvalue;
1277     MlaBackFree (mlabp);
1278 
1279     return pmid;
1280 }                                 /* s_MedArchMu2Pm() */
1281 
1282 /******************************************************************************/
1283 /*.doc s_MedArchMu2Pm(internal) */
1284 /*+
1285    This function generate request to MedArch server for
1286    convert PubMed Id to Medline Id
1287 -*/
1288 /******************************************************************************/
1289 static Int4
s_MedArchPm2Mu(Int4 pmid)1290 /*FCN*/s_MedArchPm2Mu (
1291   Int4 pmid
1292 ){
1293     MlaRequestPtr mlarp;
1294     Int4 uid;
1295 
1296     /* create request object */
1297     mlarp = ValNodeNew(NULL);                 /* allocate node */
1298     mlarp->choice = MlaRequest_pmidtouid;     /* request type */
1299     mlarp->data.intvalue = pmid;              /* Medline id */
1300     MlaRequestAsnWrite (mlarp, asnout, NULL); /* send to MedArch */
1301     AsnIoReset(asnout);                       /* fush output stream */
1302     MlaRequestFree (mlarp);                   /* free request */
1303 
1304     /* result processing */
1305     if ( (mlabp = NetMedArchReadAsn()) == NULL ) {
1306         return 0;
1307     }
1308 
1309     if ( mlabp->choice != MlaBack_outuid ) {
1310         MlaBackFree (mlabp);
1311         return 0;
1312     }
1313     uid = mlabp->data.intvalue;
1314     MlaBackFree (mlabp);
1315 
1316     return uid;
1317 }                                 /* s_MedArchPm2Mu() */
1318 
1319 /******************************************************************************/
1320 /* GLOBAL FUNCTIONS */
1321 /******************************************************************************/
1322 
1323 /******************************************************************************/
1324 /*.doc MedArchInit(external) */
1325 /*+
1326    Create INIT request and send to MedArch server
1327 -*/
1328 /******************************************************************************/
1329 Boolean
MedArchInit(void)1330 /*FCN*/MedArchInit (void)
1331 {
1332     myNetInit = MedArchInit;
1333 
1334     asnin = NULL;                /* reset I/O streams */
1335     asnout = NULL;
1336 
1337     if ( !NetInit() ) {          /* try init network */
1338         return FALSE;
1339     }
1340 
1341     /* make connection with MedArch */
1342     if ( (svcp = NI_GenericGetService (
1343                      dispatcher,
1344                      NULL,
1345                      "MED_ARCH",
1346                      "MedArch",
1347                      TRUE
1348                  )) == NULL ) {
1349         ErrPostEx (              /* report error */
1350             SEV_ERROR,
1351             0,
1352             0,
1353             "NI_ServiceGet [%s] (%s)",
1354             ni_errlist[ni_errno],
1355             ni_errtext
1356         );
1357         MedArchFini();
1358         return FALSE;
1359     }
1360 
1361     asnin = svcp->raip;
1362     asnout = svcp->waip;
1363 
1364     /* generate INIT request and send to MedArch server */
1365     mlarp = ValNodeNew(NULL);
1366     mlarp->choice = MlaRequest_init;
1367     MlaRequestAsnWrite (mlarp, asnout, NULL);
1368     AsnIoReset(asnout);
1369     MlaRequestFree (mlarp);
1370 
1371     if ( (mlabp = NetMedArchReadAsn()) != NULL ) {
1372         mlabp->data.ptrvalue = NULL;
1373         MlaBackFree (mlabp);
1374         return TRUE;
1375     }
1376 
1377     return FALSE;
1378 }                                    /* MedArchInit() */
1379 
1380 /******************************************************************************/
1381 /*.doc MedArchFini(external) */
1382 /*+
1383    The only thing done here is to suppress errors, plus call
1384    function s_MedArchFini() to real discoonnect.
1385 -*/
1386 /******************************************************************************/
1387 Boolean
MedArchFini(void)1388 /*FCN*/MedArchFini (void)
1389 {
1390     short erract;
1391     ErrDesc err;
1392     Boolean retval;
1393 
1394     ErrGetOpts(&erract, NULL);
1395     ErrSetOpts(ERR_IGNORE, 0);
1396     ErrFetch(&err);
1397 
1398     retval = s_MedArchFini();
1399 
1400     ErrSetOpts(erract, 0);
1401     ErrFetch(&err);
1402 
1403     return retval;
1404 }                                    /* MedArchFini() */
1405 
1406 /******************************************************************************/
1407 /*.doc MedArchMedlineEntryListGet(external) */
1408 /*+
1409    This function generate list of Medline entries using Medline Id,
1410    return number of result entries.
1411 -*/
1412 /******************************************************************************/
1413 Int4
MedArchMedlineEntryListGet(MedlineEntryPtr PNTR result,Int4 numuid,Int4Ptr uids,Boolean mark_missing)1414 /*FCN*/MedArchMedlineEntryListGet (
1415   MedlineEntryPtr PNTR result,
1416   Int4 numuid,
1417   Int4Ptr uids,
1418   Boolean mark_missing
1419 ){
1420     Int4 i;
1421     Int4 j;
1422     MedlineEntryPtr mep;
1423     short erract;
1424     ErrDesc err;
1425     Int4 retval = 0;
1426 
1427     for (j = 0; j < numuid; j++) {
1428         for ( i = 0; i < MEDARCH_SERV_RETRIES; i++ ) {
1429             if ( i > 0 ) {
1430                 if ( !ReestablishNetMedArch() ) {
1431                     break;
1432                 }
1433             }
1434 
1435             ErrGetOpts(&erract, NULL);
1436             ErrSetOpts(ERR_IGNORE, 0);
1437             ErrFetch(&err);
1438             mep = s_MedArchMedlineEntryGet (uids[j]);
1439             ErrSetOpts(erract, 0);
1440             if (! ErrFetch(&err))
1441                 break; /* success */
1442         }
1443         result[j] = mep;
1444         if ( mep != NULL ) {
1445             retval++;
1446         } else {
1447             if ( mark_missing ) {
1448                 uids[j] *= -1;
1449             }
1450         }
1451     }
1452 
1453     return retval;
1454 }                               /* MedArchMedlineEntryListGet() */
1455 
1456 /******************************************************************************/
1457 /*.doc MedArchMedlineEntryGet(external) */
1458 /*+
1459    This function generate single Medline entry using Medline Id,
1460    return pointer to Medline entry and NULL if some errors found.
1461 -*/
1462 /******************************************************************************/
1463 MedlineEntryPtr
MedArchMedlineEntryGet(Int4 uid)1464 /*FCN*/MedArchMedlineEntryGet (
1465   Int4 uid
1466 ){
1467     MedlineEntryPtr mep = NULL;
1468 
1469     MedArchMedlineEntryListGet (&mep, 1, &uid, FALSE);
1470 
1471     return mep;
1472 }                               /* MedArchMedlineEntryGet() */
1473 
1474 /******************************************************************************/
1475 /*.doc MedArchPubmedEntryListGetPmId(external) */
1476 /*+
1477    This function generate list of PubMed entries using PubMed Id,
1478    return number of result entries.
1479 -*/
1480 /******************************************************************************/
1481 Int4
MedArchPubmedEntryListGetPmId(PubmedEntryPtr PNTR result,Int4 numpmid,Int4Ptr pmids,Boolean mark_missing)1482 /*FCN*/MedArchPubmedEntryListGetPmId (
1483   PubmedEntryPtr PNTR result,
1484   Int4 numpmid,
1485   Int4Ptr pmids,
1486   Boolean mark_missing
1487 ){
1488     Int4 i;
1489     Int4 j;
1490     PubmedEntryPtr mep;
1491     short erract;
1492     ErrDesc err;
1493     Int4 retval = 0;
1494 
1495     for (j = 0; j < numpmid; j++) {
1496         for ( i = 0; i < MEDARCH_SERV_RETRIES; i++ ) {
1497             if ( i > 0 ) {
1498                 if ( !ReestablishNetMedArch() ) {
1499                     break;
1500                 }
1501             }
1502 
1503             ErrGetOpts(&erract, NULL);
1504             ErrSetOpts(ERR_IGNORE, 0);
1505             ErrFetch(&err);
1506             mep = s_MedArchPubmedEntryGetPmId (pmids[j]);
1507             ErrSetOpts(erract, 0);
1508             if (! ErrFetch(&err))
1509                 break; /* success */
1510         }
1511         result[j] = mep;
1512         if ( mep != NULL ) {
1513             retval++;
1514         } else {
1515             if ( mark_missing ) {
1516                 pmids[j] *= -1;
1517             }
1518         }
1519     }
1520 
1521     return retval;
1522 }                               /* MedArchPubmedEntryListGetPmId() */
1523 
1524 /******************************************************************************/
1525 /*.doc MedArchPubmedEntryGetPmId(external) */
1526 /*+
1527    This function generate single PubMed entry using Medline Id,
1528    return pointer to PubMed entry and NULL if some errors found.
1529 -*/
1530 /******************************************************************************/
1531 PubmedEntryPtr
MedArchPubmedEntryGetPmId(Int4 pmid)1532 /*FCN*/MedArchPubmedEntryGetPmId (
1533   Int4 pmid
1534 ){
1535     PubmedEntryPtr mep = NULL;
1536 
1537     MedArchPubmedEntryListGetPmId (&mep, 1, &pmid, FALSE);
1538 
1539     return mep;
1540 }                               /* MedArchPubmedEntryGetPmId() */
1541 
1542 /******************************************************************************/
1543 /*.doc MedArchPubmedEntryListGetUid(external) */
1544 /*+
1545    This function generate list of PubMed entries using PubMed Id,
1546    return number of result entries.
1547 -*/
1548 /******************************************************************************/
1549 Int4
MedArchPubmedEntryListGetUid(PubmedEntryPtr PNTR result,Int4 numpmid,Int4Ptr pmids,Boolean mark_missing)1550 /*FCN*/MedArchPubmedEntryListGetUid (
1551   PubmedEntryPtr PNTR result,
1552   Int4 numpmid,
1553   Int4Ptr pmids,
1554   Boolean mark_missing
1555 ){
1556     Int4 i;
1557     Int4 j;
1558     PubmedEntryPtr mep;
1559     short erract;
1560     ErrDesc err;
1561     Int4 retval = 0;
1562 
1563     for (j = 0; j < numpmid; j++) {
1564         for ( i = 0; i < MEDARCH_SERV_RETRIES; i++ ) {
1565             if ( i > 0 ) {
1566                 if ( !ReestablishNetMedArch() ) {
1567                     break;
1568                 }
1569             }
1570 
1571             ErrGetOpts(&erract, NULL);
1572             ErrSetOpts(ERR_IGNORE, 0);
1573             ErrFetch(&err);
1574             mep = s_MedArchPubmedEntryGetUid (pmids[j]);
1575             ErrSetOpts(erract, 0);
1576             if (! ErrFetch(&err))
1577                 break; /* success */
1578         }
1579         result[j] = mep;
1580         if ( mep != NULL ) {
1581             retval++;
1582         } else {
1583             if ( mark_missing ) {
1584                 pmids[j] *= -1;
1585             }
1586         }
1587     }
1588 
1589     return retval;
1590 }                               /* MedArchPubmedEntryListGetUid() */
1591 
1592 /******************************************************************************/
1593 /*.doc MedArchPubmedEntryGetUid(external) */
1594 /*+
1595    This function generate single PubMed entry using Medline Id,
1596    return pointer to PubMed entry and NULL if some errors found.
1597 -*/
1598 /******************************************************************************/
1599 PubmedEntryPtr
MedArchPubmedEntryGetUid(Int4 pmid)1600 /*FCN*/MedArchPubmedEntryGetUid (
1601   Int4 pmid
1602 ){
1603     PubmedEntryPtr mep = NULL;
1604 
1605     MedArchPubmedEntryListGetUid (&mep, 1, &pmid, FALSE);
1606 
1607     return mep;
1608 }                               /* MedArchPubmedEntryGetUid() */
1609 
1610 /******************************************************************************/
1611 /*.doc MedArchPubSetCreate(external) */
1612 /*+
1613    This function generate PubSet using list of Medline Id
1614 -*/
1615 /******************************************************************************/
1616 Int4
MedArchPubSetCreate(AsnIoPtr aip,Int4 numuid,Int4Ptr uids,Boolean mark_missing)1617 /*FCN*/MedArchPubSetCreate (
1618   AsnIoPtr aip,
1619   Int4 numuid,
1620   Int4Ptr uids,
1621   Boolean mark_missing
1622 ){
1623     static Boolean alreadyInited = FALSE;
1624     AsnTypePtr startAtp;
1625     AsnTypePtr pubSetAtp;
1626     AsnTypePtr pubSetEAtp;
1627     AsnModulePtr amp;
1628     Int4 j;
1629     Int4 i;
1630     MedlineEntryPtr mep;
1631     MedlineEntryPtr lastmep = NULL;
1632     Int4 retval = 0;
1633     short erract;
1634     ErrDesc err;
1635 
1636     if ( !alreadyInited ) {
1637         if ( !PubAsnLoad() ) {
1638             return 0;
1639         }
1640         amp = AsnAllModPtr();
1641         if ( amp == NULL ) {
1642             return 0;
1643         }
1644         alreadyInited = TRUE;
1645         startAtp = AsnTypeFind(amp, "Pub-set");
1646         pubSetAtp = AsnTypeFind(amp, "Pub-set.medline");
1647         pubSetEAtp = AsnTypeFind(amp, "Pub-set.medline.E");
1648         if (    startAtp == NULL
1649              || pubSetAtp == NULL
1650              || pubSetEAtp == NULL ) {
1651             return 0;
1652         }
1653     }
1654 
1655     AsnWrite (aip, startAtp, NULL);
1656     AsnStartStruct (aip, pubSetAtp);
1657 
1658     for (j = 0; j < numuid; j++) {
1659         for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1660             if ( i > 0 ) {
1661                 if ( !ReestablishNetMedArch() ) {
1662                     break;
1663                 }
1664             }
1665 
1666             ErrGetOpts(&erract, NULL);
1667             ErrSetOpts(ERR_IGNORE, 0);
1668             ErrFetch(&err);
1669             mep = s_MedArchPubSetEntryGet (
1670                       uids[j],
1671                       lastmep,
1672                       aip,
1673                       pubSetEAtp
1674                   );
1675             MedlineEntryFree (lastmep); /* do not need any more */
1676             lastmep = NULL;
1677             ErrSetOpts(erract, 0);
1678             if ( !ErrFetch(&err) ) {
1679                 break;                  /* success */
1680             }
1681         }
1682         lastmep = mep;
1683         if ( mep != NULL ) {
1684             retval++;
1685         } else {
1686             if ( mark_missing ) {
1687                 uids[j] *= -1;
1688             }
1689         }
1690     }
1691 
1692     if ( lastmep != NULL ) {            /* write final entry */
1693         MedlineEntryAsnWrite (lastmep, aip, pubSetEAtp);
1694         MedlineEntryFree (lastmep);
1695     }
1696 
1697     AsnEndStruct (aip, pubSetAtp);
1698 
1699     return retval;
1700 }                              /* MedArchPubSetCreate() */
1701 
1702 /******************************************************************************/
1703 /*.doc MedArchPubSetCreatePmId(external) */
1704 /*+
1705    This function generate PubSet using list of PubMed Id
1706 -*/
1707 /******************************************************************************/
1708 Int4
MedArchPubSetCreatePmId(AsnIoPtr aip,Int4 numpmid,Int4Ptr pmids,Boolean mark_missing)1709 /*FCN*/MedArchPubSetCreatePmId (
1710   AsnIoPtr aip,
1711   Int4 numpmid,
1712   Int4Ptr pmids,
1713   Boolean mark_missing
1714 ){
1715     return 0;
1716 }                              /* MedArchPubSetCreatePmId() */
1717 
1718 /******************************************************************************/
1719 /*.doc MedArchPubSetCreateUid(external) */
1720 /*+
1721    This function generate PubSet using list of PubMed Id
1722 -*/
1723 /******************************************************************************/
1724 Int4
MedArchPubSetCreateUid(AsnIoPtr aip,Int4 numpmid,Int4Ptr pmids,Boolean mark_missing)1725 /*FCN*/MedArchPubSetCreateUid (
1726   AsnIoPtr aip,
1727   Int4 numpmid,
1728   Int4Ptr pmids,
1729   Boolean mark_missing
1730 ){
1731     return 0;
1732 }                              /* MedArchPubSetCreateUid() */
1733 
1734 /******************************************************************************/
1735 /*.doc MedArchGetPub(external) */
1736 /*+
1737    This function generate single citation using Medline Id
1738 -*/
1739 /******************************************************************************/
1740 ValNodePtr
MedArchGetPub(Int4 uid)1741 /*FCN*/MedArchGetPub (
1742   Int4 uid
1743 ){
1744     ValNodePtr retval = NULL;
1745     Int4 i;
1746     short erract;
1747     ErrDesc err;
1748 
1749     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1750         if ( i > 0 ) {
1751             if ( !ReestablishNetMedArch() ) {
1752                 break;
1753             }
1754         }
1755 
1756         ErrGetOpts(&erract, NULL);
1757         ErrSetOpts(ERR_IGNORE, 0);
1758         ErrFetch(&err);
1759         retval = s_MedArchGetPub(uid);
1760         ErrSetOpts(erract, 0);
1761         if ( !ErrFetch(&err) ) {
1762             break;                   /* success */
1763         }
1764     }
1765 
1766     return retval;
1767 }                                    /* MedArchGetPub() */
1768 
1769 /******************************************************************************/
1770 /*.doc MedArchGetPubPmId(external) */
1771 /*+
1772    This function generate single citation using PubMed Id
1773 -*/
1774 /******************************************************************************/
1775 ValNodePtr
MedArchGetPubPmId(Int4 pmid)1776 /*FCN*/MedArchGetPubPmId (
1777   Int4 pmid
1778 ){
1779     ValNodePtr retval = NULL;
1780     Int4 i;
1781     short erract;
1782     ErrDesc err;
1783 
1784     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1785         if ( i > 0 ) {
1786             if ( !ReestablishNetMedArch() ) {
1787                 break;
1788             }
1789         }
1790 
1791         ErrGetOpts(&erract, NULL);
1792         ErrSetOpts(ERR_IGNORE, 0);
1793         ErrFetch(&err);
1794         retval = s_MedArchGetPubPmId(pmid);
1795         ErrSetOpts(erract, 0);
1796         if ( !ErrFetch(&err) ) {
1797             break;                   /* success */
1798         }
1799     }
1800 
1801     return retval;
1802 }                                    /* MedArchGetPubPmId() */
1803 
1804 /******************************************************************************/
1805 /*.doc MedArchGetTitles(external) */
1806 /*+
1807 -*/
1808 /******************************************************************************/
1809 Int4
MedArchGetTitles(CharPtr PNTR titles_found,Int1Ptr title_types_found,CharPtr title_to_lookup,Int1 request_type,Int1 response_type,Int4 max_titles_to_find)1810 /*FCN*/MedArchGetTitles (
1811   CharPtr PNTR titles_found,
1812   Int1Ptr title_types_found,
1813   CharPtr title_to_lookup,
1814   Int1 request_type,
1815   Int1 response_type,
1816   Int4 max_titles_to_find
1817 ){
1818     Int4 retval = 0;
1819     Int4 i;
1820     short erract;
1821     ErrDesc err;
1822 
1823     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1824         if (i > 0) {
1825             if ( !ReestablishNetMedArch() )
1826                 break;
1827         }
1828 
1829         ErrGetOpts(&erract, NULL);
1830         ErrSetOpts(ERR_IGNORE, 0);
1831         ErrFetch(&err);
1832         retval = s_MedArchGetTitles (
1833                      titles_found,
1834                      title_types_found,
1835                      title_to_lookup,
1836                      request_type,
1837                      response_type,
1838                      max_titles_to_find
1839                  );
1840         ErrSetOpts(erract, 0);
1841         if ( !ErrFetch(&err) ) {
1842             break; /* success */
1843         }
1844     }
1845 
1846     return retval;
1847 }                                     /* MedArchGetTitles() */
1848 
1849 /******************************************************************************/
1850 /*.doc MedArchCitMatch(external) */
1851 /*+
1852 -*/
1853 /******************************************************************************/
1854 Int4
MedArchCitMatch(ValNodePtr pub)1855 /*FCN*/MedArchCitMatch (
1856   ValNodePtr pub
1857 ){
1858     Int4 retval = 0;
1859     Int4 i;
1860     short erract;
1861     ErrDesc err;
1862 
1863     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1864         if ( i > 0 ) {
1865             if ( !ReestablishNetMedArch() ) {
1866                 break;
1867             }
1868         }
1869         ErrGetOpts(&erract, NULL);
1870         ErrSetOpts(ERR_IGNORE, 0);
1871         ErrFetch(&err);
1872         retval = s_MedArchCitMatch(pub);
1873         ErrSetOpts(erract, 0);
1874         if ( !ErrFetch(&err) ) {
1875             break; /* success */
1876         }
1877     }
1878 
1879     return retval;
1880 }                                      /* MedArchCitMatch() */
1881 
1882 /******************************************************************************/
1883 /*.doc MedArchCitMatchPmId(external) */
1884 /*+
1885 -*/
1886 /******************************************************************************/
1887 Int4
MedArchCitMatchPmId(ValNodePtr pub)1888 /*FCN*/MedArchCitMatchPmId (
1889   ValNodePtr pub
1890 ){
1891     Int4 retval = 0;
1892     Int4 i;
1893     short erract;
1894     ErrDesc err;
1895 
1896     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1897         if ( i > 0 ) {
1898             if ( !ReestablishNetMedArch() ) {
1899                 break;
1900             }
1901         }
1902         ErrGetOpts(&erract, NULL);
1903         ErrSetOpts(ERR_IGNORE, 0);
1904         ErrFetch(&err);
1905         retval = s_MedArchCitMatchPmId(pub);
1906         ErrSetOpts(erract, 0);
1907         if ( !ErrFetch(&err) ) {
1908             break; /* success */
1909         }
1910     }
1911 
1912     return retval;
1913 }                                      /* MedArchCitMatchPmId() */
1914 
1915 
1916 /******************************************************************************/
1917 /*.doc MedArchCitMatchList(internal) */
1918 /*+
1919    This function send request '' to MedArch server and recive result
1920    list. It set up error code in case some error found.
1921 -*/
1922 /******************************************************************************/
1923 ValNodePtr
MedArchCitMatchList(ValNodePtr pub,Int4Ptr pErrCode)1924 /*FCN*/MedArchCitMatchList (
1925   ValNodePtr pub,
1926   Int4Ptr    pErrCode
1927 ){
1928     register ValNodePtr pPmIdList = NULL; /* result PubMed ID list */
1929     Int4 i;
1930     short erract;
1931     ErrDesc err;
1932 
1933     for ( i = 0; i < MEDARCH_SERV_RETRIES; i++ ) {
1934         if ( i > 0 ) {
1935             if ( !ReestablishNetMedArch() ) {
1936                 break;
1937             }
1938         }
1939         ErrGetOpts(&erract, NULL);
1940         ErrSetOpts(ERR_IGNORE, 0);
1941         ErrFetch(&err);
1942         pPmIdList = s_MedArchCitMatchList(pub,pErrCode);
1943         ErrSetOpts(erract, 0);
1944         if ( !ErrFetch(&err) ) {
1945             break; /* success */
1946         }
1947     }
1948 
1949     return pPmIdList;
1950 }                                  /* MedArchCitMatchList() */
1951 
1952 /******************************************************************************/
1953 /*.doc MedArchGetMriUids(external) */
1954 /*+
1955    This function call MedArch server and send MRI code. Server will return
1956    list of articles(MuId's) that are present in this issue. This function
1957    will return NULL if MRI not found or some errors found. Caller must
1958    free list of medline id's.
1959 -*/
1960 /******************************************************************************/
1961 ValNodePtr
MedArchGetMriUids(Int4 mri)1962 /*FCN*/MedArchGetMriUids (
1963   Int4 mri                     /* MRI code (I) */
1964 ){
1965     register ValNodePtr pMuIdList = NULL; /* result Medline ID list */
1966     Int4 i;                               /* temp count */
1967     short erract;                         /* values for save error code */
1968     ErrDesc err;
1969 
1970     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
1971         if ( i > 0 ) {
1972             if ( !ReestablishNetMedArch() ) {
1973                 break;
1974             }
1975         }
1976         ErrGetOpts(&erract, NULL);
1977         ErrSetOpts(ERR_IGNORE, 0);
1978         ErrFetch(&err);
1979         pMuIdList = s_MedArchGetMriUids (mri);
1980         ErrSetOpts(erract, 0);
1981         if ( !ErrFetch(&err) ) {
1982             break; /* success */
1983         }
1984     }
1985 
1986     return pMuIdList;
1987 }                              /* MedArchGetMriUids() */
1988 
1989 /******************************************************************************/
1990 /*.doc MedArchGetMriPmids(external) */
1991 /*+
1992    This function call MedArch server and send MRI code. Server will return
1993    list of articles(MuId's) that are present in this issue. This function
1994    will return NULL if MRI not found or some errors found. Caller must
1995    free list of PubMed id's.
1996 -*/
1997 /******************************************************************************/
1998 ValNodePtr
MedArchGetMriPmids(Int4 mri)1999 /*FCN*/MedArchGetMriPmids (
2000   Int4 mri                     /* MRI code (I) */
2001 ){
2002     register ValNodePtr pMuIdList = NULL; /* result Medline ID list */
2003     Int4 i;                               /* temp count */
2004     short erract;                         /* values for save error code */
2005     ErrDesc err;
2006 
2007     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2008         if ( i > 0 ) {
2009             if ( !ReestablishNetMedArch() ) {
2010                 break;
2011             }
2012         }
2013         ErrGetOpts(&erract, NULL);
2014         ErrSetOpts(ERR_IGNORE, 0);
2015         ErrFetch(&err);
2016         pMuIdList = s_MedArchGetMriPmids (mri);
2017         ErrSetOpts(erract, 0);
2018         if ( !ErrFetch(&err) ) {
2019             break; /* success */
2020         }
2021     }
2022 
2023     return pMuIdList;
2024 }                              /* MedArchGetMriPmids() */
2025 
2026 /******************************************************************************/
2027 /*.doc MedArchGetAccUids(external) */
2028 /*+
2029    This function call MedArch server and send Xref and Accession.
2030    Server will return list of articles(MuId's) that are present in this issue.
2031    plAccNum is an optional argument in accordance with
2032    Medline-si definition. Set it to NULL if accession code is unknown.
2033    It returns NULL if there are no entries found or if some error occurred.-*/
2034 /******************************************************************************/
2035 ValNodePtr
MedArchGetAccUids(Int4 iCode,CharPtr lpAccNum)2036 /*FCN*/MedArchGetAccUids (
2037   Int4 iCode,                  /* Medline-si.type, */
2038                                /* type of xref (I) */
2039   CharPtr lpAccNum             /* Medline.si.cit, */
2040                                /* the citation/accession number (I) */
2041 ){
2042     register ValNodePtr pMuIdList = NULL; /* result Medline ID list */
2043     Int4 i;                               /* temp count */
2044     short erract;                         /* values for save error code */
2045     ErrDesc err;
2046 
2047     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2048         if ( i > 0 ) {
2049             if ( !ReestablishNetMedArch() ) {
2050                 break;
2051             }
2052         }
2053         ErrGetOpts(&erract, NULL);
2054         ErrSetOpts(ERR_IGNORE, 0);
2055         ErrFetch(&err);
2056         pMuIdList = s_MedArchGetAccUids (iCode, lpAccNum);
2057         ErrSetOpts(erract, 0);
2058         if ( !ErrFetch(&err) ) {
2059             break; /* success */
2060         }
2061     }
2062 
2063     return pMuIdList;
2064 }                              /* MedArchGetAccUids() */
2065 
2066 /******************************************************************************/
2067 /*.doc MedArchGetAccPmids(external) */
2068 /*+
2069    This function call MedArch server and send Xref and Accession.
2070    Server will return list of PmIds that are present in this issue.
2071    plAccNum is an optional argument in accordance with
2072    Medline-si definition. Set it to NULL if accession code is unknown.
2073    It returns NULL if there are no entries found or if some error occurred.-*/
2074 /******************************************************************************/
2075 ValNodePtr
MedArchGetAccPmids(Int4 iCode,CharPtr lpAccNum)2076 /*FCN*/MedArchGetAccPmids (
2077   Int4 iCode,                  /* Medline-si.type, */
2078                                /* type of xref (I) */
2079   CharPtr lpAccNum             /* Medline.si.cit, */
2080                                /* the citation/accession number (I) */
2081 ){
2082     register ValNodePtr pmIdList = NULL; /* result Medline ID list */
2083     Int4 i;                               /* temp count */
2084     short erract;                         /* values for save error code */
2085     ErrDesc err;
2086 
2087     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2088         if ( i > 0 ) {
2089             if ( !ReestablishNetMedArch() ) {
2090                 break;
2091             }
2092         }
2093         ErrGetOpts(&erract, NULL);
2094         ErrSetOpts(ERR_IGNORE, 0);
2095         ErrFetch(&err);
2096         pmIdList = s_MedArchGetAccPmids (iCode, lpAccNum);
2097         ErrSetOpts(erract, 0);
2098         if ( !ErrFetch(&err) ) {
2099             break; /* success */
2100         }
2101     }
2102 
2103     return pmIdList;
2104 }                              /* MedArchGetAccPmids() */
2105 
2106 /******************************************************************************/
2107 /*.doc MedArchPm2Mu(external) */
2108 /*+
2109   Convert PubMed id to Medline id
2110 -*/
2111 /******************************************************************************/
2112 Int4
MedArchPm2Mu(Int4 pmid)2113 /*FCN*/MedArchPm2Mu (
2114   Int4 pmid                    /* PubMed id (I) */
2115 ){
2116     Int4 muid = 0;
2117     Int4 i;                               /* temp count */
2118     short erract;                         /* values for save error code */
2119     ErrDesc err;
2120 
2121     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2122         if ( i > 0 ) {
2123             if ( !ReestablishNetMedArch() ) {
2124                 break;
2125             }
2126         }
2127         ErrGetOpts(&erract, NULL);
2128         ErrSetOpts(ERR_IGNORE, 0);
2129         ErrFetch(&err);
2130         muid = s_MedArchPm2Mu (pmid);
2131         ErrSetOpts(erract, 0);
2132         if ( !ErrFetch(&err) ) {
2133             break; /* success */
2134         }
2135     }
2136 
2137     return muid;
2138 }                              /* MedArchPm2Mu() */
2139 
2140 /******************************************************************************/
2141 /*.doc MedArchMu2Pm(external) */
2142 /*+
2143   Convert Medline id to PubMed id
2144 -*/
2145 /******************************************************************************/
2146 Int4
MedArchMu2Pm(Int4 uid)2147 /*FCN*/MedArchMu2Pm (
2148   Int4 uid                     /* Medline id (I) */
2149 ){
2150     Int4 pmid = 0;
2151     Int4 i;                               /* temp count */
2152     short erract;                         /* values for save error code */
2153     ErrDesc err;
2154 
2155     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2156         if ( i > 0 ) {
2157             if ( !ReestablishNetMedArch() ) {
2158                 break;
2159             }
2160         }
2161         ErrGetOpts(&erract, NULL);
2162         ErrSetOpts(ERR_IGNORE, 0);
2163         ErrFetch(&err);
2164         pmid = s_MedArchMu2Pm (uid);
2165         ErrSetOpts(erract, 0);
2166         if ( !ErrFetch(&err) ) {
2167             break; /* success */
2168         }
2169     }
2170 
2171     return pmid;
2172 }                              /* MedArchMu2Pm() */
2173 
2174 /******************************************************************************/
2175 /*.doc MedArchMedlarsEntryGetUid(external) */
2176 /*+
2177    Retrive single MEDLARS entry from database
2178 -*/
2179 /******************************************************************************/
2180 MedlarsEntryPtr
MedArchMedlarsEntryGetUid(Int4 uid)2181 /*FCN*/MedArchMedlarsEntryGetUid (
2182   Int4 uid
2183 ){
2184     MedlarsEntryPtr retval = NULL;
2185     Int4 i;
2186     short erract;
2187     ErrDesc err;
2188 
2189     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2190         if ( i > 0 ) {
2191             if ( !ReestablishNetMedArch() ) {
2192                 break;
2193             }
2194         }
2195 
2196         ErrGetOpts(&erract, NULL);
2197         ErrSetOpts(ERR_IGNORE, 0);
2198         ErrFetch(&err);
2199         retval = s_MedArchMedlarsEntryGetUid(uid);
2200         ErrSetOpts(erract, 0);
2201         if ( !ErrFetch(&err) ) {
2202             break;                   /* success */
2203         }
2204     }
2205 
2206     return retval;
2207 }                                    /* MedArchMedlarsEntryGetUid() */
2208 
2209 /******************************************************************************/
2210 /*.doc MedArchMedlarsEntryGetPmId(external) */
2211 /*+
2212    Retrive single MEDLARS entry from database
2213 -*/
2214 /******************************************************************************/
2215 MedlarsEntryPtr
MedArchMedlarsEntryGetPmId(Int4 uid)2216 /*FCN*/MedArchMedlarsEntryGetPmId (
2217   Int4 uid
2218 ){
2219     MedlarsEntryPtr retval = NULL;
2220     Int4 i;
2221     short erract;
2222     ErrDesc err;
2223 
2224     for (i = 0; i < MEDARCH_SERV_RETRIES; i++) {
2225         if ( i > 0 ) {
2226             if ( !ReestablishNetMedArch() ) {
2227                 break;
2228             }
2229         }
2230 
2231         ErrGetOpts(&erract, NULL);
2232         ErrSetOpts(ERR_IGNORE, 0);
2233         ErrFetch(&err);
2234         retval = s_MedArchMedlarsEntryGetPmId(uid);
2235         ErrSetOpts(erract, 0);
2236         if ( !ErrFetch(&err) ) {
2237             break;                   /* success */
2238         }
2239     }
2240 
2241     return retval;
2242 }                                    /* MedArchMedlarsEntryGetPmId() */
2243 
2244 #endif /* __MEDARCHC__ */
2245 /*EOF*/
2246