1 /*****************************************************************************
2 *
3 *   getpub.c
4 *      does an indexed lookup for medline entries by medline uid
5 *
6 *****************************************************************************/
7 #include "allpub.h"
8 
9 #define NUMARGS 5
10 Args myargs[NUMARGS] = {
11 	{ "Input binary data", "medline.val", "Pub-set", NULL, FALSE, 'i', ARG_DATA_IN, 0.0,0,NULL},
12 	{ "Medline UID to find", "88055872", NULL,NULL,FALSE,'u', ARG_INT, 0.0, 0, NULL },
13 	{ "Input index table", "medline.idx", NULL,NULL,FALSE,'t', ARG_FILE_IN, 0.0,0,NULL },
14 	{ "Output data", "stdout", "Medline-entry",NULL,FALSE,'o',ARG_DATA_OUT, 0.0,0,NULL},
15 	{ "Output data is binary", "F", NULL, NULL, FALSE , 'b', ARG_BOOLEAN, 0.0,0,NULL}};
16 
17 
Main(void)18 Int2 Main(void)
19 {
20 	AsnIoPtr aip, aipout;
21 	AsnTypePtr atp;
22 	DataVal value;
23 	Int4 seekptr, uid, uid_to_find;
24 	static CharPtr outtypes[2] = { "w", "wb" };
25 	Int2 outtype;
26 	FILE *fp;
27 	Boolean done, first;
28 	int retval;
29 
30     if (! AsnLoad())
31         Message(MSG_FATAL, "Unable to load allpub parse tree.");
32 
33 	if (! GetArgs("GetPub 1.0", NUMARGS, myargs))
34 		return 1;
35 
36 	if (myargs[4].intvalue)        /* binary output is TRUE */
37 		outtype = 1;
38 	else
39 		outtype = 0;
40 
41 	if ((aip = AsnIoOpen(myargs[0].strvalue, "rb")) == NULL)
42 	{
43 		Message(MSG_ERROR, "Couldn't open %s", myargs[0].strvalue);
44 		return 1;
45 	}
46 
47 	if ((aipout = AsnIoOpen(myargs[3].strvalue, outtypes[outtype])) == NULL)
48 	{
49 		Message(MSG_ERROR, "Couldn't open %s", myargs[3].strvalue);
50 		return 1;
51 	}
52 
53 	if ((fp = FileOpen(myargs[2].strvalue, "r")) == NULL)
54 	{
55 		Message(MSG_ERROR, "Couldn't open %s", myargs[2].strvalue);
56 		return 1;
57 	}
58 
59 	uid_to_find = myargs[1].intvalue;
60 	done = FALSE;
61 	first = TRUE;
62 	while (! done)
63 	{
64 		retval = fscanf(fp, "%d %d", &uid, &seekptr);
65 		if (retval == EOF)
66 		{
67 			Message(MSG_ERROR, "UID %ld not found", uid_to_find);
68 			return 1;
69 		}
70 		if (uid == uid_to_find)
71 			done = TRUE;
72 	}
73 	FileClose(fp);
74 
75 	atp = MEDLINE_ENTRY;
76 	AsnIoSeek(aip, seekptr);
77 	done = FALSE;
78 	while (! done)
79 	{
80 		atp = AsnReadId(aip, amp, atp);
81 		AsnReadVal(aip, atp, &value);
82 		AsnWrite(aipout, atp, &value);
83 		AsnKillValue(atp, &value);
84 
85 		if (! first)
86 		{
87 			if (atp == MEDLINE_ENTRY)
88 				done = TRUE;
89 		}
90 		else
91 			first = FALSE;
92 	}
93 
94 	AsnIoClose(aip);
95 	AsnIoClose(aipout);
96 
97 	return 0;
98 }
99 
100