File: dlg_sdts.c

package info (click to toggle)
drawmap 2.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,176 kB
  • ctags: 681
  • sloc: ansic: 13,087; makefile: 90; sh: 38
file content (5477 lines) | stat: -rw-r--r-- 181,435 bytes parent folder | download | duplicates (3)
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
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
/*
 * =========================================================================
 * dlg_sdts.c - Routines to handle DLG data from SDTS files.
 * Copyright (c) 2000,2001  Fred M. Erickson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * =========================================================================
 */


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include "drawmap.h"
#include "dlg.h"
#include "sdts_utils.h"


/*
 * The routines in this file are uniquely-dedicated to handling
 * DLG files in the Spatial Data Transfer System (SDTS) format.
 *
 * For a general description of SDTS, see sdts_utils.c.
 */



/*
 * Note to the reader of this code.  This code will probably be difficult
 * to understand unless you are very familiar with the internals of SDTS files
 * and optional-format DLG-3 files.  Normally I would provide a lot of descriptive
 * comments to help you along.  However, in this case, such comments would
 * probably end up being several times the length of the code.  I wrote this
 * program with two large documents available for reference.  If you want to
 * follow the operation of the code, you will probably need those documents
 * too.  The documents were:
 *
 * DLG-3 SDTS Transfer Description, May 7, 1996
 *
 * Standards for Digital Line Graphs, September 1999,
 * Department of the Interior, US Geological Survey,
 * National Mapping Division.
 *
 * There are comments at key points in the code, but they are not adequate
 * for a full understanding unless you have the reference materials at hand.
 *
 * Even the documents aren't really enough.  It is also useful to have
 * both sample SDTS files and sample optional-format DLG-3 files for reference as well.
 */



/*
 * The code that processes DLG files is very spaghetti-like, since
 * it got squeezed and twisted and stretched while I figured out how
 * DLG files are put together.
 *
 * Because of this, and because I don't like to write functions that
 * take 35 arguments, there are a lot of global variables used by the
 * DLG code.  Most of them are accumulated here.
 *
 * Many global variables are already allocated by dlg.c.
 * We re-use them via extern directives.
 */


/*
 * Storage for attribute types.
 */
extern long num_A_attrib;
extern long num_L_attrib;
extern struct maj_min attributes_A[MAX_A_ATTRIB];
extern struct maj_min attributes_L[MAX_L_ATTRIB];

/*
 * The sizes of the nodes, areas, and lines arrays are their theoretical maximum values.
 * It would probably be cooler to malloc() these as we go, but coolness was not an
 * objective of this program.  It would still be cool to read the maximum values from
 * the DLG file headers and check them against the values below to verify that
 * the standards haven't changed and left this program behind.
 */
extern struct nodes nodes[MAX_NODES];
extern struct areas areas[MAX_AREAS];
extern struct lines lines[MAX_LINES];


/*
 * Arrays to keep track of attributes from various SDTS files.
 */
static struct polygon_attrib polygon_attrib[MAX_POLY_NUM];
static struct attrib_files  {
	char module_name[4];
	long num_attrib;
	struct attribute_list *attrib;
} attrib_files[MAX_ATTRIB_FILES];

/*
 * Array for building line lists for output
 */
static long line_list[MAX_LINE_LIST];


/*
 * comparison function for use with qsort.
 */
static int
compare_lines(const void *lines1, const void *lines2)
{
	if (((struct lines *)lines1)->id < ((struct lines *)lines2)->id)  {
		return -1;
	}
	else if (((struct lines *)lines1)->id > ((struct lines *)lines2)->id)  {
		return 1;
	}
	else  {
		return 0;
	}
}


extern double lat_se, long_se, lat_sw, long_sw, lat_ne, long_ne, lat_nw, long_nw;
extern long dlg_x_low, dlg_y_low, dlg_x_high, dlg_y_high;
extern long x_prime;

extern long utm_zone;



long get_extra_attrib(long, long *major, long *minor, long *major2, long *minor2, struct subfield *subfield);
long process_attrib_sdts(char *, char *, long *, long *, long, long);
void uniq_attrib(struct attribute **, short *);
void get_theme(char *, char *, long, long);


/*
 * This routine parses informational data from the various SDTS files
 * comprising a DLG file set and inserts the converted data into
 * internal variables.
 * If you haven't read the DLG file guide and looked at a
 * DLG file, this code will probably be incomprehensible.
 *
 * Here are the meanings of the various module names associated with DLG files:
 *
 * There is one module associated with Identification:
 *   IDEN --- Identification
 *
 * Misc:
 *   STAT --- Transfer Statistics
 *   CATD --- Catalog/Directory
 *   CATS --- Catalog/Spatial Domain
 *   CATX --- Catalog/Cross-Reference
 *
 * There are five modules involved in data quality:
 *   DQHL --- Data Quality/Lineage
 *   DQPA --- Data Quality/Positional Accuracy
 *   DQAA --- Data Quality/Attribute Accuracy
 *   DQCG --- Data Quality/Completeness
 *   DQLC --- Data Quality/Logical Consistency
 *
 * There are three data dictionary modules:
 *   DDSH --- Data Dictionary/Schema
 *   MDEF --- Data Dictionary/Definition	(Part of Master Data Dictionary)
 *   MDOM --- Data Dictionary/Domain		(Part of Master Data Dictionary)
 *
 * There are three modules associated with spatial reference and domain:
 *   XREF --- External Spatial Reference
 *   IREF --- Internal Spatial Reference
 *
 * Files associated with data:
 *   AHDR --- Attribute Primary Header
 *   A??F --- main Attribute Primary
 *   ACOI --- Coincidence Attribute Primary
 *   ABDM --- Agencies for Boundaries
 *   ARDM --- Route Numbers for Roads and Trails
 *   AHPR --- Elevation in meters for Hypsography
 *   B??? --- Secondary attribute files (only used for 2,000,000-scale transfers)
 *   FF01 --- Composite Surfaces
 *   LE01 --- Line (Chain of Points)
 *   NA01 --- Point-Node (Area Points --- Representative points within defined areas.)
 *   NE01 --- Point-Node (Entity Points --- The location of point features like buildings and towers.)
 *   NO01 --- Point-Node (Planar Node --- The junction of two or more lines.)
 *   NP01 --- Point-Node (Registration Points --- This generally defines the four corner points of the data in UTM.
 *                        The same data is available in AHDR in the form of latitude/longitude.)
 *   PC01 --- Polygon
 */
long
process_dlg_sdts(char *passed_file_name, char *output_file_name, long gz_flag,
		struct image_corners *image_corners, long info_flag, long file_image_flag)
{
	long i, j, k, l, ret_val;
	long start_node, current_node;
	long number_of_islands;
	long module_num;
	long layer;
	long count;
	long color;
	long d, m;
	double s;
	double x, y;
	char code1, code2;
	char output_file[12];
	int output_fdesc;
	char *ptr;
	char buf[DLG_RECORD_LENGTH + 1];
	char buf3[DLG_RECORD_LENGTH + 1];
	struct point **current_point;
	struct point **current_point2;
	struct point *tmp_point;
	struct attribute **current_attrib;
	struct attribute **current_attrib2;
	struct attribute *tmp_attrib;
	long attrib;
	long current_poly;
	long num_polys;
	long num_areas = -1;
	long num_lines = -1;
	long num_nodes = -1;
	long num_NO_nodes;
	long data_type = 0;
	double latitude1, longitude1, latitude2, longitude2;
	ssize_t (* read_function)(int, void *, size_t);
	long plane_ref;
	char save_byte;
	int fdesc;
	long file_name_length;
	char file_name[MAX_FILE_NAME + 1];
	long byte_order;
	long upper_case_flag;
	long need;
	double x_scale_factor, y_scale_factor;
	double x_origin, y_origin;
	double x_resolution, y_resolution;
	char postal_code[30];
	char attribute_file[9];
	long num_attribs;
	long num_attrib_files;
	struct subfield subfield;
	char source_date[4];
	char sectional_indicator[3];
	char category_name[21];
	long vertical_datum;
	long horizontal_datum;
	long dlg_level;
	long line_list_size;
	double se_x, se_y, sw_x, sw_y, nw_x, nw_y, ne_x, ne_y;
	long major;
	long minor;
	struct datum datum;
	long record_id;


	if (file_image_flag == 0)  {
		x_prime = image_corners->x + LEFT_BORDER + right_border;
	}


	/* find the native byte-order on this machine. */
	byte_order = swab_type();


	/*
	 * Make a copy of the file name.  The one we were originally
	 * given is still stored in the command line arguments.
	 * It is probably a good idea not to alter those, lest we
	 * scribble something we don't want to scribble.
	 */
	file_name_length = strlen(passed_file_name);
	if (file_name_length > MAX_FILE_NAME)  {
		fprintf(stderr, "File name is too long.\n");
		return 1;
	}
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	file_name[MAX_FILE_NAME] = '\0';
	if (file_name_length < 12)  {
		/*
		 * Excluding the initial path, the file name should have the form
		 * ????LE??.DDF, perhaps with a ".gz" on the end.  If it isn't
		 * at least long enough to have this form, then reject it.
		 */
		fprintf(stderr, "File name doesn't look right.\n");
		return 1;
	}
	/* Check the case of the characters in the file name by examining a single character. */
	if (gz_flag == 0)  {
		if (file_name[file_name_length - 1] == 'f')  {
			upper_case_flag = 0;
		}
		else  {
			upper_case_flag = 1;
		}
	}
	else  {
		if (file_name[file_name_length - 4] == 'f')  {
			upper_case_flag = 0;
		}
		else  {
			upper_case_flag = 1;
		}
	}



	/*
	 * The first file name we need is the Attribute Primary Header (AHDR) module, which contains
	 * the latitude/longitude registration points of the four corners of the data.
	 *
	 * There is a lot of stuff in AHDR that goes into record 3.
	 * Thus we provide an extra record buffer so that we can build
	 * record 3 while processing AHDR.  The buffer is buf3[], and
	 * before we start we fill it with blanks.
	 *
	 * Note:  We fill buffer 3 even when file_image_flag == 0,
	 * since it doesn't take much processing, and that way we
	 * don't have to clutter up the code with "if" statements.
	 */
	for (i = 0; i < DLG_RECORD_LENGTH; i++)  {
		buf3[i] = ' ';
	}
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "ahdr.ddf", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "ahdr.ddf", 8);
		}
	}
	else  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "AHDR.DDF", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "AHDR.DDF", 8);
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * We also need the layer number from the file name.
	 * Some SDTS transfers have multiple LE files, and we need
	 * to pull the correct record out of the AHDR file.
	 *
	 * In theory, we should probably pull this out of the FF01
	 * module, but this would be a lot of work to get the same
	 * information.  As long as USGS files are named with the
	 * layer number in the file name, this approach is more efficient.
	 */
	if (gz_flag != 0)  {
		layer = strtol(&passed_file_name[file_name_length - 9], (char **)0, 10);
	}
	else  {
		layer = strtol(&passed_file_name[file_name_length - 6], (char **)0, 10);
	}
	if (layer <= 0)  {
		fprintf(stderr, "Got bad layer number (%d) from file %s.\n", layer, passed_file_name);
		return 1;
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	need = 25;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "ATPR") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strncmp(subfield.label, "RCID", 4) == 0))  {
				/*
				 * Check for the correct layer.
				 * set layer = -1 as a flag if you find it.
				 */
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				if (layer == strtol(subfield.value, (char **)0, 10))  {
					layer = -1;
				}
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if ((layer < 0) && (strcmp(subfield.tag, "ATTP") == 0))  {
			save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
			if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "SW_LATITUDE", 11) == 0))  {
				lat_sw = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "SW_LONGITUDE", 12) == 0))  {
				long_sw = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "NW_LATITUDE", 11) == 0))  {
				lat_nw = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "NW_LONGITUDE", 12) == 0))  {
				long_nw = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "NE_LATITUDE", 11) == 0))  {
				lat_ne = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "NE_LONGITUDE", 12) == 0))  {
				long_ne = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "SE_LATITUDE", 11) == 0))  {
				lat_se = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "SE_LONGITUDE", 12) == 0))  {
				long_se = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "BANNER", 6) == 0))  {
				strncpy(buf, subfield.value, subfield.length);
				for (i = subfield.length; i < DLG_RECORD_LENGTH; i++)  {
					buf[i] = ' ';
				}
				buf[DLG_RECORD_LENGTH] = '\0';
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "SOURCE_DATE", 11) == 0))  {
				/*
				 * Copy the original source date for later use.
				 */
				if (subfield.length == 4)  {
					strncpy(source_date, subfield.value, 4);
				}
				else  {
					strncpy(source_date, "    ", 4);
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "QUAD_NUMBER", 11) == 0))  {
				/*
				 * Copy the sectional indicator for later use.
				 */
				if (subfield.length == 3)  {
					strncpy(sectional_indicator, subfield.value, 3);
				}
				else  {
					strncpy(sectional_indicator, "   ", 3);
				}
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "L_PRIM_INTERVAL", 15) == 0))  {
				/*
				 * Put the largest primary interval into buf3.
				 * Append the comma separator.
				 *
				 * Note:  SDTS stores this in a 5-byte field Real subfield
				 * but there are only 4 bytes reserved in the DLG-3 header.
				 * The discrepancy arises from the fact that the value is
				 * stored in the header as a three-digit integer (the integer
				 * part of the 5-byte Real), followed by a single digit
				 * describing the units (which for 24K and 100K DLG files
				 * is always 2, for meters).
				 */
				if ((subfield.length == 5) && (strncmp(subfield.value, "     ", 5) != 0))  {
					i = (long)strtod(subfield.value, (char **)0);
					sprintf(&buf3[41], "%3d2,", i);
				}
				else  {
					strncpy(&buf3[41], "     ", 5);
				}
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "L_PB_INTERVAL", 13) == 0))  {
				/*
				 * Put the largest primary bathymetric interval into buf3.
				 * Append the single-space filler.
				 *
				 * Note:  SDTS stores this in a 5-byte field Real subfield
				 * but there are only 4 bytes reserved in the DLG-3 header.
				 * The discrepancy arises from the fact that the value is
				 * stored in the header as a three-digit integer (the integer
				 * part of the 5-byte Real), followed by a single digit
				 * describing the units (which for 24K and 100K DLG files
				 * is always 2, for meters).
				 */
				if ((subfield.length == 5) && (strncmp(subfield.value, "     ", 5) != 0))  {
					i = (long)strtod(subfield.value, (char **)0);
					sprintf(&buf3[46], "%3d2 ", i);
				}
				else  {
					strncpy(&buf3[46], "     ", 5);
				}
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "S_PRIM_INTERVAL", 15) == 0))  {
				/*
				 * Put the smallest primary interval into buf3.
				 * Append the comma separator.
				 *
				 * Note:  SDTS stores this in a 5-byte field Real subfield
				 * but there are only 4 bytes reserved in the DLG-3 header.
				 * The discrepancy arises from the fact that the value is
				 * stored in the header as a three-digit integer (the integer
				 * part of the 5-byte Real), followed by a single digit
				 * describing the units (which for 24K and 100K DLG files
				 * is always 2, for meters).
				 */
				if ((subfield.length == 5) && (strncmp(subfield.value, "     ", 5) != 0))  {
					i = (long)strtod(subfield.value, (char **)0);
					sprintf(&buf3[51], "%3d2,", i);
				}
				else  {
					strncpy(&buf3[51], "     ", 5);
				}
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "S_PB_INTERVAL", 13) == 0))  {
				/*
				 * Put the smallest primary bathymetric interval into buf3.
				 *
				 * Note:  SDTS stores this in a 5-byte field Real subfield
				 * but there are only 4 bytes reserved in the DLG-3 header.
				 * The discrepancy arises from the fact that the value is
				 * stored in the header as a three-digit integer (the integer
				 * part of the 5-byte Real), followed by a single digit
				 * describing the units (which for 24K and 100K DLG files
				 * is always 2, for meters).
				 */
				if ((subfield.length == 5) && (strncmp(subfield.value, "     ", 5) != 0))  {
					i = (long)strtod(subfield.value, (char **)0);
					sprintf(&buf3[56], "%3d2", i);
				}
				else  {
					strncpy(&buf3[56], "    ", 4);
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "CODED_FLAG", 10) == 0))  {
				/*
				 * Copy the coded flag into buf3.
				 * It is preceded by three bytes reserved for future use as coded flags.
				 * These 3 reserved bytes normally appear to be stored as null characters in USGS DLG-3
				 * files.  We will do the same, although it is ugly.
				 */
				if (subfield.length == 1)  {
					buf3[63] = subfield.value[0];
				}
				else  {
					buf3[63] = ' ';
				}
				buf3[60] = '\0';
				buf3[61] = '\0';
				buf3[62] = '\0';
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGEWS", 6) == 0))  {
				/*
				 * Copy the west-edge status flag into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[64] = subfield.value[0];
				}
				else  {
					buf3[64] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGEWR", 6) == 0))  {
				/*
				 * Copy the west-edge status flag reason into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[65] = subfield.value[0];
				}
				else  {
					buf3[65] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGENS", 6) == 0))  {
				/*
				 * Copy the north-edge status flag into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[66] = subfield.value[0];
				}
				else  {
					buf3[66] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGENR", 6) == 0))  {
				/*
				 * Copy the north-edge status flag reason into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[67] = subfield.value[0];
				}
				else  {
					buf3[67] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGEES", 6) == 0))  {
				/*
				 * Copy the east-edge status flag into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[68] = subfield.value[0];
				}
				else  {
					buf3[68] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGEER", 6) == 0))  {
				/*
				 * Copy the east-edge status flag reason into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[69] = subfield.value[0];
				}
				else  {
					buf3[69] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGESS", 6) == 0))  {
				/*
				 * Copy the south-edge status flag into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[70] = subfield.value[0];
				}
				else  {
					buf3[70] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "EDGESR", 6) == 0))  {
				/*
				 * Copy the south-edge status flag reason into buf3.
				 */
				if (subfield.length == 1)  {
					buf3[71] = subfield.value[0];
				}
				else  {
					buf3[71] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "VERTICAL_DATUM", 14) == 0))  {
				/*
				 * Convert and save the vertical datum for later use.
				 */
				if (strncmp(subfield.value, "NGVD", (subfield.length > 4 ? 4 : subfield.length)) == 0)  {
					vertical_datum = 0;
				}
				else if (strncmp(subfield.value, "NAVD", (subfield.length > 4 ? 4 : subfield.length)) == 0)  {
					vertical_datum = 1;
				}
				else if (strncmp(subfield.value, "LOCAL MEAN SEA LEVEL", subfield.length) == 0)  {
					vertical_datum = 2;
				}
				else  {
					vertical_datum = -1;
				}
				need--;
			}
			subfield.value[subfield.length] = save_byte;
			if (need == 0)  {
				/* This is all we need.  Break out of the loop. */
				break;
			}
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();
	/* Check that we found what we wanted. */
	if (need > 0)  {
		fprintf(stderr, "Failed to get needed data from file %s.\n", file_name);
		return 1;
	}
	else if (need < 0)  {
		fprintf(stderr, "Warning:  Got more data from file %s than expected.\n", file_name);
	}


	/*
	 * We now have enough information to open the output file.
	 *
	 * file_image_flag controls whether this routine writes to a file (file_image_flag != 0)
	 * or writes to the drawmap image buffer (file_image_flag == 0).  This is ugly, but
	 * it lets us maintain only a single version of this SDTS parsing code.
	 */
	if (file_image_flag != 0)  {
		if (output_file_name != (char *)0)  {
			if ((output_fdesc = open(output_file_name, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)  {
				fprintf(stderr, "Can't create %s for writing, errno = %d\n", output_file_name, errno);
				exit(0);
			}
		}
		else  {
			code1 = 'a' + floor((fabs(lat_se) + (lat_se < 0 ? -1.0 : 1.0) * 0.02 - floor(fabs(lat_se) + (lat_se < 0 ? -1.0 : 1.0) * 0.02)) * 8.0);
			code2 = '1' + floor((fabs(long_se) + (long_se < 0 ? 1.0 : -1.0) * 0.02 - floor(fabs(long_se) + (long_se < 0 ? 1.0 : -1.0) * 0.02)) * 8.0);
			sprintf(output_file, "%02.2d%03.3d%c%c.dlg",
				(int)(fabs(lat_se) + (lat_se < 0 ? -1.0 : 1.0) * 0.02),
				(int)(fabs(long_se) + (long_se < 0 ? 1.0 : -1.0) * 0.02),
				code1, code2);
			if ((output_fdesc = open(output_file, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)  {
				fprintf(stderr, "Can't create %s for writing, errno = %d\n", output_file, errno);
				exit(0);
			}
		}
		/*
		 * The first record of the file is simply the banner.
		 * We stored the banner in buf[].  Write it out.
		 */
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
	}



	/*
	 * The next file we need is the IDEN module, which contains the official DLG
	 * name.  The name is normally followed (in SDTS) by " / " and a category name (theme),
	 * such as HYDROGRAPHY.  We need to strip off this extra category bit, since it isn't
	 * normally part of an optional-format record 1.  However, we also need to save
	 * it because it shows up at the beginning of record 15.  We also want to
	 * extract the postal code if the user has given the info_flag.
	 *
	 * Here is a typical SDTS TITL subfield:
	 *
	 * ALZADA, MT-SD-WY / BOUNDARIES
	 */
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "iden.ddf", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "iden.ddf", 8);
		}
	}
	else  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "IDEN.DDF", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "IDEN.DDF", 8);
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	need = 3;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "IDEN") == 0)  {
			if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "TITL", 4) == 0))  {
				for (i = 0; i < subfield.length; i++)  { 
					if (subfield.value[i] == '/')  { 
						j = i;
						break;
					}
				}
				if (i < subfield.length)  {
					while ((i - 1) >= 0)  {
						i--;
						if (subfield.value[i] != ' ')  {
							i++;
							break;
						}
					}
				}
				if (info_flag != 0)  {
					/*
					 * Grab the postal code.
					 */
					for (k = 0; k < i; k++)  {
						if (subfield.value[k] == ',')  {
							break;
						}
					}
					if (k < i)  {
						k++;
						for ( ; k < i; k++)  {
							if (k != ' ')  {
								break;
							}
						}
						strncpy(postal_code, &subfield.value[k], (i - k + 1) > 29 ? 29 : (i - k + 1));
						postal_code[(i - k + 1) > 29 ? 29 : (i - k + 1)] = '\0';
					}
					else  {
						postal_code[0] = 0;
					}
				}
				/*
				 * Copy the official DLG name into the beginning of the
				 * record buffer.
				 * It is followed by a 1-byte space filler.
				 */
				if (i > 40)  {
					/* Truncate the name if it is too long.  It shouldn't be. */
					i = 40;
				}
				strncpy(buf, subfield.value, i);
				for ( ; i < 41; i++)  {
					buf[i] = ' ';
				}
				j++;
				while (j < subfield.length)  {
					if (subfield.value[j] != ' ')  {
						break;
					}
					j++;
				}
				if (j != subfield.length)  {
					if ((subfield.length - j) <= 20)  {
						strncpy(category_name, &subfield.value[j], subfield.length - j);
						category_name[subfield.length - j] = '\0';
					}
					else  {
						strncpy(category_name, &subfield.value[j], 20);
						category_name[20] = '\0';
					}
				}
				else  {
					category_name[0] = '\0';
				}
				for (i = strlen(category_name) - 1; i >= 0; i--)  {
					if (category_name[i] != ' ')  {
						category_name[i + 1] = '\0';
						break;
					}
				}
				if (i == -1)  {
					category_name[0] = '\0';
				}
				need--;
			}
			else if ((strstr(subfield.format, "I") != (char *)0) && (strncmp(subfield.label, "SCAL", 4) == 0))  {
				/*
				 * Copy the source material scale into the
				 * record buffer.
				 * It is followed by space filler out to byte 63.
				 */
				if (subfield.length >= 8)  {
					strncpy(&buf[52], subfield.value, 8);
				}
				else  {
					strncpy(&buf[52], "        ", 8);
					strncpy(&buf[52 + 8 - subfield.length], subfield.value, subfield.length);
				}
				for (i = 60; i < 63; i++)  {
					buf[i] = ' ';
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "DAST", 4) == 0))  {
				/*
				 * Save the DLG level for later use.
				 */
				if (subfield.length == 5)  {
					dlg_level = subfield.value[4] - '0';
				}
				else  {
					dlg_level = -1;
				}
				if ((dlg_level != 3) && (dlg_level != 2))  {
					fprintf(stderr, "Warning:  This does not appear to be a level 2 or 3 DLG.\n");
				}
				need--;
			}
		}
		if (need == 0)  {
			/* This is all we need.  Break out of the loop. */
			break;
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();
	/* Check that we found what we wanted. */
	if (need > 0)  {
		fprintf(stderr, "Failed to get needed data from file %s.\n", file_name);
		return 1;
	}
	else if (need < 0)  {
		fprintf(stderr, "Warning:  Got more data from file %s than expected.\n", file_name);
	}
	if (file_image_flag != 0)  {
		/*
		 * Copy the source date into the buffer.
		 * It is followed by blank filler out to byte 52.
		 */
		strncpy(&buf[41], source_date, 4);
		buf[45] = ',';
		for (i = 46; i < 52; i++)  {
			buf[i] = ' ';
		}
		/*
		 * Copy the sectional indicator into the buffer.
		 * It is followed by blank filler out to the end of the record.
		 */
		strncpy(&buf[63], sectional_indicator, 3);
		for (i = 66; i < DLG_RECORD_LENGTH; i++)  {
			buf[i] = ' ';
		}
		/*
		 * The second record of the file is set up.
		 * Write it out.
		 *
		 * Also, we completely built record 3 while processing the ADHR
		 * module.  Write it out as well.
		 */
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		if (write(output_fdesc, buf3, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
	}


	
	/*
	 * As a "just-in-case" measure, we grabbed the category name (theme) from
	 * the DLG name above.  This would be fine, for everything except TRANSPORTATION.
	 * In TRANSPORTATION files, the name we grabbed above will be "TRANSPORTATION".
	 * However, what we really need is one of the three themes:  "ROADS AND TRAILS",
	 * "RAILROADS", or "PIPE & TRANS LINES".  In order to get this information,
	 * we have at least two possible routes of attack.  We can try to deduce it from
	 * the types of attributes referenced in the Node/Area/Lines records.  Or, we
	 * can read the CATS module and try to get the THEM subfield (which stands for theme)
	 * for our passed_file_name.  We take the latter course here, but we will also
	 * try the other approach inside the process_attrib_sdts() function.
	 */
	get_theme(passed_file_name, category_name, upper_case_flag, gz_flag);



	if (info_flag != 0)  {
		/* If info_flag is nonzero, then all we need to do is print some info and return. */
		for (i = 0; i < 40; i++)  {
			/* We want only that part of the official name up to the comma. */
			if (buf[i] == ',')  {
				break;
			}
		}
		fprintf(stdout, "\t%.*s\t%s\t%.20s\t%g:%g:%g:%g\n", i, buf, postal_code, category_name, lat_se, long_se, lat_nw, long_nw);
		return 0;
	}



	/*
	 * Within the DLG data, locations are specified with pairs of
	 * Universal Transverse Mercator (x,y) coordinates.
	 *
	 * The header information for the DLG data gives 4 reference
	 * points for the corners of the polygon represented by the DLG data.  Here is a
	 * typical set of them:
	 *
	 *	SW       45.750000 -112.000000         422218.03  5066539.80                    
	 *	NW       46.000000 -112.000000         422565.07  5094315.16                    
	 *	NE       46.000000 -111.750000         441923.83  5094103.38                    
	 *	SE       45.750000 -111.750000         441663.14  5066327.07                    
	 *
	 * Note that the latitude-longitude points form a square area in latitude/longitude
	 * space (if latitudes and longitudes on a pseudo-sphere can ever be thought of as
	 * forming a square).  The UTM (x,y) grid coordinates, however, form a quadrilateral
	 * in which no two sides have the same length.
	 *
	 * If file_image_flag == 0, then we are supposed to be writing to the image buffer.
	 * There is no point in this if the data won't affect the image.
	 * Do a quick check here to find out if the data is off the map boundaries.
	 * If so, then we can return now and save a lot of work.
	 */
	if (file_image_flag == 0)  {
		if ((lat_sw > image_corners->ne_lat) ||
		    (long_sw > image_corners->ne_long) ||
		    (lat_ne < image_corners->sw_lat) ||
		    (long_ne < image_corners->sw_long))  {
			return 0;
		}
	}


	num_attrib_files = process_attrib_sdts(passed_file_name, category_name, &data_type, &color, gz_flag, upper_case_flag);



	/*
	 * The next file name we need is the XREF module, which contains
	 * the Planar Reference System, Horizontal Datum, and UTM Zone.
	 */
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "xref.ddf", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "xref.ddf", 8);
		}
	}
	else  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "XREF.DDF", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "XREF.DDF", 8);
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	need = 3;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "XREF") == 0)  {
			if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "HDAT") == 0))  {
				/*
				 * Valid choices are "NAS" for NAD-27, "NAX" for NAD-83,
				 * "Puerto Rico", "Old Hawaiian", and "Local (Astro)".
				 */
				if (strncmp(subfield.value, "NAS", subfield.length) == 0)  {
					horizontal_datum = 0;
				}
				else if (strncmp(subfield.value, "NAX", subfield.length) == 0)  {
					horizontal_datum = 1;
				}
				else if (strncmp(subfield.value, "Puerto Rico", subfield.length) == 0)  {
					horizontal_datum = 2;
				}
				else if (strncmp(subfield.value, "Old Hawaiian", subfield.length) == 0)  {
					horizontal_datum = 3;
				}
				else if (strncmp(subfield.value, "Local (Astro)", subfield.length) == 0)  {
					horizontal_datum = 4;
				}
				else  {
					horizontal_datum = -1;
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "RSNM") == 0))  {
				/*
				 * Valid choices are "UTM" and "GEO"
				 */
				if (strncmp(subfield.value, "GEO", subfield.length) == 0)  {
					plane_ref = 0;
				}
				else if (strncmp(subfield.value, "UTM", subfield.length) == 0)  {
					plane_ref = 1;
				}
				else  {
					plane_ref = -1;
				}
				need--;
			}
			else if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "ZONE") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				utm_zone = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
				need--;
			}
			if (need == 0)  {
				/* This is all we need.  Break out of the loop. */
				break;
			}
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();
	/* Check that we found what we wanted. */
	if (need > 0)  {
		fprintf(stderr, "Failed to get needed data from file %s.\n", file_name);
		return 1;
	}
	else if (need < 0)  {
		fprintf(stderr, "Warning:  Got more data from file %s than expected.\n", file_name);
	}

	/* Check the just-acquired results. */
	if (plane_ref != 1)  {
		fprintf(stderr, "DLG file does not use UTM ground planimetric coordinates.  (Plane_ref = %d)\n", plane_ref);
		exit(0);
	}
	if ((utm_zone < 1) || (utm_zone > 60))  {
		fprintf(stderr, "DLG file contains bad UTM zone %d.\n", utm_zone);
		exit(0);
	}
	if (horizontal_datum == 0)  {
		/*
		 * The file uses the NAD-27 datum.
		 * Initialize the datum parameters.
		 */
		datum.a = NAD27_SEMIMAJOR;
		datum.b = NAD27_SEMIMINOR;
		datum.e_2 = NAD27_E_SQUARED;
		datum.f_inv = NAD27_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = NAD27_A0;
		datum.a2 = NAD27_A2;
		datum.a4 = NAD27_A4;
		datum.a6 = NAD27_A6;
	}
	else if (horizontal_datum == 1)  {
		/*
		 * The file uses the NAD-83 datum.
		 * Initialize the datum parameters.
		 */
		datum.a = NAD83_SEMIMAJOR;
		datum.b = NAD83_SEMIMINOR;
		datum.e_2 = NAD83_E_SQUARED;
		datum.f_inv = NAD83_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = NAD83_A0;
		datum.a2 = NAD83_A2;
		datum.a4 = NAD83_A4;
		datum.a6 = NAD83_A6;
	}
	else  {
		/*
		 * We don't currently handle any other datums.
		 * Default to tne NAD-27 datum.
		 */
		datum.a = NAD27_SEMIMAJOR;
		datum.b = NAD27_SEMIMINOR;
		datum.e_2 = NAD27_E_SQUARED;
		datum.f_inv = NAD27_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = NAD27_A0;
		datum.a2 = NAD27_A2;
		datum.a4 = NAD27_A4;
		datum.a6 = NAD27_A6;

		fprintf(stderr, "DLG file uses a horizontal datum that I don't handle (%d).\n", horizontal_datum);
		fprintf(stderr, "Defaulting to NAD-27.  This may result in positional errors in the data.\n");
	}



	/*
	 * The next file name we need is the IREF module, which contains
	 * the x and y scale factors that are multiplied times the x and y UTM coordinate values,
	 * the x and y origins, which are added to the UTM coordinate values, and the
	 * x and y horizontal resolutions.
	 */
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "iref.ddf", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "iref.ddf", 8);
		}
	}
	else  {
		if (gz_flag != 0)  {
			strncpy(&file_name[file_name_length - 11], "IREF.DDF", 8);
		}
		else  {
			strncpy(&file_name[file_name_length - 8], "IREF.DDF", 8);
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	need = 6;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "IREF") == 0)  {
			save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
			if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "SFAX") == 0))  {
				x_scale_factor = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "SFAY") == 0))  {
				y_scale_factor = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "XORG") == 0))  {
				x_origin = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "YORG") == 0))  {
				y_origin = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "XHRS") == 0))  {
				x_resolution = strtod(subfield.value, (char **)0);
				need--;
			}
			else if ((strstr(subfield.format, "R") != (char *)0) && (strcmp(subfield.label, "YHRS") == 0))  {
				y_resolution = strtod(subfield.value, (char **)0);
				need--;
			}
			subfield.value[subfield.length] = save_byte;
			if (need == 0)  {
				/* This is all we need.  Break out of the loop. */
				break;
			}
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();
	/* Check that we found what we wanted. */
	if (need > 0)  {
		fprintf(stderr, "Failed to get needed data from file %s.\n", file_name);
		return 1;
	}
	else if (need < 0)  {
		fprintf(stderr, "Warning:  Got more data from file %s than expected.\n", file_name);
	}



	/*
	 * Parse with the NP?? file, which contains the UTM x/y
	 * coordinates of the four corner registration points.
	 */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'p';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'p';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'P';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'P';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	record_id = -1;
	need = 8;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "PNTS") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				record_id = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "SADR") == 0)  {
			if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "X") == 0))  {
				if (subfield.length != 4)  {
					/* Error */
					x = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						x = (double)i * x_scale_factor + x_origin;
					}
				}
				switch (record_id)  {
				case 1:
					sw_x = x;
					break;
				case 2:
					nw_x = x;
					break;
				case 3:
					ne_x = x;
					break;
				case 4:
					se_x = x;
					break;
				default:
					fprintf(stderr, "Problem parsing NP?? module record %d in file %s.\n", record_id, file_name);
					break;
				}
				need--;
			}
			else if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "Y") == 0))  {
				if (subfield.length != 4)  {
					/* Error */
					y = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						y = (double)i * y_scale_factor + y_origin;
					}
				}
				switch (record_id)  {
				case 1:
					sw_y = y;
					break;
				case 2:
					nw_y = y;
					break;
				case 3:
					ne_y = y;
					break;
				case 4:
					se_y = y;
					break;
				default:
					fprintf(stderr, "Problem parsing NP?? module record %d in file %s.\n", record_id, file_name);
					break;
				}
				need--;
			}
		}
		if (need == 0)  {
			/* This is all we need.  Break out of the loop. */
			break;
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();
	/* Check that we found what we wanted. */
	if (need > 0)  {
		fprintf(stderr, "Failed to get needed data from file %s.\n", file_name);
		return 1;
	}
	else if (need < 0)  {
		fprintf(stderr, "Warning:  Got more data from file %s than expected.\n", file_name);
	}



	/*
	 * We are now in a position to write a bunch more records to the output file.
	 */
	if (file_image_flag != 0)  {
		/*
		 * Prepare and write record 4.
		 * The 2 code says that the file uses meters to measure horizontal distance.
		 * (There is no other choice.)
		 * The first 4 code is the number of file to map transformation paramters.
		 * (There is no other choice.)
		 * The 0 code is the number of accuracy/miscellaneous records.
		 * (There is no other choice.)
		 * The second 4 code is the number of control points.
		 * (There is no other choice.)
		 * The 1 code is the number of categories in the DLG file.
		 * (I assume this means hydrography versus transportation versus whatever)
		 * (There is no other choice.)
		 */
		sprintf(buf, "%6d%6d%6d%6d% 18.11E%6d%6d%6d%6d%3d%3d        ",
				dlg_level, plane_ref, utm_zone, 2, x_resolution, 4, 0, 4, 1, horizontal_datum, vertical_datum);
		for (i = 24; i < 42; i++)  {
			if (buf[i] == 'E')  buf[i] = 'D';	// USGS files use 'D' for exponentiation.
		}
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}


		/*
		 * Records 5 through 9 contain 15 map transformation parameters,
		 * three per record.
		 * For 24K and 100K DLGs, that use UTM coordinates, only the first
		 * two parameters are non-zero.  The first one is the longitude of
		 * the center of the cell.  The second one is the latitude of the
		 * center of the cell.
		 *
		 * The values are in the form:  degrees * 1000000 + minutes * 1000 + seconds
		 */
		latitude1 = (lat_se + lat_ne) / 2.0;
		decimal_degrees_to_dms(latitude1, &d, &m, &s);
		latitude2 = (d < 0 ? -1.0 : 1.0) * (fabs((double)d) * 1000000.0 + (double)m * 1000.0 + s);
		longitude1 = (long_se + long_sw) / 2.0;
		decimal_degrees_to_dms(longitude1, &d, &m, &s);
		longitude2 = (d < 0 ? -1.0 : 1.0) * (fabs((double)d) * 1000000.0 + (double)m * 1000.0 + s);

		sprintf(buf, "% 24.15E% 24.15E% 24.15E        ", longitude2, latitude2, 0.0);
		for (i = 0; i < 72; i++)  {
			if (buf[i] == 'E')  buf[i] = 'D';	// USGS files use 'D' for exponentiation.
		}

		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		if (write(output_fdesc, "   0.000000000000000D+00   0.000000000000000D+00   0.000000000000000D+00        ", DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		if (write(output_fdesc, "   0.000000000000000D+00   0.000000000000000D+00   0.000000000000000D+00        ", DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		if (write(output_fdesc, "   0.000000000000000D+00   0.000000000000000D+00   0.000000000000000D+00        ", DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		if (write(output_fdesc, "   0.000000000000000D+00   0.000000000000000D+00   0.000000000000000D+00        ", DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}



		/*
		 * Record 10 contains 4 internal file-to-map projection transformation parameters.
		 * These apparently are used to convert internal file coordinates into a ground
		 * planimetric system (presumably UTM).  Since the internal coordinates are already
		 * in UTM, the record takes the form:  1.0 0.0 0.0 0.0.
		 */
		if (write(output_fdesc, " 0.10000000000D+01 0.00000000000D+00 0.00000000000D+00 0.00000000000D+00        ", DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}



		/*
		 * Records 11 through 14 define the four corners of the data.  Each record takes
		 * a form like:
		 *
		 * SW       45.750000 -111.250000         480554.09  5066083.19
		 *
		 * where there is a two-letter code defining the corner, the latitude/longitude
		 * in decimal degrees, and the UTM x and y coordinates.
		 */
		sprintf(buf, "%2.2s    % 12.6f%12.6f      % 12.2f% 12.2f                    ", "SW", lat_sw, long_sw, sw_x, sw_y);
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		sprintf(buf, "%2.2s    % 12.6f%12.6f      % 12.2f% 12.2f                    ", "NW", lat_nw, long_nw, nw_x, nw_y);
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		sprintf(buf, "%2.2s    % 12.6f%12.6f      % 12.2f% 12.2f                    ", "NE", lat_ne, long_ne, ne_x, ne_y);
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}
		sprintf(buf, "%2.2s    % 12.6f%12.6f      % 12.2f% 12.2f                    ", "SE", lat_se, long_se, se_x, se_y);
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}


		/*
		 * Record 15 has to wait until we read in all of the node/line/area data,
		 * since it contains numbers that tell how many nodes/lines/areas there are.
		 */
	}



	/*
	 * Parse the LE?? file, which contains segmented lines, this was the file name
	 * originally passed into this routine.
	 */
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(passed_file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", passed_file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	attrib = -1;	// Use this convenient variable as a non-related flag for first trip through loop.
	count = 0;
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "LINE") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				/* We are starting a new line.  Initialize what need initializing. */
				if (attrib >= 0)  {
					/*
					 * If we aren't starting the first line,
					 * then terminate the attribute string and node list of the
					 * previous line and update the counts.
					 */
					*current_attrib = (struct attribute *)0;
					*current_point = (struct point *)0;
					lines[num_lines].number_attrib = attrib;
					lines[num_lines].number_coords = count;
					uniq_attrib(&lines[num_lines].attribute, &lines[num_lines].number_attrib);
				}
				module_num = -1;
				num_lines++;
				count = 0;
				attrib = 0;
				if (num_lines >= MAX_LINES)  {
					fprintf(stderr, "Ran out of space to store lines.  Some lines may be missing.\n");
					break;
				}
				current_attrib = &lines[num_lines].attribute;
				current_point = &lines[num_lines].point;
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				lines[num_lines].id = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "ATID") == 0)  {
			if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
				if (subfield.length != 4)  {
					fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
					module_num = -1;
					continue;
				}
				for (module_num = 0; module_num < num_attrib_files; module_num++)  {
					if (strncmp(subfield.value, attrib_files[module_num].module_name, 4) == 0)  {
						break;
					}
				}
				if (module_num == num_attrib_files)  {
					fprintf(stderr, "Warning:  Attribute module has unexpected name (%.*s).  Attributes may be in error.\n", subfield.length, subfield.value);
					module_num = -1;
				}
			}
			else if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				if (module_num < 0)  {
					continue;
				}
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				i =  strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
				if (i <= attrib_files[module_num].num_attrib)  {
					for (j = 0; j < MAX_EXTRA; j++)  {
						if (attrib_files[module_num].attrib[i - 1].major[j] != 0)  {
							*current_attrib = (struct attribute *)malloc(sizeof(struct attribute));
							if (*current_attrib == (struct attribute *)0)  {
								fprintf(stderr, "malloc failed\n");
								exit(0);
							}
							(*current_attrib)->major = attrib_files[module_num].attrib[i - 1].major[j];
							(*current_attrib)->minor = attrib_files[module_num].attrib[i - 1].minor[j];

							current_attrib = &((*current_attrib)->attribute);
							attrib++;
						}
					}
				}
			}
		}
		else if (strcmp(subfield.tag, "PIDL") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				lines[num_lines].left_area = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "PIDR") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				lines[num_lines].right_area = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "SNID") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				lines[num_lines].start_node = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "ENID") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				lines[num_lines].end_node = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "SADR") == 0)  {
			/*
			 * We assume that the X coordinate always comes first.
			 */
			if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "X") == 0))  {
				*current_point = (struct point *)malloc(sizeof(struct point));
				if (*current_point == (struct point *)0)  {
					fprintf(stderr, "malloc failed\n");
					exit(0);
				}

				if (subfield.length != 4)  {
					/* Error */
					(*current_point)->x = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						(*current_point)->x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						(*current_point)->x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						(*current_point)->x = (double)i * x_scale_factor + x_origin;
					}
				}
			}
			else if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "Y") == 0))  {
				if (subfield.length != 4)  {
					/* Error */
					(*current_point)->y = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						(*current_point)->y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						(*current_point)->y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						(*current_point)->y = (double)i * y_scale_factor + y_origin;
					}
				}

				current_point = &((*current_point)->point);
				count++;
			}
		}
	}
	if (num_lines >= 0)  {
		/*
		 * If we had at least one line
		 * then close out the attribute and node information of the
		 * previous line and update the counts.
		 */
		*current_attrib = (struct attribute *)0;
		*current_point = (struct point *)0;
		lines[num_lines].number_attrib = attrib;
		lines[num_lines].number_coords = count;
		uniq_attrib(&lines[num_lines].attribute, &lines[num_lines].number_attrib);
	}
	num_lines++;
	/* We are done with this file, so close it. */
	end_ddf();



	/*
	 * Parse with the NO?? file, which contains normal planar nodes.
	 * These nodes lie at either end of a piecewise-linear feature.
	 */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'o';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'o';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'O';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'O';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	attrib = -1;	// Use this convenient variable as a non-related flag for first trip through loop.
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "PNTS") == 0)  {
			if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				/* We are starting a new node.  Initialize what needs initializing. */
				if (attrib >= 0)  {
					/*
					 * If we aren't starting the first node,
					 * then terminate the attribute string of the
					 * previous node and update the count.
					 */
					*current_attrib = (struct attribute *)0;
					nodes[num_nodes].number_attrib = attrib;
					uniq_attrib(&nodes[num_nodes].attribute, &nodes[num_nodes].number_attrib);
				}
				module_num = -1;
				num_nodes++;
				attrib = 0;
				if (num_nodes >= MAX_NODES)  {
					fprintf(stderr, "Ran out of space to store nodes.  Some nodes may be missing.\n");
					break;
				}
				current_attrib = &nodes[num_nodes].attribute;
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				nodes[num_nodes].id = strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
			}
		}
		else if (strcmp(subfield.tag, "ATID") == 0)  {
			if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
				if (subfield.length != 4)  {
					fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
					module_num = -1;
					continue;
				}
				for (module_num = 0; module_num < num_attrib_files; module_num++)  {
					if (strncmp(subfield.value, attrib_files[module_num].module_name, 4) == 0)  {
						break;
					}
				}
				if (module_num == num_attrib_files)  {
					fprintf(stderr, "Warning:  Attribute module has unexpected name (%.*s).  Attributes may be in error.\n", subfield.length, subfield.value);
					module_num = -1;
				}
			}
			else if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
				if (module_num < 0)  {
					continue;
				}
				save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
				i =  strtol(subfield.value, (char **)0, 10);
				subfield.value[subfield.length] = save_byte;
				if (i <= attrib_files[module_num].num_attrib)  {
					for (j = 0; j < MAX_EXTRA; j++)  {
						if (attrib_files[module_num].attrib[i - 1].major[j] != 0)  {
							*current_attrib = (struct attribute *)malloc(sizeof(struct attribute));
							if (*current_attrib == (struct attribute *)0)  {
								fprintf(stderr, "malloc failed\n");
								exit(0);
							}
							(*current_attrib)->major = attrib_files[module_num].attrib[i - 1].major[j];
							(*current_attrib)->minor = attrib_files[module_num].attrib[i - 1].minor[j];

							current_attrib = &((*current_attrib)->attribute);
							attrib++;
						}
					}
				}
			}
		}
		else if (strcmp(subfield.tag, "SADR") == 0)  {
			/*
			 * We assume that the X coordinate always comes first.
			 */
			if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "X") == 0))  {
				if (subfield.length != 4)  {
					/* Error */
					nodes[num_nodes].x = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
					}
				}
			}
			else if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "Y") == 0))  {
				if (subfield.length != 4)  {
					/* Error */
					nodes[num_nodes].y = -1.0;
				}
				else  {
					i = (((long)subfield.value[3] & 0xff) << 24) |
					    (((long)subfield.value[2] & 0xff) << 16) |
					    (((long)subfield.value[1] & 0xff) <<  8) |
					     ((long)subfield.value[0] & 0xff);
					if (byte_order == 0)  {
						nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 1)  {
						LE_SWAB(&i);
						nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
					}
					else if (byte_order == 2)  {
						PDP_SWAB(&i);
						nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
					}
				}
			}
		}
	}
	if (num_nodes >= 0)  {
		/*
		 * If we had at least one node
		 * then close out the attribute information of the
		 * previous line and node and update the counts.
		 */
		*current_attrib = (struct attribute *)0;
		nodes[num_nodes].number_attrib = attrib;
		uniq_attrib(&nodes[num_nodes].attribute, &nodes[num_nodes].number_attrib);
	}
	num_nodes++;
	num_NO_nodes = num_nodes;
	/* We are done with this file, so close it. */
	end_ddf();



	/*
	 * Parse the NE?? file, which contains degenerate lines.
	 * In the optional-format DLG files, degenerate lines appear both in the
	 * node section and the line section.  In SDTS, they appear only in the
	 * NE?? module.  We simultaneously add them to the line list and the node list.
	 *
	 * When we add the node to the line array, we must artificially build
	 * a node list that is two nodes long, although the two nodes are
	 * identical.
	 *
	 * We have already read nodes from the NO?? module into the nodes array.
	 * Add the new nodes at the end of the array.
	 *
	 * The only other node files are NP??, which contains registration
	 * points for the four corners of the data, and NA??, which contains
	 * area representative points.  Neither of these files affects the nodes
	 * array.  Thus, after this block of code, the nodes array should be complete.
	 */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'e';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'e';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'E';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'E';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) >= 0)  {
		/*
		 * Loop through the subfields until we find what we want.
		 */
		attrib = -1;	// Use this convenient variable as a non-related flag for first trip through loop.
		count = 0;
		num_lines--;
		num_nodes--;
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "PNTS") == 0)  {
				if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					/* We are starting a new node.  Initialize what needs initializing. */
					if (attrib >= 0)  {
						/*
						 * If we aren't starting the first node,
						 * then terminate the attribute string of the
						 * previous line and update the counts.
						 */
						*current_attrib = (struct attribute *)0;
						*current_point = (struct point *)0;
						lines[num_lines].number_attrib = attrib;
						lines[num_lines].number_coords = count;
						uniq_attrib(&lines[num_lines].attribute, &lines[num_lines].number_attrib);
	
//						*current_attrib2 = (struct attribute *)0;
//						nodes[num_nodes].number_attrib = attrib;
//						uniq_attrib(&nodes[num_nodes].attribute, &nodes[num_nodes].number_attrib);
					}
					module_num = -1;
					num_nodes++;
					num_lines++;
					count = 0;
					attrib = 0;
					if (num_nodes >= MAX_NODES)  {
						fprintf(stderr, "Ran out of space to store nodes.  Some nodes may be missing.\n");
						break;
					}
					if (num_lines >= MAX_LINES)  {
						fprintf(stderr, "Ran out of space to store lines.  Some lines may be missing.\n");
						break;
					}
//					current_attrib2 = &nodes[num_nodes].attribute;
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					nodes[num_nodes].id = strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
	
					current_attrib = &lines[num_lines].attribute;
					current_point = &lines[num_lines].point;
					lines[num_lines].id = nodes[num_nodes].id;
					lines[num_lines].start_node = nodes[num_nodes].id;
					lines[num_lines].end_node = nodes[num_nodes].id;
				}
			}
			else if (strcmp(subfield.tag, "ATID") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
					if (subfield.length != 4)  {
						fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
						module_num = -1;
						continue;
					}
					for (module_num = 0; module_num < num_attrib_files; module_num++)  {
						if (strncmp(subfield.value, attrib_files[module_num].module_name, 4) == 0)  {
							break;
						}
					}
					if (module_num == num_attrib_files)  {
						fprintf(stderr, "Warning:  Attribute module has unexpected name (%.*s).  Attributes may be in error.\n", subfield.length, subfield.value);
						module_num = -1;
					}
				}
				else if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					if (module_num < 0)  {
						continue;
					}
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					i =  strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
					if (i <= attrib_files[module_num].num_attrib)  {
						for (j = 0; j < MAX_EXTRA; j++)  {
							if (attrib_files[module_num].attrib[i - 1].major[j] != 0)  {
								*current_attrib = (struct attribute *)malloc(sizeof(struct attribute));
								if (*current_attrib == (struct attribute *)0)  {
									fprintf(stderr, "malloc failed\n");
									exit(0);
								}
//								*current_attrib2 = (struct attribute *)malloc(sizeof(struct attribute));
//								if (*current_attrib2 == (struct attribute *)0)  {
//									fprintf(stderr, "malloc failed\n");
//									exit(0);
//								}

								(*current_attrib)->major = attrib_files[module_num].attrib[i - 1].major[j];	// line attribute entry
								(*current_attrib)->minor = attrib_files[module_num].attrib[i - 1].minor[j];

//								(*current_attrib2)->major = attrib_files[module_num].attrib[i - 1].major[j];	// node attribute entry
//								(*current_attrib2)->minor = attrib_files[module_num].attrib[i - 1].minor[j];

								current_attrib = &((*current_attrib)->attribute);
//								current_attrib2 = &((*current_attrib2)->attribute);
								attrib++;
							}
						}
					}
				}
			}
			else if (strcmp(subfield.tag, "ARID") == 0)  {
				if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					i =  strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
					lines[num_lines].left_area = i;
					lines[num_lines].right_area = i;
				}
			}
			else if (strcmp(subfield.tag, "SADR") == 0)  {
				/*
				 * We assume that the X coordinate always comes first.
				 */
				if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "X") == 0))  {
					/* We must add an extra endpoint, so get two storage slots instead of one. */
					*current_point = (struct point *)malloc(sizeof(struct point));
					if (*current_point == (struct point *)0)  {
						fprintf(stderr, "malloc failed\n");
						exit(0);
					}
					current_point2 = &((*current_point)->point);
					*current_point2 = (struct point *)malloc(sizeof(struct point));
					if (*current_point2 == (struct point *)0)  {
						fprintf(stderr, "malloc failed\n");
						exit(0);
					}
	
					if (subfield.length != 4)  {
						/* Error */
						nodes[num_nodes].x = -1.0;
						(*current_point)->x = -1.0;
						(*current_point2)->x = -1.0;
					}
					else  {
						i = (((long)subfield.value[3] & 0xff) << 24) |
						    (((long)subfield.value[2] & 0xff) << 16) |
						    (((long)subfield.value[1] & 0xff) <<  8) |
						     ((long)subfield.value[0] & 0xff);
						if (byte_order == 0)  {
							nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
							(*current_point)->x = nodes[num_nodes].x;
							(*current_point2)->x = nodes[num_nodes].x;
						}
						else if (byte_order == 1)  {
							LE_SWAB(&i);
							nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
							(*current_point)->x = nodes[num_nodes].x;
							(*current_point2)->x = nodes[num_nodes].x;
						}
						else if (byte_order == 2)  {
							PDP_SWAB(&i);
							nodes[num_nodes].x = (double)i * x_scale_factor + x_origin;
							(*current_point)->x = nodes[num_nodes].x;
							(*current_point2)->x = nodes[num_nodes].x;
						}
					}
				}
				else if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "Y") == 0))  {
					if (subfield.length != 4)  {
						/* Error */
						nodes[num_nodes].y = -1.0;
						(*current_point)->y = -1.0;
						(*current_point2)->y = -1.0;
					}
					else  {
						i = (((long)subfield.value[3] & 0xff) << 24) |
						    (((long)subfield.value[2] & 0xff) << 16) |
						    (((long)subfield.value[1] & 0xff) <<  8) |
						     ((long)subfield.value[0] & 0xff);
						if (byte_order == 0)  {
							nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
							(*current_point)->y = nodes[num_nodes].y;
							(*current_point2)->y = nodes[num_nodes].y;
						}
						else if (byte_order == 1)  {
							LE_SWAB(&i);
							nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
							(*current_point)->y = nodes[num_nodes].y;
							(*current_point2)->y = nodes[num_nodes].y;
						}
						else if (byte_order == 2)  {
							PDP_SWAB(&i);
							nodes[num_nodes].y = (double)i * y_scale_factor + y_origin;
							(*current_point)->y = nodes[num_nodes].y;
							(*current_point2)->y = nodes[num_nodes].y;
						}
					}
	
					current_point = &((*current_point2)->point);
					count++;
					count++;
				}
			}
		}
		if (num_nodes >= 0)  {
			/*
			 * If we had at least one node
			 * then close out the attribute information of the
			 * previous line and node and update the counts.
			 */
			*current_attrib = (struct attribute *)0;
			*current_point = (struct point *)0;
			lines[num_lines].number_attrib = attrib;
			lines[num_lines].number_coords = count;
			uniq_attrib(&lines[num_lines].attribute, &lines[num_lines].number_attrib);
	
//			*current_attrib2 = (struct attribute *)0;
//			nodes[num_nodes].number_attrib = attrib;
//			uniq_attrib(&nodes[num_nodes].attribute, &nodes[num_nodes].number_attrib);
// For degenerate lines, the nodes don't appear to have the attributes attached.
// Thus, we simply zero out the attribute count.  However, we will leave the
// code in place for now, that keeps an attribute list for the nodes, just in
// case I am misunderstanding something.
			nodes[num_nodes].number_attrib = 0;
		}
		num_lines++;
		num_nodes++;
		/* We are done with this file, so close it. */
		end_ddf();
	}



	/*
	 * Before we can process the areas, we need to read the Polygon module
	 * and find out which attributes are associated with each polygon.
	 * Then we can read the NA?? module and associate the Area representative
	 * points, from the NA?? module, with specific attributes, referenced in
	 * the polygon (PC??) module, via the polygon linkage in the NA?? module.
	 */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'p';
			file_name[file_name_length - 10] = 'c';
		}
		else  {
			file_name[file_name_length -  8] = 'p';
			file_name[file_name_length -  7] = 'c';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'P';
			file_name[file_name_length - 10] = 'C';
		}
		else  {
			file_name[file_name_length -  8] = 'P';
			file_name[file_name_length -  7] = 'C';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	num_polys = 0;
	if (begin_ddf(file_name) >= 0)  {
		/*
		 * Loop through the subfields until we find what we want.
		 */
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "POLY") == 0)  {
				if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					current_poly =  strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
				}
			}
			else if (strcmp(subfield.tag, "ATID") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
					if (subfield.length != 4)  {
						fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
						module_num = -1;
						continue;
					}
					for (module_num = 0; module_num < num_attrib_files; module_num++)  {
						if (strncmp(subfield.value, attrib_files[module_num].module_name, 4) == 0)  {
							break;
						}
					}
					if (module_num == num_attrib_files)  {
						fprintf(stderr, "Warning:  Attribute module has unexpected name (%.*s).  Attributes may be in error.\n", subfield.length, subfield.value);
						module_num = -1;
					}
				}
				else if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					if (module_num < 0)  {
						continue;
					}
					if (num_polys >= MAX_POLY_NUM)  {
						fprintf(stderr, "Ran out of polygon space.  Some attributes may not show up.\n");
						break;
					}
					polygon_attrib[num_polys].poly_id = current_poly;
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					polygon_attrib[num_polys].attrib = strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
					polygon_attrib[num_polys++].module_num = module_num;
				}
			}
		}
		/* We are done with this file, so close it. */
		end_ddf();
	}


	/*
	 * Now read in the Areas Module and store the data.
	 *
	 * Before we do, though, generate an entry for area 1.
	 * This is the neatline polygon that surrounds the data
	 * area.  It is not encoded in the NA??  module,
	 * but there is an entry for the polygon in
	 * the PC?? module.  Since we won't find it while searching
	 * the NA?? module, and since we need it for a complete
	 * DLG-3 file, insert it artificially at the beginning
	 * of the array.  There does not appear to be any way
	 * to recover the original pre-SDTS representative point for this
	 * area, so just insert the southwest registration point,
	 * since the few sample files I looked at seemed to use a
	 * representative point near that corner.
	 */
	num_areas++;
	areas[num_areas].id = 1;
	areas[num_areas].x = sw_x;
	areas[num_areas].y = sw_y;
	areas[num_areas].number_attrib = 0;
	areas[num_areas].attribute = (struct attribute *)0;

	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'a';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'a';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'A';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'A';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) >= 0)  {
		/*
		 * Loop through the subfields until we find what we want.
		 */
		attrib = -1;	// Use this convenient variable as a non-related flag for first trip through loop.
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "PNTS") == 0)  {
				if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					/* We are starting a new line.  Initialize what need initializing. */
					if (attrib >= 0)  {
						/*
						 * If we aren't starting the first area,
						 * then terminate the attribute string of the
						 * previous area and update the counts.
						 */
						*current_attrib = (struct attribute *)0;
						areas[num_areas].number_attrib = attrib;
						uniq_attrib(&areas[num_areas].attribute, &areas[num_areas].number_attrib);
					}
					num_areas++;
					attrib = 0;
					if (num_areas >= MAX_AREAS)  {
						fprintf(stderr, "Ran out of space to store areas.  Some areas may be missing.\n");
						break;
					}
					current_attrib = &areas[num_areas].attribute;
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					areas[num_areas].id = strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
				}
			}
			else if (strcmp(subfield.tag, "SADR") == 0)  {
				/*
				 * We assume that the X coordinate always comes first.
				 */
				if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "X") == 0))  {
					if (subfield.length != 4)  {
						/* Error */
						areas[num_areas].x = -1.0;
					}
					else  {
						i = (((long)subfield.value[3] & 0xff) << 24) |
						    (((long)subfield.value[2] & 0xff) << 16) |
						    (((long)subfield.value[1] & 0xff) <<  8) |
						     ((long)subfield.value[0] & 0xff);
						if (byte_order == 0)  {
							areas[num_areas].x = (double)i * x_scale_factor + x_origin;
						}
						else if (byte_order == 1)  {
							LE_SWAB(&i);
							areas[num_areas].x = (double)i * x_scale_factor + x_origin;
						}
						else if (byte_order == 2)  {
							PDP_SWAB(&i);
							areas[num_areas].x = (double)i * x_scale_factor + x_origin;
						}
					}
				}
				else if ((strstr(subfield.format, "B") != (char *)0) && (strcmp(subfield.label, "Y") == 0))  {
					if (subfield.length != 4)  {
						/* Error */
						areas[num_areas].y = -1.0;
					}
					else  {
						i = (((long)subfield.value[3] & 0xff) << 24) |
						    (((long)subfield.value[2] & 0xff) << 16) |
						    (((long)subfield.value[1] & 0xff) <<  8) |
						     ((long)subfield.value[0] & 0xff);
						if (byte_order == 0)  {
							areas[num_areas].y = (double)i * y_scale_factor + y_origin;
						}
						else if (byte_order == 1)  {
							LE_SWAB(&i);
							areas[num_areas].y = (double)i * y_scale_factor + y_origin;
						}
						else if (byte_order == 2)  {
							PDP_SWAB(&i);
							areas[num_areas].y = (double)i * y_scale_factor + y_origin;
						}
					}
				}
			}
			else if (strcmp(subfield.tag, "ARID") == 0)  {
				if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
					save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
					i =  strtol(subfield.value, (char **)0, 10);
					subfield.value[subfield.length] = save_byte;
	
					for (j = 0; j < num_polys; j++)  {
						if (polygon_attrib[j].poly_id == i)  {
							if (polygon_attrib[j].attrib <= attrib_files[polygon_attrib[j].module_num].num_attrib)  {
								for (k = 0; k < MAX_EXTRA; k++)  {
									if (attrib_files[polygon_attrib[j].module_num].attrib[polygon_attrib[j].attrib - 1].major[k] != 0)  {
										*current_attrib = (struct attribute *)malloc(sizeof(struct attribute));
										if (*current_attrib == (struct attribute *)0)  {
											fprintf(stderr, "malloc failed\n");
											exit(0);
										}
	
										(*current_attrib)->major = attrib_files[polygon_attrib[j].module_num].attrib[polygon_attrib[j].attrib - 1].major[k];
										(*current_attrib)->minor = attrib_files[polygon_attrib[j].module_num].attrib[polygon_attrib[j].attrib - 1].minor[k];
	
										current_attrib = &((*current_attrib)->attribute);
										attrib++;
									}
								}
							}
						}
					}
				}
			}
		}
		if (num_areas >= 0)  {
			/*
			 * If we had at least one area
			 * then close out the attribute and node information of the
			 * previous area and update the counts.
			 */
			*current_attrib = (struct attribute *)0;
			areas[num_areas].number_attrib = attrib;
			uniq_attrib(&areas[num_areas].attribute, &areas[num_areas].number_attrib);
		}
		/* We are done with this file, so close it. */
		end_ddf();
	}
	num_areas++;



	/*
	 * Because the various cross-links between Nodes, Lines, and Areas depend
	 * on the Record IDs, we are faced with a choice.  Either we make sure
	 * that each array index corresponds to the Record ID stored therein,
	 * or we have to search the array every time we want a specific record.
	 * We choose to do the former, so that the Record ID stored in array
	 * element i is equal to i + 1.
	 *
	 * Rather than try to fill the arrays based on record IDs, and have to
	 * do error checking to find empty slots in the arrays, we choose to
	 * pack records into the arrays without regard for ordering.  After the
	 * arrays are full, we sort them into order with qsort().  This should
	 * be reasonably low-cost, in terms of CPU time, because the arrays
	 * should be pretty close to the correct order before we start.
	 *
	 * The areas array should be in order and won't need sorting.
	 * The lines array may have some deviations from correct
	 * ordering because the degenerate lines are stored in a separate file (the NE?? file),
	 * and read in separately at the end of the array.
	 *
	 * The nodes array will not have the record-ID-to-index correspondence
	 * because it results from reading both the NE?? module and the NO?? module,
	 * with the corresponding records appearing in two separate groups in the
	 * array.
	 *
	 * We won't sort the nodes array with qsort.  This is because the degenerate lines,
	 * from the NE?? module, have the same Record IDs as the associated entry in
	 * the lines array.  (In other words, in the original data, Node 42 and Line
	 * 243 may have both represented the same degenerate line.  In the SDTS data,
	 * both the line and the node have Record ID 243.  (I think this may be a bug
	 * in the USGS conversion procedure, but maybe they did it on purpose.)  The
	 * big problem with this is that we could easily have a node with the same
	 * Record ID of 243 in the NO?? module, resulting in two nodes in the nodes array
	 * that have the same ID.)  We will reconstruct the original node
	 * numbers and sort the array at the same time.
	 * We do this by assuming that holes in the NO?? node numbering should be
	 * sequentially filled by the points that were placed into the NE?? module.
	 */
	qsort(lines, num_lines, sizeof(struct lines), compare_lines);



	/*
	 * All of the useful data is parsed.
	 * Now do something with it.
	 *
	 * Either enter the big block of code that writes all of the
	 * data to an optional-format DLG file, or enter the big block
	 * of code that writes the data to the image buffer.
	 *
	 * We sort the nodes array within the first big block, because
	 * the node data isn't used when drawing a map.  If we ever
	 * start using the node data for maps, then we will have to
	 * move the sorting up to where we do the qsort() above.
	 */
	if (file_image_flag != 0)  {
		/*
		 * This is the big block of code that writes the data
		 * to an optional-format DLG file.
		 *
		 * We now have enough information to write Record 15.
		 */
		sprintf(buf, "%-20.20s   0%6d%6d 010%6d%6d 010%6d%6d   1        ",
			category_name, num_nodes, num_nodes, num_areas, num_areas, num_lines, num_lines);
		if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
			fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
			exit(0);
		}



		/*
		 * Now we are ready to write the node records to the file.
		 * Begin by reconstructing the node list.
		 * Fold all of the degenerate-line nodes into
		 * the missing node slots in the node list.
		 */
		if (num_nodes > num_NO_nodes)  {
			if (num_nodes >= MAX_NODES)  {	// We need one extra node slot for temporary storage
				fprintf(stderr, "Ran out of space to store nodes.  Non-recoverable error.\n");
				exit(0);
			}
			i = num_NO_nodes;
			for (j = 0; j < num_nodes; j++)  {
				if (nodes[j].id != (j + 1))  {
					nodes[num_nodes].id = nodes[i].id;
					nodes[num_nodes].x = nodes[i].x;
					nodes[num_nodes].y = nodes[i].y;
					nodes[num_nodes].number_attrib = nodes[i].number_attrib;
					nodes[num_nodes].attribute = nodes[i].attribute;
					for (k = i; k > j; k--)  {
						nodes[k].id = nodes[k - 1].id;
						nodes[k].x = nodes[k - 1].x;
						nodes[k].y = nodes[k - 1].y;
						nodes[k].number_attrib = nodes[k - 1].number_attrib;
						nodes[k].attribute = nodes[k - 1].attribute;
					}
					nodes[j].id = nodes[num_nodes].id;
					nodes[j].x = nodes[num_nodes].x;
					nodes[j].y = nodes[num_nodes].y;
					nodes[j].number_attrib = nodes[num_nodes].number_attrib;
					nodes[j].attribute = nodes[num_nodes].attribute;
					lines[nodes[j].id - 1].end_node = j + 1;
					lines[nodes[j].id - 1].start_node = j + 1;
					nodes[j].id = j + 1;
					i++;
					if (i == num_nodes)  {
						break;
					}
				}
			}
		}
		if (nodes[num_nodes - 1].id != num_nodes)  {
			fprintf(stderr, "Warning:  The node section may have some problems.\n");
		}
		/*
		 * Now go through the patched up nodes, and build a line list for
		 * each node, and then print out the node record.
		 * (The line list consists of all linear features for which this node is an endpoint.
		 * The lines appear in no particular order.)
		 */
		for (i = 0; i < num_nodes; i++)  {
			line_list_size = -1;
			for (j = 0; j < num_lines; j++)  {
				if (lines[j].start_node == nodes[i].id)  {
					if ((line_list_size + 1) == MAX_LINE_LIST)  {
						fprintf(stderr, "Ran out of space for a nodal line list (node %d).  Some lines are missing.\n", i + 1);
						break;
					}
					line_list_size++;
					line_list[line_list_size] = lines[j].id;
				}
				/* Note:  This is not an "else if" because degenerate lines must appear twice in the list. */
				if (lines[j].end_node == nodes[i].id)  {
					if ((line_list_size + 1) == MAX_LINE_LIST)  {
						fprintf(stderr, "Ran out of space for a nodal line list (node %d).  Some lines are missing.\n", i + 1);
						break;
					}
					line_list_size++;
					line_list[line_list_size] = -lines[j].id;
				}
			}
			line_list_size++;

			/*
			 * Print the first record of the node.
			 */
			sprintf(buf, "N%5d%12.2f%12.2f      %6d      %6d     0                    ",
				i + 1, nodes[i].x, nodes[i].y, line_list_size, nodes[i].number_attrib);
			if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
				fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
				exit(0);
			}

			/*
			 * Print the line-list records.
			 */
			j = 0;
			for (k = 0; k < line_list_size; k++)  {
				sprintf(&buf[j], "%6d", line_list[k]);
				j = j + 6;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}

			/*
			 * Print the attribute records.
			 */
			j = 0;
			current_attrib = &nodes[i].attribute;
			for (k = 0; k < nodes[i].number_attrib; k++)  {
				sprintf(&buf[j], "%6d%6d", (*current_attrib)->major, (*current_attrib)->minor);
				j = j + 12;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
				current_attrib = &((*current_attrib)->attribute);
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}
		}

		/*
		 * Now go through the areas, and build a line list for
		 * each area, and then print out the area record.
		 * (The line list is a list of the lines that bound this area.
		 * The lines should appear in clockwise order around the perimeter
		 * of the area.  If there are islands, their sublists are delimited by
		 * inserting a line number of zero into the list ahead of them.
		 * Island nodes are listed in counterclockwise order.)
		 */
		for (i = 0; i < num_areas; i++)  {
			line_list_size = -1;
			for (j = 0; j < num_lines; j++)  {
				if (lines[j].left_area == lines[j].right_area)  {
					continue;
				}
				if ((lines[j].right_area == areas[i].id) || (lines[j].left_area == areas[i].id))  {
					if ((line_list_size + 2) == MAX_LINE_LIST)  {	// Reserve an empty spot at the end of the list for later.
						fprintf(stderr, "Ran out of space for an areal line list.  Some lines are missing for area %d.\n", i + 1);
						break;
					}
					line_list_size++;
					line_list[line_list_size] = lines[j].id;
				}
			}
			line_list_size++;

			/*
			 * Now we have the line list, but it still must be sorted
			 * so that the linear features are contiguous.
			 *
			 * First, though, we want to make sure that we get the
			 * primary area first in the list, with any islands coming after it
			 * in the list.  In order to do this, we search for the
			 * point in the line list that has the greatest y value,
			 * and we put that point first in the list.  (No matter what
			 * the shape of the enclosing polygon, no island should have
			 * any points that are as far north as the northernmost point
			 * in the enclosing polygon.)  The sorting
			 * algorithm will pull contiguous points toward this first point,
			 * and thus pull the primary area toward the front of the list.
			 */
			y = -11000000.0;
			for (j = 0; j < line_list_size; j++)  {
				current_point = &lines[line_list[j] - 1].point;
				while (*current_point != (struct point *)0)  {
					if ((*current_point)->y > y)  {
						y = (*current_point)->y;
						k = j;
					}
					current_point = &((*current_point)->point);
				}
			}
			j = line_list[k];
			line_list[k] = line_list[0];
			line_list[0] = j;
			/*
			 * We have the northernmost point first in the list.
			 * Now do the sorting.
			 *
			 * Whether sorting the main polygon, or an island,
			 * the nodes are ordered so that the referenced area
			 * is always on the right as we traverse each linear
			 * component of the boundary.
			 */
			number_of_islands = 0;
			for (j = 0; j < line_list_size; j++)  {
				if (lines[line_list[j] - 1].right_area == areas[i].id)  {
					start_node = lines[line_list[j] - 1].start_node;
					current_node = lines[line_list[j] - 1].end_node;
				}
				else  {
					start_node = lines[line_list[j] - 1].end_node;
					current_node = lines[line_list[j] - 1].start_node;
				}
				if (start_node != current_node)  {
					for (k = j + 1; k < line_list_size; k++)  {
						if (lines[line_list[k] - 1].right_area == areas[i].id)  {
							if (lines[line_list[k] - 1].start_node == current_node)  {
								line_list[MAX_LINE_LIST - 1] = line_list[j + 1];
								line_list[j + 1] = line_list[k];
								line_list[k] = line_list[MAX_LINE_LIST - 1];
								break;
							}
						}
						else  {
							if (lines[line_list[k] - 1].end_node == current_node)  {
								line_list[MAX_LINE_LIST - 1] = line_list[j + 1];
								line_list[j + 1] = line_list[k];
								line_list[k] = line_list[MAX_LINE_LIST - 1];
								break;
							}
						}
					}
				}
				if (lines[line_list[j] - 1].left_area == areas[i].id)  {
					line_list[j] = - line_list[j];
				}
				if (((start_node == current_node) || (k == line_list_size)) && (j < (line_list_size - 1)))  {
					if ((line_list_size + 2) == MAX_LINE_LIST)  {	// Reserve an empty spot at the end of the list for later.
						fprintf(stderr, "Ran out of space for an areal line list.  There may be errors in the line list for area %d.\n", i + 1);
						break;
					}
					j++;
					line_list[line_list_size] = line_list[j];
					line_list[j] = 0;
					line_list_size++;
					number_of_islands++;
				}
			}

			/*
			 * Print the first record of the area.
			 *
			 * Special case for the Universe polygon, which needs a single attribute
			 * that isn't stored in the attribute list.  It isn't in the list because
			 * we artificially generated the Universe polygon from scratch before reading
			 * in the NA?? module.
			 */
			if (i == 0)  {
				sprintf(buf, "A%5d%12.2f%12.2f      %6d     0%6d     0%6d              ",
					i + 1, areas[i].x, areas[i].y, line_list_size, 1, number_of_islands);
			}
			else  {
				sprintf(buf, "A%5d%12.2f%12.2f      %6d     0%6d     0%6d              ",
					i + 1, areas[i].x, areas[i].y, line_list_size, areas[i].number_attrib, number_of_islands);
			}
			if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
				fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
				exit(0);
			}

			/*
			 * Print the line-list records.
			 */
			j = 0;
			for (k = 0; k < line_list_size; k++)  {
				sprintf(&buf[j], "%6d", line_list[k]);
				j = j + 6;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}

			/*
			 * Print the attribute records.
			 */
			j = 0;
			current_attrib = &areas[i].attribute;
			for (k = 0; k < areas[i].number_attrib; k++)  {
				sprintf(&buf[j], "%6d%6d", (*current_attrib)->major, (*current_attrib)->minor);
				j = j + 12;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
				current_attrib = &((*current_attrib)->attribute);
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}
			/*
			 * Special case for the Universe polygon, which is
			 * supposed to have an attribute of 0, 0.
			 */
			if (i == 0)  {
				sprintf(buf, "%6d%6d", 0, 0);
				for (j = 12 ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}
		}

		/*
		 * Now go through the lines, and print out the records.
		 */
		for (i = 0; i < num_lines; i++)  {
			/*
			 * Print the first record of the node.
			 */
			sprintf(buf, "L%5d%6d%6d%6d%6d            %6d%6d     0                    ",
				i + 1, lines[i].start_node, lines[i].end_node, lines[i].left_area, lines[i].right_area,
				lines[i].number_coords, lines[i].number_attrib);
			if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
				fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
				exit(0);
			}

			/*
			 * Print the coordinate-list records.
			 */
			j = 0;
			current_point = &lines[i].point;
			for (k = 0; k < lines[i].number_coords; k++)  {
				sprintf(&buf[j], "%12.2f%12.2f", (*current_point)->x, (*current_point)->y);
				j = j + 24;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
				current_point = &((*current_point)->point);
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}

			/*
			 * Print the attribute records.
			 */
			j = 0;
			current_attrib = &lines[i].attribute;
			for (k = 0; k < lines[i].number_attrib; k++)  {
				sprintf(&buf[j], "%6d%6d", (*current_attrib)->major, (*current_attrib)->minor);
				j = j + 12;
				if (j == 72)  {
					for ( ; j < DLG_RECORD_LENGTH; j++)  {
						buf[j] = ' ';
					}
					if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
						fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
						exit(0);
					}
					j = 0;
				}
				current_attrib = &((*current_attrib)->attribute);
			}
			if (j > 0)  {
				for ( ; j < DLG_RECORD_LENGTH; j++)  {
					buf[j] = ' ';
				}
				if (write(output_fdesc, buf, DLG_RECORD_LENGTH) != DLG_RECORD_LENGTH)  {
					fprintf(stderr, "Failed to write output file.  errno = %d.\n", errno);
					exit(0);
				}
			}
		}
		close(output_fdesc);
	}
	else  {
		/*
		 * This is the big block of code that writes the data
		 * to the drawmap image buffer.
		 *
		 * First find the x and y image coordinates that border this DLG chunk.
		 *
		 * Then draw the lines for which we have appropriate atribute codes stored,
		 * but don't go outside the x-y border.
		 *
		 * Then fill in all of the areas for which we have
		 * appropriate attribute codes stored, but don't go outside
		 * the x-y border.
		 */
		dlg_x_low = -1 + round((long_sw - image_corners->sw_long) * (double)image_corners->x / (image_corners->ne_long - image_corners->sw_long));
		dlg_y_low = image_corners->y - 1 - round((lat_ne - image_corners->sw_lat) * (double)image_corners->y / (image_corners->ne_lat - image_corners->sw_lat));
		dlg_x_high = -1 + round((long_ne - image_corners->sw_long) * (double)image_corners->x / (image_corners->ne_long - image_corners->sw_long));
		dlg_y_high = image_corners->y - 1 - round((lat_sw - image_corners->sw_lat) * (double)image_corners->y / (image_corners->ne_lat - image_corners->sw_lat));
		if (dlg_x_low < -1)  {
			dlg_x_low = -1;
		}
		if (dlg_y_low < -1)  {
			dlg_y_low = -1;
		}
		if (dlg_x_high >= image_corners->x)  {
			dlg_x_high = image_corners->x - 1;
		}
		if (dlg_y_high >= image_corners->y)  {
			dlg_y_high = image_corners->y - 1;
		}

		/*
		 * Cycle through all of the line data and draw all of the appropriate lines
		 * onto the image (overlaying any previous data).
		 */
		for (i = 0; i < num_lines; i++)  {
			/*
			 * In the DLG-3 format, the first area element listed
			 * represents the universe outside of the map area.
			 * (In SDTS, this is apparently no longer encodes as an area,
			 * but is stored in connection with the definition of the data boundaries.)
			 * Thus, lines that have area 1 as a boundary should be
			 * "neatlines" that bound the map area.
			 * Since these clutter up a map, we normally discard them.
			 * (If you want to keep them, then change the #define of OMIT_NEATLINES
			 * so that it is zero, rather than non-zero.)
			 *
			 * Here are relevant quotes from the DLG-3 guide:
			 *
			 *	expressed by network data is that of connectivity.  The network case
			 *	differs from the area case in that, irrespective of the number of closed
			 *	areas forming the graph, only two areas are encoded:  (1) the area out-
			 *	side the graph, termed the outside area; and (2) the area within the
			 *	graph, termed the background area.  All lines except the graph boundary,
			 *	or neatline, are considered to be contained within the background area.
			 *
			 *	map border.  There is one outside area for each DLG-3. It is always the
			 *	first area encountered (its ID is 1) and has the attribute code 000 0000.
			 */

			/*
			 * If the user provided a file full of attributes, then
			 * use them to control whether or not the lines are drawn.
			 * If not, then just go ahead and draw everything.
			 *
			 * Note:  If a major or minor attribute code in the attribute
			 *        file (supplied by the user) is less than
			 *        zero, it is treated as a wild card and matches
			 *        anything.
			 */
			if ((num_A_attrib > 0) || (num_L_attrib > 0))  {
				if ((OMIT_NEATLINES == 0) || ((lines[i].left_area != 1) && (lines[i].right_area != 1)))  {
					current_attrib = &lines[i].attribute;
					if (*current_attrib != (struct attribute *)0)  {
						while (*current_attrib != (struct attribute *)0)  {
							for (j = 0; j < num_L_attrib; j++)  {
								if (((attributes_L[j].major < 0) ||
								     (attributes_L[j].major == ((*current_attrib)->major))) &&
								    ((attributes_L[j].minor < 0) ||
								     (attributes_L[j].minor == ((*current_attrib)->minor))))  {
									draw_lines(&datum, lines[i].point, color, image_corners);
									goto FIN1;
								}
							}
							current_attrib = &((*current_attrib)->attribute);
						}
					}
					else  {
						/*
						 * If the feature had no attribute codes, then check if
						 * it is covered by a wild card in the attributes file.
						 */
						for (j = 0; j < num_L_attrib; j++)  {
							if (((attributes_L[j].major < 0) ||
							     (attributes_L[j].major == data_type)) &&
							    (attributes_L[j].minor < 0))  {
								draw_lines(&datum, lines[i].point, color, image_corners);
								goto FIN1;
							}
						}
					}
				}

				/*
				 * For those (hopefully rare) occasions in which something
				 * goes wrong, we provide the capability for a user to
				 * specifically request a single line from a DLG file so that
				 * the cause of the problem can be isolated.
				 * The user specifies a specific line by providing a major
				 * attribute number of 10000, and a minor attribute number
				 * equal to the desired line ID number.  Since no
				 * valid attribute (as far as I know) is ever as large as
				 * 10,000, such user-specified attribute pairs will not
				 * affect the search for legitimate attributes above (since
				 * they can't possibly match anything).  If we reach this point,
				 * then we failed to draw a line due to the legitimate-attribute
				 * checks above; so we give it one more try here, based on
				 * user-requested ID numbers.
				 *
				 * Note:  If you are using this feature, then it doesn't make
				 *        a lot of sense to process more than one DLG file,
				 *        since the ID number you give (as the minor attribute)
				 *        will be matched in every DLG file that has a
				 *        Line with that ID.  If you are trying to isolate
				 *        one (or a few) Line(s), then you probably want to
				 *        be certain which file is the source of the data.
				 */
				for (j = 0; j < num_L_attrib; j++)  {
					if ((attributes_L[j].major == 10000) &&
					     (attributes_L[j].minor == lines[i].id))  {
						draw_lines(&datum, lines[i].point, color, image_corners);
						goto FIN1;
					}
				}
			}
			else  {
				if ((OMIT_NEATLINES == 0) || ((lines[i].left_area != 1) && (lines[i].right_area != 1)))  {
					draw_lines(&datum, lines[i].point, color, image_corners);
				}
			}
FIN1:
			{;}
		}

		/*
		 * Now we fill in each interesting area on the map with the
		 * same color that bounds the area.  (For example,
		 * lakes (attribute code 050 0421) might be filled in.)
		 * However, sometimes areas might be filled in improperly.
		 * The code assumes that the reference point for an area falls
		 * within the polygon of lines that define that area.
		 * According to the DLG guide, this isn't guaranteed
		 * to always be the case, but the assumption has nonetheless
		 * worked reasonably well in practice.
		 *
		 * Area attributes are processed a bit differently than the
		 * attributes for lines:  no areas are filled in automatically.
		 * If the user did not specify any Area attributes in the attribute
		 * file, then no areas are filled in.  This is because the area-fill
		 * algorithm can occasionally run amok, and therefore the appropriate
		 * default is to not give it a chance.  For extensive details on the
		 * area-fill algorithm, see the comments at the top of fill_area().
		 */
		if (num_A_attrib > 0)  {
			for (i = 0; i < num_areas; i++)  {
				if (areas[i].number_attrib <= 0)  {
					continue;
				}

				current_attrib = &areas[i].attribute;
				while (*current_attrib != (struct attribute *)0)  {
					for (j = 0; j < num_A_attrib; j++)  {
						if (((attributes_A[j].major < 0) ||
						     (attributes_A[j].major == ((*current_attrib)->major))) &&
						    ((attributes_A[j].minor < 0) ||
						     (attributes_A[j].minor == ((*current_attrib)->minor))))  {
							fill_area(&datum, areas[i].x, areas[i].y, color, image_corners);
							goto FIN2;
						}
					}
					current_attrib = &((*current_attrib)->attribute);
				}

				/*
				 * As with the Line attributes, we provide an interface
				 * for the user to select specific areas, via their IDs.
				 */
				for (j = 0; j < num_A_attrib; j++)  {
					if ((attributes_A[j].major == 10000) &&
					     (attributes_A[j].minor == areas[i].id))  {
						fill_area(&datum, areas[i].x, areas[i].y, color, image_corners);
						goto FIN2;
					}
				}
FIN2:
				{;}
			}
		}
	}



	/* Free up all of the malloc() memory */
	for (i = 0; i < num_lines; i++)  {
		if (lines[i].number_coords > 0)  {
			current_point = &lines[i].point;

			while (*current_point != (struct point *)0)  {
				tmp_point = (*current_point)->point;
				free(*current_point);
				*current_point = tmp_point;
			}
		}
		if (lines[i].number_attrib > 0)  {
			current_attrib = &lines[i].attribute;

			while (*current_attrib != (struct attribute *)0)  {
				tmp_attrib = (*current_attrib)->attribute;
				free(*current_attrib);
				*current_attrib = tmp_attrib;
			}
		}
	}
	for (i = 0; i < num_areas; i++)  {
		if (areas[i].number_attrib > 0)  {
			current_attrib = &areas[i].attribute;

			while (*current_attrib != (struct attribute *)0)  {
				tmp_attrib = (*current_attrib)->attribute;
				free(*current_attrib);
				*current_attrib = tmp_attrib;
			}
		}
	}
	for (i = 0; i < num_nodes; i++)  {
		if (nodes[i].number_attrib > 0)  {
			current_attrib = &nodes[i].attribute;

			while (*current_attrib != (struct attribute *)0)  {
				tmp_attrib = (*current_attrib)->attribute;
				free(*current_attrib);
				*current_attrib = tmp_attrib;
			}
		}
	}
	for (i = 0; i < num_attrib_files; i++)  {
		free(attrib_files[i].attrib);
	}
}





/*
 * Feature definitions for the additional
 * features associated with primary attributes.
 */
#define NUM_FEATURES 117
struct	features  {
	long key;		// An internal key used to keep track of the entries
	long main_major;	// The major attribute number for this category
	long major;		// The major attribute number for this category, modified to suit individual features
	long minor;		// The minor attribute number for this feature
	char *feature_name;
	long feature_name_length;
} features[NUM_FEATURES] =  {
  0,	 20,	 20,	202,	"SUPPLEMENTARY",	13,	// Hypsography
  1,	 20,	 20,	204,	"AMENDED",		 7,
  2,	 20,	 20,	610,	"APPROXIMATE",		11,
  3,	 20,	 20,	611,	"DEPRESSION",		10,
  4,	 20,	 20,	612,	"GLACIER_OR_SNOW",	15,
  5,	 20,	 20,	613,	"UNDERWATER",		10,
  6,	 20,	 20,	614,	"BEST_ESTIMATE",	13,
  7,	 20,	 26,	 -1,	"SPOT_CATEGORY",	13,
  8,	 20,	 26,	  0,	"PHOTOREVISED",		12,
  9,	 50,	 50,	  0,	"PHOTOREVISED",		12,	// Hydrography
 10,	 50,	 50,	 -1,	"RELATION_TO_GROUND",	18,
 11,	 50,	 50,	 -1,	"VERTICAL_RELATION",	17,
 12,	 50,	 50,	 -1,	"BANK",			 4,
 13,	 50,	 50,	 -1,	"OPERATIONAL_STATUS",	18,
 14,	 50,	 50,	608,	"SALT",			 4,
 15,	 50,	 50,	609,	"UNSURVEYED",		10,
 16,	 50,	 50,	610,	"INTERMITTENT",		12,
 17,	 50,	 50,	612,	"SUBMERGED",		 9,
 18,	 50,	 50,	614,	"DRY",			 3,
 19,	 50,	 50,	615,	"MINERAL_OR_HOT",	14,
 20,	 50,	 50,	616,	"NAVIGABLE",		 9,
 21,	 50,	 50,	618,	"EARTHEN",		 7,
 22,	 50,	 50,	619,	"INTERPOLATED",		12,
 23,	 50,	 -1,	 -1,	"ELEVATION",		 9,
 24,	 50,	 53,	 -1,	"ROTATION_ANGLE",	14,
 25,	 50,	 55,	 -1,	"RIVER_MILE",		10,
 26,	 50,	 58,	  0,	"BEST_ESTIMATE",	13,
 27,	 70,	 78,	  0,	"BEST_ESTIMATE",	13,	// Vegetative Surface Cover
 28,	 80,	 80,	  0,	"PHOTOREVISED",		12,	// Non-Vegetative Features
 29,	 80,	 88,	  0,	"BEST_ESTIMATE",	13,
 30,	 90,	 90,	100,	"CIVIL_TOWNSHIP",	14,	// Boundaries
 31,	 90,	 90,	101,	"CITY",			 4,
 32,	 90,	 90,	104,	"NATIONAL_FOREST",	15,
 33,	 90,	 90,	106,	"WILDERNESS_AREA",	15,
 34,	 90,	 90,	135,	"AHUPUAA",		 7,
 35,	 90,	 90,	136,	"HAWAIIAN_HOMESTEAD",	18,
 36,	 90,	 90,	401,	"FEDERALLY_ADMIN",	15,
 37,	 90,	 90,	601,	"IN_DISPUTE",		10,
 38,	 90,	 91,	 -1,	"STATE",		 5,
 39,	 90,	 92,	 -1,	"COUNTY",		 6,
 40,	 90,	 -1,	 -1,	"TOWNSHIP_CODE",	13,
 41,	 90,	 90,	  0,	"PHOTOREVISED",		12,
 42,	 90,	 -1,	 -1,	"MONUMENT_NUMBER",	15,
 43,	 90,	 98,	  0,	"BEST_ESTIMATE",	13,
 44,	150,	151,	 -1,	"STATE",		 5,	// Survey Control
 45,	150,	152,	 -1,	"COUNTY",		 6,
 46,	150,	 -1,	 -1,	"ELEVATION",		 9,
 47,	170,	170,	216,	"ARBITRARY_EXT",	13,	// Roads and Trails
 48,	170,	170,	 -1,	"RELATION_TO_GROUND",	18,
 49,	170,	170,	 -1,	"VERTICAL_RELATION",	17,
 50,	170,	170,	 -1,	"OPERATIONAL_STATUS",	18,
 51,	170,	170,	 -1,	"ACCESS_RESTRICTION",	18,
 52,	170,	170,	605,	"OLD_RAILROAD_GRADE",	18,
 53,	170,	170,	623,	"WITH_RAILROAD",	13,
 54,	170,	170,	624,	"COVERED",		 7,
 55,	170,	170,	600,	"HISTORICAL",		10,
 56,	170,	170,	608,	"LIMITED_ACCESS",	14,
 57,	170,	170,	  0,	"PHOTOREVISED",		12,
 58,	170,	171,	 -1,	"LANES",		 5,
 59,	170,	170,	 -1,	"ROAD_WIDTH",		10,
 60,	170,	178,	  0,	"BEST_ESTIMATE",	13,
 61,	180,	180,	 -1,	"RELATION_TO_GROUND",	18,	// Railroads
 62,	180,	180,	 -1,	"VERTICAL_RELATION",	17,
 63,	180,	180,	 -1,	"OPERATIONAL_STATUS",	18,
 64,	180,	180,	 -1,	"ACCESS_RESTRICTIONS",	19,
 65,	180,	180,	606,	"NARROW_GAUGE",		12,
 66,	180,	180,	607,	"IN_SNOWSHED",		11,
 67,	180,	180,	610,	"RAPID_TRANSIT",	13,
 68,	180,	180,	614,	"JUXTAPOSITION",	13,
 69,	180,	180,	210,	"ARBITRARY_EXT",	13,
 70,	180,	180,	600,	"HISTORICAL",		10,
 71,	180,	180,	  0,	"PHOTOREVISED",		12,
 72,	180,	181,	 -1,	"TRACKS",		 6,
 73,	180,	183,	 -1,	"ROTATION_ANGLE",	14,
 74,	180,	188,	  0,	"BEST_ESTIMATE",	13,
 75,	190,	190,	 -1,	"RELATION_TO_GROUND",	18,	// Pipelines
 76,	190,	190,	 -1,	"OPERATIONAL_STATUS",	18,
 77,	190,	190,	605,	"UNIMPROVED",		10,
 78,	190,	190,	607,	"NUCLEAR",		 7,
 79,	190,	190,	205,	"ARBITRARY_EXT",	13,
 80,	190,	190,	  0,	"PHOTOREVISED",		12,
 81,	190,	193,	 -1,	"ROTATION_ANGLE",	14,
 82,	190,	198,	  0,	"BEST_ESTIMATE",	13,
 83,	190,	196,	 -1,	"STATE",		 5,
 84,	190,	197,	 -1,	"AIRPORT",		 7,
 85,	200,	200,	 -1,	"RELATION_TO_GROUND",	18,	// Manmade Features
 86,	200,	200,	 -1,	"OPERATIONAL_STATUS",	18,
 87,	200,	200,	 -1,	"PRODUCT",		 7,
 88,	200,	200,	608,	"COVERED",		 7,
 89,	200,	200,	 -1,	"TOWER_TYPE",		10,
 90,	200,	200,	615,	"UNINCORPORATED",	14,
 91,	200,	200,	616,	"NO_POPULATION",	13,
 92,	200,	200,	690,	"NATIONAL_CAPITAL",	16,
 93,	200,	200,	691,	"STATE_CAPITAL",	13,
 94,	200,	200,	692,	"COUNTY_SEAT",		11,
 95,	200,	200,	 -1,	"POPULATION_CLASS",	16,
 96,	200,	200,	  0,	"PHOTOREVISED",		12,
 97,	200,	202,	 -1,	"WIDTH",		 5,
 98,	200,	203,	 -1,	"ROTATION_ANGLE",	14,
 99,	200,	208,	  0,	"BEST_ESTIMATE",	13,
100,	200,	206,	 -1,	"STATE",		 5,
101,	200,	207,	 -1,	"POPULATED_PLACE",	15,
102,	300,	300,	 40,	"ID_IN_FIELD",		11,	// Public Land Survey
103,	300,	300,	 41,	"WITH_HORIZONTAL",	15,
104,	300,	300,	 42,	"WITH_ELEVATION",	14,
105,	300,	300,	201,	"APPROXIMATE_POS",	15,
106,	300,	300,	202,	"PROTRACTED_POS",	14,
107,	300,	306,	 -1,	"ORIGIN_OF_SURVEY",	16,
108,	300,	 -1,	 -1,	"TOWNSHIP",		 8,
109,	300,	 -1,	 -1,	"RANGE",		 5,
110,	300,	301,	 -1,	"SECTION",		 7,
111,	300,	307,	 -1,	"LAND_GRANT",		10,
112,	300,	 -1,	 -1,	"MONUMENT_NUMBER",	15,
113,	300,	308,	  0,	"BEST_ESTIMATE",	13,
114,	300,	306,	 -1,	"OHIO_NAMED_SURVEY",	17,
115,	300,	300,	612,	"REFUGEE_LANDS",	13,
116,	190,	190,	605,	"UNPAVED",		 7,
};





/*
 * A primary attribute may have additional attribute characteristics
 * associated with it.
 * Process this addtional attribute information.
 */
long
get_extra_attrib(long category_major, long *major, long *minor, long *major2, long *minor2, struct subfield *subfield)
{
	long i, j;
	double f;
	char save_byte;
	char *end_ptr;


	/*
	 * Do some preliminary checks to avoid a lot of
	 * unnecessary processing.
	 */
	if (subfield->length <= 0)  {
		return -1;
	}
	if ((subfield->length == 1)  && (subfield->value[0] == ' '))  {
		return 1;
	}
	if ((subfield->length == 2) && (subfield->value[0] == ' ') && (subfield->value[1] == ' '))  {
		return 1;
	}

	for (i = 0; i < NUM_FEATURES; i++)  {
		if (features[i].main_major == category_major)  {
			if (strncmp(subfield->label, features[i].feature_name, features[i].feature_name_length) == 0)  {
				break;
			}
		}
	}
	if (i == NUM_FEATURES)  {
		fprintf(stderr, "Couldn't find attribute feature name (%s) for major %d.  Attribute feature ignored.\n", subfield->label, category_major);
		return 1;
	}



	switch (category_major)  {
	case HYPSOGRAPHY:
		switch (features[i].key)  {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 8:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		case 7:
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') && (subfield->value[1] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		}
		break;

	case HYDROGRAPHY:
		switch (features[i].key)  {
		case  9:
	 	case 14:
	 	case 15:
	 	case 16:
	 	case 17:
	 	case 18:
	 	case 19:
	 	case 20:
	 	case 21:
	 	case 22:
	 	case 26:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
	 	case 10:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 601;
			}
	 		else if (subfield->value[0] == 'E')  {
				*minor = 603;
			}
	 		else if (subfield->value[0] == 'T')  {
				*minor = 604;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 11:
			*major = features[i].major;
	 		if (subfield->value[0] == 'O')  {
				*minor = 602;
			}
	 		else if (subfield->value[0] == 'U')  {
				*minor = 617;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 12:
			*major = features[i].major;
	 		if (subfield->value[0] == 'R')  {
				*minor = 605;
			}
	 		else if (subfield->value[0] == 'L')  {
				*minor = 606;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 13:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 607;
			}
	 		else if (subfield->value[0] == 'A')  {
				*minor = 611;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 23:
			if (strncmp(subfield->value, "-9999.99", 8) == 0)  {
				return 1;	// -9999.99 indicates unused
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			f =  strtod(subfield->value, (char **)0);
			subfield->value[subfield->length] = save_byte;
			if (f < 0.0)  {
				*major = 57;
				*minor = -round(f);
			}
			else  {
				*major = 52;
				*minor = round(f);
			}
			return 0;
	 	case 24:
			if (strncmp(subfield->value, "-99", 3) == 0)  {
				return 1;	// -99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 25:
			if (strncmp(subfield->value, "-999.99", 6) == 0)  {
				return 1;	// -999.99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			f =  strtod(subfield->value, (char **)0);
			subfield->value[subfield->length] = save_byte;
			*minor = round(f);
			return 0;
		default:
			break;
		};
		break;

	case VEG_SURFACE_COVER:
		switch (features[i].key)  {
 		case 27:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		default:
			break;
		};
		break;

	case NON_VEG_FEATURES:
		switch (features[i].key)  {
 		case 28:
 		case 29:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		default:
			break;
		};
		break;

	case BOUNDARIES:
		switch (features[i].key)  {
 		case 30:
 		case 31:
 		case 32:
 		case 33:
 		case 34:
 		case 35:
 		case 36:
 		case 37:
 		case 41:
 		case 43:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		case 38:
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 39:
			if ((subfield->length != 3) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 40:
			if ((subfield->length != 5) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ')))  {
				return 1;
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			*major = 93;	// Carries first two digits of 5-digit code
			*major2 = 94;	// Carries last three digits of 5-digit code
			*minor2 = *minor % 1000;
			*minor = *minor / 1000;
			return 0;
		case 42:
			if ((subfield->length != 8) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ') &&
							(subfield->value[5] == ' ') &&
							(subfield->value[6] == ' ') &&
							(subfield->value[7] == ' ')))  {
				return 1;
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			*major = 95;
			/*
			 * So far, when I check the original DLG-3 (non-SDTS) source material,
			 * the monument number attributes aren't included.  In other words, if I comment out
			 * this whole case, the resulting SDTS-to-DLG conversion will match the
			 * original.  If I leave the case uncommented, the conversion won't match
			 * the original.
			 *
			 * Note that there is also a major code 96, for the alphabetic portion of
			 * a monument number.  I'm not sure how this is handled, since I've yet
			 * to find an example.
			 */
			return 0;
		default:
			break;
		};
		break;

	case SURVEY_CONTROL:
		switch (features[i].key)  {
		case 44:
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 45:
			if ((subfield->length != 3) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 46:
			/*
			 * The spec says 9999.99 means not applicable.
			 * This is at odds with other categories of data, which use -9999.99.
			 * Assume that the spec is wrong, since it wouldn't make much sense to
			 * restrict ourselves to elevations that don't equal 9999.99.
			 */
//			if (strncmp(subfield->value, "9999.99", 7) == 0)  {
//				return 1;	// 9999.99 indicates unused
//			}
			if (strncmp(subfield->value, "-9999.99", 8) == 0)  {
				return 1;	// -9999.99 indicates unused
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			f =  strtod(subfield->value, (char **)0);
			subfield->value[subfield->length] = save_byte;
			if (f < 0.0)  {
				*major = 157;
				*minor = -round(f);
			}
			else  {
				*major = 154;
				*minor = round(f);
			}
			return 0;
		default:
			break;
		};
		break;

	case ROADS_AND_TRAILS:
		switch (features[i].key)  {
 		case 47:
 		case 52:
 		case 53:
 		case 54:
 		case 55:
 		case 56:
 		case 57:
 		case 60:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		case 48:
			*major = features[i].major;
	 		if (subfield->value[0] == 'T')  {
				*minor = 601;
			}
	 		else if (subfield->value[0] == 'S')  {
				*minor = 606;
			}
	 		else if (subfield->value[0] == 'D')  {
				*minor = 612;
			}
	 		else if (subfield->value[0] == 'E')  {
				*minor = 614;
			}
	 		else if (subfield->value[0] == 'R')  {
				*minor = 618;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 49:
			*major = features[i].major;
	 		if (subfield->value[0] == 'O')  {
				*minor = 602;
			}
	 		else if (subfield->value[0] == 'U')  {
				*minor = 607;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 50:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 603;
			}
	 		else if (subfield->value[0] == 'X')  {
				*minor = 604;
			}
	 		else if (subfield->value[0] == 'P')  {
				*minor = 611;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 51:
			*major = features[i].major;
	 		if (subfield->value[0] == 'T')  {
				*minor = 609;
			}
	 		else if (subfield->value[0] == 'P')  {
				*minor = 610;
			}
			else  {
				return 1;
			}
			return 0;
		case 58:
			/*
			 * The spec says this field is all blanks if unused.
			 * However, sample files have it as "-9".
			 * Check it both ways.
			 */
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ')))  {
				return 1;
			}
			if (strncmp(subfield->value, "-9", 2) == 0)  {
				return 1;	// -9 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 59:
			if (strncmp(subfield->value, "-99", 3) == 0)  {
				return 1;	// -99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = 600 + strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		};
		break;

	case RAILROADS:
		switch (features[i].key)  {
 		case 65:
 		case 66:
 		case 67:
 		case 68:
 		case 69:
 		case 70:
 		case 71:
 		case 74:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		case 61:
			*major = features[i].major;
	 		if (subfield->value[0] == 'T')  {
				*minor = 601;
			}
	 		else if (subfield->value[0] == 'E')  {
				*minor = 609;
			}
	 		else if (subfield->value[0] == 'R')  {
				*minor = 611;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 62:
			*major = features[i].major;
	 		if (subfield->value[0] == 'O')  {
				*minor = 602;
			}
	 		else if (subfield->value[0] == 'U')  {
				*minor = 605;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 63:
			*major = features[i].major;
	 		if (subfield->value[0] == 'A')  {
				*minor = 603;
			}
	 		else if (subfield->value[0] == 'D')  {
				*minor = 604;
			}
	 		else if (subfield->value[0] == 'U')  {
				*minor = 608;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 64:
			*major = features[i].major;
	 		if (subfield->value[0] == 'P')  {
				*minor = 612;
			}
	 		else if (subfield->value[0] == 'G')  {
				*minor = 613;
			}
			else  {
				return 1;
			}
			return 0;
		case 72:
			if (strncmp(subfield->value, "-9", 2) == 0)  {
				return 1;	// -9 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 73:
			if (strncmp(subfield->value, "-99", 3) == 0)  {
				return 1;	// -99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		};
		break;

	case PIPE_TRANS_LINES:
		switch (features[i].key)  {
 		case 77:
 		case 78:
 		case 79:
 		case 80:
 		case 82:
 		case 116:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
	 	case 75:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 600;
			}
	 		else if (subfield->value[0] == 'A')  {
				*minor = 603;
			}
	 		else if (subfield->value[0] == 'S')  {
				*minor = 606;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 76:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 601;
			}
	 		else if (subfield->value[0] == 'A')  {
				*minor = 602;
			}
	 		else if (subfield->value[0] == 'C')  {
				*minor = 604;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 81:
			if (strncmp(subfield->value, "-99", 3) == 0)  {
				return 1;	// -99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 83:
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 84:
			if ((subfield->length != 4) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		};
		break;

	case MANMADE_FEATURES:
		switch (features[i].key)  {
 		case 88:
 		case 90:
 		case 91:
 		case 92:
 		case 93:
 		case 94:
 		case 96:
 		case 99:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
	 	case 85:
			*major = features[i].major;
	 		if (subfield->value[0] == 'U')  {
				*minor = 601;
			}
	 		else if (subfield->value[0] == 'S')  {
				*minor = 617;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 86:
			*major = features[i].major;
	 		if (subfield->value[0] == 'C')  {
				*minor = 602;
			}
	 		else if (subfield->value[0] == 'A')  {
				*minor = 603;
			}
	 		else if (subfield->value[0] == 'R')  {
				*minor = 618;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 87:
			*major = features[i].major;
	 		if (subfield->value[0] == 'W')  {
				*minor = 604;
			}
	 		else if (subfield->value[0] == 'O')  {
				*minor = 605;
			}
	 		else if (subfield->value[0] == 'G')  {
				*minor = 606;
			}
	 		else if (subfield->value[0] == 'C')  {
				*minor = 607;
			}
	 		else if (subfield->value[0] == 'V')  {
				*minor = 609;
			}
	 		else if (subfield->value[0] == 'S')  {
				*minor = 610;
			}
	 		else if (subfield->value[0] == 'L')  {
				*minor = 611;
			}
	 		else if (subfield->value[0] == 'B')  {
				*minor = 612;
			}
	 		else if (subfield->value[0] == 'A')  {
				*minor = 619;
			}
	 		else if (subfield->value[0] == 'H')  {
				*minor = 620;
			}
	 		else if (subfield->value[0] == 'I')  {
				*minor = 621;
			}
	 		else if (subfield->value[0] == 'P')  {
				*minor = 622;
			}
	 		else if (subfield->value[0] == 'E')  {
				*minor = 623;
			}
	 		else if (subfield->value[0] == 'R')  {
				*minor = 624;
			}
			else  {
				return 1;
			}
			return 0;
	 	case 89:
			*major = features[i].major;
	 		if (subfield->value[0] == 'R')  {
				*minor = 613;
			}
	 		else if (subfield->value[0] == 'L')  {
				*minor = 614;
			}
			else  {
				return 1;
			}
			return 0;
		case 95:
			if (strncmp(subfield->value, "-9", 2) == 0)  {
				return 1;	// -9 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = 680 + strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 97:
			if (strncmp(subfield->value, "-999", 4) == 0)  {
				return 1;	// -999 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
	 	case 98:
			if (strncmp(subfield->value, "-99", 3) == 0)  {
				return 1;	// -99 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 100:
			// Only for 2M-scale DLGs.
			if ((subfield->length != 2) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 101:
			// Not sure this is handled properly, but it is only for 2M-scale DLGs.
			if ((subfield->length != 4) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			// Minor code may be an alphabetic database key.  Don't know.
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		};
		break;

	case PUBLIC_LAND_SURVEYS:
		switch (features[i].key)  {
 		case 102:
 		case 103:
 		case 104:
 		case 105:
 		case 106:
 		case 113:
 		case 115:
			if (subfield->value[0] == 'Y')  {
				*major = features[i].major;
				*minor = features[i].minor;
				return 0;
			}
			else  {
				return 1;
			}
		case 107:
		case 114:
			if (strncmp(subfield->value, "-9", 2) == 0)  {
				return 1;	// -9 indicates unused
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor = strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 108:
			if ((subfield->length != 8) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ') &&
							(subfield->value[5] == ' ') &&
							(subfield->value[6] == ' ') &&
							(subfield->value[7] == ' ')))  {
				return 1;
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, &end_ptr, 10);
			/*
			 * If there is a fractional number, we need to add in the special code
			 * for it.
			 */
			if (strncmp(end_ptr, " 1/4", 4) == 0)  {
				end_ptr += 4;
				*minor += 2000;
			}
			else if (strncmp(end_ptr, " 1/2", 4) == 0)  {
				end_ptr += 4;
				*minor += 4000;
			}
			else if (strncmp(end_ptr, " 3/4", 4) == 0)  {
				end_ptr += 4;
				*minor += 6000;
			}
			while ((*end_ptr != '\0') && (*end_ptr == ' '))  { end_ptr++; }
			if (*end_ptr != '\0')  {
				if (*end_ptr == 'N')  {
					*major = 302;
				}
				else if (*end_ptr == 'S')  {
					*major = 303;
				}
				else  {
					fprintf(stderr, "Warning:  Township number (SDTS=%.*s) has an unknown form.  Assuming this is a northern township.\n", subfield->length, subfield->value);
					*major = 302;
				}
			}
			else  {
				fprintf(stderr, "Warning:  Township number (SDTS=%.*s) has no N/S designator.  N assumed.\n", subfield->length, subfield->value);
				*major = 302;
			}
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 109:
			if ((subfield->length != 8) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ') &&
							(subfield->value[5] == ' ') &&
							(subfield->value[6] == ' ') &&
							(subfield->value[7] == ' ')))  {
				return 1;
			}
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, &end_ptr, 10);
			/*
			 * If there is a fractional number, we need to add in the special digit for it at the
			 * beginning of the minor attribute code.  Note that there are theoretically also
			 * two other special digits:  8 for duplicate to north
			 * or east of original township, 9 for triplicate to north or east of original
			 * township.  As yet, I don't know if or how these latter two codes are encoded
			 * in SDTS, so they aren't handled here.
			 *
			 * I have also found some odd range specifiers in the file:
			 *
			 *       DLG/LARGE_SCALE/H/hooven_OH/public_lands/PL01APLF.DDF
			 *
			 * that take the form "1AE" and "2AE".  I have no idea what the "A" is for.
			 * So far, out of several thousands of files checked, this is the only file that I
			 * have found these in.  Until I find out otherwise, I am assuming that these
			 * are an error.  However, since there are 14 instances in this one SDTS file,
			 * error may not be the correct explanation.
			 */
			if (strncmp(end_ptr, " 1/4", 4) == 0)  {
				end_ptr += 4;
				*minor += 2000;
			}
			else if (strncmp(end_ptr, " 1/2", 4) == 0)  {
				end_ptr += 4;
				*minor += 4000;
			}
			else if (strncmp(end_ptr, " 3/4", 4) == 0)  {
				end_ptr += 4;
				*minor += 6000;
			}
			while ((*end_ptr != '\0') && (*end_ptr == ' '))  { end_ptr++; }
			if (*end_ptr != '\0')  {
				if (*end_ptr == 'E')  {
					*major = 304;
				}
				else if (*end_ptr == 'W')  {
					*major = 305;
				}
				else  {
					fprintf(stderr, "Warning:  Range number (SDTS=%.*s) has an unknown form.  Assuming this is an eastern range.\n", subfield->length, subfield->value);
					*major = 304;
				}
			}
			else  {
				fprintf(stderr, "Warning:  Range number (SDTS=%.*s) has no E/W designator.  E assumed.\n", subfield->length, subfield->value);
				*major = 304;
			}
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 110:
			/*
			 * There are theoretically some possible alphabetic modifiers
			 * for this case, but I have yet to find a file that contains any.
			 */
			if ((subfield->length != 4) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 111:
			/*
			 * There are theoretically some possible alphabetic modifiers
			 * for this case, but I have yet to find a file that contains any.
			 */
			if ((subfield->length != 4) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ')))  {
				return 1;
			}
			*major = features[i].major;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		case 112:
			/*
			 * In theory, there can be monument numbers that contain
			 * fractions of 1/2.  They require an extra "300 0625" to
			 * be emitted.  I have yet to find an example, so we don't
			 * handle such fractions yet.
			 */
			if ((subfield->length != 8) || ((subfield->value[0] == ' ') &&
							(subfield->value[1] == ' ') &&
							(subfield->value[2] == ' ') &&
							(subfield->value[3] == ' ') &&
							(subfield->value[4] == ' ') &&
							(subfield->value[5] == ' ') &&
							(subfield->value[6] == ' ') &&
							(subfield->value[7] == ' ')))  {
				return 1;
			}
			*major = 308;
			save_byte = subfield->value[subfield->length]; subfield->value[subfield->length] = '\0';
			*minor =  strtol(subfield->value, (char **)0, 10);
			subfield->value[subfield->length] = save_byte;
			return 0;
		default:
			break;
		};
		break;

	default:
		fprintf(stderr, "Couldn't find attribute feature name (%s).  Attribute feature ignored.  Internal codes: %d,%d\n", subfield->label, i, features[i].key);
		return 1;
		break;
	}

	return 1;
}




/*
 * Read in all of the attribute files that affect this SDTS transfer.
 *
 * The process returns the number of attribute file entries in the attrib_files structure.
 */
long
process_attrib_sdts(char *passed_file_name, char *category_name, long *data_type, long *color, long gz_flag, long upper_case_flag)
{
	struct subfield subfield;
	long i, j, k;
	double f;
	long num_attrib_files;
	long file_name_length;
	char file_name[MAX_FILE_NAME + 1];
	long current_size;
	char type[2];
	long parse_type;
	long record_id;
	char save_byte;
	char *ptr;
	long major, minor;
	long major2, minor2;


	num_attrib_files = 0;
	file_name_length = strlen(passed_file_name);
	file_name[MAX_FILE_NAME] = '\0';


	/*
	 * There are separate SDTS DLG files containing data records for
	 * Nodes, Areas, and Lines.  We are going to open all of them and
	 * search them for the attribute files that they reference.
	 * The relevant modules are LE??, PC??, NE??, and NO??.
	 * We will eventually read these line/area/node files again to get their other information.
	 *
	 * The Line, Area, and Node files reference the attribute Record IDs to attach
	 * attributes to lines, areas, and nodes (degenerate lines) so we need this
	 * information before we read in the Line, Area, and Node files, so that we
	 * can build complete entries for each of the Nodes, Lines, and Areas.
	 *
	 * We store the attributes from each file in Record ID order.  (The Record IDs don't have
	 * to be sequential numbers, under the SDTS standard, but we assume that they
	 * are because the attribute files would have had to be generated from scratch during
	 * conversion to SDTS.
	 *
	 * If attributes are used, they will be used in one of the four (NE??, PC??, NE??, NO??)
	 * files.  Note that, while the main primary attribute file is named A??F, there may
	 * be other primary attribute files, named ACOI (attributes that describe
	 * concident features), AHPR (elevation attributes, in meters, for hypsography files),
	 * AHPT (elevation attributes, in feet, for hypsography files), ARDM (route numbers
	 * and route types for roads and trails), or ABDM (agency attributes for boundary
	 * data, mostly for 2M-scale files, but occasionally for 100K-scale files).
	 * (There are several types of secondary attribute files too, but they are only used for
	 * 2,000,000 scale DLG data, so we ignore them.)
	 *
	 * We could also obtain the name of the attribute files by reading the CATD module.
	 * By doing it the way we do here, we find out if any attributes are actually used.
	 * The code was written the way it is because there might be multiple primary
	 * attribute files in a given transfer, which would make the
	 * CATD information ambiguous.  For example, a Transportation transfer could
	 * contain ARDF, AMTF, and ARRF for roads, misc features, and railroads.  Generally,
	 * we should only need one of these to process a given LE?? module.
	 */
	/*
	 * Open the LE?? module in preparation for parsing.
	 */
	if (begin_ddf(passed_file_name) < 0)  {
		fprintf(stderr, "Can't open %s for reading, errno = %d\n", passed_file_name, errno);
		exit(0);
	}
	/*
	 * Loop through the subfields until we find what we want.
	 */
	while (get_subfield(&subfield) != 0)  {
		if (strcmp(subfield.tag, "ATID") == 0)  {
			if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
				if (subfield.length == 4)  {
					for (i = 0; i < num_attrib_files; i++)  {
						if ((attrib_files[i].module_name[0] == subfield.value[0]) &&
						    (attrib_files[i].module_name[1] == subfield.value[1]) &&
						    (attrib_files[i].module_name[2] == subfield.value[2]) &&
						    (attrib_files[i].module_name[3] == subfield.value[3]))  {
							break;
						}
					}
					if (i == num_attrib_files)  {
						if (num_attrib_files == MAX_ATTRIB_FILES)  {
							fprintf(stderr, "Ran out of space for attribute file names.\n");
							break;
						}
						attrib_files[i].module_name[0] = subfield.value[0];
						attrib_files[i].module_name[1] = subfield.value[1];
						attrib_files[i].module_name[2] = subfield.value[2];
						attrib_files[i].module_name[3] = subfield.value[3];
						num_attrib_files++;
					}
				}
				else  {
					fprintf(stderr, "Attribute module ID %.*s does not appear correct.\n", subfield.length, subfield.value);
				}
			}
		}
	}
	/* We are done with this file, so close it. */
	end_ddf();



	/* Now go on the the PC?? module (the polygon module). */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'p';
			file_name[file_name_length - 10] = 'c';
		}
		else  {
			file_name[file_name_length -  8] = 'p';
			file_name[file_name_length -  7] = 'c';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'P';
			file_name[file_name_length - 10] = 'C';
		}
		else  {
			file_name[file_name_length -  8] = 'P';
			file_name[file_name_length -  7] = 'C';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) >= 0)  {
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "ATID") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
					if (subfield.length == 4)  {
						for (i = 0; i < num_attrib_files; i++)  {
							if ((attrib_files[i].module_name[0] == subfield.value[0]) &&
							    (attrib_files[i].module_name[1] == subfield.value[1]) &&
							    (attrib_files[i].module_name[2] == subfield.value[2]) &&
							    (attrib_files[i].module_name[3] == subfield.value[3]))  {
								break;
							}
						}
						if (i == num_attrib_files)  {
							if (num_attrib_files == MAX_ATTRIB_FILES)  {
								fprintf(stderr, "Ran out of space for attribute file names.\n");
								break;
							}
							attrib_files[i].module_name[0] = subfield.value[0];
							attrib_files[i].module_name[1] = subfield.value[1];
							attrib_files[i].module_name[2] = subfield.value[2];
							attrib_files[i].module_name[3] = subfield.value[3];
							num_attrib_files++;
						}
					}
					else  {
						fprintf(stderr, "Attribute module ID %.*s does not appear correct.\n", subfield.length, subfield.value);
					}
				}
			}
		}
		/* We are done with this file, so close it. */
		end_ddf();
	}



	/* Now go on the the NO?? module (the planar-node module). */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'o';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'o';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'O';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'O';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) >= 0)  {
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "ATID") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
					if (subfield.length == 4)  {
						for (i = 0; i < num_attrib_files; i++)  {
							if ((attrib_files[i].module_name[0] == subfield.value[0]) &&
							    (attrib_files[i].module_name[1] == subfield.value[1]) &&
							    (attrib_files[i].module_name[2] == subfield.value[2]) &&
							    (attrib_files[i].module_name[3] == subfield.value[3]))  {
								break;
							}
						}
						if (i == num_attrib_files)  {
							if (num_attrib_files == MAX_ATTRIB_FILES)  {
								fprintf(stderr, "Ran out of space for attribute file names.\n");
								break;
							}
							attrib_files[i].module_name[0] = subfield.value[0];
							attrib_files[i].module_name[1] = subfield.value[1];
							attrib_files[i].module_name[2] = subfield.value[2];
							attrib_files[i].module_name[3] = subfield.value[3];
							num_attrib_files++;
						}
					}
					else  {
						fprintf(stderr, "Attribute module ID %.*s does not appear correct.\n", subfield.length, subfield.value);
					}
				}
			}
		}
		/* We are done with this file, so close it. */
		end_ddf();
	}



	/* Now go on the the NE?? module (the node-entity module). */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'n';
			file_name[file_name_length - 10] = 'e';
		}
		else  {
			file_name[file_name_length -  8] = 'n';
			file_name[file_name_length -  7] = 'e';
		}
	}
	else  {
		if (gz_flag != 0)  {
			file_name[file_name_length - 11] = 'N';
			file_name[file_name_length - 10] = 'E';
		}
		else  {
			file_name[file_name_length -  8] = 'N';
			file_name[file_name_length -  7] = 'E';
		}
	}
	/*
	 * Open the file in preparation for parsing.
	 */
	if (begin_ddf(file_name) >= 0)  {
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "ATID") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
					if (subfield.length == 4)  {
						for (i = 0; i < num_attrib_files; i++)  {
							if ((attrib_files[i].module_name[0] == subfield.value[0]) &&
							    (attrib_files[i].module_name[1] == subfield.value[1]) &&
							    (attrib_files[i].module_name[2] == subfield.value[2]) &&
							    (attrib_files[i].module_name[3] == subfield.value[3]))  {
								break;
							}
						}
						if (i == num_attrib_files)  {
							if (num_attrib_files == MAX_ATTRIB_FILES)  {
								fprintf(stderr, "Ran out of space for attribute file names.\n");
								break;
							}
							attrib_files[i].module_name[0] = subfield.value[0];
							attrib_files[i].module_name[1] = subfield.value[1];
							attrib_files[i].module_name[2] = subfield.value[2];
							attrib_files[i].module_name[3] = subfield.value[3];
							num_attrib_files++;
						}
					}
					else  {
						fprintf(stderr, "Attribute module ID %.*s does not appear correct.\n", subfield.length, subfield.value);
					}
				}
			}
		}
		/* We are done with this file, so close it. */
		end_ddf();
	}



	/*
	 * Check to see that we didn't get any unknown file types.
	 *
	 * There are additional file types for 2,000,000-scale files,
	 * but we don't handle such files so ignore those file types.
	 */
	for (i = 0; i < num_attrib_files; i++)  {
		if ((strncmp(attrib_files[i].module_name, "AHPF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "AHYF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ASCF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ANVF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ABDF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "AMTF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ARDF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ARRF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "AMSF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ASMF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "APLF", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ACOI", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "AHPR", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "AHPT", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ABDM", 4) != 0) &&
		    (strncmp(attrib_files[i].module_name, "ARDM", 4) != 0))  {
			fprintf(stderr, "Unknown attribute file type (%s).  File type ignored.\n", attrib_files[i].module_name);
			attrib_files[i].module_name[0] = attrib_files[num_attrib_files - 1].module_name[0];
			attrib_files[i].module_name[1] = attrib_files[num_attrib_files - 1].module_name[1];
			attrib_files[i].module_name[2] = attrib_files[num_attrib_files - 1].module_name[2];
			attrib_files[i].module_name[3] = attrib_files[num_attrib_files - 1].module_name[3];
			num_attrib_files--;
		}
	}



	/*
	 * The two middle characters of the Attribute Features file name
	 * tell us the type of data in this SDTS theme.
	 *
	 * We can't reliably get this from the file name or category name
	 * because Transportation files may have TR in their file name
	 * or TRANSPORTATION as a category name.  (If we could pry it out,
	 * we updated the category name from TRANSPORTATION to the proper
	 * theme by looking in CATS.  However, we may have failed in the update.)
	 * We need the more detailed RD, RR, or MT designators for transportation.
	 *
	 * Check that we have one and only one main Primary Attribute File.
	 * If so, then get the theme from the module name.
	 * If there is no main Primary Attribute File, or more than one, then
	 * do our best to deduce the data type from the category name or
	 * the passed_file_name.
	 *
	 * This may seem like a lot of work for a piece of data of so-so
	 * importance.  However, we choose the color scheme of the map
	 * based on this data, and it would be quite confusing to get
	 * the color wrong.
	 */
	j = 0;
	for (i = 0; i < num_attrib_files; i++)  {
		if (attrib_files[i].module_name[3] == 'F')  {
			j++;
			k = i;
		}
	}
	if (j != 1)  {
		if (j > 1)  {
			fprintf(stderr, "Warning:  More than one main primary attribute file.  Handling ambiguity as best I can.\n");
		}
		if ((category_name[0] != '\0') && (category_name[1] != '\0'))  {
			switch(category_name[0])  {
			case 'B':	/* BOUNDARIES */
				type[0] = 'B';
				type[1] = 'D';
				break;
			case 'H':
				type[0] = 'H';
				if (category_name[2] == 'D')  {
					/* HYDROGRAPHY */
					type[1] = 'Y';
					break;
				}
				else  {
					/* HYPSOGRAPHY */
					type[1] = 'P';
					break;
				}
			case 'P':
				if (category_name[1] == 'I')  {
					/* PIPE & TRANS LINES */
					type[0] = 'M';
					type[1] = 'T';
					break;
				}
				else  {
					/* PUBLIC LAND SURVEYS */
					type[0] = 'P';
					type[1] = 'L';
					break;
				}
			case 'R':
				type[0] = 'R';
				if (category_name[1] == 'A')  {
					/* RAILROADS */
					type[1] = 'R';
					break;
				}
				else  {
					/* ROADS AND TRAILS */
					type[1] = 'D';
					break;
				}
			case 'M':	/* MANMADE FEATURES */
				type[0] = 'M';
				type[1] = 'S';
				break;
			case 'S':	/* SURVEY CONTROL */
				type[0] = 'S';
				type[1] = 'M';
				break;
			case 'V':	/* VEG SURFACE COVER */
				type[0] = 'S';
				type[1] = 'C';
				break;
			case 'N':	/* NON-VEG FEATURES */
				type[0] = 'N';
				type[1] = 'V';
				break;
			default:
				fprintf(stderr, "Unknown theme %20.20s\n", category_name);
				exit(0);
				break;
			}
		}
		else  {
			if ((gz_flag != 0) && (file_name_length >= 15))  {
				type[0] = toupper(passed_file_name[file_name_length - 15]);
				type[1] = toupper(passed_file_name[file_name_length - 14]);
			}
			else if ((gz_flag == 0) && (file_name_length >= 12))  {
				type[0] = toupper(passed_file_name[file_name_length - 12]);
				type[1] = toupper(passed_file_name[file_name_length - 11]);
			}
			else  {
				type[0] = '\0';
				type[1] = '\0';
			}
		}
	}
	else {
		type[0] = attrib_files[k].module_name[1];
		type[1] = attrib_files[k].module_name[2];
	}
	switch(type[0])  {
	case 'B':	/* BD: BOUNDARIES */
		*color = GRAY;
		*data_type = BOUNDARIES;
		break;
	case 'H':
		if (type[1] == 'Y')  {
			/* HY: HYDROGRAPHY */
			*color = B_BLUE;
			*data_type = HYDROGRAPHY;
			break;
		}
		else  {
			/* HP: HYPSOGRAPHY */
			*color = L_ORANGE;
			*data_type = HYPSOGRAPHY;
			break;
		}
	case 'P':	/* PL: US PUBLIC LAND SURVEY SYSTEM */
		*color = BLACK;
		*data_type = PUBLIC_LAND_SURVEYS;
		break;
	case 'R':
		if (type[1] == 'R')  {
			/* RR: RAILROADS */
			*color = BLACK;
			*data_type = RAILROADS;
			break;
		}
		else  {
			/* RD: ROADS AND TRAILS */
			*color = B_RED;
			*data_type = ROADS_AND_TRAILS;
			break;
		}
	case 'T':	/* TR: general transportation features.  Roads and trails assumed. */
		*color = B_RED;
		*data_type = ROADS_AND_TRAILS;
		break;
	case 'M':
		if (type[1] == 'T')  {
			/* MT: PIPELINES, TRANSMISSION LINES, and MISC TRANSPORTATION FEATURES */
			*color = BLACK;
			*data_type = PIPE_TRANS_LINES;
		}
		else  {
			/* MS: MANMADE FEATURES */
			*color = BLACK;
			*data_type = MANMADE_FEATURES;
		}
		break;
	case 'S':
		if (type[1] == 'C')  {
			/* SC: VEGETATIVE SURFACE COVER */
			*color = B_GREEN;
			*data_type = VEG_SURFACE_COVER;
		}
		else  {
			/* SM: SURVEY CONTROL AND MARKERS */
			*color = BLACK;
			*data_type = SURVEY_CONTROL;
		}
		break;
	case 'N':	/* NV: NON-VEG FEATURES */
		*color = BLACK;
		*data_type = NON_VEG_FEATURES;
		break;
	default:
		fprintf(stderr, "Unknown data type %c%c, assuming Boundaries\n", type[0], type[1]);
		*color = BLACK;
		*data_type = BOUNDARIES;
		break;
	}



	/*
	 * At this point, we should have a complete list of attribute files
	 * that need to be read in.  Thus, we go ahead and do the reading.
	 */
	for (i = 0; i < num_attrib_files; i++)  {
		attrib_files[i].num_attrib = -1;
		attrib_files[i].attrib = (struct attribute_list *)0;
		current_size = 0;


		/*
		 * Convert the module name into a single number for later use.
		 */
		switch (attrib_files[i].module_name[3])  {
		case 'F':	// main Primary Attribute Module.  (Files with names of the form A??F.)
			parse_type = 0;
			break;
		case 'I':	// Coincidence Attribute Primary Module.  (Files with names of the form ACOI.)
			parse_type = 1;
			break;
		case 'R':	// Elevation Attribute Primary Module (meters). (Files with names of the form AHPR.)
			parse_type = 2;
			break;
		case 'T':	// Elevation Attribute Primary Module (feet). (Files with names of the form AHPT.)
			parse_type = 3;
			break;
		case 'M':
			if (attrib_files[i].module_name[1] == 'R')  {
				// Route Attribute Primary Module. (Files with names of the form ARDM.)
				parse_type = 4;
			}
			else  {
				// Agency Attribute Primary Module. (Files with names of the form ABDM.)
				parse_type = 5;
			}
			break;
		default:
			fprintf(stderr, "Unknown attribute file type (%s).  Should have been detected earlier.\n", attrib_files[i].module_name);
			exit(0);
			break;
		}


		/*
		 * Generate the file name to be opened.
		 */
		strncpy(file_name, passed_file_name, MAX_FILE_NAME);
		if (upper_case_flag == 0)  {
			if (gz_flag != 0)  {
				file_name[file_name_length - 11] = tolower(attrib_files[i].module_name[0]);
				file_name[file_name_length - 10] = tolower(attrib_files[i].module_name[1]);
				file_name[file_name_length -  9] = tolower(attrib_files[i].module_name[2]);
				file_name[file_name_length -  8] = tolower(attrib_files[i].module_name[3]);
			}
			else  {
				file_name[file_name_length -  8] = tolower(attrib_files[i].module_name[0]);
				file_name[file_name_length -  7] = tolower(attrib_files[i].module_name[1]);
				file_name[file_name_length -  6] = tolower(attrib_files[i].module_name[2]);
				file_name[file_name_length -  5] = tolower(attrib_files[i].module_name[3]);
			}
		}
		else  {
			if (gz_flag != 0)  {
				file_name[file_name_length - 11] = attrib_files[i].module_name[0];
				file_name[file_name_length - 10] = attrib_files[i].module_name[1];
				file_name[file_name_length -  9] = attrib_files[i].module_name[2];
				file_name[file_name_length -  8] = attrib_files[i].module_name[3];
			}
			else  {
				file_name[file_name_length -  8] = attrib_files[i].module_name[0];
				file_name[file_name_length -  7] = attrib_files[i].module_name[1];
				file_name[file_name_length -  6] = attrib_files[i].module_name[2];
				file_name[file_name_length -  5] = attrib_files[i].module_name[3];
			}
		}
		/*
		 * Open and process the file.
		 */
		if (begin_ddf(file_name) >= 0)  {
			while (get_subfield(&subfield) != 0)  {
				if (strcmp(subfield.tag, "ATPR") == 0)  {
					if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "MODN") == 0))  {
						if (subfield.length != 4)  {
							fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
							continue;
						}
						if (strncmp(subfield.value, attrib_files[i].module_name, 4) != 0)  {
							fprintf(stderr, "Module name in record (%.*s) doesn't match global module name.  Entry ignored.\n",
									subfield.length, subfield.value);
							continue;
						}
					}
					else if ((strstr(subfield.format, "I") != (char *)0) && (strcmp(subfield.label, "RCID") == 0))  {
						save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
						record_id = strtol(subfield.value, (char **)0, 10);
						subfield.value[subfield.length] = save_byte;

						/*
						 * Hopefully, record IDs won't backtrack very often,
						 * but I have seen it happen.  Print a warning if it
						 * does happen.
						 */
						if ((record_id - 1) < attrib_files[i].num_attrib)  {
							fprintf(stderr, "Warning:  Record IDs don't appear to be sequential in file %s.  Some attributes may be lost or corrupted.\n", file_name);
						}
						else  {
							attrib_files[i].num_attrib++;

							/*
							 * If the record_id numbers exceed the number of attribs,
							 * then make a hole in the attrib table to accommodate them.
							 */
							if ((record_id - 1) > attrib_files[i].num_attrib)  {
								attrib_files[i].num_attrib = record_id - 1;
							}

							/*
							 * If we need more space, get it.
							 */
							if (attrib_files[i].num_attrib > (current_size - 1))  {
								current_size = attrib_files[i].num_attrib + 100;
								attrib_files[i].attrib = (struct attribute_list *)realloc(attrib_files[i].attrib, sizeof(struct attribute_list) * current_size);
								if (attrib_files[i].attrib == (struct attribute_list *)0)  {
									fprintf(stderr, "realloc of attrib_files[].attrib failed.\n");
									exit(0);
								}
							}

							/*
							 * Null out all of the attributes for the new entry.
							 */
							for (j = 0; j < MAX_EXTRA; j++)  {
								attrib_files[i].attrib[attrib_files[i].num_attrib].minor[j] = 0;
								attrib_files[i].attrib[attrib_files[i].num_attrib].major[j] = 0;
							}
						}
					}
				}
				else if (strcmp(subfield.tag, "ATTP") == 0)  {
					if (attrib_files[i].num_attrib < 0)  {
						fprintf(stderr, "Attribute labels out of sequence in %s.\n", file_name);
						exit(0);
					}
					switch (parse_type)  {
					case 0:	// main Primary Attribute Module.  (Modules with names of the form A??F.)
						if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "ENTITY_LABEL", 12) == 0))  {
							if (subfield.length == 7)  {
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								attrib_files[i].attrib[record_id - 1].minor[0] = strtol(&subfield.value[3], (char **)0, 10);
								subfield.value[subfield.length] = save_byte;
		
								save_byte = subfield.value[3]; subfield.value[3] = '\0';
								attrib_files[i].attrib[record_id - 1].major[0] =  strtol(subfield.value, (char **)0, 10);
								subfield.value[3] = save_byte;
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							/*
							 * Rather than have multiple attribute codes, the SDTS TVP
							 * modules encode extra attributes as additional features
							 * of the primary attribute, as part of the single ISO 8211
							 * record associated with the attribute.  (Occasionally there may still be
							 * multiple primary attributes, and hence multiple ISO 8211 records.)
							 * Because the code to parse the extra information is quite bulky,
							 * it is relegated to a separate function.
							 *
							 * If we haven't already found an attribute, then there is something
							 * wrong.  Avoid a core dump by checking num_attrib.
							 */
							major2 = 0;
							if (get_extra_attrib(*data_type, &major, &minor, &major2, &minor2, &subfield) == 0)  {
								for (j = 0; j < MAX_EXTRA; j++)  {
									if (attrib_files[i].attrib[record_id - 1].major[j] == 0)  {
										break;
									}
								}
								if (j == MAX_EXTRA)  {
									fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
									continue;
								}
								attrib_files[i].attrib[record_id - 1].major[j] = major;
								attrib_files[i].attrib[record_id - 1].minor[j] = minor;
								if (major2 != 0)  {
									if ((j + 1) == MAX_EXTRA)  {
										fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
										continue;
									}
									attrib_files[i].attrib[record_id - 1].major[j + 1] = major2;
									attrib_files[i].attrib[record_id - 1].minor[j + 1] = minor2;
								}
							}
						}
						break;
					case 1:	// Coincidence Attribute Primary Module.  (Modules with names of the form ACOI.)
						if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "COINCIDENT", 10) == 0))  {
							if (subfield.length == 2)  {
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								attrib_files[i].attrib[record_id - 1].minor[0] = strtol(subfield.value, (char **)0, 10);
								subfield.value[subfield.length] = save_byte;
		
								attrib_files[i].attrib[record_id - 1].major[0] =  *data_type + 9;
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							fprintf(stderr, "Unrecognized attribute label (%s) in file %s.\n", subfield.label, file_name);
						}
						break;
					case 2:	// Elevation Attribute Primary Module (meters). (Modules with names of the form AHPR.)
						if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "ELEVATION", 9) == 0))  {
							if (subfield.length == 8)  {
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								f = strtod(subfield.value, (char **)0);
								subfield.value[subfield.length] = save_byte;
								if (f < 0.0)  {
									attrib_files[i].attrib[record_id - 1].major[0] = 25;
									attrib_files[i].attrib[record_id - 1].minor[0] = -f;
								}
								else  {
									attrib_files[i].attrib[record_id - 1].major[0] = 24;
									attrib_files[i].attrib[record_id - 1].minor[0] = f;
								}
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							fprintf(stderr, "Unrecognized attribute label (%s) in file %s.\n", subfield.label, file_name);
						}
						break;
					case 3:	// Elevation Attribute Primary Module (feet). (Modules with names of the form AHPT.)
						if ((strstr(subfield.format, "R") != (char *)0) && (strncmp(subfield.label, "ELEVATION", 9) == 0))  {
							if (subfield.length == 8)  {
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								f = strtod(subfield.value, (char **)0);
								subfield.value[subfield.length] = save_byte;
								if (f < 0.0)  {
									attrib_files[i].attrib[record_id - 1].major[0] = 23;
									attrib_files[i].attrib[record_id - 1].minor[0] = -f;
								}
								else  {
									if (f > 9999.0)  {
										attrib_files[i].attrib[record_id - 1].major[0] = 21;
										attrib_files[i].attrib[record_id - 1].minor[0] = f - 10000.0;
									}
									else  {
										attrib_files[i].attrib[record_id - 1].major[0] = 22;
										attrib_files[i].attrib[record_id - 1].minor[0] = f;
									}
								}
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							fprintf(stderr, "Unrecognized attribute label (%s) in file %s.\n", subfield.label, file_name);
						}
						break;
					case 4:	// Route Attribute Primary Module. (Modules with names of the form ARDM.)
						if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "ROUTE_NUMBER", 12) == 0))  {
							/*
							 * In theory, these can be quite complicated.
							 * In general, they presumably can take the form:
							 *
							 * US A27Z
							 *
							 * Where US is the jurisdiction, and the route
							 * number may be all alphabetic, all numeric, or
							 * mixed.  We assume that these don't get any more
							 * complicated than this:  jurisdiction, followed
							 * by a numeric number, with something alphabetic in
							 * front or behind (but not both).
							 *
							 * The spec also seems to imply that, if there is a route type,
							 * it should follow the jurisdiction and route number,
							 * but preceed any trailing alphabetic on the route number.
							 * This would require some amazing contortions to do,
							 * so we will just tack on the route type after everything
							 * else.  (It is handled in another code block, below.)
							 */
							if (subfield.length == 7)  {
								for (j = 0; j < MAX_EXTRA; j++)  {
									if (attrib_files[i].attrib[record_id - 1].major[j] == 0)  {
										break;
									}
								}
								if (j == MAX_EXTRA)  {
									fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
									continue;
								}
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								if ((ptr = strstr(subfield.value, "I")) != (char *)0)  {
									attrib_files[i].attrib[record_id - 1].major[j] = 172;
									k = ptr - subfield.value + 1;
								}
								else if ((ptr = strstr(subfield.value, "US")) != (char *)0)  {
									attrib_files[i].attrib[record_id - 1].major[j] = 173;
									k = ptr - subfield.value + 2;
								}
								else if ((ptr = strstr(subfield.value, "SR")) != (char *)0)  {
									attrib_files[i].attrib[record_id - 1].major[j] = 174;
									k = ptr - subfield.value + 2;
								}
								else if ((ptr = strstr(subfield.value, "RR")) != (char *)0)  {
									attrib_files[i].attrib[record_id - 1].major[j] = 175;
									k = ptr - subfield.value + 2;
								}
								else if ((ptr = strstr(subfield.value, "CR")) != (char *)0)  {
									attrib_files[i].attrib[record_id - 1].major[j] = 176;
									k = ptr - subfield.value + 2;
								}
								else  {
									k = 0;
								}
								ptr += k;
								while ((*ptr != '\0') && (*ptr == ' '))  {ptr++;}
								if (*ptr != '\0')  {
									/* We have jurisdiction, followed by numeric route. */
									if ((*ptr >= '0') && (*ptr <= '9'))  {
										attrib_files[i].attrib[record_id - 1].minor[j] = strtol(ptr, &ptr, 10);
										while ((*ptr != '\0') && (*ptr == ' '))  {ptr++;}
										if (*ptr != '\0')  {
											if ((j + 1) == MAX_EXTRA)  {
												fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
												continue;
											}
											j++;
											attrib_files[i].attrib[record_id - 1].major[j] = 177;
											attrib_files[i].attrib[record_id - 1].minor[j] = *ptr - 'A' + 1;
											ptr++;
											if ((*ptr != '\0') && (*ptr != ' '))  {
												attrib_files[i].attrib[record_id - 1].minor[j] *= 100;
												attrib_files[i].attrib[record_id - 1].minor[j] += *ptr - 'A' + 1;
											}
										}
									}
									else  {
										/*
										 * We have jurisdiction, followed by alphabetic route.
										 * The spec seems to imply that the alpha code should
										 * preceed the jurisdictional code, so do it.
										 */
										if ((j + 1) == MAX_EXTRA)  {
											fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
											continue;
										}
										attrib_files[i].attrib[record_id - 1].major[j + 1] = attrib_files[i].attrib[record_id - 1].major[j];
										attrib_files[i].attrib[record_id - 1].minor[j] = *ptr - 'A' + 1;
										ptr++;
										if ((*ptr != '\0') && (*ptr != ' '))  {
											attrib_files[i].attrib[record_id - 1].minor[j] *= 100;
											attrib_files[i].attrib[record_id - 1].minor[j] += *ptr - 'A' + 1;
										}
										attrib_files[i].attrib[record_id - 1].minor[j + 1] = strtol(ptr, (char **)0, 10);
									}
								}
								else  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 0;
								}
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "ROUTE_TYPE", 10) == 0))  {
							if (subfield.length == 9)  {
								for (j = 0; j < MAX_EXTRA; j++)  {
									if (attrib_files[i].attrib[record_id - 1].major[j] == 0)  {
										break;
									}
								}
								if (j == MAX_EXTRA)  {
									fprintf(stderr, "Ran out of space for attribute features.  One attribute is missing.\n");
									continue;
								}
								if (strncmp(subfield.value, "Bypass", 6) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 615;
								}
								else if (strncmp(subfield.value, "Alternate", 9) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 616;
								}
								else if (strncmp(subfield.value, "Business", 8) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 617;
								}
								else if (strncmp(subfield.value, "Spur", 4) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 619;
								}
								else if (strncmp(subfield.value, "Loop", 4) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 620;
								}
								else if (strncmp(subfield.value, "Connector", 9) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 621;
								}
								else if (strncmp(subfield.value, "Truck", 5) == 0)  {
									attrib_files[i].attrib[record_id - 1].minor[j] = 622;
								}
								else  {
									continue;
								}
								attrib_files[i].attrib[record_id - 1].major[j] = 170;
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							fprintf(stderr, "Unrecognized attribute label (%s) in file %s.\n", subfield.label, file_name);
						}
						break;
					case 5:	// Agency Attribute Primary Module. (Modules with names of the form ABDM.)
						if ((strstr(subfield.format, "A") != (char *)0) && (strncmp(subfield.label, "AGENCY", 6) == 0))  {
							if (subfield.length == 3)  {
								attrib_files[i].attrib[record_id - 1].major[0] = 97;
								save_byte = subfield.value[subfield.length]; subfield.value[subfield.length] = '\0';
								attrib_files[i].attrib[record_id - 1].minor[0] = strtol(subfield.value, (char **)0, 10);
								subfield.value[subfield.length] = save_byte;
							}
							else  {
								fprintf(stderr, "unexpected attribute length (%d) in file %s\n", subfield.length, file_name);
							}
						}
						else  {
							fprintf(stderr, "Unrecognized attribute label (%s) in file %s.\n", subfield.label, file_name);
						}
						break;
					}
				}
			}
			/* We are done with this file, so close it. */
			end_ddf();
		}
		attrib_files[i].num_attrib++;
	}

	return num_attrib_files;
}







/*
 * Because of the way attributes are stored in the main Primary Attribute File,
 * we can end up with duplicate attributes in the list.  This routine removes
 * the duplicates.  It removes the earliest occurence(s) of the dups since
 * that appears to be most likely to make the list ordering match the ordering
 * of the original (pre-SDTS) files.  As far as I know, there is no preferred
 * ordering for attribute lists.
 *
 * As an example, the following attribute list:
 *
 * "    50   412    50   202    50   610"
 *
 * contains the "50   610" attribute (which stands for "INTERMITTENT").
 * This attribute is coded in SDTS as a flag in the attribute record.
 * The attribute list will be encoded as two SDTS attribute records,
 * one for "50   412" (Stream) and one for "50   202" (Closure Line).
 * Each of these records will have the "INTERMITTENT" flag set.
 *
 * When we decode these records, we will end up with two copies of "50   610".
 * One of these should be removed.
 */
void
uniq_attrib(struct attribute **initial_attrib, short *attrib)
{
	short i;
	struct attribute **current_base;
	struct attribute **current_attrib;
	struct attribute **search_attrib;
	struct attribute **next_attrib;
	struct attribute **prev_attrib;

	current_base = initial_attrib;
	for (i = 0; i < *attrib; i++)  {
		search_attrib = current_base;
		while (*(search_attrib = &((*search_attrib)->attribute)) != (struct attribute *)0)  {
			if (((*search_attrib)->major != 177) &&		// 177 is a special case for spelling out alphabetic items
			    ((*search_attrib)->major == (*current_base)->major) &&
			    ((*search_attrib)->minor == (*current_base)->minor))  {
				/*
				 * We have found a duplicate.  Remove the earlier entry.
				 *
				 * It is easier to alter the list by unlinking the
				 * last entry than by removing an entry from the middle
				 * of the list.  Thus, we shift the data backwards
				 * to fill in the unwanted entry, rather than removing the
				 * actual list element of the unwanted entry.
				 * Then we simply unlink and free the final entry.
				 */
				prev_attrib = (struct attribute **)0;	// Induce a core dump if there is a bug in the code.
				current_attrib = current_base;
				next_attrib = current_base;
				while (*(next_attrib = &((*next_attrib)->attribute)) != (struct attribute *)0)  {
					(*current_attrib)->major = (*next_attrib)->major;
					(*current_attrib)->minor = (*next_attrib)->minor;
					prev_attrib = current_attrib;
					current_attrib = next_attrib;
				}
				(*prev_attrib)->attribute = (struct attribute *)0;
				free(*current_attrib);
				(*attrib)--;
				search_attrib = current_base;
			}
		}
		current_base = &((*current_base)->attribute);
	}
}




/*
 * Read the CATS module and try to find the theme.
 */
void get_theme(char *passed_file_name, char *category_name, long upper_case_flag, long gz_flag)
{
	struct subfield subfield;
	long file_name_length;
	char file_name[MAX_FILE_NAME + 1];
	char lookin_for[4];
	long got_it;
	long i;


	/*
	 * Generate the file name for the CATS module.
	 */
	strncpy(file_name, passed_file_name, MAX_FILE_NAME);
	file_name[MAX_FILE_NAME] = '\0';
	file_name_length = strlen(file_name);
	if (upper_case_flag == 0)  {
		if (gz_flag != 0)  {
			lookin_for[0] = toupper(file_name[file_name_length - 11]);
			lookin_for[1] = toupper(file_name[file_name_length - 10]);
			lookin_for[2] = toupper(file_name[file_name_length -  9]);
			lookin_for[3] = toupper(file_name[file_name_length -  8]);

			file_name[file_name_length - 11] = 'c';
			file_name[file_name_length - 10] = 'a';
			file_name[file_name_length -  9] = 't';
			file_name[file_name_length -  8] = 's';
		}
		else  {
			lookin_for[0] = toupper(file_name[file_name_length -  8]);
			lookin_for[1] = toupper(file_name[file_name_length -  7]);
			lookin_for[2] = toupper(file_name[file_name_length -  6]);
			lookin_for[3] = toupper(file_name[file_name_length -  5]);

			file_name[file_name_length -  8] = 'c';
			file_name[file_name_length -  7] = 'a';
			file_name[file_name_length -  6] = 't';
			file_name[file_name_length -  5] = 's';
		}
	}
	else  {
		if (gz_flag != 0)  {
			lookin_for[0] = file_name[file_name_length - 11];
			lookin_for[1] = file_name[file_name_length - 10];
			lookin_for[2] = file_name[file_name_length -  9];
			lookin_for[3] = file_name[file_name_length -  8];

			file_name[file_name_length - 11] = 'C';
			file_name[file_name_length - 10] = 'A';
			file_name[file_name_length -  9] = 'T';
			file_name[file_name_length -  8] = 'S';
		}
		else  {
			lookin_for[0] = file_name[file_name_length -  8];
			lookin_for[1] = file_name[file_name_length -  7];
			lookin_for[2] = file_name[file_name_length -  6];
			lookin_for[3] = file_name[file_name_length -  5];

			file_name[file_name_length -  8] = 'C';
			file_name[file_name_length -  7] = 'A';
			file_name[file_name_length -  6] = 'T';
			file_name[file_name_length -  5] = 'S';
		}
	}
	/*
	 * Open and process the file.
	 */
	got_it = 0;
	if (begin_ddf(file_name) >= 0)  {
		while (get_subfield(&subfield) != 0)  {
			if (strcmp(subfield.tag, "CATS") == 0)  {
				if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "NAME") == 0))  {
					if (subfield.length != 4)  {
						fprintf(stderr, "Attribute module name (%.*s) is not 4 characters long.\n", subfield.length, subfield.value);
						continue;
					}
					if (strncmp(subfield.value, lookin_for, 4) == 0)  {
						got_it = 1;
					}
					else  {
						got_it = 0;
					}
				}
				else if ((strstr(subfield.format, "A") != (char *)0) && (strcmp(subfield.label, "THEM") == 0))  {
					if (got_it != 0)  {
						if ((subfield.length != 20) || (subfield.value[0] == ' '))  {
							continue;
						}
						strncpy(category_name, subfield.value, subfield.length);
						for (i = 19; i >= 0; i--)  {
							if (category_name[i] != ' ')  {
								category_name[i + 1] = '\0';
								break;
							}
						}

						return;
					}
				}
			}
		}
	}

	return;
}