File: avc_e00read.c

package info (click to toggle)
avce00 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 896 kB
  • ctags: 718
  • sloc: ansic: 9,851; makefile: 46
file content (2110 lines) | stat: -rw-r--r-- 84,124 bytes parent folder | download | duplicates (7)
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
/**********************************************************************
 * $Id: avc_e00read.c,v 1.23 2006/08/17 19:51:01 dmorissette Exp $
 *
 * Name:     avc_e00read.c
 * Project:  Arc/Info vector coverage (AVC)  BIN->E00 conversion library
 * Language: ANSI C
 * Purpose:  Functions to open a binary coverage and read it as if it
 *           was an ASCII E00 file.  This file is the main entry point
 *           for the library.
 * Author:   Daniel Morissette, dmorissette@dmsolutions.ca
 *
 **********************************************************************
 * Copyright (c) 1999-2005, Daniel Morissette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************
 *
 * $Log: avc_e00read.c,v $
 * Revision 1.23  2006/08/17 19:51:01  dmorissette
 * #include <unistd.h> to solve warning on 64 bit platforms (bug 1461)
 *
 * Revision 1.22  2006/08/17 18:56:42  dmorissette
 * Support for reading standalone info tables (just tables, no coverage
 * data) by pointing AVCE00ReadOpen() to the info directory (bug 1549).
 *
 * Revision 1.21  2006/06/27 18:38:43  dmorissette
 * Cleaned up E00 reading (bug 1497, patch from James F.)
 *
 * Revision 1.20  2006/06/27 18:06:34  dmorissette
 * Applied patch for EOP processing from James F. (bug 1497)
 *
 * Revision 1.19  2006/06/16 11:48:11  daniel
 * New functions to read E00 files directly as opposed to translating to
 * binary coverage. Used in the implementation of E00 read support in OGR.
 * Contributed by James E. Flemer. (bug 1497)
 *
 * Revision 1.18  2006/06/14 16:31:28  daniel
 * Added support for AVCCoverPC2 type (bug 1491)
 *
 * Revision 1.17  2005/06/03 03:49:58  daniel
 * Update email address, website url, and copyright dates
 *
 * Revision 1.16  2004/07/14 18:49:50  daniel
 * Fixed leak when trying to open something that's not a coverage (bug513)
 *
 * Revision 1.15  2002/08/27 15:46:15  daniel
 * Applied fix made in GDAL/OGR by 'aubin' (moved include ctype.h after avc.h)
 *
 * Revision 1.14  2000/09/22 19:45:21  daniel
 * Switch to MIT-style license
 *
 * Revision 1.13  2000/05/29 15:31:31  daniel
 * Added Japanese DBCS support
 *
 * Revision 1.12  2000/02/14 17:21:01  daniel
 * Made more robust for corrupted or invalid files in cover directory
 *
 * Revision 1.11  2000/02/02 04:26:04  daniel
 * Support reading TX6/TX7/RXP/RPL files in weird coverages
 *
 * Revision 1.10  2000/01/10 02:56:30  daniel
 * Added read support for "weird" coverages
 *
 * Revision 1.9  2000/01/07 07:12:49  daniel
 * Added support for reading PC Coverage TXT files
 *
 * Revision 1.8  1999/12/24 07:41:08  daniel
 * Check fname length before testing for extension in AVCE00ReadFindCoverType()
 *
 * Revision 1.7  1999/12/24 07:18:34  daniel
 * Added PC Arc/Info coverages support
 *
 * Revision 1.6  1999/08/26 17:22:18  daniel
 * Use VSIFopen() instead of fopen() directly
 *
 * Revision 1.5  1999/08/23 18:21:41  daniel
 * New syntax for AVCBinReadListTables()
 *
 * Revision 1.4  1999/05/11 02:10:01  daniel
 * Free psInfo struct inside AVCE00ReadClose()
 *
 * Revision 1.3  1999/04/06 19:43:26  daniel
 * Added E00 coverage path in EXP 0  header line
 *
 * Revision 1.2  1999/02/25 04:19:01  daniel
 * Added TXT, TX6/TX7, RXP and RPL support + other minor changes
 *
 * Revision 1.1  1999/01/29 16:28:52  daniel
 * Initial revision
 *
 **********************************************************************/

#ifdef WIN32
#  include <direct.h>   /* getcwd() */
#else
#  include <unistd.h>   /* getcwd() */
#endif

#include "avc.h"

#include <ctype.h>      /* toupper() */

static void _AVCE00ReadScanE00(AVCE00ReadE00Ptr psRead);
static int _AVCE00ReadBuildSqueleton(AVCE00ReadPtr psInfo,
                                     char **papszCoverDir);
static AVCCoverType _AVCE00ReadFindCoverType(char **papszCoverDir);


/**********************************************************************
 *                          AVCE00ReadOpen()
 *
 * Open a Arc/Info coverage to read it as if it was an E00 file.
 *
 * You can either pass the name of the coverage directory, or the path
 * to one of the files in the coverage directory.  The name of the
 * coverage MUST be included in pszCoverPath... this means that
 * passing "." is invalid.
 * The following are all valid values for pszCoverPath:
 *               /home/data/country
 *               /home/data/country/
 *               /home/data/country/arc.adf
 * (Of course you should replace the '/' with '\\' on DOS systems!)
 *
 * Returns a new AVCE00ReadPtr handle or NULL if the coverage could 
 * not be opened or if it does not appear to be a valid Arc/Info coverage.
 *
 * The handle will eventually have to be released with AVCE00ReadClose().
 **********************************************************************/
AVCE00ReadPtr  AVCE00ReadOpen(const char *pszCoverPath)
{
    AVCE00ReadPtr   psInfo;
    int             i, nLen, nCoverPrecision;
    VSIStatBuf      sStatBuf;
    char            **papszCoverDir = NULL;

    CPLErrorReset();

    /*-----------------------------------------------------------------
     * pszCoverPath must be either a valid directory name or a valid
     * file name.
     *----------------------------------------------------------------*/
    if (pszCoverPath == NULL || strlen(pszCoverPath) == 0 ||
        VSIStat(pszCoverPath, &sStatBuf) == -1)
    {
        CPLError(CE_Failure, CPLE_OpenFailed, 
                 "Invalid coverage path: %s.", 
                 pszCoverPath?pszCoverPath:"(NULL)");
        return NULL;
    }

    /*-----------------------------------------------------------------
     * Alloc the AVCE00ReadPtr handle
     *----------------------------------------------------------------*/
    psInfo = (AVCE00ReadPtr)CPLCalloc(1, sizeof(struct AVCE00ReadInfo_t));

    /*-----------------------------------------------------------------
     * 2 possibilities about the value passed in pszCoverPath:
     * - It can be the directory name of the coverage
     * - or it can be the path to one of the files in the coverage
     *
     * If the name passed in pszCoverPath is not a directory, then we
     * need to strip the last part of the filename to keep only the
     * path, terminated by a '/' (or a '\\').
     *----------------------------------------------------------------*/
    if (VSI_ISDIR(sStatBuf.st_mode))
    {
        /*-------------------------------------------------------------
         * OK, we have a valid directory name... make sure it is 
         * terminated with a '/' (or '\\')
         *------------------------------------------------------------*/
        nLen = strlen(pszCoverPath);

        if (pszCoverPath[nLen-1] == '/' || pszCoverPath[nLen-1] == '\\')
            psInfo->pszCoverPath = CPLStrdup(pszCoverPath);
        else
        {
#ifdef WIN32
            psInfo->pszCoverPath = CPLStrdup(CPLSPrintf("%s\\",pszCoverPath));
#else
            psInfo->pszCoverPath = CPLStrdup(CPLSPrintf("%s/",pszCoverPath));
#endif
        }
    }
    else
    {
        /*-------------------------------------------------------------
         * We are dealing with a filename.
         * Extract the coverage path component and store it.
         * The coverage path will remain terminated by a '/' or '\\' char.
         *------------------------------------------------------------*/
        psInfo->pszCoverPath = CPLStrdup(pszCoverPath);

        for( i = strlen(psInfo->pszCoverPath)-1; 
             i > 0 && psInfo->pszCoverPath[i] != '/' &&
                 psInfo->pszCoverPath[i] != '\\';
             i-- ) {}

        psInfo->pszCoverPath[i+1] = '\0';
    }


    /*-----------------------------------------------------------------
     * Extract the coverage name from the coverage path.  Note that
     * for this the coverage path must be in the form:
     * "dir1/dir2/dir3/covername/" ... if it is not the case, then
     * we would have to use getcwd() to find the current directory name...
     * but for now we'll just produce an error if this happens.
     *----------------------------------------------------------------*/
    nLen = 0;
    for( i = strlen(psInfo->pszCoverPath)-1; 
	 i > 0 && psInfo->pszCoverPath[i-1] != '/' &&
	          psInfo->pszCoverPath[i-1] != '\\'&&
	          psInfo->pszCoverPath[i-1] != ':';
	 i-- ) 
    {
        nLen++;
    }

    if (nLen > 0)
    {
        psInfo->pszCoverName = CPLStrdup(psInfo->pszCoverPath+i);
        psInfo->pszCoverName[nLen] = '\0';
    }
    else
    {
        CPLError(CE_Failure, CPLE_OpenFailed, 
                 "Invalid coverage path (%s): "
                 "coverage name must be included in path.", pszCoverPath);

        CPLFree(psInfo->pszCoverPath);
        CPLFree(psInfo);
        return NULL;
    }

    /*-----------------------------------------------------------------
     * Read the coverage directory listing and try to establish the cover type
     *----------------------------------------------------------------*/
    papszCoverDir = CPLReadDir(psInfo->pszCoverPath);

    psInfo->eCoverType = _AVCE00ReadFindCoverType(papszCoverDir);

    if (psInfo->eCoverType == AVCCoverTypeUnknown  )
    {
        CPLError(CE_Failure, CPLE_OpenFailed, 
                 "Invalid coverage (%s): directory does not appear to "
                 "contain any supported vector coverage file.",  pszCoverPath);
        CPLFree(psInfo->pszCoverName);
        CPLFree(psInfo->pszCoverPath);
        CPLFree(psInfo->pszInfoPath);
        CPLFree(psInfo);
        CSLDestroy(papszCoverDir);
        return NULL;
    }

   
    /*-----------------------------------------------------------------
     * INFO path: PC Coverages have all files in the same dir, and unix
     * covers have the INFO files in ../info
     *----------------------------------------------------------------*/
    if (psInfo->eCoverType == AVCCoverPC || psInfo->eCoverType == AVCCoverPC2)
    {
        psInfo->pszInfoPath = CPLStrdup(psInfo->pszCoverPath);
    }
    else
    {
        /*-------------------------------------------------------------
         * Lazy way to build the INFO path: simply add "../info/"...
         * this could probably be improved!
         *------------------------------------------------------------*/
        psInfo->pszInfoPath =(char*)CPLMalloc((strlen(psInfo->pszCoverPath)+9)*
                                           sizeof(char));
#ifdef WIN32
#  define AVC_INFOPATH "..\\info\\"
#else
#  define AVC_INFOPATH "../info/"
#endif
        sprintf(psInfo->pszInfoPath, "%s%s", psInfo->pszCoverPath, 
                                             AVC_INFOPATH);

        AVCAdjustCaseSensitiveFilename(psInfo->pszInfoPath);
    }

    /*-----------------------------------------------------------------
     * For Unix coverages, check that the info directory exists and 
     * contains the "arc.dir".  In AVCCoverWeird, the arc.dir is 
     * called "../INFO/ARCDR9".
     * PC Coverages have their info tables in the same direcotry as 
     * the coverage files.
     *----------------------------------------------------------------*/
    if (((psInfo->eCoverType == AVCCoverV7 || 
          psInfo->eCoverType == AVCCoverV7Tables) &&
         ! AVCFileExists(psInfo->pszInfoPath, "arc.dir") ) ||
         (psInfo->eCoverType == AVCCoverWeird &&
         ! AVCFileExists(psInfo->pszInfoPath, "arcdr9") ) )
    {
        CPLError(CE_Failure, CPLE_OpenFailed, 
             "Invalid coverage (%s): 'info' directory not found or invalid.", 
                                              pszCoverPath);
        CPLFree(psInfo->pszCoverName);
        CPLFree(psInfo->pszCoverPath);
        CPLFree(psInfo->pszInfoPath);
        CPLFree(psInfo);
        CSLDestroy(papszCoverDir);
        return NULL;
    }

    /*-----------------------------------------------------------------
     * Make sure there was no error until now before we build squeleton.
     *----------------------------------------------------------------*/
    if (CPLGetLastErrorNo() != 0)
    {
        CPLFree(psInfo->pszCoverName);
        CPLFree(psInfo->pszCoverPath);
        CPLFree(psInfo->pszInfoPath);
        CPLFree(psInfo);
        CSLDestroy(papszCoverDir);
        return NULL;
    }

    /*-----------------------------------------------------------------
     * Build the E00 file squeleton and be ready to return a E00 header...
     * We'll also read the coverage precision by the same way.
     *----------------------------------------------------------------*/
    nCoverPrecision = _AVCE00ReadBuildSqueleton(psInfo, papszCoverDir);

    /* Ignore warnings produced while building squeleton */
    CPLErrorReset();

    CSLDestroy(papszCoverDir);
    papszCoverDir = NULL;

    psInfo->iCurSection = 0;
    psInfo->iCurStep = AVC_GEN_NOTSTARTED;
    psInfo->bReadAllSections = TRUE;

    /*-----------------------------------------------------------------
     * Init the E00 generator.
     *----------------------------------------------------------------*/
    psInfo->hGenInfo = AVCE00GenInfoAlloc(nCoverPrecision);

    /*-----------------------------------------------------------------
     * Init multibyte encoding info
     *----------------------------------------------------------------*/
    psInfo->psDBCSInfo = AVCAllocDBCSInfo();

    /*-----------------------------------------------------------------
     * If an error happened during the open call, cleanup and return NULL.
     *----------------------------------------------------------------*/
    if (CPLGetLastErrorNo() != 0)
    {
        AVCE00ReadClose(psInfo);
        psInfo = NULL;
    }

    return psInfo;
}

/**********************************************************************
 *                          AVCE00ReadOpenE00()
 *
 * Open a E00 file for reading.
 *
 * Returns a new AVCE00ReadE00Ptr handle or NULL if the file could
 * not be opened or if it does not appear to be a valid E00 file.
 *
 * The handle will eventually have to be released with
 * AVCE00ReadCloseE00().
 **********************************************************************/
AVCE00ReadE00Ptr AVCE00ReadOpenE00(const char *pszE00FileName)
{
    AVCE00ReadE00Ptr psRead;
    VSIStatBuf       sStatBuf;
    FILE             *fp;
    char             *p;

    CPLErrorReset();

    /*-----------------------------------------------------------------
     * pszE00FileName must be a valid file that can be opened for
     * reading
     *----------------------------------------------------------------*/
    if (pszE00FileName == NULL || strlen(pszE00FileName) == 0 ||
        VSIStat(pszE00FileName, &sStatBuf) == -1 ||
        VSI_ISDIR(sStatBuf.st_mode))
    {
        CPLError(CE_Failure, CPLE_OpenFailed, 
                 "Invalid E00 file path: %s.", 
                 pszE00FileName?pszE00FileName:"(NULL)");
        return NULL;
    }

    if (NULL == (fp = fopen(pszE00FileName, "r")))
        return NULL;

    /*-----------------------------------------------------------------
     * Alloc the AVCE00ReadE00Ptr handle
     *----------------------------------------------------------------*/
    psRead = (AVCE00ReadE00Ptr)CPLCalloc(1,
            sizeof(struct AVCE00ReadInfoE00_t));

    psRead->hFile = fp;
    psRead->pszCoverPath = CPLStrdup(pszE00FileName);
    psRead->eCurFileType = AVCFileUnknown;

    /*-----------------------------------------------------------------
     * Extract the coverage name from the coverage path.
     *----------------------------------------------------------------*/
    if (NULL != (p = strrchr(psRead->pszCoverPath, '/')) ||
        NULL != (p = strrchr(psRead->pszCoverPath, '\\')) ||
        NULL != (p = strrchr(psRead->pszCoverPath, ':')))
    {
        psRead->pszCoverName = CPLStrdup(p + 1);
    }
    else
    {
        psRead->pszCoverName = CPLStrdup(psRead->pszCoverPath);
    }
    if (NULL != (p = strrchr(psRead->pszCoverName, '.')))
    {
        *p = '\0';
    }

    /*-----------------------------------------------------------------
     * Make sure there was no error until now before we scan file.
     *----------------------------------------------------------------*/
    if (CPLGetLastErrorNo() != 0)
    {
        AVCE00ReadCloseE00(psRead);
        return NULL;
    }

    psRead->hParseInfo = AVCE00ParseInfoAlloc();

    /*-----------------------------------------------------------------
     * Scan the E00 file for sections
     *----------------------------------------------------------------*/
    _AVCE00ReadScanE00(psRead);
    AVCE00ReadRewindE00(psRead);
    CPLErrorReset();

    if (psRead->numSections < 1)
    {
        AVCE00ReadCloseE00(psRead);
        return NULL;
    }

    psRead->bReadAllSections = TRUE;

    /*-----------------------------------------------------------------
     * If an error happened during the open call, cleanup and return NULL.
     *----------------------------------------------------------------*/
    if (CPLGetLastErrorNo() != 0)
    {
        AVCE00ReadCloseE00(psRead);
        psRead = NULL;
    }

    return psRead;
}

/**********************************************************************
 *                          AVCE00ReadClose()
 *
 * Close a coverage and release all memory used by the AVCE00ReadPtr
 * handle.
 **********************************************************************/
void AVCE00ReadClose(AVCE00ReadPtr psInfo)
{
    CPLErrorReset();

    if (psInfo == NULL)
        return;

    CPLFree(psInfo->pszCoverPath);
    CPLFree(psInfo->pszInfoPath);
    CPLFree(psInfo->pszCoverName);

    if (psInfo->hFile)
        AVCBinReadClose(psInfo->hFile);

    if (psInfo->hGenInfo)
        AVCE00GenInfoFree(psInfo->hGenInfo);

    if (psInfo->pasSections)
    {
        int i;
        for(i=0; i<psInfo->numSections; i++)
        {
            CPLFree(psInfo->pasSections[i].pszName);
            CPLFree(psInfo->pasSections[i].pszFilename);
        }
        CPLFree(psInfo->pasSections);
    }

    AVCFreeDBCSInfo(psInfo->psDBCSInfo);

    CPLFree(psInfo);
}

/**********************************************************************
 *                          AVCE00ReadCloseE00()
 *
 * Close a coverage and release all memory used by the AVCE00ReadE00Ptr
 * handle.
 **********************************************************************/
void AVCE00ReadCloseE00(AVCE00ReadE00Ptr psRead)
{
    CPLErrorReset();

    if (psRead == NULL)
        return;

    CPLFree(psRead->pszCoverPath);
    CPLFree(psRead->pszCoverName);

    if (psRead->hFile)
    {
        fclose(psRead->hFile);
        psRead->hFile = 0;
    }

    if (psRead->pasSections)
    {
        int i;
        for(i=0; i<psRead->numSections; i++)
        {
            CPLFree(psRead->pasSections[i].pszName);
            CPLFree(psRead->pasSections[i].pszFilename);
        }
        CPLFree(psRead->pasSections);
    }

    /* These Free calls handle NULL's */
    AVCE00ParseInfoFree(psRead->hParseInfo);
    psRead->hParseInfo = NULL;

    CPLFree(psRead);
}


/**********************************************************************
 *                          _AVCIncreaseSectionsArray()
 *
 * Add a number of structures to the Sections array and return the
 * index of the first one that was added.  Note that the address of the
 * original array (*pasArray) is quite likely to change!
 *
 * The value of *pnumItems will be updated to reflect the new array size.
 **********************************************************************/
static int _AVCIncreaseSectionsArray(AVCE00Section **pasArray, int *pnumItems,
                                    int numToAdd)
{
    int i;

    *pasArray = (AVCE00Section*)CPLRealloc(*pasArray, 
                                           (*pnumItems+numToAdd)*
                                                    sizeof(AVCE00Section));

    for(i=0; i<numToAdd; i++)
    {
        (*pasArray)[*pnumItems+i].eType = AVCFileUnknown;
        (*pasArray)[*pnumItems+i].pszName = NULL;
        (*pasArray)[*pnumItems+i].pszFilename = NULL;
        (*pasArray)[*pnumItems+i].nLineNum = 0;
        (*pasArray)[*pnumItems+i].nFeatureCount = -1;
    }

    i = *pnumItems;
    (*pnumItems) += numToAdd;

    return i;
}

/**********************************************************************
 *                         _AVCE00ReadFindCoverType()
 *
 * This functions tries to establish the coverage type by looking
 * at the coverage directory listing passed as argument.
 *
 * Returns one of AVCCoverV7 for Arc/Info V7 (Unix) coverages, or
 *                AVCCoverPC for PC Arc/Info coverages.
 *                AVCCoverWeird for an hybrid between V7 and PC
 *
 * If coverage type cannot be established then AVCCoverTypeUnknown is 
 * returned.
 **********************************************************************/
static AVCCoverType _AVCE00ReadFindCoverType(char **papszCoverDir)
{
    int         i, nLen;
    GBool       bFoundAdfFile=FALSE, bFoundArcFile=FALSE, 
                bFoundTableFile=FALSE, bFoundDbfFile=FALSE,
                bFoundArcDirFile=FALSE;

    /*-----------------------------------------------------------------
     * Scan the list of files, looking for well known filenames.
     * Start with the funky types first...
     *----------------------------------------------------------------*/
    for(i=0; papszCoverDir && papszCoverDir[i]; i++)
    {
        nLen = strlen(papszCoverDir[i]);
        if (nLen > 4 && EQUAL(papszCoverDir[i]+nLen-4, ".adf") )
        {
            bFoundAdfFile = TRUE;
        }
        else if (nLen > 4 && EQUAL(papszCoverDir[i]+nLen-4, ".dbf") )
        {
            bFoundDbfFile = TRUE;
        }
        else if (EQUAL(papszCoverDir[i], "arc") ||
                 EQUAL(papszCoverDir[i], "cnt") ||
                 EQUAL(papszCoverDir[i], "pal") ||
                 EQUAL(papszCoverDir[i], "lab") ||
                 EQUAL(papszCoverDir[i], "prj") ||
                 EQUAL(papszCoverDir[i], "tol") )
        {
            bFoundArcFile = TRUE;
        }
        else if (EQUAL(papszCoverDir[i], "aat") ||
                 EQUAL(papszCoverDir[i], "pat") ||
                 EQUAL(papszCoverDir[i], "bnd") ||
                 EQUAL(papszCoverDir[i], "tic") )
        {
            bFoundTableFile = TRUE;
        }
        else if (EQUAL(papszCoverDir[i], "arc.dir") )
        {
            bFoundArcDirFile = TRUE;
        }

    }

    /*-----------------------------------------------------------------
     * Check for PC Arc/Info coverage - variant 1.
     * These PC coverages have files with no extension (e.g. "ARC","PAL",...)
     * and their tables filenames are in the form "???.dbf"
     *----------------------------------------------------------------*/
    if (bFoundArcFile && bFoundDbfFile)
        return AVCCoverPC;

    /*-----------------------------------------------------------------
     * Check for PC Arc/Info coverage - variant 2.
     * looks like a hybrid between AVCCoverPC and AVCCoverV7
     * These PC coverages have files with .adf extension (e.g."ARC.ADF"),
     * and their tables filenames are in the form "???.dbf"
     *----------------------------------------------------------------*/
    if (bFoundAdfFile && bFoundDbfFile)
        return AVCCoverPC2;

    /*-----------------------------------------------------------------
     * Check for the weird coverages.
     * Their coverage files have no extension just like PC Coverages, 
     * and their tables have 3 letters filenames with no extension
     * either (e.g. "AAT", "PAT", etc.)
     * They also have a ../info directory, but we don't really need
     * to check that (not yet!).
     *----------------------------------------------------------------*/
    if (bFoundArcFile && bFoundTableFile)
        return AVCCoverWeird;

    /*-----------------------------------------------------------------
     * V7 Coverages... they are the easiest to recognize
     * because of the ".adf" file extension
     *----------------------------------------------------------------*/
    if (bFoundAdfFile)
        return AVCCoverV7;

    /*-----------------------------------------------------------------
     * Standalone info tables.
     * We were pointed at the "info" directory. We'll treat this as
     * a coverage with just info tables.
     *----------------------------------------------------------------*/
    if (bFoundArcDirFile)
        return AVCCoverV7Tables;

    return AVCCoverTypeUnknown;
}


/**********************************************************************
 *                         _AVCE00ReadAddJabberwockySection()
 *
 * Add to the squeleton a section that contains subsections 
 * for all the files with a given extension.
 *
 * Returns Updated Coverage precision
 **********************************************************************/
static int _AVCE00ReadAddJabberwockySection(AVCE00ReadPtr psInfo,
                                            AVCFileType   eFileType,
                                            const char   *pszSectionName,
                                            int           nCoverPrecision,
                                            const char   *pszFileExtension,
                                            char        **papszCoverDir )
{
    int         iSect, iDirEntry, nLen, nExtLen;
    GBool       bFoundFiles = FALSE;
    AVCBinFile *psFile=NULL;

    nExtLen = strlen(pszFileExtension);

    /*-----------------------------------------------------------------
     * Scan the directory for files with a ".txt" extension.
     *----------------------------------------------------------------*/

    for (iDirEntry=0; papszCoverDir && papszCoverDir[iDirEntry]; iDirEntry++)
    {
        nLen = strlen(papszCoverDir[iDirEntry]);

        if (nLen > nExtLen && EQUAL(papszCoverDir[iDirEntry] + nLen-nExtLen, 
                                    pszFileExtension) &&
            (psFile = AVCBinReadOpen(psInfo->pszCoverPath, 
                                     papszCoverDir[iDirEntry],
                                     psInfo->eCoverType, eFileType,
                                     psInfo->psDBCSInfo)) != NULL)
        {
            if (nCoverPrecision == AVC_DEFAULT_PREC)
                nCoverPrecision = psFile->nPrecision;
            AVCBinReadClose(psFile);

            if (bFoundFiles == FALSE)
            {
                /* Insert a "TX6 #" header before the first TX6 file
                 */
                iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                                  &(psInfo->numSections), 1);
                psInfo->pasSections[iSect].eType = AVCFileUnknown;

                psInfo->pasSections[iSect].pszName = 
                            CPLStrdup(CPLSPrintf("%s  %c", pszSectionName,
                                  (nCoverPrecision==AVC_DOUBLE_PREC)?'3':'2'));

                bFoundFiles = TRUE;
            }

            /* Add this file to the squeleton 
             */
            iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                              &(psInfo->numSections), 1);

            psInfo->pasSections[iSect].eType = eFileType;
            psInfo->pasSections[iSect].pszFilename= 
                                   CPLStrdup(papszCoverDir[iDirEntry]);

            /* pszName will contain only the classname without the file 
             * extension */
            psInfo->pasSections[iSect].pszName =
                                   CPLStrdup(papszCoverDir[iDirEntry]);
            psInfo->pasSections[iSect].pszName[nLen-nExtLen] = '\0';
        }
    }

    if (bFoundFiles)
    {
        /* Add a line to close the TX6 section.
         */
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                          &(psInfo->numSections), 1);
        psInfo->pasSections[iSect].eType = AVCFileUnknown;
        psInfo->pasSections[iSect].pszName = CPLStrdup("JABBERWOCKY");
    }

    return nCoverPrecision;
}

/**********************************************************************
 *                     _AVCE00ReadNextLineE00()
 *
 * Processes the next line of input from the E00 file.
 * (See AVCE00WriteNextLine() for similar processing.)
 *
 * Returns the next object from the E00 file, or NULL.
 **********************************************************************/
static void *_AVCE00ReadNextLineE00(AVCE00ReadE00Ptr psRead,
        const char *pszLine)
{
    int nStatus = 0;
    void *psObj = 0;

    AVCE00ParseInfo *psInfo = psRead->hParseInfo;

    CPLErrorReset();

    ++psInfo->nCurLineNum;

    if (psInfo->bForceEndOfSection)
    {
        /*-------------------------------------------------------------
         * The last call encountered an implicit end of section, so
         * we close the section now without waiting for an end-of-section
         * line (there won't be any!)... and get ready to proceed with
         * the next section.
         * This is used for TABLEs.
         *------------------------------------------------------------*/
        AVCE00ParseSectionEnd(psInfo, pszLine, TRUE);
        psRead->eCurFileType = AVCFileUnknown;
    }

    /*-----------------------------------------------------------------
     * If we're at the top level inside a supersection... check if this
     * supersection ends here.
     *----------------------------------------------------------------*/
    if (AVCE00ParseSuperSectionEnd(psInfo, pszLine) == TRUE)
    {
        /* Nothing to do... it's all been done by the call to 
         * AVCE00ParseSuperSectionEnd()
         */
    }
    else if (psRead->eCurFileType == AVCFileUnknown)
    {
        /*-------------------------------------------------------------
         * We're at the top level or inside a supersection... waiting 
         * to encounter a valid section or supersection header 
         * (i.e. "ARC  2", etc...)
         *------------------------------------------------------------*/

        /*-------------------------------------------------------------
         * First check for a supersection header (TX6, RXP, IFO, ...)
         *------------------------------------------------------------*/
        if ( AVCE00ParseSuperSectionHeader(psInfo,
                                           pszLine) == AVCFileUnknown )
        {
            /*---------------------------------------------------------
             * This was not a supersection header... check if it's a simple
             * section header
             *--------------------------------------------------------*/
            psRead->eCurFileType = AVCE00ParseSectionHeader(psInfo,
                    pszLine);
        }
        else
        {
            /* got supersection */
        }

        if (psRead->eCurFileType == AVCFileTABLE)
        {
            /*---------------------------------------------------------
             * send the first header line to the parser and wait until
             * the whole header has been read.
             *--------------------------------------------------------*/
            AVCE00ParseNextLine(psInfo, pszLine); 
        }
        else if (psRead->eCurFileType != AVCFileUnknown)
        {
            /*---------------------------------------------------------
             * found a valid section header
             *--------------------------------------------------------*/
        }
    }
    else if (psRead->eCurFileType == AVCFileTABLE &&
             ! psInfo->bTableHdrComplete )
    {
        /*-------------------------------------------------------------
         * We're reading a TABLE header... continue reading lines
         * from the header
         *
         * Note: When parsing a TABLE, the first object returned will 
         * be the AVCTableDef, then data records will follow.
         *------------------------------------------------------------*/
        psObj = AVCE00ParseNextLine(psInfo, pszLine); 
        if (psObj)
        {
			/* got table header */
            /* TODO: Enable return of table definition? */
            psObj = NULL;
        }
    }
    else
    {
        /*-------------------------------------------------------------
         * We're are in the middle of a section... first check if we
         * have reached the end.
         *
         * note: The first call to AVCE00ParseSectionEnd() with FALSE will 
         *       not reset the parser until we close the file... and then
         *       we call the function again to reset the parser.
         *------------------------------------------------------------*/
        if (AVCE00ParseSectionEnd(psInfo, pszLine, FALSE))
        {
            psRead->eCurFileType = AVCFileUnknown;
            AVCE00ParseSectionEnd(psInfo, pszLine, TRUE);
        }
        else
        /*-------------------------------------------------------------
         * ... not at the end yet, so continue reading objects.
         *------------------------------------------------------------*/
        {
            psObj = AVCE00ParseNextLine(psInfo, pszLine);

            if (psObj)
            {
				/* got object */
            }
        }
    }

    if (CPLGetLastErrorNo() != 0)
        nStatus = -1;

    return psObj;
}

/**********************************************************************
 *                         _AVCE00ReadBuildSqueleton()
 *
 * Build the squeleton of the E00 file corresponding to the specified
 * coverage and set the appropriate fields in the AVCE00ReadPtr struct.
 *
 * Note that the order of the sections in the squeleton is important
 * since some software may rely on this ordering when they read E00 files.
 *
 * The function returns the coverage precision that it will read from one
 * of the file headers.  
 **********************************************************************/
static int _AVCE00ReadBuildSqueleton(AVCE00ReadPtr psInfo, 
                                     char **papszCoverDir)
{
    int         iSect, iTable, numTables, iFile, nLen;
    char      **papszTables, **papszFiles, szCWD[75]="", *pcTmp;
    char       *pszEXPPath=NULL;
    int         nCoverPrecision = AVC_DEFAULT_PREC;
    char        cPrecisionCode = '2';
    const char *szFname = NULL;
    AVCBinFile *psFile=NULL;

    psInfo->numSections = 0;
    psInfo->pasSections = NULL;

    /*-----------------------------------------------------------------
     * Build the absolute coverage path to include in the EXP  0 line
     * This line usually contains the full path of the E00 file that
     * is being created, but since the lib does not write the output
     * file directly, there is no simple way to get that value.  Instead,
     * we will use the absolute coverage path to which we add a .E00
     * extension.
     * We need also make sure cover path is all in uppercase.
     *----------------------------------------------------------------*/
#ifdef WIN32
    if (psInfo->pszCoverPath[0] != '\\' &&
        !(isalpha(psInfo->pszCoverPath[0]) && psInfo->pszCoverPath[1] == ':'))
#else
    if (psInfo->pszCoverPath[0] != '/')
#endif
    {
        if (getcwd(szCWD, 74) == NULL)
            szCWD[0] = '\0';    /* Failed: buffer may be too small */

        nLen = strlen(szCWD);

#ifdef WIN32
        if (nLen > 0 && szCWD[nLen -1] != '\\')
            strcat(szCWD, "\\");
#else
        if (nLen > 0 && szCWD[nLen -1] != '/')
            strcat(szCWD, "/");
#endif
    }

    pszEXPPath = CPLStrdup(CPLSPrintf("EXP  0 %s%-.*s.E00", szCWD,
                                      strlen(psInfo->pszCoverPath)-1,
                                      psInfo->pszCoverPath));
    pcTmp = pszEXPPath;
    for( ; *pcTmp != '\0'; pcTmp++)
        *pcTmp = toupper(*pcTmp);

    /*-----------------------------------------------------------------
     * EXP Header
     *----------------------------------------------------------------*/
    iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                  &(psInfo->numSections), 1);
    psInfo->pasSections[iSect].eType = AVCFileUnknown;
    psInfo->pasSections[iSect].pszName = pszEXPPath;

    /*-----------------------------------------------------------------
     * We have to try to open each file as we go for 2 reasons:
     * - To validate the file's signature in order to detect cases like a user
     *   that places files such as "mystuff.txt" in the cover directory...
     *   this has already happened and obviously lead to problems!)
     * - We also need to find the coverage's precision from the headers
     *----------------------------------------------------------------*/

    /*-----------------------------------------------------------------
     * ARC section (arc.adf)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "arc.adf": "arc";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileARC,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileARC;
        psInfo->pasSections[iSect].pszName = CPLStrdup("ARC");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * CNT section (cnt.adf)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "cnt.adf": "cnt";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileCNT,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileCNT;
        psInfo->pasSections[iSect].pszName = CPLStrdup("CNT");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * LAB section (lab.adf)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "lab.adf": "lab";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileLAB,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileLAB;
        psInfo->pasSections[iSect].pszName = CPLStrdup("LAB");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * PAL section (pal.adf)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "pal.adf": "pal";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFilePAL,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFilePAL;
        psInfo->pasSections[iSect].pszName = CPLStrdup("PAL");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * TOL section (tol.adf for single precision, par.adf for double)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "tol.adf": "tol";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileTOL,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileTOL;
        psInfo->pasSections[iSect].pszName = CPLStrdup("TOL");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "par.adf": "par";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileTOL,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileTOL;
        psInfo->pasSections[iSect].pszName = CPLStrdup("TOL");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * TXT section (txt.adf)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "txt.adf": "txt";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 &&
         (psFile = AVCBinReadOpen(psInfo->pszCoverPath, szFname,
                                  psInfo->eCoverType, AVCFileTXT,
                                  psInfo->psDBCSInfo)) != NULL)
    {
        if (nCoverPrecision == AVC_DEFAULT_PREC)
            nCoverPrecision = psFile->nPrecision;
        AVCBinReadClose(psFile);
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFileTXT;
        psInfo->pasSections[iSect].pszName = CPLStrdup("TXT");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * TX6 section (*.txt)
     * Scan the directory for files with a ".txt" extension.
     * Note: Never seen those in a PC Arc/Info coverage!
     * In weird coverages, the filename ends with "txt" but there is no "."
     *----------------------------------------------------------------*/
    if (psInfo->eCoverType == AVCCoverV7)
        nCoverPrecision = _AVCE00ReadAddJabberwockySection(psInfo, AVCFileTX6,
                                                           "TX6", 
                                                           nCoverPrecision,
                                                           ".txt",
                                                           papszCoverDir);
    else if (psInfo->eCoverType == AVCCoverWeird)
        nCoverPrecision = _AVCE00ReadAddJabberwockySection(psInfo, AVCFileTX6,
                                                           "TX6", 
                                                           nCoverPrecision,
                                                           "txt",
                                                           papszCoverDir);

    /*-----------------------------------------------------------------
     * At this point, we should have read the coverage precsion... and if
     * we haven't yet then we'll just use single by default.
     * We'll need cPrecisionCode for some of the sections that follow.
     *----------------------------------------------------------------*/
    if (nCoverPrecision == AVC_DOUBLE_PREC)
        cPrecisionCode = '3';
    else
        cPrecisionCode = '2';

    /*-----------------------------------------------------------------
     * SIN  2/3 and EOX lines ... ???
     *----------------------------------------------------------------*/
    iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                  &(psInfo->numSections), 2);
    psInfo->pasSections[iSect].eType = AVCFileUnknown;
    psInfo->pasSections[iSect].pszName = CPLStrdup("SIN  X");
    psInfo->pasSections[iSect].pszName[5] = cPrecisionCode;
    iSect++;
    psInfo->pasSections[iSect].eType = AVCFileUnknown;
    psInfo->pasSections[iSect].pszName = CPLStrdup("EOX");
    iSect++;

    /*-----------------------------------------------------------------
     * LOG section (log.adf) (ends with EOL)
     *----------------------------------------------------------------*/

    /*-----------------------------------------------------------------
     * PRJ section (prj.adf) (ends with EOP)
     *----------------------------------------------------------------*/
    szFname = (psInfo->eCoverType==AVCCoverV7 || 
               psInfo->eCoverType==AVCCoverPC2 ) ? "prj.adf": "prj";
    if ( (iFile=CSLFindString(papszCoverDir, szFname)) != -1 )
    {
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), 1);

        psInfo->pasSections[iSect].eType = AVCFilePRJ;
        psInfo->pasSections[iSect].pszName = CPLStrdup("PRJ");
        psInfo->pasSections[iSect].pszFilename=CPLStrdup(papszCoverDir[iFile]);
    }

    /*-----------------------------------------------------------------
     * RXP section (*.rxp)
     * Scan the directory for files with a ".rxp" extension.
     *----------------------------------------------------------------*/
    if (psInfo->eCoverType == AVCCoverV7)
        _AVCE00ReadAddJabberwockySection(psInfo, AVCFileRXP, "RXP", 
                                         nCoverPrecision,".rxp",papszCoverDir);
    else if (psInfo->eCoverType == AVCCoverWeird)
        _AVCE00ReadAddJabberwockySection(psInfo, AVCFileRXP, "RXP", 
                                         nCoverPrecision,"rxp",papszCoverDir);


    /*-----------------------------------------------------------------
     * RPL section (*.pal)
     * Scan the directory for files with a ".rpl" extension.
     *----------------------------------------------------------------*/
    if (psInfo->eCoverType == AVCCoverV7)
        _AVCE00ReadAddJabberwockySection(psInfo, AVCFileRPL, "RPL", 
                                         nCoverPrecision,".pal",papszCoverDir);
    else if (psInfo->eCoverType == AVCCoverWeird)
        _AVCE00ReadAddJabberwockySection(psInfo, AVCFileRPL, "RPL", 
                                         nCoverPrecision,"rpl",papszCoverDir);

    /*-----------------------------------------------------------------
     * IFO section (tables)
     *----------------------------------------------------------------*/
    papszTables = papszFiles = NULL;
    if (psInfo->eCoverType == AVCCoverV7 || 
        psInfo->eCoverType == AVCCoverV7Tables || 
        psInfo->eCoverType == AVCCoverWeird)
    {
        /*-------------------------------------------------------------
         * Unix coverages: get tables from the ../info/arc.dir
         * Weird coverages: the arc.dir is similar but called "arcdr9"
         *------------------------------------------------------------*/
        papszTables = AVCBinReadListTables(psInfo->pszInfoPath, 
                                           psInfo->pszCoverName,
                                           &papszFiles, psInfo->eCoverType,
                                           psInfo->psDBCSInfo);
    }
    else if (psInfo->eCoverType == AVCCoverPC ||
             psInfo->eCoverType == AVCCoverPC2)
    {
        /*-------------------------------------------------------------
         * PC coverages: look for "???.dbf" in the coverage directory
         *               and build the table name using the coverage name
         *               as the table basename, and the dbf file basename
         *               as the table extension.
         *------------------------------------------------------------*/
        for(iFile=0; papszCoverDir && papszCoverDir[iFile]; iFile++)
        {
            if ((nLen = strlen(papszCoverDir[iFile])) == 7 &&
                EQUAL(papszCoverDir[iFile] + nLen -4, ".dbf"))
            {
                papszCoverDir[iFile][nLen - 4] = '\0';
                szFname = CPLSPrintf("%s.%s", psInfo->pszCoverName,
                                              papszCoverDir[iFile]);
                pcTmp = (char*)szFname;
                for( ; *pcTmp != '\0'; pcTmp++)
                    *pcTmp = toupper(*pcTmp);
                papszCoverDir[iFile][nLen - 4] = '.';

                papszTables = CSLAddString(papszTables, szFname);
                papszFiles = CSLAddString(papszFiles, papszCoverDir[iFile]);
            }
        }
    }

    if ((numTables = CSLCount(papszTables)) > 0)
    {
        iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                      &(psInfo->numSections), numTables+2);

        psInfo->pasSections[iSect].eType = AVCFileUnknown;
        psInfo->pasSections[iSect].pszName = CPLStrdup("IFO  X");
        psInfo->pasSections[iSect].pszName[5] = cPrecisionCode;
        iSect++;

        for(iTable=0; iTable<numTables; iTable++)
        {
            psInfo->pasSections[iSect].eType = AVCFileTABLE;
            psInfo->pasSections[iSect].pszName=CPLStrdup(papszTables[iTable]);
            if (papszFiles)
            {
                psInfo->pasSections[iSect].pszFilename=
                                              CPLStrdup(papszFiles[iTable]);
            }
            iSect++;
        }

        psInfo->pasSections[iSect].eType = AVCFileUnknown;
        psInfo->pasSections[iSect].pszName = CPLStrdup("EOI");
        iSect++;

    }
    CSLDestroy(papszTables);
    CSLDestroy(papszFiles);

    /*-----------------------------------------------------------------
     * File ends with EOS
     *----------------------------------------------------------------*/
    iSect = _AVCIncreaseSectionsArray(&(psInfo->pasSections), 
                                  &(psInfo->numSections), 1);
    psInfo->pasSections[iSect].eType = AVCFileUnknown;
    psInfo->pasSections[iSect].pszName = CPLStrdup("EOS");


    return nCoverPrecision;
}


/**********************************************************************
 *                     _AVCE00ReadScanE00()
 *
 * Processes an entire E00 file to find all the interesting sections.
 **********************************************************************/
static void _AVCE00ReadScanE00(AVCE00ReadE00Ptr psRead)
{
    AVCE00ParseInfo *psInfo = psRead->hParseInfo;

    const char *pszLine;
    const char *pszName = 0;
    void       *obj;
    int        iSect = 0;

    while (CPLGetLastErrorNo() == 0 &&
            (pszLine = CPLReadLine(psRead->hFile) ) != NULL )
    {
        obj = _AVCE00ReadNextLineE00(psRead, pszLine);

        if (obj)
        {
            pszName = 0;
            switch (psInfo->eFileType)
            {
            case AVCFileARC:
                pszName = "ARC";
                break;

            case AVCFilePAL:
                pszName = "PAL";
                break;

            case AVCFileCNT:
                pszName = "CNT";
                break;

            case AVCFileLAB:
                pszName = "LAB";
                break;

            case AVCFileRPL:
                pszName = "RPL";
                break;

            case AVCFileTXT:
                pszName = "TXT";
                break;

            case AVCFileTX6:
                pszName = "TX6";
                break;

            case AVCFilePRJ:
                pszName = "PRJ";
                break;

            case AVCFileTABLE:
                pszName = psInfo->hdr.psTableDef->szTableName;
                break;

            default:
                break;
            }

            if (pszName && (psRead->numSections == 0 ||
                    psRead->pasSections[iSect].eType != psInfo->eFileType ||
                    !EQUAL(pszName, psRead->pasSections[iSect].pszName)))
            {
                iSect = _AVCIncreaseSectionsArray(&(psRead->pasSections), 
                                      &(psRead->numSections), 1);

                psRead->pasSections[iSect].eType = psInfo->eFileType;
                /* psRead->pasSections[iSect].pszName = CPLStrdup(psRead->pszCoverName); */
                psRead->pasSections[iSect].pszName = CPLStrdup(pszName);
                psRead->pasSections[iSect].pszFilename = CPLStrdup(psRead->pszCoverPath);
                psRead->pasSections[iSect].nLineNum = psInfo->nStartLineNum;
                psRead->pasSections[iSect].nFeatureCount = 0;
            }

            if (pszName && psRead->numSections)
            {
                /* increase feature count for current layer */
                ++psRead->pasSections[iSect].nFeatureCount;
            }
        }
    }
}

/**********************************************************************
 *                         _AVCE00ReadNextTableLine()
 *
 * Return the next line of the E00 representation of a info table.
 *
 * This function is used by AVCE00ReadNextLine() to generate table
 * output... it should never be called directly.
 **********************************************************************/
static const char *_AVCE00ReadNextTableLine(AVCE00ReadPtr psInfo)
{
    const char *pszLine = NULL;
    AVCE00Section *psSect;

    psSect = &(psInfo->pasSections[psInfo->iCurSection]);

    CPLAssert(psSect->eType == AVCFileTABLE);

    if (psInfo->iCurStep == AVC_GEN_NOTSTARTED)
    {
        /*---------------------------------------------------------
         * Open table and start returning header
         *--------------------------------------------------------*/
        if (psInfo->eCoverType == AVCCoverPC ||
            psInfo->eCoverType == AVCCoverPC2)
        {
            /*---------------------------------------------------------
             * PC Arc/Info: We pass the DBF table's full filename + the
             * Arc/Info table name (for E00 header)
             *--------------------------------------------------------*/
            char *pszFname;
            pszFname = CPLStrdup(CPLSPrintf("%s%s", psInfo->pszInfoPath,
                                                    psSect->pszFilename ));
            psInfo->hFile = AVCBinReadOpen(pszFname, psSect->pszName, 
                                           psInfo->eCoverType, psSect->eType,
                                           psInfo->psDBCSInfo);
            CPLFree(pszFname);
        }
        else
        {
            /*---------------------------------------------------------
             * AVCCoverV7 and AVCCoverWeird: 
             * We pass the INFO dir's path, and the Arc/Info table name
             * will be searched in the arc.dir
             *--------------------------------------------------------*/
            psInfo->hFile = AVCBinReadOpen(psInfo->pszInfoPath, 
                                           psSect->pszName, 
                                           psInfo->eCoverType, psSect->eType,
                                           psInfo->psDBCSInfo);
        }


        /* For some reason the file could not be opened... abort now.
         * An error message should have already been produced by 
         * AVCBinReadOpen()
         */
        if (psInfo->hFile == NULL)
            return NULL;

        psInfo->iCurStep = AVC_GEN_TABLEHEADER;

        pszLine = AVCE00GenTableHdr(psInfo->hGenInfo,
                                    psInfo->hFile->hdr.psTableDef,
                                    FALSE);
    }
        
    if (pszLine == NULL &&
        psInfo->iCurStep == AVC_GEN_TABLEHEADER)
    {
        /*---------------------------------------------------------
         * Continue table header
         *--------------------------------------------------------*/
        pszLine = AVCE00GenTableHdr(psInfo->hGenInfo,
                                    psInfo->hFile->hdr.psTableDef,
                                    TRUE);

        if (pszLine == NULL)
        {
            /* Finished with table header... time to proceed with the
             * table data.
             * Reset the AVCE00GenInfo struct. so that it returns NULL,
             * which will force reading of the first record from the 
             * file on the next call to AVCE00ReadNextLine()
             */
            AVCE00GenReset(psInfo->hGenInfo);
            psInfo->iCurStep = AVC_GEN_TABLEDATA;
        }

    }

    if (pszLine == NULL &&
        psInfo->iCurStep == AVC_GEN_TABLEDATA)
    {
        /*---------------------------------------------------------
         * Continue with records of data
         *--------------------------------------------------------*/

        pszLine = AVCE00GenTableRec(psInfo->hGenInfo, 
                                    psInfo->hFile->hdr.psTableDef->numFields,
                                    psInfo->hFile->hdr.psTableDef->pasFieldDef,
                                    psInfo->hFile->cur.pasFields,
                                    TRUE);

        if (pszLine == NULL)
        {
            /* Current record is finished generating... we need to read 
             * a new one from the file.
             */
            if (AVCBinReadNextObject(psInfo->hFile) != NULL)
            {
                pszLine = AVCE00GenTableRec(psInfo->hGenInfo, 
                                    psInfo->hFile->hdr.psTableDef->numFields,
                                    psInfo->hFile->hdr.psTableDef->pasFieldDef,
                                    psInfo->hFile->cur.pasFields,
                                    FALSE);
            }            

        }
    }

    if (pszLine == NULL)
    {
        /*---------------------------------------------------------
         * No more lines to output for this table ... Close it.
         *--------------------------------------------------------*/
        AVCBinReadClose(psInfo->hFile);
        psInfo->hFile = NULL;

        /*---------------------------------------------------------
         * And now proceed to the next section...
         * OK, I don't really like recursivity either... but it was
         * the simplest way to do this, and anyways we should never
         * have more than one level of recursivity.
         *--------------------------------------------------------*/
        if (psInfo->bReadAllSections)
            psInfo->iCurSection++;
        else
            psInfo->iCurSection = psInfo->numSections;
        psInfo->iCurStep = AVC_GEN_NOTSTARTED;

        pszLine = AVCE00ReadNextLine(psInfo);
    }

    /*-----------------------------------------------------------------
     * Check for errors... if any error happened, tehn return NULL
     *----------------------------------------------------------------*/
    if (CPLGetLastErrorNo() != 0)
    {
        pszLine = NULL;
    }

    return pszLine;
}


/**********************************************************************
 *                          AVCE00ReadNextLine()
 *
 * Returns the next line of the E00 representation of the coverage
 * or NULL when there are no more lines to generate, or if an error happened.
 * The returned line is a null-terminated string, and it does not
 * include a newline character.
 *
 * Call CPLGetLastErrorNo() after calling AVCE00ReadNextLine() to 
 * make sure that the line was generated succesfully.
 *
 * Note that AVCE00ReadNextLine() returns a reference to an
 * internal buffer whose contents will
 * be valid only until the next call to this function.  The caller should
 * not attempt to free() the returned pointer.
 **********************************************************************/
const char *AVCE00ReadNextLine(AVCE00ReadPtr psInfo)
{
    const char *pszLine = NULL;
    AVCE00Section *psSect;

    CPLErrorReset();

    /*-----------------------------------------------------------------
     * Check if we have finished generating E00 output
     *----------------------------------------------------------------*/
    if (psInfo->iCurSection >= psInfo->numSections)
        return NULL;

    psSect = &(psInfo->pasSections[psInfo->iCurSection]);

    /*-----------------------------------------------------------------
     * For simplicity, the generation of table output is in a separate
     * function.
     *----------------------------------------------------------------*/
    if (psSect->eType == AVCFileTABLE)
    {
        return _AVCE00ReadNextTableLine(psInfo);
    }

    if (psSect->eType == AVCFileUnknown)
    {
    /*-----------------------------------------------------------------
     * Section not attached to any file, used to hold header lines
     * or section separators, etc... just return the line directly and
     * move pointer to the next section.
     *----------------------------------------------------------------*/
        pszLine = psSect->pszName;
        if (psInfo->bReadAllSections)
            psInfo->iCurSection++;
        else
            psInfo->iCurSection = psInfo->numSections;
        psInfo->iCurStep = AVC_GEN_NOTSTARTED;
    }
    /*=================================================================
     *              ARC, PAL, CNT, LAB, TOL and TXT
     *================================================================*/
    else if (psInfo->iCurStep == AVC_GEN_NOTSTARTED &&
             (psSect->eType == AVCFileARC ||
              psSect->eType == AVCFilePAL ||
              psSect->eType == AVCFileRPL ||
              psSect->eType == AVCFileCNT ||
              psSect->eType == AVCFileLAB ||
              psSect->eType == AVCFileTOL ||
              psSect->eType == AVCFileTXT ||
              psSect->eType == AVCFileTX6 ||
              psSect->eType == AVCFileRXP   ) )
    {
    /*-----------------------------------------------------------------
     * Start processing of an ARC, PAL, CNT, LAB or TOL section:
     *   Open the file, get ready to read the first object from the 
     *   file, and return the header line.
     *  If the file fails to open then we will return NULL.
     *----------------------------------------------------------------*/
        psInfo->hFile = AVCBinReadOpen(psInfo->pszCoverPath, 
                                       psSect->pszFilename, 
                                       psInfo->eCoverType, psSect->eType,
                                       psInfo->psDBCSInfo);

        /*-------------------------------------------------------------
         * For some reason the file could not be opened... abort now.
         * An error message should have already been produced by 
         * AVCBinReadOpen()
         *------------------------------------------------------------*/
        if (psInfo->hFile == NULL)
            return NULL;

        pszLine = AVCE00GenStartSection(psInfo->hGenInfo, 
                                        psSect->eType, psSect->pszName);

        /*-------------------------------------------------------------
         * Reset the AVCE00GenInfo struct. so that it returns NULL,
         * which will force reading of the first object from the 
         * file on the next call to AVCE00ReadNextLine()
         *------------------------------------------------------------*/
        AVCE00GenReset(psInfo->hGenInfo);
        psInfo->iCurStep = AVC_GEN_DATA;
    }
    else if (psInfo->iCurStep == AVC_GEN_DATA &&
             (psSect->eType == AVCFileARC ||
              psSect->eType == AVCFilePAL ||
              psSect->eType == AVCFileRPL ||
              psSect->eType == AVCFileCNT ||
              psSect->eType == AVCFileLAB ||
              psSect->eType == AVCFileTOL ||
              psSect->eType == AVCFileTXT ||
              psSect->eType == AVCFileTX6 ||
              psSect->eType == AVCFileRXP    ) )
    {
    /*-----------------------------------------------------------------
     * Return the next line of an ARC/PAL/CNT/TOL/TXT object... 
     * if necessary, read the next object from the binary file.
     *----------------------------------------------------------------*/
        pszLine = AVCE00GenObject(psInfo->hGenInfo, 
                                  psSect->eType,
                  (psSect->eType==AVCFileARC?(void*)(psInfo->hFile->cur.psArc):
                   psSect->eType==AVCFilePAL?(void*)(psInfo->hFile->cur.psPal):
                   psSect->eType==AVCFileRPL?(void*)(psInfo->hFile->cur.psPal):
                   psSect->eType==AVCFileCNT?(void*)(psInfo->hFile->cur.psCnt):
                   psSect->eType==AVCFileLAB?(void*)(psInfo->hFile->cur.psLab):
                   psSect->eType==AVCFileTOL?(void*)(psInfo->hFile->cur.psTol):
                   psSect->eType==AVCFileTXT?(void*)(psInfo->hFile->cur.psTxt):
                   psSect->eType==AVCFileTX6?(void*)(psInfo->hFile->cur.psTxt):
                   psSect->eType==AVCFileRXP?(void*)(psInfo->hFile->cur.psRxp):
                   NULL),
                                  TRUE);
        if (pszLine == NULL)
        {
            /*---------------------------------------------------------
             * Current object is finished generating... we need to read 
             * a new one from the file.
             *--------------------------------------------------------*/
            if (AVCBinReadNextObject(psInfo->hFile) != NULL)
            {
                pszLine = AVCE00GenObject(psInfo->hGenInfo, 
                                          psSect->eType,
                  (psSect->eType==AVCFileARC?(void*)(psInfo->hFile->cur.psArc):
                   psSect->eType==AVCFilePAL?(void*)(psInfo->hFile->cur.psPal):
                   psSect->eType==AVCFileRPL?(void*)(psInfo->hFile->cur.psPal):
                   psSect->eType==AVCFileCNT?(void*)(psInfo->hFile->cur.psCnt):
                   psSect->eType==AVCFileLAB?(void*)(psInfo->hFile->cur.psLab):
                   psSect->eType==AVCFileTOL?(void*)(psInfo->hFile->cur.psTol):
                   psSect->eType==AVCFileTXT?(void*)(psInfo->hFile->cur.psTxt):
                   psSect->eType==AVCFileTX6?(void*)(psInfo->hFile->cur.psTxt):
                   psSect->eType==AVCFileRXP?(void*)(psInfo->hFile->cur.psRxp):
                   NULL),
                                          FALSE);
            }            
        }
        if (pszLine == NULL)
        {
            /*---------------------------------------------------------
             * Still NULL ??? This means we finished reading this file...
             * Start returning the "end of section" line(s)...
             *--------------------------------------------------------*/
            AVCBinReadClose(psInfo->hFile);
            psInfo->hFile = NULL;
            psInfo->iCurStep = AVC_GEN_ENDSECTION;
            pszLine = AVCE00GenEndSection(psInfo->hGenInfo, psSect->eType,
                                          FALSE);
        }
    }
    /*=================================================================
     *                          PRJ
     *================================================================*/
    else if (psInfo->iCurStep == AVC_GEN_NOTSTARTED &&
              psSect->eType == AVCFilePRJ   )
    {
        /*-------------------------------------------------------------
         * Start processing of PRJ section... return first header line.
         *------------------------------------------------------------*/
        pszLine = AVCE00GenStartSection(psInfo->hGenInfo, 
                                        psSect->eType, NULL);

        psInfo->hFile = NULL;
        psInfo->iCurStep = AVC_GEN_DATA;
    }
    else if (psInfo->iCurStep == AVC_GEN_DATA &&
             psSect->eType == AVCFilePRJ  )
    {
        /*-------------------------------------------------------------
         * Return the next line of a PRJ section
         *------------------------------------------------------------*/
        if (psInfo->hFile == NULL)
        {
            /*---------------------------------------------------------
             * File has not been read yet...
             * Read the PRJ file, and return the first PRJ line.
             *--------------------------------------------------------*/
            psInfo->hFile = AVCBinReadOpen(psInfo->pszCoverPath, 
                                           psSect->pszFilename, 
                                           psInfo->eCoverType, psSect->eType,
                                           psInfo->psDBCSInfo);

            /* For some reason the file could not be opened... abort now.
             * An error message should have already been produced by 
             * AVCBinReadOpen()
             */
            if (psInfo->hFile == NULL)
                return NULL;

            pszLine = AVCE00GenPrj(psInfo->hGenInfo, 
                                   psInfo->hFile->cur.papszPrj, FALSE);
        }
        else
        {
            /*---------------------------------------------------------
             * Generate the next line of output.
             *--------------------------------------------------------*/
            pszLine = AVCE00GenPrj(psInfo->hGenInfo, 
                                   psInfo->hFile->cur.papszPrj, TRUE);
        }

        if (pszLine == NULL)
        {
            /*---------------------------------------------------------
             * Still NULL ??? This means we finished generating this PRJ 
             * section...
             * Start returning the "end of section" line(s)...
             *--------------------------------------------------------*/
            AVCBinReadClose(psInfo->hFile);
            psInfo->hFile = NULL;
            psInfo->iCurStep = AVC_GEN_ENDSECTION;
            pszLine = AVCE00GenEndSection(psInfo->hGenInfo, psSect->eType,
                                          FALSE);
        }
    }
    else if (psInfo->iCurStep != AVC_GEN_ENDSECTION)
    {
        /* We should never get here! */
        CPLAssert(FALSE);
    }


    /*=================================================================
     *                End of section, for all files
     *================================================================*/

    /*-----------------------------------------------------------------
     * Finished processing of an ARC, PAL, CNT, LAB, TOL, PRJ file ...
     * continue returning the "end of section" line(s), and move the pointer
     * to the next section once we're done.
     *----------------------------------------------------------------*/
    if (psInfo->iCurStep == AVC_GEN_ENDSECTION && pszLine == NULL)
    {
        pszLine = AVCE00GenEndSection(psInfo->hGenInfo, psSect->eType, TRUE);

        if (pszLine == NULL)
        {
            /*---------------------------------------------------------
             * Finished returning the last lines of the section...
             * proceed to the next section...
             * OK, I don't really like recursivity either... but it was
             * the simplest way to do this, and anyways we should never
             * have more than one level of recursivity.
             *--------------------------------------------------------*/
            if (psInfo->bReadAllSections)
                psInfo->iCurSection++;
            else
                psInfo->iCurSection = psInfo->numSections;
            psInfo->iCurStep = AVC_GEN_NOTSTARTED;

            pszLine = AVCE00ReadNextLine(psInfo);
        }
    }

    return pszLine;
}



/**********************************************************************
 *                         AVCE00ReadSectionsList()
 *
 * Returns an array of AVCE00Section structures that describe the
 * squeleton of the whole coverage.  The value of *numSect will be
 * set to the number of sections in the array.
 *
 * You can scan the returned array, and use AVCE00ReadGotoSection() to move
 * the read pointer directly to the beginning of a given section
 * of the file.
 *
 * Sections of type AVCFileUnknown correspond to lines in the
 * E00 output that are not directly linked to any coverage file, like 
 * the "EXP 0" line, the "IFO X", "SIN X", etc.
 *
 * THE RETURNED ARRAY IS AN INTERNAL STRUCTURE AND SHOULD NOT BE
 * MODIFIED OR FREED BY THE CALLER... its contents will be valid
 * for as long as the coverage will remain open.
 **********************************************************************/
AVCE00Section *AVCE00ReadSectionsList(AVCE00ReadPtr psInfo, int *numSect)
{
    CPLErrorReset();

    *numSect = psInfo->numSections;
    return psInfo->pasSections;
}

/**********************************************************************
 *                         AVCE00ReadGotoSection()
 *
 * Move the read pointer to the E00 section (coverage file) described in 
 * the psSect structure.  Call AVCE00ReadSectionsList() to get the list of
 * sections for the current coverage.
 *
 * if bContinue=TRUE, then reading will automatically continue with the
 * next sections of the file once the requested section is finished.
 * Otherwise, if bContinue=FALSE then reading will stop at the end
 * of this section (i.e. AVCE00ReadNextLine() will return NULL when 
 * it reaches the end of this section)
 *
 * Sections of type AVCFileUnknown returned by AVCE00ReadSectionsList()
 * correspond to lines in the E00 output that are not directly linked
 * to any coverage file, like the "EXP 0" line, the "IFO X", "SIN X", etc.
 * You can jump to these sections or any other one without problems.
 *
 * This function returns 0 on success or -1 on error.
 **********************************************************************/
int AVCE00ReadGotoSection(AVCE00ReadPtr psInfo, AVCE00Section *psSect,
                          GBool bContinue)
{
    int     iSect;
    GBool   bFound = FALSE;

    CPLErrorReset();

    /*-----------------------------------------------------------------
     * Locate the requested section in the array.
     *----------------------------------------------------------------*/
    for(iSect=0; iSect<psInfo->numSections; iSect++)
    {
        if (psInfo->pasSections[iSect].eType == psSect->eType &&
            EQUAL(psInfo->pasSections[iSect].pszName, psSect->pszName))
        {
            bFound = TRUE;
            break;
        }
    }

    /*-----------------------------------------------------------------
     * Not found ... generate an error...
     *----------------------------------------------------------------*/
    if (!bFound)
    {
        CPLError(CE_Failure, CPLE_IllegalArg, 
                 "Requested E00 section does not exist!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Found it ... close current section and get ready to read 
     * the new one.
     *----------------------------------------------------------------*/
    if (psInfo->hFile)
    {
        AVCBinReadClose(psInfo->hFile);
        psInfo->hFile = NULL;
    }

    psInfo->bReadAllSections = bContinue;
    psInfo->iCurSection = iSect;
    psInfo->iCurStep = AVC_GEN_NOTSTARTED;

    return 0;
}

/**********************************************************************
 *                         AVCE00ReadRewind()
 *
 * Rewinds the AVCE00ReadPtr just like the stdio rewind() 
 * function would do if you were reading an ASCII E00 file.
 *
 * Returns 0 on success or -1 on error.
 **********************************************************************/
int  AVCE00ReadRewind(AVCE00ReadPtr psInfo)
{
    CPLErrorReset();

    return AVCE00ReadGotoSection(psInfo, &(psInfo->pasSections[0]), TRUE);
}

/**********************************************************************
 *                         AVCE00ReadRewindE00()
 *
 * Rewinds the AVCE00ReadE00Ptr just like the stdio rewind() 
 * function would do if you were reading an ASCII E00 file.
 *
 * Returns 0 on success or -1 on error.
 **********************************************************************/
int  AVCE00ReadRewindE00(AVCE00ReadE00Ptr psRead)
{
    CPLErrorReset();

    psRead->bReadAllSections = TRUE;
    psRead->eCurFileType = AVCFileUnknown;

    psRead->hParseInfo->nCurLineNum = 0;
    psRead->hParseInfo->nStartLineNum = 0;
    psRead->hParseInfo->bForceEndOfSection = TRUE;
    psRead->hParseInfo->eSuperSectionType = AVCFileUnknown;
    AVCE00ParseSectionEnd(psRead->hParseInfo, NULL, 1);

    return fseek(psRead->hFile, 0, SEEK_SET);
}

/**********************************************************************
 *                        _AVCE00ReadSeekE00()
 *
 * Seeks to a new location in the E00 file, keeping parse state
 * appropriately.
 *
 * NOTE: This is a pretty slow implementation.
 * NOTE: The SEEK_END is not implemented.
 *
 * Returns 0 on success or -1 on error.
 **********************************************************************/
static int _AVCE00ReadSeekE00(AVCE00ReadE00Ptr psRead, int nOffset,
        int nWhence)
{
    const char *pszLine;
    void       *obj;

    switch (nWhence)
    {
    case SEEK_CUR:
        break;

    case SEEK_SET:
        AVCE00ReadRewindE00(psRead);
        break;

    default:
        CPLAssert(nWhence == SEEK_CUR || nWhence == SEEK_SET);
        break;
    }

    while (nOffset-- &&
            CPLGetLastErrorNo() == 0 &&
            (pszLine = CPLReadLine(psRead->hFile) ) != NULL )
    {
        obj = _AVCE00ReadNextLineE00(psRead, pszLine);
    }

    return nOffset ? -1 : 0;
}

/**********************************************************************
 *                      AVCE00ReadNextObjectE00()
 *
 * Returns the next object in an E00 file or NULL when there are no
 * more objects, or if an error happened.  The object type can be
 * determined via the eCurFileType attribute of the
 * AVCE00ReadE00Ptr object.
 *
 * Note that AVCE00ReadNextLine() returns a reference to an internal
 * buffer whose contents will be valid only until the next call to
 * this function.  The caller should not attempt to free() the
 * returned pointer.
 **********************************************************************/
void *AVCE00ReadNextObjectE00(AVCE00ReadE00Ptr psRead)
{
    const char *pszLine;
    void       *obj = NULL;

    do
    {
        pszLine = CPLReadLine(psRead->hFile);
        if (pszLine == 0)
            break;
        obj = _AVCE00ReadNextLineE00(psRead, pszLine);
    }
    while (obj == NULL &&
            (psRead->bReadAllSections ||
             psRead->eCurFileType != AVCFileUnknown) &&
            CPLGetLastErrorNo() == 0);
    return obj;
}

/**********************************************************************
 *                         AVCE00ReadSectionsListE00()
 *
 * Returns an array of AVCE00Section structures that describe the
 * sections in the E00 file.  The value of *numSect will be set to the
 * number of sections in the array.
 *
 * You can scan the returned array, and use AVCE00ReadGotoSectionE00()
 * to move the read pointer directly to the beginning of a given
 * section of the file.
 *
 * THE RETURNED ARRAY IS AN INTERNAL STRUCTURE AND SHOULD NOT BE
 * MODIFIED OR FREED BY THE CALLER... its contents will be valid
 * for as long as the coverage will remain open.
 **********************************************************************/
AVCE00Section *AVCE00ReadSectionsListE00(AVCE00ReadE00Ptr psRead,
        int *numSect)
{
    CPLErrorReset();

    *numSect = psRead->numSections;
    return psRead->pasSections;
}

/**********************************************************************
 *                         AVCE00ReadGotoSectionE00()
 *
 * Move the read pointer to the E00 section described in the psSect
 * structure.  Call AVCE00ReadSectionsListE00() to get the list of
 * sections for the current coverage.
 *
 * If bContinue is TRUE, then reading will automatically continue with
 * the next section of the file once the requested section is finished.
 * Otherwise, if bContinue is FALSE then reading will stop at the end
 * of this section (i.e. AVCE00ReadNextObjectE00() will return NULL
 * when the end of this section is reached)
 *
 * This function returns 0 on success or -1 on error.
 **********************************************************************/
int AVCE00ReadGotoSectionE00(AVCE00ReadE00Ptr psRead,
        AVCE00Section *psSect, GBool bContinue)
{
    int     iSect;
    GBool   bFound = FALSE;

    CPLErrorReset();

    /*-----------------------------------------------------------------
     * Locate the requested section in the array.
     *----------------------------------------------------------------*/
    for(iSect=0; iSect<psRead->numSections; iSect++)
    {
        if (psRead->pasSections[iSect].eType == psSect->eType &&
            EQUAL(psRead->pasSections[iSect].pszName, psSect->pszName))
        {
            bFound = TRUE;
            break;
        }
    }

    /*-----------------------------------------------------------------
     * Not found ... generate an error...
     *----------------------------------------------------------------*/
    if (!bFound)
    {
        CPLError(CE_Failure, CPLE_IllegalArg, 
                 "Requested E00 section does not exist!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Found it ... advance parser to line number of start of section
     *----------------------------------------------------------------*/
    _AVCE00ReadSeekE00(psRead, psRead->pasSections[iSect].nLineNum, SEEK_SET);

    psRead->bReadAllSections = bContinue;

    return 0;
}