File: objbibli.c

package info (click to toggle)
ncbi-tools6 6.1.20041020-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 65,588 kB
  • ctags: 74,723
  • sloc: ansic: 1,020,512; xml: 3,390; sh: 2,668; csh: 587; makefile: 546; perl: 292; lisp: 81; cpp: 59
file content (3995 lines) | stat: -rw-r--r-- 101,174 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
/*  objbibli.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE                          
*               National Center for Biotechnology Information
*                                                                          
*  This software/database is a "United States Government Work" under the   
*  terms of the United States Copyright Act.  It was written as part of    
*  the author's official duties as a United States Government employee and 
*  thus cannot be copyrighted.  This software/database is freely available 
*  to the public for use. The National Library of Medicine and the U.S.    
*  Government have not placed any restriction on its use or reproduction.  
*                                                                          
*  Although all reasonable efforts have been taken to ensure the accuracy  
*  and reliability of the software and data, the NLM and the U.S.          
*  Government do not and cannot warrant the performance or results that    
*  may be obtained by using this software or data. The NLM and the U.S.    
*  Government disclaim all warranties, express or implied, including       
*  warranties of performance, merchantability or fitness for any particular
*  purpose.                                                                
*                                                                          
*  Please cite the author in any work or product based on this material.   
*
* ===========================================================================
*
* File Name:  objbibli.c
*
* Author:  James Ostell
*   
* Version Creation Date: 1/1/91
*
* $Revision: 6.5 $
*
* File Description:  Object manager for module NCBI-Biblio
*
* Modifications:  
* --------------------------------------------------------------------------
* Date	   Name        Description of modification
* -------  ----------  -----------------------------------------------------
* 05-13-93 Schuler     All public functions are now declared LIBCALL.
* 07-19-93 Ostell      Support for ASN30 added
*
* $Log: objbibli.c,v $
* Revision 6.5  2001/10/09 15:57:38  kans
* AuthListMatch does StringICmp if both are structured last names, StringNICmp otherwise
*
* Revision 6.4  2000/01/18 19:40:51  ostell
* added support for PubStatusDate, ArticleIds
*
* Revision 6.3  2000/01/05 18:44:12  kans
* increased buffer size in AuthListMatch, which was overflowing
*
* Revision 6.2  1998/08/24 18:27:59  kans
* removed solaris -v -fd warnings
*
* Revision 6.1  1997/11/13 21:31:59  kans
* fixed endless loop in CitArtFree (Kuzio)
*
* Revision 6.0  1997/08/25 18:49:19  madden
* Revision changed to 6.0
*
* Revision 4.7  1997/08/20 19:16:53  madden
* Version 6 of ASN.1
*
* Revision 4.6  1997/06/19 18:40:46  vakatov
* [WIN32,MSVC++]  Adopted for the "NCBIOBJ.LIB" DLL'ization
*
* Revision 4.5  1997/03/18 15:30:07  ostell
* make IdPatMatch case insensitive
*
 * Revision 4.4  1996/12/12  21:49:08  tatiana
 * CiSubMatch changed to compare the dates from both ImpPrint.date and date
 *
 * Revision 4.3  1996/12/04  18:18:41  kans
 * CitSubMatch calls ImprintMatch only if both imp fields not NULL, and
 * calls DateMatch if date fields not NULL
 *
 * Revision 4.2  1996/07/30  15:50:50  epstein
 * correct logic errors for different spec_versions
 *
 * Revision 4.1  1996/03/29  21:09:59  ostell
 * added support for PubMedId
 *
 * Revision 4.0  1995/07/26  13:48:06  ostell
 * force revision to 4.0
 *
 * Revision 3.6  1995/05/24  21:20:50  ostell
 * typo fix
 *
 * Revision 3.5  1995/05/24  19:10:27  ostell
 * added filtering (grep for "ASN3 strip") to conform to old version 3.0
 * asn.1 spec.
 *
 * Revision 3.4  1995/05/15  21:22:00  ostell
 * added Log line
 *
*
*
* ==========================================================================
*/
#include <asnbibli.h>
#include <objbibli.h>


static Boolean loaded = FALSE;

/*****************************************************************************
*
*   Bibliographic Object routines
*
*****************************************************************************/

/*****************************************************************************
*
*   BiblioAsnLoad()
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL BiblioAsnLoad (void)
{
    if (loaded)
        return TRUE;
    loaded = TRUE;

    if (! GeneralAsnLoad())
    {
        loaded = FALSE;
        return FALSE;
    }
    if (! AsnLoad())
    {
        loaded = FALSE;
        return FALSE;
    }
    return TRUE;
}

/*****************************************************************************
*
*   AffilNew()
*
*****************************************************************************/
NLM_EXTERN AffilPtr LIBCALL AffilNew (void)
{
	AffilPtr afp;

	afp = (AffilPtr)MemNew(sizeof(Affil));
	return afp;
}

/*****************************************************************************
*
*   AffilFree()
*
*****************************************************************************/
NLM_EXTERN AffilPtr LIBCALL AffilFree (AffilPtr afp)
{
	if (afp == NULL)
		return afp;
	MemFree(afp->affil);
	MemFree(afp->div);
	MemFree(afp->city);
	MemFree(afp->sub);
	MemFree(afp->country);
	MemFree(afp->street);
	MemFree(afp->email);
	MemFree(afp->fax);
	MemFree(afp->phone);
	MemFree(afp->postal_code);
	return (AffilPtr)MemFree(afp);
}

/*****************************************************************************
*
*   AffilAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN AffilPtr LIBCALL AffilAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	AffilPtr afp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return afp;
	}

	if (aip == NULL)
		return afp;

	if (orig == NULL)           /* Affil ::= */
		atp = AsnReadId(aip, amp, AFFIL);
	else
		atp = AsnLinkType(orig, AFFIL);
    if (atp == NULL)
        return afp;

	afp = AffilNew();
    if (afp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE */
    atp = AsnReadId(aip, amp, atp);   /* read the CHOICE id */
    if (atp == NULL)
        goto erret;

    if (atp == AFFIL_str)         /* just the VisibleString */
    {
        afp->choice = 1;    /* choice type is str */
        if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* get the string */
        afp->affil = (CharPtr)av.ptrvalue;    /* keep it */
    }
    else                          /* affiliation structure */
    {
        afp->choice = 2;     /* choice type is std */
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the START_STRUCT */

        while ((atp = AsnReadId(aip, amp, atp)) != AFFIL_std)
        {
            if (atp == NULL)
                goto erret;
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
            if (atp == AFFIL_std_affil)
                afp->affil = (CharPtr)av.ptrvalue;
            else if (atp == AFFIL_std_div)
                afp->div = (CharPtr)av.ptrvalue;
            else if (atp == AFFIL_std_city)
                afp->city = (CharPtr)av.ptrvalue;
            else if (atp == AFFIL_std_sub)
                afp->sub = (CharPtr)av.ptrvalue;
            else if (atp == AFFIL_std_country)
                afp->country = (CharPtr)av.ptrvalue;
			else if (atp == AFFIL_std_street)
				afp->street = (CharPtr)av.ptrvalue;
			else if (atp == AFFIL_std_email)
				afp->email = (CharPtr)av.ptrvalue;
			else if (atp == AFFIL_std_fax)
				afp->fax = (CharPtr)av.ptrvalue;
			else if (atp == AFFIL_std_phone)
				afp->phone = (CharPtr)av.ptrvalue;
			else if (atp == AFFIL_std_postal_code)
			{
				if (aip->spec_version == 3)    /* ASN3 strip new value */
				{
					ErrPostEx(SEV_ERROR,0,0,"ASN3: postal_code stripped");
					MemFree((CharPtr)(av.ptrvalue));
				}
				else
					afp->postal_code = (CharPtr)av.ptrvalue;
			}
        }
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the END STRUCT */
    }
ret:
	AsnUnlinkType(orig);
	return afp;
erret:
    afp = AffilFree(afp);
    goto ret;
}

/*****************************************************************************
*
*   AffilAsnWrite(afp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL AffilAsnWrite (AffilPtr afp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, AFFIL);
    if (atp == NULL)
        return FALSE;

	if (afp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnWrite(aip, atp, &av))   /* write the tag */
        goto erret;

    if (afp->choice == 1)      /* string */
    {
        av.ptrvalue = afp->affil;
        if (! AsnWrite(aip, AFFIL_str, &av))  /* write the string */
            goto erret;
    }
    else                      /* std */
    {
    	if (! AsnOpenStruct(aip, AFFIL_std, (Pointer)afp))
            goto erret;
        if (afp->affil != NULL)
        {
            av.ptrvalue = afp->affil;
            if (! AsnWrite(aip, AFFIL_std_affil, &av))
                goto erret;
        }
        if (afp->div != NULL)
        {
            av.ptrvalue = afp->div;
            if (! AsnWrite(aip, AFFIL_std_div, &av)) goto erret;
        }
        if (afp->city != NULL)
        {
            av.ptrvalue = afp->city;
            if (! AsnWrite(aip, AFFIL_std_city, &av)) goto erret;
        }
        if (afp->sub != NULL)
        {
            av.ptrvalue = afp->sub;
            if (! AsnWrite(aip, AFFIL_std_sub, &av)) goto erret;
        }
        if (afp->country != NULL)
        {
            av.ptrvalue = afp->country;
            if (! AsnWrite(aip, AFFIL_std_country, &av)) goto erret;
        }
        if (afp->street != NULL)
        {
            av.ptrvalue = afp->street;
            if (! AsnWrite(aip, AFFIL_std_street, &av)) goto erret;
        }
        if (afp->email != NULL)
        {
            av.ptrvalue = afp->email;
            if (! AsnWrite(aip, AFFIL_std_email, &av)) goto erret;
        }
        if (afp->fax != NULL)
        {
            av.ptrvalue = afp->fax;
            if (! AsnWrite(aip, AFFIL_std_fax, &av)) goto erret;
        }
        if (afp->phone != NULL)
        {
            av.ptrvalue = afp->phone;
            if (! AsnWrite(aip, AFFIL_std_phone, &av)) goto erret;
        }
        if (afp->postal_code != NULL)
        {
				if (aip->spec_version == 3)    /* ASN3 strip new value */
				{
					ErrPostEx(SEV_ERROR,0,0,"ASN3: postal_code stripped");
				}
				else
				{
	            av.ptrvalue = afp->postal_code;
   	         if (! AsnWrite(aip, AFFIL_std_postal_code, &av)) goto erret;
				}
        }

    	if (! AsnCloseStruct(aip, AFFIL_std, (Pointer)afp))
            goto erret;
    }
    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   AuthListNew()
*
*****************************************************************************/
NLM_EXTERN AuthListPtr LIBCALL AuthListNew (void)
{
	AuthListPtr alp;

	alp = (AuthListPtr)MemNew(sizeof(AuthList));
	return alp;
}

/*****************************************************************************
*
*   AuthListFree()
*
*****************************************************************************/
NLM_EXTERN AuthListPtr LIBCALL AuthListFree (AuthListPtr alp)
{
    ValNodePtr curr, next;

    if (alp == NULL)
        return alp;

    curr = alp->names;
    while (curr != NULL)
    {
        if (alp->choice == 1)    /* std type */
            AuthorFree((AuthorPtr) curr->data.ptrvalue);
        else                      /* ml or str */
            MemFree(curr->data.ptrvalue);
        next = curr->next;
        MemFree(curr);
        curr = next;
    }
    if (alp->affil != NULL)
        AffilFree(alp->affil);
	return (AuthListPtr)MemFree(alp);
}

/*****************************************************************************
*
*   AuthListAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN AuthListPtr LIBCALL AuthListAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	AuthListPtr alp=NULL;
	DataVal av;
	AsnTypePtr atp, seqofptr;
    Uint1 choice;
    ValNodePtr anp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return alp;
	}

	if (aip == NULL)
		return alp;

	if (orig == NULL)           /* AuthList ::= */
		atp = AsnReadId(aip, amp, AUTH_LIST);
	else
		atp = AsnLinkType(orig, AUTH_LIST);
    if (atp == NULL)
        return alp;

	alp = AuthListNew();
    if (alp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the START STRUCT */
    atp = AsnReadId(aip, amp, atp);   /* read names id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE */
    atp = AsnReadId(aip, amp, atp);   /* read the CHOICE id */
    if (atp == NULL)
        goto erret;
    seqofptr = atp;              /* keep to find end of loop */
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;     /* read the START for SEQUENCE OF */

    if (atp == AUTH_LIST_names_std)         /* Authors */
        choice = 1;    /* choice type is str */
    else if (atp == AUTH_LIST_names_ml)     /* medline */
        choice = 2;
    else                                    /* strings */
        choice = 3;

    alp->choice = choice;

    anp = NULL;
    while ((atp = AsnReadId(aip, amp, atp)) != seqofptr)
    {
        if (atp == NULL)
            goto erret;
        anp = ValNodeNew(anp);      /* add to linked list */
        if (anp == NULL)
            goto erret;
        if (alp->names == NULL)
            alp->names = anp;
        anp->choice = choice;    /* not really necessary */
        if (choice == 1)    /* std */
            anp->data.ptrvalue = (Pointer) AuthorAsnRead(aip, atp);
        else
        {
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;     /* string types */
            anp->data.ptrvalue = av.ptrvalue;
        }
        if (anp->data.ptrvalue == NULL)
            goto erret;
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* SEQ OF end STRUCT */

    atp = AsnReadId(aip, amp, atp);
    if (atp == NULL)
        goto erret;
    if (atp == AUTH_LIST_affil)     /* has an affiliation */
    {
        alp->affil = AffilAsnRead(aip, atp);
        if (alp->affil == NULL)
            goto erret;
        atp = AsnReadId(aip, amp, atp);
        if (atp == NULL)
            goto erret;
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;     /* END STRUCT */
ret:
    AsnUnlinkType(orig);
	return alp;
erret:
    alp = AuthListFree(alp);
    goto ret;
}

/*****************************************************************************
*
*   AuthListAsnWrite(alp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL AuthListAsnWrite (AuthListPtr alp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp, seqofptr, elementptr;
    Int1 choice;
    ValNodePtr anp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, AUTH_LIST);
    if (atp == NULL)
        return FALSE;

	if (alp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)alp))   /* open AuthList SEQUENCE */
        goto erret;

	av.ptrvalue = alp->names;
    if (! AsnWriteChoice(aip, AUTH_LIST_names, (Int2)alp->choice, &av)) goto erret; 

                              /* write CHOICE for SEQUENCE OF */
    choice = alp->choice;
    if (choice == 1 )     /* std */
    {
        seqofptr = AUTH_LIST_names_std;
        elementptr = AUTH_LIST_names_std_E;
    }
    else if (choice == 2)   /* ml */
    {
        seqofptr = AUTH_LIST_names_ml;
        elementptr = AUTH_LIST_names_ml_E;
    }
    else
    {
        seqofptr = AUTH_LIST_names_str;
        elementptr = AUTH_LIST_names_str_E;
    }

    if (! AsnOpenStruct(aip, seqofptr, (Pointer)alp->names)) /* start SEQUENCE OF */
        goto erret;

    anp = alp->names;

    while (anp != NULL)
    {
        if (choice == 1)  /* Author */
        {
            if (! AuthorAsnWrite((AuthorPtr) anp->data.ptrvalue, aip, elementptr))
                goto erret;
        }
        else              /* str or medline */
        {
            av.ptrvalue = anp->data.ptrvalue;
            if (! AsnWrite(aip, elementptr, &av)) goto erret;
        }
        anp = anp->next;
    }

    if (! AsnCloseStruct(aip, seqofptr, (Pointer)alp->names))  /* end SEQUENCE OF */
        goto erret;

    if (alp->affil != NULL)     /* affiliation */
    {
        if (! AffilAsnWrite(alp->affil, aip, AUTH_LIST_affil))
            goto erret;
    }

   	if (! AsnCloseStruct(aip, atp, (Pointer)alp))    /* end AuthList SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   AuthorNew()
*
*****************************************************************************/
NLM_EXTERN AuthorPtr LIBCALL AuthorNew (void)
{
	AuthorPtr ap;

	ap = (AuthorPtr)MemNew(sizeof(Author));
	if (ap == NULL) return ap;
	ap->is_corr = (Uint1)255;
	return ap;
}

/*****************************************************************************
*
*   AuthorFree()
*
*****************************************************************************/
NLM_EXTERN AuthorPtr LIBCALL AuthorFree (AuthorPtr ap)
{
    if (ap == NULL)
        return ap;

    PersonIdFree(ap->name);
    if (ap->affil != NULL)
        AffilFree(ap->affil);
	return (AuthorPtr)MemFree(ap);
}

/*****************************************************************************
*
*   AuthorAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN AuthorPtr LIBCALL AuthorAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	AuthorPtr ap=NULL;
	DataVal av;
	AsnTypePtr atp, oldatp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return ap;
	}

	if (aip == NULL)
		return ap;

	if (orig == NULL)           /* Author ::= */
		atp = AsnReadId(aip, amp, AUTHOR);
	else
		atp = AsnLinkType(orig, AUTHOR);
    oldatp = atp;     /* points to start of SEQUENCE */
    if (atp == NULL)
        return ap;

	ap = AuthorNew();
    if (ap == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read the name id */
    if (atp == NULL)
        goto erret;
    ap->name = PersonIdAsnRead(aip, atp);   /* read the PersonId */
    if (ap->name == NULL)
        goto erret;

    while ((atp = AsnReadId(aip, amp, atp)) != oldatp)   /* read SEQUENCE */
    {
        if (atp == NULL)
            goto erret;
        if (atp == AUTHOR_affil)
        {
            ap->affil = AffilAsnRead(aip, atp);
            if (ap->affil == NULL)
                goto erret;
        }
        else
        {
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
            if (atp == AUTHOR_level)
                ap->lr[0] = (Uint1) av.intvalue;
            else if (atp == AUTHOR_role)
                ap->lr[1] = (Uint1) av.intvalue;
			else if (atp == AUTHOR_is_corr)
			{
				if (av.boolvalue)
					ap->is_corr = 1;
				else
					ap->is_corr = 0;
			}
        }
    }

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return ap;
erret:
    ap = AuthorFree(ap);
    goto ret;
}

/*****************************************************************************
*
*   AuthorAsnWrite(ap, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL AuthorAsnWrite (AuthorPtr ap, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, AUTHOR);
    if (atp == NULL)
        return FALSE;

	if (ap == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)ap))  /* open the SEQUENCE */
        goto erret;

    if (! PersonIdAsnWrite(ap->name, aip, AUTHOR_name))   /* write the name */
        goto erret;

    if (ap->lr[0] != 0)       /* level set */
    {
        av.intvalue = (Int4) ap->lr[0];
        if (! AsnWrite(aip, AUTHOR_level, &av)) goto erret;
    }

    if (ap->lr[1] != 0)      /* role set */
    {
        av.intvalue = (Int4) ap->lr[1];
        if (! AsnWrite(aip, AUTHOR_role, &av)) goto erret;
    }

    if (ap->affil != NULL)
    {
        if (! AffilAsnWrite(ap->affil, aip, AUTHOR_affil))
            goto erret;
    }

	if (ap->is_corr != 255)
	{
		av.boolvalue = (Boolean) ap->is_corr;
		AsnWrite(aip, AUTHOR_is_corr, &av);
	}

   	if (! AsnCloseStruct(aip, atp, (Pointer)ap))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitArtNew()
*
*****************************************************************************/
NLM_EXTERN CitArtPtr LIBCALL CitArtNew (void)
{
	CitArtPtr cap;

	cap = (CitArtPtr)MemNew(sizeof(CitArt));
	return cap;
}

/*****************************************************************************
*
*   CitArtFree()
*
*****************************************************************************/
NLM_EXTERN CitArtPtr LIBCALL CitArtFree (CitArtPtr cap)
{
    ArticleIdPtr aip, aipnext;

    if (cap == NULL)
        return cap;

    TitleFree(cap->title);
    AuthListFree(cap->authors);
    if (cap->from == 1)      /* journal */
        CitJourFree((CitJourPtr) cap->fromptr);
    else                     /* book or proceedings */
        CitBookFree((CitBookPtr) cap->fromptr);

    for (aip = cap->ids; aip != NULL; )
    {
        aipnext = aip->next;
        ArticleIdFree(aip);
        aip = aipnext;
    }
	return (CitArtPtr)MemFree(cap);
}

/*****************************************************************************
*
*   CitArtAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitArtPtr LIBCALL CitArtAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitArtPtr cap=NULL;
	DataVal av;
	AsnTypePtr atp;
        ArticleIdPtr aidp;
        ArticleIdPtr PNTR aidp_add;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cap;
	}

	if (aip == NULL)
		return cap;

	if (orig == NULL)           /* CitArt ::= */
		atp = AsnReadId(aip, amp, CIT_ART);
	else
		atp = AsnLinkType(orig, CIT_ART);
    if (atp == NULL)
        return cap;

	cap = CitArtNew();
    if (cap == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */

    atp = AsnReadId(aip, amp, atp);   /* read title id */
	if (atp == CIT_ART_title)
	{
	    cap->title = TitleAsnRead(aip, atp);   /* read the titles */
    	if (cap->title == NULL)
        	goto erret;

	    atp = AsnReadId(aip, amp, atp);   /* read the authors id */
	}
    if (atp == CIT_ART_authors)
	{
	    cap->authors = AuthListAsnRead(aip, atp);
    	if (cap->authors == NULL)
        	goto erret;

	    atp = AsnReadId(aip, amp, atp);    /* read from tag */
	}
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret; /* balance from CHOICE */

    atp = AsnReadId(aip, amp, atp);    /* read the from CHOICE */
    if (atp == NULL)
        goto erret;
    if (atp == CIT_ART_from_journal)
    {
        cap->from = 1;
        cap->fromptr = (Pointer) CitJourAsnRead(aip, atp);
    }
    else if (atp == CIT_ART_from_book)
    {
        cap->from = 2;
        cap->fromptr = (Pointer) CitBookAsnRead(aip, atp);
    }
    else
    {
        cap->from = 3;
        cap->fromptr = (Pointer) CitProcAsnRead(aip, atp);
    }

    if (cap->fromptr == NULL)
        goto erret;

    atp = AsnReadId(aip, amp, atp);
    if (atp == NULL)
        goto erret;

    if (atp == CIT_ART_ids)
    {
       if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
       aidp_add = &(cap->ids);
       while ((atp = AsnReadId(aip, amp, atp)) == ARTICLEIDSET_E)
       {
          aidp = ArticleIdAsnRead(aip, atp);
          if (aidp == NULL) goto erret;
          *aidp_add = aidp;
          aidp_add = &(aidp->next);
       }
       if (AsnReadVal(aip, atp, &av) <= 0) goto erret;  /* END STRUCT */
       atp = AsnReadId(aip, amp, atp);
       
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cap;
erret:
    cap = CitArtFree(cap);
    goto ret;
}

/*****************************************************************************
*
*   CitArtAsnWrite(cap, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitArtAsnWrite (CitArtPtr cap, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;
        ArticleIdPtr aidp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_ART);
    if (atp == NULL)
        return FALSE;

	if (cap == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cap))  /* open the SEQUENCE */
        goto erret;

	if (cap->title != NULL)
	{
	    if (! TitleAsnWrite(cap->title, aip, CIT_ART_title))  /* write the title(s) */
    	    goto erret;
	}
	if (cap->authors != NULL)
	{
	    if (! AuthListAsnWrite(cap->authors, aip, CIT_ART_authors))  /* authors */
    	    goto erret;
	}

	av.ptrvalue = cap->fromptr;
    if (! AsnWriteChoice(aip, CIT_ART_from, (Int2)cap->from, &av)) goto erret;

    if (cap->from == 1)
        retval = CitJourAsnWrite((CitJourPtr) cap->fromptr, aip, CIT_ART_from_journal);
    else if (cap->from == 2)
        retval = CitBookAsnWrite((CitBookPtr) cap->fromptr, aip, CIT_ART_from_book);
    else
        retval = CitProcAsnWrite((CitBookPtr) cap->fromptr, aip, CIT_ART_from_proc);

    if (retval == FALSE)
        goto erret;
    retval = FALSE;

    if (cap->ids != NULL)
    {
       if (! AsnOpenStruct(aip, CIT_ART_ids, (Pointer)cap)) goto erret;
       for (aidp = cap->ids; aidp != NULL; aidp = aidp->next)
       {
          if (! ArticleIdAsnWrite(aidp, aip, ARTICLEIDSET_E)) goto erret;
       }
       if (! AsnCloseStruct(aip, CIT_ART_ids, (Pointer)cap)) goto erret;
    }

    if (! AsnCloseStruct(aip, atp, (Pointer)cap))    /* close the SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   ImprintNew()
*
*****************************************************************************/
NLM_EXTERN ImprintPtr LIBCALL ImprintNew (void)
{
	ImprintPtr ip;

	ip = (ImprintPtr)MemNew(sizeof(Imprint));
	return ip;
}

/*****************************************************************************
*
*   ImprintFree()
*
*****************************************************************************/
NLM_EXTERN ImprintPtr LIBCALL ImprintFree (ImprintPtr ip)
{
    PubStatusDatePtr psdp, psdpnext;

    if (ip == NULL)
        return ip;

    DateFree(ip->date);
    DateFree(ip->cprt);
    MemFree(ip->volume);
    MemFree(ip->issue);
    MemFree(ip->pages);
    MemFree(ip->section);
    MemFree(ip->part_sup);
    MemFree(ip->language);
	MemFree(ip->part_supi);
    AffilFree(ip->pub);
	CitRetractFree(ip->retract);
    for (psdp = ip->history; psdp != NULL; )
    {
       psdpnext = psdp->next;
       PubStatusDateFree(psdp);
       psdp = psdpnext;
    }
	return (ImprintPtr)MemFree(ip);
}

/*****************************************************************************
*
*   ImprintAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN ImprintPtr LIBCALL ImprintAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	ImprintPtr ip=NULL;
	DataVal av;
	AsnTypePtr atp, oldatp;
        PubStatusDatePtr psdp;
        PubStatusDatePtr PNTR psdp_add;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return ip;
	}

	if (aip == NULL)
		return ip;

	if (orig == NULL)           /* Imprint ::= */
		atp = AsnReadId(aip, amp, IMPRINT);
	else
		atp = AsnLinkType(orig, IMPRINT);
    oldatp = atp;
    if (atp == NULL)
        return ip;

	ip = ImprintNew();
    if (ip == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the SEQUENCE start */
    atp = AsnReadId(aip, amp, atp);   /* read the date */
    if (atp == NULL)
        goto erret;
    ip->date = DateAsnRead(aip, atp);
    if (ip->date == NULL)
        goto erret;

    psdp_add = &(ip->history);
    while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
    {
        if (atp == NULL)
            goto erret;
        if (atp == IMPRINT_pub)
        {
            ip->pub = AffilAsnRead(aip, atp);
            if (ip->pub == NULL)
                goto erret;
        }
        else if (atp == IMPRINT_cprt)
        {
            ip->cprt = DateAsnRead(aip, atp);
            if (ip->cprt == NULL)
                goto erret;
        }
		else if (atp == IMPRINT_retract)
		{
			ip->retract = CitRetractAsnRead(aip, atp);
			if (ip->retract == NULL) goto erret;
		}
        else if (atp == PUBSTATUSDATESET_E)
        {
          psdp = PubStatusDateAsnRead(aip, atp);
          if (psdp == NULL) goto erret;
          *psdp_add = psdp;
          psdp_add = &(psdp->next);
	}
        else
        {
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
            if (atp == IMPRINT_volume)
                ip->volume = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_issue)
                ip->issue = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_pages)
                ip->pages = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_section)
                ip->section = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_part_sup)
                ip->part_sup = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_language)
                ip->language = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_prepub)
                ip->prepub = (Uint1)av.intvalue;
            else if (atp == IMPRINT_part_supi)
                ip->part_supi = (CharPtr)av.ptrvalue;
            else if (atp == IMPRINT_pubstatus)
                ip->pubstatus = (Uint1)(av.intvalue);

        }
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
ret:
	AsnUnlinkType(orig);
	return ip;
erret:
    ip = ImprintFree(ip);
    goto ret;
}

/*****************************************************************************
*
*   ImprintAsnWrite(ip, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL ImprintAsnWrite (ImprintPtr ip, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;
	PubStatusDatePtr psdp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, IMPRINT);
    if (atp == NULL)
        return FALSE;

	if (ip == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)ip))
        goto erret;
    if (! DateAsnWrite(ip->date, aip, IMPRINT_date))
        goto erret;
    if (ip->volume != NULL)
    {
        av.ptrvalue = ip->volume;
        if (! AsnWrite(aip, IMPRINT_volume, &av)) goto erret;
    }
    if (ip->issue != NULL)
    {
        av.ptrvalue = ip->issue;
        if (! AsnWrite(aip, IMPRINT_issue, &av)) goto erret;
    }
    if (ip->pages != NULL)
    {
        av.ptrvalue = ip->pages;
        if (! AsnWrite(aip, IMPRINT_pages, &av)) goto erret;
    }
    if (ip->section != NULL)
    {
        av.ptrvalue = ip->section;
        if (! AsnWrite(aip, IMPRINT_section, &av)) goto erret;
    }
    if (ip->pub != NULL)
    {
        if (! AffilAsnWrite(ip->pub, aip, IMPRINT_pub))
            goto erret;
    }
    if (ip->cprt != NULL)
    {
        if (! DateAsnWrite(ip->cprt, aip, IMPRINT_cprt))
            goto erret;
    }
    if (ip->part_sup != NULL)
    {
        av.ptrvalue = ip->part_sup;
        if (! AsnWrite(aip, IMPRINT_part_sup, &av)) goto erret;
    }
    if (ip->language != NULL)
    {
        av.ptrvalue = ip->language;
        if (! AsnWrite(aip, IMPRINT_language, &av)) goto erret;
    }
    if (ip->prepub != 0)
    {
        av.intvalue = ip->prepub;
        if (! AsnWrite(aip, IMPRINT_prepub, &av)) goto erret;
    }
    if (ip->part_supi != NULL)
    {
        av.ptrvalue = ip->part_supi;
        if (! AsnWrite(aip, IMPRINT_part_supi, &av)) goto erret;
    }
    if (ip->retract != NULL)
    {
        if (! CitRetractAsnWrite(ip->retract, aip, IMPRINT_retract))
            goto erret;
    }
    if (ip->pubstatus)
    {
        av.intvalue = (Int4)(ip->pubstatus);
        if (! AsnWrite(aip, IMPRINT_pubstatus, &av)) goto erret;
    }
    if (ip->history != NULL)
    {
	if (! AsnOpenStruct(aip, IMPRINT_history, (Pointer)ip)) goto erret;
        for (psdp = ip->history; psdp != NULL; psdp = psdp->next)
        {
           if (! PubStatusDateAsnWrite(psdp, aip, PUBSTATUSDATESET_E))
             goto erret;
	}
        if (! AsnCloseStruct(aip, IMPRINT_history, (Pointer)ip)) goto erret;
    }
    if (! AsnCloseStruct(aip, atp, (Pointer)ip))
        goto erret;
    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitRetractNew()
*
*****************************************************************************/
NLM_EXTERN CitRetractPtr LIBCALL CitRetractNew (void)
{
	CitRetractPtr crp;

	crp = (CitRetractPtr)MemNew(sizeof(CitRetract));
	return crp;
}

/*****************************************************************************
*
*   CitRetractFree()
*
*****************************************************************************/
NLM_EXTERN CitRetractPtr LIBCALL CitRetractFree (CitRetractPtr crp)
{
    if (crp == NULL)
        return crp;

	MemFree(crp->exp);
	return (CitRetractPtr)MemFree(crp);
}

/*****************************************************************************
*
*   CitRetractAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitRetractPtr LIBCALL CitRetractAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitRetractPtr  crp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return crp;
	}

	if (aip == NULL)
		return crp;

	if (orig == NULL)           /* CitRetract ::= */
		atp = AsnReadId(aip, amp, CITRETRACT);
	else
		atp = AsnLinkType(orig, CITRETRACT);
    if (atp == NULL)
        return crp;

	crp = CitRetractNew();
    if (crp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the SEQUENCE start */

    atp = AsnReadId(aip, amp, atp);   /* read the type */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
	crp->type = (Uint1)av.intvalue;

    atp = AsnReadId(aip, amp, atp);   /* read the exp? */
    if (atp == CITRETRACT_exp)
	{
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
		crp->exp = (CharPtr)av.ptrvalue;
	    atp = AsnReadId(aip, amp, atp);   /* read the CLOSE */
	}

	if (atp == NULL) goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
ret:
	AsnUnlinkType(orig);
	return crp;
erret:
    crp = CitRetractFree(crp);
    goto ret;
}

/*****************************************************************************
*
*   CitRetractAsnWrite(ip, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitRetractAsnWrite (CitRetractPtr crp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CITRETRACT);
    if (atp == NULL)
        return FALSE;

	if (crp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)crp))
        goto erret;

    av.intvalue = (Int4)crp->type;
    if (! AsnWrite(aip, CITRETRACT_type, &av)) goto erret;

	if (crp->exp != NULL)
	{
	    av.ptrvalue = (Pointer)crp->exp;
    	if (! AsnWrite(aip, CITRETRACT_exp, &av)) goto erret;
	}

    if (! AsnCloseStruct(aip, atp, (Pointer)crp))
        goto erret;
    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitJourNew()
*
*****************************************************************************/
NLM_EXTERN CitJourPtr LIBCALL CitJourNew (void)
{
	CitJourPtr cjp;

	cjp = (CitJourPtr)MemNew(sizeof(CitJour));
	return cjp;
}

/*****************************************************************************
*
*   CitJourFree()
*
*****************************************************************************/
NLM_EXTERN CitJourPtr LIBCALL CitJourFree (CitJourPtr cjp)
{
    if (cjp == NULL)
        return cjp;

    TitleFree(cjp->title);
    ImprintFree(cjp->imp);
	return (CitJourPtr)MemFree(cjp);
}

/*****************************************************************************
*
*   CitJourAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitJourPtr LIBCALL CitJourAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitJourPtr cjp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cjp;
	}

	if (aip == NULL)
		return cjp;

	if (orig == NULL)           /* CitJour ::= */
		atp = AsnReadId(aip, amp, CIT_JOUR);
	else
		atp = AsnLinkType(orig, CIT_JOUR);
    if (atp == NULL)
        return cjp;

	cjp = CitJourNew();
    if (cjp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read title id */
    if (atp == NULL)
        goto erret;
    cjp->title = TitleAsnRead(aip, atp);
    if (cjp->title == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);   /* read imprint id */
    if (atp == NULL)
        goto erret;
    cjp->imp = ImprintAsnRead(aip, atp);
    if (cjp->imp == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);   /* read the close SEQUENCE id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cjp;
erret:
    cjp = CitJourFree(cjp);
    goto ret;
}

/*****************************************************************************
*
*   CitJourAsnWrite(cjp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitJourAsnWrite (CitJourPtr cjp, AsnIoPtr aip, AsnTypePtr orig)
{
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_JOUR);
    if (atp == NULL)
        return FALSE;

	if (cjp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cjp))  /* open the SEQUENCE */
        goto erret;
    if (! TitleAsnWrite(cjp->title, aip, CIT_JOUR_title))
        goto erret;
    if (! ImprintAsnWrite(cjp->imp, aip, CIT_JOUR_imp))
        goto erret;
   	if (! AsnCloseStruct(aip, atp, (Pointer)cjp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitBookNew()
*
*****************************************************************************/
NLM_EXTERN CitBookPtr LIBCALL CitBookNew (void)
{
	CitBookPtr cbp;

	cbp = (CitBookPtr)MemNew(sizeof(CitBook));
	return cbp;
}

/*****************************************************************************
*
*   CitBookFree()
*
*****************************************************************************/
NLM_EXTERN CitBookPtr LIBCALL CitBookFree (CitBookPtr cbp)
{
    ValNodePtr anp, nextp;

    if (cbp == NULL)
        return cbp;

    TitleFree(cbp->title);
    TitleFree(cbp->coll);
	AuthListFree(cbp->authors);
    ImprintFree(cbp->imp);
    if (cbp->othertype == 1)      /* Cit-proc */
    {
       anp = (ValNodePtr)cbp->otherdata;
       while (anp != NULL)
       {
           switch (anp->choice)
           {
               case 1:     /* number ... a CharPtr */
                 MemFree(anp->data.ptrvalue);
                break;
                case 2:    /* date ... a DatePtr */
                 DateFree((DatePtr)anp->data.ptrvalue);
                break;
                case 3:    /* place ... an AffilPtr */
                 AffilFree((AffilPtr)anp->data.ptrvalue);
                 break;
            }
            nextp = anp->next;
            MemFree(anp);
            anp = nextp;
        }
    }
    else if (cbp->othertype == 2)    /* Cit-let */
        MemFree(cbp->otherdata);    /* man-id ... a CharPtr */

    return (CitBookPtr)MemFree(cbp);
}

/*****************************************************************************
*
*   CitBookAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitBookPtr LIBCALL CitBookAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitBookPtr cbp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cbp;
	}

	if (aip == NULL)
		return cbp;

	if (orig == NULL)           /* CitBook ::= */
		atp = AsnReadId(aip, amp, CIT_BOOK);
	else
		atp = AsnLinkType(orig, CIT_BOOK);
    if (atp == NULL)
        return cbp;

	cbp = CitBookNew();
    if (cbp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read title id */
    if (atp == NULL)
        goto erret;
    cbp->title = TitleAsnRead(aip, atp);
    if (cbp->title == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);   /* read coll or authors id */
    if (atp == NULL)
        goto erret;
    if (atp == CIT_BOOK_coll)
    {
        cbp->coll = TitleAsnRead(aip, atp);
        if (cbp->coll == NULL)
            goto erret;
        atp = AsnReadId(aip, amp, atp);     /* read authors id */
        if (atp == NULL)
            goto erret;
    }
    cbp->authors = AuthListAsnRead(aip, atp);
    if (cbp->authors == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);         /* read imprint id */
    if (atp == NULL)
        goto erret;
    cbp->imp = ImprintAsnRead(aip, atp);
    if (cbp->imp == NULL)
        goto erret;
    
    atp = AsnReadId(aip, amp, atp);   /* read the close SEQUENCE id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cbp;
erret:
    cbp = CitBookFree(cbp);
    goto ret;
}

/*****************************************************************************
*
*   CitBookAsnWrite(cbp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitBookAsnWrite (CitBookPtr cbp, AsnIoPtr aip, AsnTypePtr orig)
{
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_BOOK);
    if (atp == NULL)
        return FALSE;

	if (cbp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cbp))  /* open the SEQUENCE */
        goto erret;
    if (! TitleAsnWrite(cbp->title, aip, CIT_BOOK_title))
        goto erret;
    if (cbp->coll != NULL)
    {
        if (! TitleAsnWrite(cbp->coll, aip, CIT_BOOK_coll))
            goto erret;
    }
    if (! AuthListAsnWrite(cbp->authors, aip, CIT_BOOK_authors))
        goto erret;
    if (! ImprintAsnWrite(cbp->imp, aip, CIT_BOOK_imp))
        goto erret;
   	if (! AsnCloseStruct(aip, atp, (Pointer)cbp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitProcAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitBookPtr LIBCALL CitProcAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitBookPtr cpp=NULL;
	DataVal av;
	AsnTypePtr atp;
    ValNodePtr anp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cpp;
	}

	if (aip == NULL)
		return cpp;

	if (orig == NULL)           /* CitProc ::= */
		atp = AsnReadId(aip, amp, CIT_PROC);
	else
		atp = AsnLinkType(orig, CIT_PROC);
    if (atp == NULL)
        return cpp;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read book id */
    if (atp == NULL)
        goto erret;
    cpp = CitBookAsnRead(aip, atp);   /* read the book */
    if (cpp == NULL)
        goto erret;
    cpp->othertype = 1;               /* mark as a Cit-proc */
    atp = AsnReadId(aip, amp, atp);   /* read the meet id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;        /* open struct */
    atp = AsnReadId(aip, amp, atp);   /* number id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    anp = ValNodeNew(NULL);
    if (anp == NULL)
        goto erret;
    cpp->otherdata = (Pointer)anp;
    anp->choice = 1;
    anp->data.ptrvalue = av.ptrvalue;
    atp = AsnReadId(aip, amp, atp);   /* date id */
    if (atp == NULL)
        goto erret;
    anp = ValNodeNew(anp);
    if (anp == NULL)
        goto erret;
    anp->choice = 2;
    anp->data.ptrvalue = (Pointer)DateAsnRead(aip, atp);
    if (anp->data.ptrvalue == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);   /* place id */
    if (atp == NULL)
        goto erret;
    anp = ValNodeNew(anp);
    if (anp == NULL)
        goto erret;
    anp->choice = 3;
    anp->data.ptrvalue = (Pointer)AffilAsnRead(aip, atp);
    if (anp->data.ptrvalue == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);   /* end meet struct */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    
    atp = AsnReadId(aip, amp, atp);   /* read the close SEQUENCE id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cpp;
erret:
    cpp = CitBookFree(cpp);
    goto ret;
}

/*****************************************************************************
*
*   CitProcAsnWrite(cpp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitProcAsnWrite (CitBookPtr cpp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    ValNodePtr anp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_PROC);
    if (atp == NULL)
        return FALSE;

	if (cpp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cpp))  /* open the SEQUENCE */
        goto erret;
    if (! CitBookAsnWrite(cpp, aip, CIT_PROC_book))   /* write the book */
        goto erret;
    if (! AsnOpenStruct(aip, CIT_PROC_meet, (Pointer)cpp->otherdata))
        goto erret;
    anp = (ValNodePtr)cpp->otherdata;
    av.ptrvalue = anp->data.ptrvalue;
    if (! AsnWrite(aip, MEETING_number, &av)) goto erret;
    anp = anp->next;
    if (! DateAsnWrite((DatePtr)anp->data.ptrvalue, aip, MEETING_date))
        goto erret;
    anp = anp->next;
    if (! AffilAsnWrite((AffilPtr)anp->data.ptrvalue, aip, MEETING_place))
        goto erret;
    if (! AsnCloseStruct(aip, CIT_PROC_meet, (Pointer)cpp->otherdata))
        goto erret;
   	if (! AsnCloseStruct(aip, atp, (Pointer)cpp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitLetAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitBookPtr LIBCALL CitLetAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitBookPtr clp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return clp;
	}

	if (aip == NULL)
		return clp;

	if (orig == NULL)           /* CitLet ::= */
		atp = AsnReadId(aip, amp, CIT_LET);
	else
		atp = AsnLinkType(orig, CIT_LET);
    if (atp == NULL)
        return clp;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read book id */
    if (atp == NULL)
        goto erret;
    clp = CitBookAsnRead(aip, atp);   /* read the book */
    if (clp == NULL)
        goto erret;
    clp->othertype = 2;               /* mark as a Cit-let */
    atp = AsnReadId(aip, amp, atp);   /* read the man-id if present */
    if (atp == NULL)
        goto erret;
    if (atp == CIT_LET_man_id)
    {
        if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
        clp->otherdata = av.ptrvalue;
        atp = AsnReadId(aip, amp, atp);    /* end struct */
        if (atp == NULL)
            goto erret;
    }
	if (atp == CIT_LET_type)         /* read type if present */
	{
		if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
		clp->let_type = (Uint1) av.intvalue;
		atp = AsnReadId(aip, amp, atp);
        if (atp == NULL)
            goto erret;
	}
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return clp;
erret:
    clp = CitBookFree(clp);
    goto ret;
}

/*****************************************************************************
*
*   CitLetAsnWrite(clp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitLetAsnWrite (CitBookPtr clp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_LET);
    if (atp == NULL)
        return FALSE;

	if (clp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)clp))  /* open the SEQUENCE */
        goto erret;
    if (! CitBookAsnWrite(clp, aip, CIT_LET_cit))   /* write the book */
        goto erret;
    if (clp->otherdata != NULL)   /* man-id */
    {
        av.ptrvalue = clp->otherdata;
        if (! AsnWrite(aip, CIT_LET_man_id, &av)) goto erret;
    }
	if (clp->let_type)            /* type of Cit-let */
	{
		av.intvalue = (Int4)clp->let_type;
		if (! AsnWrite(aip, CIT_LET_type, &av)) goto erret;
	}
   	if (! AsnCloseStruct(aip, atp, (Pointer)clp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitPatNew()
*
*****************************************************************************/
NLM_EXTERN CitPatPtr LIBCALL CitPatNew (void)
{
	CitPatPtr cpp;

	cpp = (CitPatPtr)MemNew(sizeof(CitPat));
	return cpp;
}

/*****************************************************************************
*
*   CitPatFree()
*
*****************************************************************************/
NLM_EXTERN CitPatPtr LIBCALL CitPatFree (CitPatPtr cpp)
{
    if (cpp == NULL)
        return cpp;

    MemFree(cpp->title);
    AuthListFree(cpp->authors);
    MemFree(cpp->country);
    MemFree(cpp->doc_type);
    MemFree(cpp->number);
    DateFree(cpp->date_issue);
	ValNodeFreeData(cpp->_class);
    MemFree(cpp->app_number);
    DateFree(cpp->app_date);

	AuthListFree(cpp->applicants);
	AuthListFree(cpp->assignees);
	PatPrioritySetFree(cpp->priority);
	MemFree(cpp->abstract);

    return (CitPatPtr)MemFree(cpp);
}

/*****************************************************************************
*
*   CitPatAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitPatPtr LIBCALL CitPatAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitPatPtr cpp=NULL;
	DataVal av;
	AsnTypePtr atp, oldatp;
	ValNodePtr vnp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cpp;
	}

	if (aip == NULL)
		return cpp;

	if (orig == NULL)           /* CitPat ::= */
		atp = AsnReadId(aip, amp, CIT_PAT);
	else
		atp = AsnLinkType(orig, CIT_PAT);
    oldatp = atp;
    if (atp == NULL)
        return cpp;

	cpp = CitPatNew();
    if (cpp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read title id */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    cpp->title = (CharPtr)av.ptrvalue;
    atp = AsnReadId(aip, amp, atp);   /* read author list id */
    if (atp == NULL)
        goto erret;
    cpp->authors = AuthListAsnRead(aip, atp);
    if (cpp->authors == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp );  /* country */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    cpp->country = (CharPtr)av.ptrvalue;
    atp = AsnReadId(aip, amp, atp );  /* doc_type */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    cpp->doc_type = (CharPtr)av.ptrvalue;

    while ((atp = AsnReadId(aip, amp, atp)) != oldatp)  /* read options */
    {
        if (atp == NULL)
            goto erret;
        if (atp == CIT_PAT_date_issue)
        {
            cpp->date_issue = DateAsnRead(aip, atp);
            if (cpp->date_issue == NULL)
                goto erret;
        }
        else if (atp == CIT_PAT_app_date)
        {
            cpp->app_date = DateAsnRead(aip, atp);
            if (cpp->app_date == NULL)
                goto erret;
        }
		else if (atp == CIT_PAT_applicants)
		{
			cpp->applicants = AuthListAsnRead(aip, atp);
			if (cpp->applicants == NULL)
				goto erret;
		}
		else if (atp == CIT_PAT_assignees)
		{
			cpp->assignees = AuthListAsnRead(aip, atp);
			if (cpp->assignees == NULL)
				goto erret;
		}
		else if (atp == CIT_PAT_priority)
		{
			cpp->priority = PatPrioritySetAsnRead(aip, atp, CIT_PAT_priority_E);
			if (cpp->priority == NULL)
				goto erret;
		}
        else
        {
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
			if (atp == CIT_PAT_number)
			{
				cpp->number = (CharPtr)av.ptrvalue;
			}
            if (atp == CIT_PAT_class)
			{
				while ((atp = AsnReadId(aip, amp, atp)) != CIT_PAT_class)
				{
					if (atp == NULL) goto erret;
					if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
					vnp = ValNodeNew(cpp->_class);
					if (vnp == NULL) goto erret;
					if (cpp->_class == NULL)
						cpp->_class = vnp;
					vnp->data.ptrvalue = (CharPtr)av.ptrvalue;
				}
				if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
			}
            else if (atp == CIT_PAT_app_number)
                cpp->app_number = (CharPtr)av.ptrvalue;
			else if (atp == CIT_PAT_abstract)
				cpp->abstract = (CharPtr)av.ptrvalue;
        }
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cpp;
erret:
    cpp = CitPatFree(cpp);
    goto ret;
}

/*****************************************************************************
*
*   CitPatAsnWrite(cpp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitPatAsnWrite (CitPatPtr cpp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;
	ValNodePtr vnp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_PAT);
    if (atp == NULL)
        return FALSE;

	if (cpp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cpp))  /* open the SEQUENCE */
        goto erret;
    av.ptrvalue = cpp->title;
    if (! AsnWrite(aip, CIT_PAT_title, &av)) goto erret;
    if (! AuthListAsnWrite(cpp->authors, aip, CIT_PAT_authors))
        goto erret;
    av.ptrvalue = cpp->country;
    if (! AsnWrite(aip, CIT_PAT_country, &av)) goto erret;
    av.ptrvalue = cpp->doc_type;
    if (! AsnWrite(aip, CIT_PAT_doc_type, &av)) goto erret;
	if (cpp->number != NULL)
	{
		av.ptrvalue = cpp->number;
		if (! AsnWrite(aip, CIT_PAT_number, &av)) goto erret;
	}
	if (cpp->date_issue != NULL)
	{
		if (! DateAsnWrite(cpp->date_issue, aip, CIT_PAT_date_issue))
			goto erret;
	}
    if (cpp->_class != NULL)
    {
		if (! AsnOpenStruct(aip, CIT_PAT_class, (Pointer)(cpp->_class)))
			goto erret;
		for (vnp = cpp->_class; vnp != NULL; vnp = vnp->next)
		{
			av.ptrvalue = vnp->data.ptrvalue;
			if (! AsnWrite(aip, CIT_PAT_class_E, &av)) goto erret;
		}
		if (! AsnCloseStruct(aip, CIT_PAT_class, (Pointer)(cpp->_class)))
			goto erret;
    }
    if (cpp->app_number != NULL)
    {
        av.ptrvalue = cpp->app_number;
        if (! AsnWrite(aip, CIT_PAT_app_number, &av)) goto erret;
    }
    if (cpp->app_date != NULL)
    {
        if (! DateAsnWrite(cpp->app_date, aip, CIT_PAT_app_date))
            goto erret;
    }

	if (cpp->applicants != NULL)
	{
		if (! AuthListAsnWrite(cpp->applicants, aip, CIT_PAT_applicants))
			goto erret;
	}
	if (cpp->assignees != NULL)
	{
		if (! AuthListAsnWrite(cpp->assignees, aip, CIT_PAT_assignees))
			goto erret;
	}
	if (cpp->priority != NULL)
	{
		if (! PatPrioritySetAsnWrite(cpp->priority, aip, CIT_PAT_priority, CIT_PAT_priority_E))
			goto erret;
	}
	if (cpp->abstract != NULL)
	{
		av.ptrvalue = cpp->abstract;
		if (! AsnWrite(aip, CIT_PAT_abstract, &av)) goto erret;
	}

    if (! AsnCloseStruct(aip, atp, (Pointer)cpp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   IdPatNew()
*
*****************************************************************************/
NLM_EXTERN IdPatPtr LIBCALL IdPatNew (void)
{
	IdPatPtr idp;

	idp = (IdPatPtr)MemNew(sizeof(IdPat));
	return idp;
}

/*****************************************************************************
*
*   IdPatFree()
*
*****************************************************************************/
NLM_EXTERN IdPatPtr LIBCALL IdPatFree (IdPatPtr idp)
{
    if (idp == NULL)
        return idp;

    MemFree(idp->country);
    MemFree(idp->number);
    MemFree(idp->app_number);
	MemFree(idp->doc_type);
    return (IdPatPtr)MemFree(idp);
}

/*****************************************************************************
*
*   IdPatAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN IdPatPtr LIBCALL IdPatAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	IdPatPtr idp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return idp;
	}

	if (aip == NULL)
		return idp;

	if (orig == NULL)           /* IdPat ::= */
		atp = AsnReadId(aip, amp, ID_PAT);
	else
		atp = AsnLinkType(orig, ID_PAT);
    if (atp == NULL)
        return idp;

	idp = IdPatNew();
    if (idp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp );  /* country */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    idp->country = (CharPtr)av.ptrvalue;
    atp = AsnReadId(aip, amp, atp );  /* CHOICE */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    atp = AsnReadId(aip, amp, atp );  /* number or app_number */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    if (atp == ID_PAT_id_number)
        idp->number = (CharPtr)av.ptrvalue;
    else
        idp->app_number = (CharPtr)av.ptrvalue;
    atp = AsnReadId(aip, amp, atp);  /* end or doc-type */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
	if (atp == ID_PAT_doc_type)
	{
		idp->doc_type = av.ptrvalue;
		atp = AsnReadId(aip, amp, atp);
		if (atp == NULL) goto erret;
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret; /* end of SEQUENCE */
	}
ret:
	AsnUnlinkType(orig);
	return idp;
erret:
    idp = IdPatFree(idp);
    goto ret;
}

/*****************************************************************************
*
*   IdPatAsnWrite(idp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL IdPatAsnWrite (IdPatPtr idp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp, numtype;
	Int2 choicenum;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, ID_PAT);
    if (atp == NULL)
        return FALSE;

	if (idp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)idp))  /* open the SEQUENCE */
        goto erret;
    av.ptrvalue = idp->country;
    if (! AsnWrite(aip, ID_PAT_country, &av)) goto erret;
    if (idp->number != NULL)
	{
		choicenum = 0;		/* number */
		av.ptrvalue = idp->number;
		numtype = ID_PAT_id_number;
	}
	else
	{
		choicenum = 1;		/* app_number */
		av.ptrvalue = idp->app_number;
		numtype = ID_PAT_id_app_number;
	}
	
    if (! AsnWriteChoice(aip, ID_PAT_id, choicenum, &av)) goto erret;
	if (! AsnWrite(aip, numtype, &av)) goto erret;

	if (idp->doc_type != NULL)
	{
		av.ptrvalue = idp->doc_type;
		if (! AsnWrite(aip, ID_PAT_doc_type, &av)) goto erret;
	}

    if (! AsnCloseStruct(aip, atp, (Pointer)idp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   Boolean IdPatMatch(a,b)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL IdPatMatch (IdPatPtr a, IdPatPtr b)
{
    if ((a == NULL) || (b == NULL))
        return FALSE;

    if (StringICmp(a->country, b->country))   /* countries must match */
        return FALSE;

    if ((a->number != NULL) && (b->number != NULL))
    {
        if (! StringICmp(a->number, b->number))
            return TRUE;
        else
            return FALSE;
    }
    else
    {
        if (! StringICmp(a->app_number, b->app_number))
            return TRUE;
        else
            return FALSE;
    }
}

/*****************************************************************************
*
*   CitGenNew()
*
*****************************************************************************/
NLM_EXTERN CitGenPtr LIBCALL CitGenNew (void)
{
	CitGenPtr cgp;

	cgp = (CitGenPtr)MemNew(sizeof(CitGen));
	if (cgp == NULL) return cgp;
    cgp->muid = -1;
	cgp->pmid = -1;
    cgp->serial_number = -1;  /* not set */
	return cgp;
}

/*****************************************************************************
*
*   CitGenFree()
*
*****************************************************************************/
NLM_EXTERN CitGenPtr LIBCALL CitGenFree (CitGenPtr cgp)
{
    if (cgp == NULL)
        return cgp;

    MemFree(cgp->cit);
    AuthListFree(cgp->authors);
    TitleFree(cgp->journal);
    MemFree(cgp->volume);
    MemFree(cgp->issue);
    MemFree(cgp->pages);
    DateFree(cgp->date);
	MemFree(cgp->title);

    return (CitGenPtr)MemFree(cgp);
}

/*****************************************************************************
*
*   CitGenAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitGenPtr LIBCALL CitGenAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitGenPtr cgp=NULL;
	DataVal av;
	AsnTypePtr atp, oldatp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return cgp;
	}

	if (aip == NULL)
		return cgp;

	if (orig == NULL)           /* CitGen ::= */
		atp = AsnReadId(aip, amp, CIT_GEN);
	else
		atp = AsnLinkType(orig, CIT_GEN);
    oldatp = atp;
    if (atp == NULL)
        return cgp;

	cgp = CitGenNew();
    if (cgp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
    {
        if (atp == NULL)
            goto erret;
        if (atp == CIT_GEN_authors)
        {
            cgp->authors = AuthListAsnRead(aip, atp);
            if (cgp->authors == NULL)
                goto erret;
        }
        else if (atp == CIT_GEN_journal)
        {
            cgp->journal = TitleAsnRead(aip, atp);
            if (cgp->journal == NULL)
                goto erret;
        }
        else if (atp == CIT_GEN_date)
        {
            cgp->date = DateAsnRead(aip, atp);
            if (cgp->date == NULL)
                goto erret;
        }
        else
        {
            if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
            if (atp == CIT_GEN_cit)
                cgp->cit = (CharPtr)av.ptrvalue;
            else if (atp == CIT_GEN_volume)
                cgp->volume = (CharPtr)av.ptrvalue;
            else if (atp == CIT_GEN_issue)
                cgp->issue = (CharPtr)av.ptrvalue;
            else if (atp == CIT_GEN_pages)
                cgp->pages = (CharPtr)av.ptrvalue;
            else if (atp == CIT_GEN_serial_number)
                cgp->serial_number = (Int2) av.intvalue;
            else if (atp == CIT_GEN_title)
                cgp->title = (CharPtr)av.ptrvalue;
            else if (atp == CIT_GEN_muid)
                cgp->muid = av.intvalue;
            else if (atp == CIT_GEN_pmid)
                cgp->pmid = av.intvalue;
        }
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return cgp;
erret:
    cgp = CitGenFree(cgp);
    goto ret;
}

/*****************************************************************************
*
*   CitGenAsnWrite(cgp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitGenAsnWrite (CitGenPtr cgp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_GEN);
    if (atp == NULL)
        return FALSE;

	if (cgp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)cgp))  /* open the SEQUENCE */
        goto erret;
    if (cgp->cit != NULL)
    {
        av.ptrvalue = cgp->cit;
        if (! AsnWrite(aip, CIT_GEN_cit, &av)) goto erret;
    }
    if (cgp->authors != NULL)
    {
        if (! AuthListAsnWrite(cgp->authors, aip, CIT_GEN_authors))
            goto erret;
    }
    if (cgp->muid >= 0)
    {
        av.intvalue = cgp->muid;
        if (! AsnWrite(aip, CIT_GEN_muid, &av)) goto erret;
    }
    if (cgp->journal != NULL)
    {
        if (! TitleAsnWrite(cgp->journal, aip, CIT_GEN_journal))
            goto erret;
    }
    if (cgp->volume != NULL)
    {
        av.ptrvalue = cgp->volume;
        if (! AsnWrite(aip, CIT_GEN_volume, &av)) goto erret;
    }
    if (cgp->issue != NULL)
    {
        av.ptrvalue = cgp->issue;
        if (! AsnWrite(aip, CIT_GEN_issue, &av)) goto erret;
    }
    if (cgp->pages != NULL)
    {
        av.ptrvalue = cgp->pages;
        if (! AsnWrite(aip, CIT_GEN_pages, &av)) goto erret;
    }
    if (cgp->date != NULL)
    {
        if (! DateAsnWrite(cgp->date, aip, CIT_GEN_date))
            goto erret;
    }
    if (cgp->serial_number >= 0)
    {
        av.intvalue = (Int4)cgp->serial_number;
        if (! AsnWrite(aip, CIT_GEN_serial_number, &av)) goto erret;
    }
    if (cgp->title != NULL)
    {
        av.ptrvalue = cgp->title;
        if (! AsnWrite(aip, CIT_GEN_title, &av)) goto erret;
    }
    if (cgp->pmid > 0)
    {
		if (aip->spec_version > 0 && aip->spec_version < 5)  /* ASN4 strip new value */
		{
			ErrPostEx(SEV_ERROR,0,0,"ASN4: PubMedId stripped");
		}
		else
		{
			av.intvalue = cgp->pmid;
			if (! AsnWrite(aip, CIT_GEN_pmid, &av)) goto erret;
		}
    }
    if (! AsnCloseStruct(aip, atp, (Pointer)cgp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   TitleFree()
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL TitleFree (ValNodePtr tp)
{
    ValNodePtr next;

    if (tp == NULL)
        return tp;

    while (tp != NULL)
    {
        MemFree(tp->data.ptrvalue);
        next = tp->next;
        MemFree(tp);
        tp = next;
    }

    return tp;
}

/*****************************************************************************
*
*   TitleAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL TitleAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	ValNodePtr first=NULL, tp;
	DataVal av;
	AsnTypePtr atp, oldatp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return first;
	}

	if (aip == NULL)
		return NULL;

	if (orig == NULL)           /* Title ::= */
		atp = AsnReadId(aip, amp, TITLE);
	else
		atp = AsnLinkType(orig, TITLE);
    oldatp = atp;
    if (atp == NULL)
        return first;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SET OF */
    while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
    {
        if (atp == NULL)
            goto erret;
        if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the CHOICE */
        atp = AsnReadId(aip, amp, atp);    /* type of CHOICE */
        if (atp == NULL)
            goto erret;
        if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
        tp = ValNodeNew(first);
        if (tp == NULL)
            goto erret;
        tp->data.ptrvalue = av.ptrvalue;
        if (first == NULL)
            first = tp;
        if (atp == TITLE_E_name)
            tp->choice = 1;
        else if (atp == TITLE_E_tsub)
            tp->choice = 2;
        else if (atp == TITLE_E_trans)
            tp->choice = 3;
        else if (atp == TITLE_E_jta)
            tp->choice = 4;
        else if (atp == TITLE_E_iso_jta)
            tp->choice = 5;
        else if (atp == TITLE_E_ml_jta)
            tp->choice = 6;
        else if (atp == TITLE_E_coden)
            tp->choice = 7;
        else if (atp == TITLE_E_issn)
            tp->choice = 8;
        else if (atp == TITLE_E_abr)
            tp->choice = 9;
        else if (atp == TITLE_E_isbn)
            tp->choice = 10;
    }
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SET OF */
ret:
	AsnUnlinkType(orig);
	return first;
erret:
    first = TitleFree(first);
    goto ret;
}

/*****************************************************************************
*
*   TitleAsnWrite(tp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL TitleAsnWrite (ValNodePtr tp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp, atp2;
	ValNodePtr oldtp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, TITLE);
    if (atp == NULL)
        return FALSE;

	if (tp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

	oldtp = tp;
    if (! AsnOpenStruct(aip, atp, (Pointer)oldtp))  /* open the SET OF */
        goto erret;
    while (tp != NULL)
    {
        av.ptrvalue = tp->data.ptrvalue;
 		switch (tp->choice)
 		{
 			case Cit_title_name:
 	            atp2 = TITLE_E_name;
 				break;
 			case Cit_title_tsub:
 	            atp2 = TITLE_E_tsub;
 				break;
 			case Cit_title_trans:
 	            atp2 = TITLE_E_trans;
 				break;
 			case Cit_title_jta:
 	            atp2 = TITLE_E_jta;
 				break;
 			case Cit_title_iso_jta:
 	            atp2 = TITLE_E_iso_jta;
 				break;
 			case Cit_title_ml_jta:
 	            atp2 = TITLE_E_ml_jta;
 				break;
 			case Cit_title_coden:
 	            atp2 = TITLE_E_coden;
 				break;
 			case Cit_title_issn:
 	            atp2 = TITLE_E_issn;
 				break;
 			case Cit_title_abr:
 	            atp2 = TITLE_E_abr;
 				break;
 			case Cit_title_isbn:
 	            atp2 = TITLE_E_isbn;
 				break;
 			default:
 				atp2 = NULL;
 				break;
 		}
 		if (atp2 != NULL)
 		{
 	        if (! AsnWriteChoice(aip, TITLE_E, (Int2)tp->choice, &av)) goto erret;
     	    if (! AsnWrite(aip, atp2, &av)) goto erret;
 		}
        tp = tp->next;
    }
    if (! AsnCloseStruct(aip, atp, (Pointer)oldtp))    /* end SET OF */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   CitSubNew()
*
*****************************************************************************/
NLM_EXTERN CitSubPtr LIBCALL CitSubNew (void)
{
	CitSubPtr csp;

	csp = (CitSubPtr)MemNew(sizeof(CitSub));
	return csp;
}

/*****************************************************************************
*
*   CitSubFree()
*
*****************************************************************************/
NLM_EXTERN CitSubPtr LIBCALL CitSubFree (CitSubPtr csp)
{
    if (csp == NULL)
        return csp;
	
	AuthListFree(csp->authors);
    ImprintFree(csp->imp);
	DateFree(csp->date);
	MemFree(csp->descr);
    return (CitSubPtr)MemFree(csp);
}

/*****************************************************************************
*
*   CitSubAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN CitSubPtr LIBCALL CitSubAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	CitSubPtr csp=NULL;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return csp;
	}

	if (aip == NULL)
		return csp;

	if (orig == NULL)           /* CitSub ::= */
		atp = AsnReadId(aip, amp, CIT_SUB);
	else
		atp = AsnLinkType(orig, CIT_SUB);
    if (atp == NULL)
        return csp;

	csp = CitSubNew();
    if (csp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    atp = AsnReadId(aip, amp, atp);   /* read authors id */
    if (atp == NULL)
        goto erret;
    csp->authors = AuthListAsnRead(aip, atp);
    if (csp->authors == NULL)
        goto erret;
    atp = AsnReadId(aip, amp, atp);         /* read imprint id */
    if (atp == NULL)
        goto erret;

	if (atp == CIT_SUB_imp)
	{
	    csp->imp = ImprintAsnRead(aip, atp);
    	if (csp->imp == NULL)
        	goto erret;
    
	    atp = AsnReadId(aip, amp, atp);
    	if (atp == NULL)
        	goto erret;
	}

	if (atp == CIT_SUB_medium)    /* nope, got medium */
	{
		if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
		csp->medium = (Uint1) av.intvalue;
		atp = AsnReadId(aip, amp, atp);
        if (atp == NULL)
            goto erret;
	}

	if (atp == CIT_SUB_date)    /* nope, got date */
	{
		csp->date = DateAsnRead(aip, atp);
		if (csp->date == NULL) goto erret;
		atp = AsnReadId(aip, amp, atp);
        if (atp == NULL)
            goto erret;
	}

	if (atp == CIT_SUB_descr)    /* nope, got descr */
	{
		if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
		csp->descr = (CharPtr) av.ptrvalue;
		atp = AsnReadId(aip, amp, atp);
        if (atp == NULL)
            goto erret;
	}

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end of SEQUENCE */
ret:
	AsnUnlinkType(orig);
	return csp;
erret:
    csp = CitSubFree(csp);
    goto ret;
}

/*****************************************************************************
*
*   CitSubAsnWrite(csp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CitSubAsnWrite (CitSubPtr csp, AsnIoPtr aip, AsnTypePtr orig)
{
	AsnTypePtr atp;
	DataVal av;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, CIT_SUB);
    if (atp == NULL)
        return FALSE;

	if (csp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)csp))  /* open the SEQUENCE */
        goto erret;
    if (! AuthListAsnWrite(csp->authors, aip, CIT_SUB_authors))
        goto erret;
	
	if (csp->imp != NULL)
	{
	    if (! ImprintAsnWrite(csp->imp, aip, CIT_SUB_imp))
    	    goto erret;
	}

	if (csp->medium)
	{
		av.intvalue = (Int4)csp->medium;
		if (! AsnWrite(aip, CIT_SUB_medium, &av)) goto erret;
	}

	if (csp->date != NULL)
	{
		if (! DateAsnWrite(csp->date, aip, CIT_SUB_date)) goto erret;
	}

	if (csp->descr != NULL)
	{
		av.ptrvalue = (Pointer)csp->descr;
		if (! AsnWrite(aip, CIT_SUB_descr, &av)) goto erret;
	}

   	if (! AsnCloseStruct(aip, atp, (Pointer)csp))    /* end SEQUENCE */
        goto erret;
    retval = TRUE;
erret:
    AsnUnlinkType(orig);
	return retval;
}

/*****************************************************************************
*
*   Int2 CitArtMatch(a, b)
*   	returns 0 = same article
*   	1,-1 = different article
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL CitArtMatch(CitArtPtr a, CitArtPtr b)
{
	Int2 retval = 1;


	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return retval;

	if (a->from != b->from)    /* different types */
	{
		if (a->from < b->from)
			return (Int2) -1;
		else
			return retval;
	}


	if (a->from == 1)   /* journal article */
		retval = CitJourMatch((CitJourPtr)a->fromptr, (CitJourPtr)b->fromptr);
	else                /* book or proceedings */
		retval = CitBookMatch((CitBookPtr)a->fromptr, (CitBookPtr)b->fromptr);



	if (retval)   /* different sources */
		return retval;


										   /* check all authors */
	retval = AuthListMatch(a->authors, b->authors, TRUE);


	if (retval)
		return retval;



	retval = TitleMatch(a->title, b->title, Cit_title_name);
	if ((retval > 1) || (retval < -1))   /* not same type or NULL */
		retval = 0;   /* matched everything else ok */



	return retval;
}

/*****************************************************************************
*
*   Int2 CitBookMatch(a, b)
*   	returns 0 = same book
*   	1,-1 = different book
*   Also does Cit-proc and Cit-let
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL CitBookMatch (CitBookPtr a, CitBookPtr b)
{
	Int2 retval = 1;



	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return retval;

	if (a->othertype > b->othertype)
		return (Int2)1;
	else if (a->othertype < b->othertype)
		return (Int2)-1;

	retval = AuthListMatch(a->authors, b->authors, TRUE);
	if (retval)
		return retval;

	retval = ImprintMatch(a->imp, b->imp, FALSE);
	if (retval)
		return retval;

	retval = TitleMatch(a->title, b->title, Cit_title_name);
	if (retval == 2)   /* not same type, it fails */
		retval = 1;
	else if (retval == -2)
		retval = -1;
	return retval;
}

/*****************************************************************************
*
*   Int2 CitGenMatch(a, b, all)
*   	returns 0 = same Cit-gen
*   	1,-1 = different Cit-gen
*   	if (all) then all fields must be shared
*   		otherwise, match only shared fields
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL CitGenMatch (CitGenPtr a, CitGenPtr b, Boolean all)
{
	Int2 retval = 0, i;
	CharPtr ap, bp;

	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return (Int2)1;

	for (i = 0; i < 5; i++)
	{
		switch (i)
		{
			case 0:
				ap = a->volume;
				bp = b->volume;
				break;
			case 1:
				ap = a->issue;
				bp = b->issue;
				break;
			case 2:
				ap = a->pages;
				bp = b->pages;
				break;
			case 3:
				ap = a->title;
				bp = b->title;
				break;
			case 4:
				ap = a->cit;
				bp = b->cit;
				break;
		}
		if ((ap != NULL) && (bp != NULL))
		{
			retval = StringICmp(ap, bp);
			if (retval > 0)
				return (Int2) 1;
			else if (retval < 0)
				return (Int2) -1;
		}
		else if (all)
		{
			if ((ap == NULL) && (bp != NULL))
				return (Int2) -1;
			else if ((ap != NULL) && (bp == NULL))
				return (Int2) 1;
		}
	}

	if (((a->authors != NULL) && (b->authors != NULL)) ||
		(((a->authors != NULL) || (b->authors != NULL)) && all))
	{
		retval = AuthListMatch(a->authors, b->authors, TRUE);
		if (retval)
			return retval;
	}

	if (((a->muid != 0) && (b->muid != 0)) || all)
	{
		if (a->muid > b->muid)
			return (Int2) 1;
		else if (a->muid < b->muid)
			return (Int2) -1;
	}

	if (((a->serial_number != -1) && (b->serial_number != -1)) || all)
	{
		if (a->serial_number > b->serial_number)
			return (Int2) 1;
		else if (a->serial_number < b->serial_number)
			return (Int2) -1;
	}

	if (((a->journal != NULL) && (b->journal != NULL)) || all)
	{
		for (i = 1; i <= 10; i++)
		{
			if (a->journal == NULL && b->journal == NULL)	/* JAK */
				break;
			retval = TitleMatch(a->journal, b->journal, (Uint1)i);
			if (! retval)  /* match */
				break;
			if ((retval == -1) || (retval == 1))  /* same type, diff journal */
				return retval;
		}
		if (retval == 2)   /* couldn't match the journal names, fail */
			return (Int2) 1;
		else if (retval == -2)
			return (Int2) -1;
	}

	if (((a->date != NULL) && (b->date != NULL)) ||
		(((a->date != NULL) || (b->date != NULL)) && all))
	{
		retval = DateMatch(a->date, b->date, TRUE);
		if (retval == 2)
			retval = 1;
		else if (retval == -2)
			retval = -1;
	}

	if (all) {							/* JAK */
		if (a->volume == NULL && b->volume == NULL &&
			a->issue == NULL && b->issue == NULL &&
			a->pages == NULL && b->pages == NULL &&
			a->title == NULL && b->title == NULL &&
			a->cit == NULL && b->cit == NULL &&
			a->authors == NULL && b->authors == NULL &&
			a->muid == -1 && b->muid == -1 &&
			a->journal == NULL && b->journal == NULL &&
			a->date == NULL && b->date == NULL &&
			a->serial_number != -1 && b->serial_number != -1) {
			return -1;
		}
	}

	return retval;
}

/*****************************************************************************
*
*   Int2 CitSubMatch(a, b)
*   	returns 0 = same submission
*   	1,-1 = different submission
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL CitSubMatch (CitSubPtr a, CitSubPtr b)
{
	Int2 retval = 1;
	DatePtr da = NULL, db = NULL;

	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return retval;

	retval = AuthListMatch(a->authors, b->authors, TRUE);
	if (retval)
		return retval;

	if (a->imp != NULL && b->imp != NULL) {
		retval = ImprintMatch(a->imp, b->imp, FALSE);
		if (retval)
			return retval;
	} else {
		if (a->imp != NULL) {
			da = a->imp->date;
		}
		if (b->imp != NULL) {
			db = b->imp->date;
		}
	}
	if (da == NULL && a->date != NULL) {
		da = a->date;
	}
	if (db == NULL && b->date != NULL) {
		db = b->date;
	}
	if (da != NULL && db != NULL) {
		retval = DateMatch(a->date, b->date, TRUE);
		if (retval == 2)
			retval = 1;
		else if (retval == -2)
			retval = -1;
	}

	return retval;
}

/*****************************************************************************
*
*   Int2 CitJourMatch(a, b)
*   	returns 0 = same journal
*   	1,-1 = different journal
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL CitJourMatch (CitJourPtr a, CitJourPtr b)
{
	Int2 retval = 1, i;
	static Uint1 journals[6] = {   /* precedence for matching journal names */
		5 ,	/* iso_jta */
		6 , /* medline_jta */
		7 , /* coden */
		8 , /* issn */
		1 , /* name */
		4 }; /* jta */


	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return retval;

	for (i = 0; i < 6; i++)
	{
		retval = TitleMatch(a->title, b->title, journals[i]);
		if (! retval)  /* match */
			break;
		if ((retval == -1) || (retval == 1))  /* same type, diff journal */
			return retval;
	}
	if (retval == 2)   /* couldn't match the journal names, fail */
		return (Int2) 1;
	else if (retval == -2)
		return (Int2) -1;

	retval = ImprintMatch(a->imp, b->imp, FALSE);
	if (retval)
		return retval;
	return retval;
}

/*****************************************************************************
*
*   Int2 AuthListMatch(a, b, all)
*   	returns 0 = same first author
*   	1,-1 = different first author
*   	if (all) all authors must match
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL AuthListMatch(AuthListPtr a, AuthListPtr b, Boolean all)
{
	Int2 retval = 1, lastnames = 0, i;
	size_t len, tlen;
	Char name[2][256];
	ValNodePtr vnp[2];
	Uint1 listtype[2];
	AuthorPtr ap;
	PersonIdPtr pip;
	NameStdPtr nsp;



	if (a == NULL)
		return (Int2) -1;
	else if (b == NULL)
		return retval;

	vnp[0] = a->names;
	vnp[1] = b->names;
	listtype[0] = a->choice;
	listtype[1] = b->choice;
	while ((vnp[0] != NULL) && (vnp[1] != NULL))
	{
		len = 32000;
		for (i = 0; i < 2; i++)
		{
			name[i][0] = '\0';
			if (listtype[i] == 1)   /* std */
			{
				ap = (AuthorPtr)(vnp[i]->data.ptrvalue);
				pip = ap->name;
				if (pip->choice == 2)   /* std */
				{
					nsp = (NameStdPtr)pip->data;
					if (nsp->names[0] != NULL) {  /* last name */
						StringMove(name[i], nsp->names[0]);
						lastnames++;
					} else if (nsp->names[3] != NULL)  /* full name */
						StringMove(name[i], nsp->names[0]);
				}
				else if (pip->choice > 2)
					StringMove(name[i], (CharPtr)pip->data);
			}
			else
				StringMove(name[i], (CharPtr)(vnp[i]->data.ptrvalue));
			tlen = StringLen(name[i]);
			if (tlen < len)
				len = tlen;
			vnp[i] = vnp[i]->next;
		}
		if (len == 0)   /* nothing in one or more names */
		{
			if (StringLen(name[0]) < tlen)
				return (Int2)-1;
			else
				return (Int2) 1;
		}
		if (lastnames == 2) {
			retval = (Int2)StringICmp(name[0], name[1]);
		} else {
			retval = (Int2)StringNICmp(name[0], name[1], tlen);
		}
		if (retval < 0)
			return (Int2) -1;
		else if (retval > 0)
			return (Int2) 1;
		if (! all)   /* first only */
			return retval;
	}

	if (vnp[0] != NULL)   /* more names in a */
		return (Int2) 1;
	else if (vnp[1] != NULL)  /* more names in b */
		return (Int2) -1;
	else
		return (Int2) 0;
}

/*****************************************************************************
*
*   Int2 TitleMatch(a,b,type)
*	   if can find two titles of "type" returns
*   	0=they match
*   	1,-1 = they don't match
*   	2,-2 = couldn't find two of "type"
*   
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL TitleMatch( ValNodePtr a, ValNodePtr b, Uint1 type)
{
	Int2 retval = 2;
	Uint1 ca=0, cb=0;
	CharPtr t1=NULL, t2=NULL;
	


	if ((a == NULL) || (b == NULL))
		return retval;

	while (a != NULL)
	{
		if (a->choice == type)
		{
			t1 = (CharPtr)(a->data.ptrvalue);
			ca = type;
			break;
		}
		else if (a->choice > ca)
			ca = a->choice;
		a = a->next;
	}

	while (b != NULL)
	{
		if (b->choice == type)
		{
			t2 = (CharPtr)(b->data.ptrvalue);
			cb = type;
			break;
		}
		else if (b->choice > cb)
			cb = b->choice;
		b = b->next;
	}

	if (ca < cb)
		retval = -2;
	else
		retval = 2;

	if ((t1 == NULL) || (t2 == NULL))
		return retval;

	retval = StringICmp(t1, t2);
	if (retval > 0)
		return (Int2) 1;
	else if (retval < 0)
		return (Int2) -1;
	else
		return retval;
}

/*****************************************************************************
*
*   Int2 ImprintMatch(a, b, all)
*   	returns 0 = same
*   	1, -1 = different
*   	if (all) then all fields in one must be in the other
*   		else, matches only fields found in both
*       NOTE: does not match on Imprint.pub or Imprint.prepub
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL ImprintMatch (ImprintPtr a, ImprintPtr b, Boolean all)
{
	Int2 retval = 1, i;
	CharPtr ap, bp;



	if ((a == NULL) || (b == NULL))
		return retval;

	retval = DateMatch(a->date, b->date, all);
	if (retval == 2)
		retval = 1;
	else if (retval == -2)
		retval = -1;
	if (retval)
		return retval;

	for (i = 0; i < 6; i++)
	{
		switch (i)
		{
			case 0:
				ap = a->volume;
				bp = b->volume;
				break;
			case 1:
				ap = a->issue;
				bp = b->issue;
				break;
			case 2:
				ap = a->pages;
				bp = b->pages;
				break;
			case 3:
				ap = a->section;
				bp = b->section;
				break;
			case 4:
				ap = a->part_sup;
				bp = b->part_sup;
				break;
			case 5:
				ap = a->language;
				bp = b->language;
				break;

		}
		if ((ap != NULL) && (bp != NULL))
		{
			retval = StringICmp(ap, bp);
			if (retval > 0)
				return (Int2) 1;
			else if (retval < 0)
				return (Int2) -1;
		}
		else if (all)
		{
			if ((ap == NULL) && (bp != NULL))
				return (Int2) -1;
			else if ((ap != NULL) && (bp == NULL))
				return (Int2) 1;
		}
	}

	if ((a->cprt != NULL) || (b->cprt != NULL))
	{
		retval = DateMatch(a->cprt, b->cprt, all);
		if (retval == 2)
			retval = 1;
		else if (retval == -2)
			retval = -1;
		if (retval)
			return retval;
	}
	return retval;
}

/*****************************************************************************
*
*   PatPriorityNew()
*
*****************************************************************************/
NLM_EXTERN PatPriorityPtr LIBCALL PatPriorityNew (void)
{
	PatPriorityPtr ppp;

	ppp = (PatPriorityPtr)MemNew(sizeof(PatPriority));
	return ppp;
}

/*****************************************************************************
*
*   PatPrioritySetFree()
*
*****************************************************************************/
NLM_EXTERN PatPriorityPtr LIBCALL PatPrioritySetFree (PatPriorityPtr ppp)
{
	PatPriorityPtr next;

	while (ppp != NULL)
	{
		next = ppp->next;
		MemFree(ppp->country);
		MemFree(ppp->number);
		DateFree(ppp->date);
		MemFree(ppp);
		ppp = next;
	}
	return ppp;
}

/*****************************************************************************
*
*   PatPrioritySetAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN PatPriorityPtr LIBCALL PatPrioritySetAsnRead (AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
{
	PatPriorityPtr ppp=NULL, head, last;
	DataVal av;
	AsnTypePtr atp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return ppp;
	}

	if ((aip == NULL) || (set == NULL) || (element == NULL))
		return ppp;


	if (! AsnReadVal(aip, set, &av)) goto erret;   /* START_STRUCT */

	atp = set;
	head = NULL;
	last = NULL;

	while ((atp = AsnReadId(aip, amp, atp)) != set)
	{
		if (atp == NULL) goto erret;
		ppp = PatPriorityNew();
    	if (ppp == NULL)
        	goto erret;

		if (head == NULL)
			head = ppp;
		else
			last->next = ppp;
		last = ppp;

	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the open SEQUENCE */
    	atp = AsnReadId(aip, amp, atp );  /* country */
	    if (atp == NULL)
    	    goto erret;
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    	ppp->country = av.ptrvalue;
	    atp = AsnReadId(aip, amp, atp );  /* number */
    	if (atp == NULL)
        	goto erret;
	    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
		ppp->number = av.ptrvalue;
	    atp = AsnReadId(aip, amp, atp );  /* date */
    	if (atp == NULL)
        	goto erret;
		ppp->date = DateAsnRead(aip, atp);
		if (ppp->date == NULL) goto erret;
		atp = AsnReadId(aip, amp, atp);   /* end struct */
		if (atp == NULL) goto erret;
		if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
	}
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret; /* end SEQUENCE OF */
ret:
	return head;
erret:
    head = PatPrioritySetFree(head);
    goto ret;
}

/*****************************************************************************
*
*   PatPrioritySetAsnWrite(ppp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL PatPrioritySetAsnWrite (PatPriorityPtr ppp, AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
{
	DataVal av;
    Boolean retval = FALSE;
	PatPriorityPtr tmp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if ((aip == NULL) || (set == NULL) || (element == NULL))
		return FALSE;

	if (ppp == NULL) { AsnNullValueMsg(aip, set); goto erret; }

    if (! AsnOpenStruct(aip, set, (Pointer)ppp))  /* open the SEQUENCE OF */
        goto erret;
	
	for (tmp = ppp; tmp != NULL; tmp = tmp->next)
	{
	    if (! AsnOpenStruct(aip, element, (Pointer)tmp))  /* open the SEQUENCE */
    	    goto erret;
	

	    av.ptrvalue = tmp->country;
	    if (! AsnWrite(aip, PATENT_PRIORITY_country, &av)) goto erret;
	    av.ptrvalue = tmp->number;
	    if (! AsnWrite(aip, PATENT_PRIORITY_number, &av)) goto erret;
		if (! DateAsnWrite(tmp->date, aip, PATENT_PRIORITY_date)) goto erret;

		if (! AsnCloseStruct(aip, element, (Pointer)tmp))
			goto erret;
	}

    if (! AsnCloseStruct(aip, set, (Pointer)ppp))    /* end SEQUENCE OF */
        goto erret;
    retval = TRUE;
erret:
	return retval;
}


/*****************************************************************************
*
*   ArticleIdNew()
*
*****************************************************************************/
NLM_EXTERN ArticleIdPtr LIBCALL ArticleIdNew (void)
{
	ArticleIdPtr aidp;

	aidp = (ArticleIdPtr)MemNew(sizeof(ArticleId));
	return aidp;
}

/*****************************************************************************
*
*   ArticleIdFree()
*
*****************************************************************************/
NLM_EXTERN ArticleIdPtr LIBCALL ArticleIdFree (ArticleIdPtr aidp)
{
	if (aidp == NULL)
		return aidp;
	
	switch (aidp->choice)
	{
		case ARTICLEID_DOI:   /* string types */
		case ARTICLEID_PII:
		case ARTICLEID_PMCPID:
		case ARTICLEID_PMPID:
			MemFree (aidp->data.ptrvalue);
			break;

		case ARTICLEID_OTHER:   /* dbtag */
			DbtagFree ((DbtagPtr)(aidp->data.ptrvalue));
			break;
		default:              /* integer types */
			break;
	}

	return (ArticleIdPtr)MemFree(aidp);
}

/*****************************************************************************
*
*   ArticleIdAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN ArticleIdPtr LIBCALL ArticleIdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	ArticleIdPtr aidp=NULL;
	AsnTypePtr atp;
        Uint1 choice = 0;
        DataVal av;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return aidp;
	}

	if (aip == NULL)
		return aidp;

	if (orig == NULL)           /* ArticleId ::= */
		atp = AsnReadId(aip, amp, ARTICLEID);
	else
		atp = AsnLinkType(orig, ARTICLEID);
    if (atp == NULL)
        return aidp;

	aidp = ArticleIdNew();
    if (aidp == NULL)
        goto erret;

    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE */
    atp = AsnReadId(aip, amp, atp);   /* read the CHOICE id */
    if (atp == NULL)
        goto erret;

    if (atp == ARTICLEID_pubmed)
	choice = 1;
    else if (atp == ARTICLEID_medline)
	choice = 2;
    else if (atp == ARTICLEID_doi)
	choice = 3;
    else if (atp == ARTICLEID_pii)
	choice = 4;
    else if (atp == ARTICLEID_pmcid)
	choice = 5;
    else if (atp == ARTICLEID_pmcpid)
        choice = 6;
    else if (atp == ARTICLEID_pmpid)
        choice = 7;
    else if (atp == ARTICLEID_other)
        choice = 8;
    else
    {
       ErrPostEx(SEV_ERROR, 0,0, "Unrecognized ArticleId");
	goto erret;
    }

    aidp->choice = choice;
    if (choice == ARTICLEID_OTHER)
    {
       aidp->data.ptrvalue = DbtagAsnRead(aip, atp);
       if (aidp->data.ptrvalue == NULL)
	  goto erret;
    }
    else
    {
       if (AsnReadVal(aip, atp, &(aidp->data)) <= 0)
          goto erret;
    }

ret:
	AsnUnlinkType(orig);
	return aidp;
erret:
    aidp = ArticleIdFree(aidp);
    goto ret;
}

/*****************************************************************************
*
*   ArticleIdAsnWrite(aidp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL ArticleIdAsnWrite (ArticleIdPtr aidp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp, atp2;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, ARTICLEID);
    if (atp == NULL)
        return FALSE;

	if (aidp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! aidp->choice)
	goto erret;

    if (! AsnWrite(aip, atp, &av))   /* write the tag */
        goto erret;

    atp2 = NULL;
    switch (aidp->choice)
    {
	case ARTICLEID_PUBMED:
          atp2 = ARTICLEID_pubmed;
          break;
	case ARTICLEID_MEDLINE:
          atp2 = ARTICLEID_medline;
          break;
	case ARTICLEID_DOI:
          atp2 = ARTICLEID_doi;
          break;
	case ARTICLEID_PII:
          atp2 = ARTICLEID_pii;
          break;
	case ARTICLEID_PMCID:
          atp2 = ARTICLEID_pmcid;
          break;
	case ARTICLEID_PMCPID:
          atp2 = ARTICLEID_pmcpid;
          break;
	case ARTICLEID_PMPID:
          atp2 = ARTICLEID_pmpid;
          break;
        case ARTICLEID_OTHER:
          atp2 = ARTICLEID_other;
          break;
	default:
          break;
    }

    if (aidp->choice == ARTICLEID_OTHER)
    {
        if (! DbtagAsnWrite((DbtagPtr)(aidp->data.ptrvalue), aip, atp2))
           goto erret;
    }
    else if (atp2 != NULL)
   {
        if (! AsnWrite(aip, atp2, &(aidp->data)))
           goto erret;
   }
   else
        goto erret;

    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}


/*****************************************************************************
*
*   PubStatusDateNew()
*
*****************************************************************************/
NLM_EXTERN PubStatusDatePtr LIBCALL PubStatusDateNew (void)
{
	PubStatusDatePtr psdp;

	psdp = (PubStatusDatePtr)MemNew(sizeof(PubStatusDate));
	return psdp;
}

/*****************************************************************************
*
*   PubStatusDateFree()
*
*****************************************************************************/
NLM_EXTERN PubStatusDatePtr LIBCALL PubStatusDateFree (PubStatusDatePtr psdp)
{
	if (psdp == NULL)
		return psdp;
        DateFree (psdp->date);
	return (PubStatusDatePtr)MemFree(psdp);
}

/*****************************************************************************
*
*   PubStatusDateAsnRead(aip, atp)
*
*****************************************************************************/
NLM_EXTERN PubStatusDatePtr LIBCALL PubStatusDateAsnRead (AsnIoPtr aip, AsnTypePtr orig)
{
	PubStatusDatePtr psdp=NULL;
	DataVal av;
	AsnTypePtr atp, oldatp;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return psdp;
	}

	if (aip == NULL)
		return psdp;

	if (orig == NULL)           /* PubStatusDate ::= */
		atp = AsnReadId(aip, amp, PUBSTATUSDATE);
	else
		atp = AsnLinkType(orig, PUBSTATUSDATE);
    if (atp == NULL)
        return psdp;

	psdp = PubStatusDateNew();
    if (psdp == NULL)
        goto erret;

    oldatp = atp;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret; /* read the START STRUCT */
    atp = AsnReadId(aip, amp, atp);   /* read the PubStatus */
    if (atp == NULL)
        goto erret;
    if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
    psdp->pubstatus = (Uint1)(av.intvalue);
    atp = AsnReadId(aip, amp, atp);
    if (atp == NULL) goto erret;
    psdp->date = DateAsnRead(aip, atp);
    if (psdp->date == NULL)
       goto erret;
    atp = AsnReadId(aip, amp, atp);
    if (atp == NULL) goto erret;
    if (AsnReadVal(aip, oldatp, &av) <= 0) goto erret;    /* read the END STRUCT */

ret:
	AsnUnlinkType(orig);
	return psdp;
erret:
    psdp = PubStatusDateFree(psdp);
    goto ret;
}

/*****************************************************************************
*
*   PubStatusDateAsnWrite(psdp, aip, atp)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL PubStatusDateAsnWrite (PubStatusDatePtr psdp, AsnIoPtr aip, AsnTypePtr orig)
{
	DataVal av;
	AsnTypePtr atp;
    Boolean retval = FALSE;

	if (! loaded)
	{
		if (! BiblioAsnLoad())
			return FALSE;
	}

	if (aip == NULL)
		return FALSE;

	atp = AsnLinkType(orig, PUBSTATUSDATE);
    if (atp == NULL)
        return FALSE;

	if (psdp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }

    if (! AsnOpenStruct(aip, atp, (Pointer)psdp))
        goto erret;

    av.intvalue = (Int4)(psdp->pubstatus);
    if (! AsnWrite(aip, PUBSTATUSDATE_pubstatus, &av)) goto erret;
    if (! DateAsnWrite(psdp->date, aip, PUBSTATUSDATE_date)) goto erret;

    if (! AsnCloseStruct(aip, atp, (Pointer)psdp)) goto erret;

    retval = TRUE;
erret:
	AsnUnlinkType(orig);
	return retval;
}