File: embdbi.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 571,544 kB
  • sloc: ansic: 460,579; java: 29,439; perl: 13,573; sh: 12,754; makefile: 3,283; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (2265 lines) | stat: -rw-r--r-- 58,445 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
/* @source embdbi *************************************************************
**
** General routines for alignment.
**
** @author Copyright (c) 2000 Peter Rice
** @version $Revision: 1.63 $
** @modified $Date: 2012/07/14 14:52:40 $ by $Author: rice $
** @@
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301,  USA.
**
******************************************************************************/

#include "ajlib.h"

#include "embdbi.h"
#include "ajfile.h"
#include "ajlist.h"
#include "ajutil.h"
#include "ajtime.h"
#include "ajreg.h"
#include "ajsys.h"
#include "ajfileio.h"

#include <math.h>
#ifndef WIN32
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#else
#include "win32.h"
#include "dirent_w32.h"
#endif

#include <errno.h>



static AjPStr dbiCmdStr    = NULL;
static AjPStr dbiCmdStr2    = NULL;
static AjPStr dbiDirFix    = NULL;
static AjPStr dbiWildFname = NULL;
static AjPStr dbiInFname   = NULL;
static AjPStr dbiOutFname  = NULL;
static AjPStr dbiOutRecord = NULL;
static AjPStr dbiSortExt   = NULL;
static AjPStr dbiLastId    = NULL;
static AjPStr dbiFieldId   = NULL;
static AjPStr dbiIdStr     = NULL;
static AjPStr dbiTmpStr     = NULL;
static AjPStr dbiRdLine     = NULL;
static AjPStr dbiIdLine     = NULL;
static AjPStr dbiFieldSort     = NULL;
static AjPStr dbiFieldSort2     = NULL;
static AjPStr dbiFieldStr     = NULL;
static AjPStr dbiFieldName     = NULL;
static AjPStr dbiFieldId2     = NULL;
static AjPStr dbiCurrentId = NULL;

static AjPRegexp dbiRegFieldIdSort    = NULL;
static AjPRegexp dbiRegFieldTokSort   = NULL;
static AjPRegexp dbiRegFieldTokIdSort = NULL;
static AjPRegexp dbiRegEntryIdSort    = NULL;
static AjPRegexp dbiRegDate           = NULL;




/* @datastatic DbiOField ******************************************************
**
** Database index field names and index filenames
**
** @attr name [const char*] Field name as used in USAs
** @attr index [const char*] Index filename for EMBLCD indices
** @attr desc [const char*] Field description
******************************************************************************/

typedef struct DbiSField
{
    const char* name;
    const char* index;
    const char* desc;
} DbiOField;


static DbiOField fieldDef[] =
{
   /* Name  Index      Description */
    {"acc", "acnum",   "accession number"},
    {"sv",  "seqvn",   "seqeunce version and GI number"},
    {"des", "des",     "entry description"},
    {"org", "taxon",   "taxonomy and organism"},
    {"key", "keyword", "keywords"},
    {NULL, NULL, NULL}
};

static const char* dbiFieldFile(const AjPStr fieldname);




/* @func embDbiFieldNew *******************************************************
**
** Constructor for field token structures.
**
** @return [EmbPField] Field token structure.
**
** @release 2.4.0
******************************************************************************/

EmbPField embDbiFieldNew(void)
{
    EmbPField ret;
    AJNEW0(ret);

    return ret;
}




/* @func embDbiFieldDel *******************************************************
**
** Destructor for field token structures.
**
** @param [d] pthys [EmbPField*] Field token structure.
** @return [void]
**
** @release 4.1.0
******************************************************************************/

void embDbiFieldDel(EmbPField* pthys)
{
    EmbPField thys;

    if(!pthys || !*pthys)
	return;

    thys = *pthys;

    AJFREE(thys->field);
    AJFREE(thys->entry);
    AJFREE(*pthys);

    return;
}




/* @func embDbiFieldDelMap ****************************************************
**
** Destructor for field token structures to be mapped to lists or tables.
**
** @param [d] pthys [void**] Field token structure.
** @param [u] cl [void*] Unused
** @return [void]
**
** @release 4.1.0
******************************************************************************/

void embDbiFieldDelMap(void** pthys, void* cl)
{
    EmbPField thys = (*(EmbPField*)pthys);

    if(!thys)
	return;

    (void) cl;				/* make it used */

    thys = *pthys;

    /*AJFREE(thys->field);*/
    /*AJFREE(thys->entry);*/
    AJFREE(*pthys);

    return;
}




/* @func embDbiCmpId **********************************************************
**
** Comparison function for two entries.
**
** @param [r] a [const void*] First id (EmbPEntry*)
** @param [r] b [const void*] Second id (EmbPEntry*)
** @return [ajint] Comparison value, -1, 0 or +1.
**
** @release 1.13.0
** @@
******************************************************************************/

ajint embDbiCmpId(const void* a, const void* b)
{
    const EmbPEntry aa;
    const EmbPEntry bb;

    aa = *(EmbPEntry const *) a;
    bb = *(EmbPEntry const *) b;

    return strcmp(aa->entry, bb->entry);
}




/* @func embDbiCmpFieldId *****************************************************
**
** Comparison function for the entrynames in two field structures.
**
** @param [r] a [const void*] First id (EmbPField*)
** @param [r] b [const void*] Second id (EmbPField*)
** @return [ajint] Comparison value, -1, 0 or +1.
**
** @release 2.4.0
** @@
******************************************************************************/

ajint embDbiCmpFieldId(const void* a, const void* b)
{
    const EmbPField aa;
    const EmbPField bb;

    aa = *(EmbPField const *) a;
    bb = *(EmbPField const *) b;

    return strcmp(aa->entry, bb->entry);
}




/* @func embDbiCmpFieldField **************************************************
**
** Comparison function for two field token values
**
** @param [r] a [const void*] First id (EmbPField*)
** @param [r] b [const void*] Second id (EmbPField*)
** @return [ajint] Comparison value, -1, 0 or +1.
**
** @release 2.4.0
** @@
******************************************************************************/

ajint embDbiCmpFieldField(const void* a, const void* b)
{
    ajint ret;

    const EmbPField aa;
    const EmbPField bb;

    aa = *(EmbPField const *) a;
    bb = *(EmbPField const *) b;

    ret = strcmp(aa->field, bb->field);

    if(ret)
	return ret;

    return strcmp(aa->entry, bb->entry);
}




/* @func embDbiEntryNew *******************************************************
**
** Constructor for entry structures.
**
** @param [r] nfields [ajuint] Number of data fields to be included
** @return [EmbPEntry] Entry structure.
**
** @release 1.13.0
******************************************************************************/

EmbPEntry embDbiEntryNew(ajuint nfields)
{
    EmbPEntry ret;

    AJNEW0(ret);
    ret->nfields = nfields;
    AJCNEW0(ret->nfield, nfields);
    AJCNEW0(ret->field, nfields);

    return ret;
}




/* @func embDbiEntryDel *******************************************************
**
** Destructor for entry structures.
**
** @param [d] Pentry [EmbPEntry*] Entry structure
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiEntryDel(EmbPEntry* Pentry)
{
    EmbPEntry entry;
    ajuint i;
    ajuint j;

    if(!*Pentry)
        return;

    entry = *Pentry;

    for(i=0;i<entry->nfields;i++)
    {
	for(j=0;j<entry->nfield[i];j++)
	{
	    AJFREE(entry->field[i][j]);
	}

	AJFREE(entry->field[i]);
    }

    AJFREE(entry->nfield);
    AJFREE(entry->field);
    AJFREE(entry->entry);
    AJFREE(*Pentry);

    return;
}




/* @func embDbiEntryDelMap ****************************************************
**
** Destructor for entry structures to be mapped to lists or tables.
**
** @param [d] pthys [void**] Field token structure.
** @param [u] cl [void*] Unused
** @return [void]
**
** @release 4.1.0
******************************************************************************/

void embDbiEntryDelMap(void** pthys, void* cl)
{
    EmbPEntry entry;
    ajuint i;
    ajuint j;

    if(!pthys || !*pthys)
	return;

    (void) cl;				/* make it used */

    entry = (*(EmbPEntry*)pthys);

    for(i=0;i<entry->nfields;i++)
    {
	for(j=0;j<entry->nfield[i];j++)
	{
	    AJFREE(entry->field[i][j]);
	}

	AJFREE(entry->field[i]);
    }

    AJFREE(entry->nfield);
    AJFREE(entry->field);
    AJFREE(entry->entry);
    AJFREE(*pthys);

    return;
}




/* @func embDbiFileList *******************************************************
**
** Makes a list of all files in a directory matching a wildcard file name.
**
** @param [r] dir [const AjPStr] Directory
** @param [r] wildfile [const AjPStr] Wildcard file name
** @param [r] trim [AjBool] Expand to search, trim results
** @return [AjPList] New list of all files with full paths
**
** @release 1.13.0
** @@
******************************************************************************/

AjPList embDbiFileList(const AjPStr dir, const AjPStr wildfile, AjBool trim)
{
    AjPList retlist = NULL;

    DIR* dp;
    struct dirent* de;
    ajuint dirsize;

    AjPStr name = NULL;
    AjPStr tmp;
    AjPStr s;
    AjPStr s2;
    AjPStr t;

    char *p;
    char *q;
    AjPList l;
    ajuint ll;
    ajuint i;
    AjBool d;

    ajDebug("embDbiFileList dir '%S' wildfile '%S' maxsize %Ld\n",
	    dir, wildfile, (ajlong) INT_MAX);

    ajStrAssignS(&dbiWildFname,wildfile);

    tmp = ajStrNewS(dbiWildFname);

    if(ajStrGetLen(dir))
	ajStrAssignS(&dbiDirFix, dir);
    else
	ajStrAssignC(&dbiDirFix, CURRENT_DIR);

    if(ajStrGetCharLast(dbiDirFix) != SLASH_CHAR)
	ajStrAppendC(&dbiDirFix, SLASH_STRING);

    if(trim)
	ajStrAppendC(&dbiWildFname,"*");

    dp = opendir(ajStrGetPtr(dbiDirFix));

    if(!dp)
	ajFatal("opendir failed on '%S'", dbiDirFix);

    s = ajStrNew();
    l = ajListNew();
    dirsize = 0;
    retlist = ajListstrNew();

    while((de = readdir(dp)))
    {
#ifndef __CYGWIN__
	if(!de->d_ino)
	    continue; 		/* skip deleted files with inode zero */
#endif

	if(ajCharMatchC(de->d_name, "."))
	    continue;

	if(ajCharMatchC(de->d_name, ".."))
	    continue;

	if(!ajCharMatchWildS(de->d_name, dbiWildFname))
	    continue;

	ajStrAssignC(&s,de->d_name);
	p = q =ajStrGetuniquePtr(&s);

	if(trim)
	{
	    p=strrchr(p,(int)'.');

	    if(p)
		*p='\0';
	}

	s2 = ajStrNewC(q);
	ll = (ajuint) ajListGetLength(l);
	d = ajFalse;

	for(i=0;i<ll;++i)
	{
	    ajListPop(l,(void *)&t);

	    if(ajStrMatchS(t,s2))
		d=ajTrue;

	    ajListPushAppend(l,(void *)t);
	}

	if(!d)
	    ajListPush(l,(void *)s2);
	else
	{
	    ajStrDel(&s2);
	    continue;
	}

	dirsize++;
	name = NULL;
	ajFmtPrintS(&name, "%S%S", dbiDirFix, s2);

	if(ajFilenameGetSize(name) > (ajlong) INT_MAX)
	  ajDie("File '%S' too large for DBI indexing", name);

	ajDebug("accept '%S' (%Ld)\n", s2, ajFilenameGetSize(name));
	ajListstrPushAppend(retlist, name);
    }

    if(!ajListGetLength(retlist))
	ajFatal("No match for file specification %S",tmp);

    while(ajListPop(l,(void *)&t))
	ajStrDel(&t);

    ajListFree(&l);

    ajStrDel(&s);
    ajStrDel(&tmp);

    closedir(dp);
    ajDebug("%u files for '%S' '%S'\n", dirsize, dir, dbiWildFname);

    return retlist;
}




/* @func embDbiFileListExc ****************************************************
**
** Makes a list of all files in a directory matching a wildcard file name.
**
** @param [r] dir [const AjPStr] Directory
** @param [r] wildfile [const AjPStr] Wildcard file list
** @param [r] exclude [const AjPStr] Wildcard file list
**                                   (NULL if none to exclude)
** @return [AjPList] New list of all files with full paths
**
** @release 1.13.2
** @@
******************************************************************************/

AjPList embDbiFileListExc(const AjPStr dir, const AjPStr wildfile,
			  const AjPStr exclude)
{
    AjPList retlist = NULL;

    DIR* dp;
    struct dirent* de;
    ajuint dirsize;
    AjPStr name = NULL;

    ajDebug("embDbiFileListExc dir '%S' wildfile '%S' exclude '%S' "
            "maxsize %Ld\n",
	    dir, wildfile, exclude, (ajlong) INT_MAX);

    if(ajStrGetLen(dir))
	ajStrAssignS(&dbiDirFix, dir);
    else
	ajStrAssignC(&dbiDirFix, CURRENT_DIR);

    if(ajStrGetCharLast(dbiDirFix) != SLASH_CHAR)
	ajStrAppendC(&dbiDirFix, SLASH_STRING);

    ajDebug("dirfix '%S'\n", dbiDirFix);

    dp = opendir(ajStrGetPtr(dbiDirFix));

    if(!dp)
	ajFatal("opendir failed on '%S'", dbiDirFix);

    dirsize = 0;
    retlist = ajListstrNew();

    while((de = readdir(dp)))
    {
	/* skip deleted files with inode zero */	
#ifndef __CYGWIN__
	if(!de->d_ino)
	    continue;
#endif

	if(ajCharMatchC(de->d_name, "."))
	    continue;

	if(ajCharMatchC(de->d_name, ".."))
	    continue;

	ajStrAssignC(&dbiInFname, de->d_name);

	if(exclude && !ajFilenameTestExclude(dbiInFname, exclude, wildfile))
	    continue;

	dirsize++;
	name = NULL;
	ajFmtPrintS(&name, "%S%S", dbiDirFix, dbiInFname);

	if(ajFilenameGetSize(name) > (ajlong) INT_MAX)
	  ajDie("File '%S' too large for DBI indexing", name);

	ajDebug("accept '%S' (%Ld)\n", dbiInFname, ajFilenameGetSize(name));
	ajListstrPushAppend(retlist, name);
    }

    closedir(dp);
    ajDebug("%u files for '%S' '%S'\n", dirsize, dir, wildfile);

    return retlist;
}




/* @func embDbiFlatOpenlib ****************************************************
**
** Open a flat file library
**
** @param [r] lname [const AjPStr] Source file basename
** @param [u] libr [AjPFile*] Database file
** @return [AjBool] ajTrue on success
**
** @release 2.3.0
** @@
******************************************************************************/

AjBool embDbiFlatOpenlib(const AjPStr lname, AjPFile* libr)
{
    ajFileClose(libr);

    *libr = ajFileNewInNameS(lname);

    if(!*libr)
	ajFatal("Cannot open %S for reading",lname);

    if(!*libr)
    {
	ajErr(" cannot open library flat file: %S\n",
	      lname);

	return ajFalse;
    }

    return ajTrue;
}




/* @func embDbiRmFile *********************************************************
**
** Remove a file or a set of numbered files
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] ext [const char*] Base file extension
** @param [r] nfiles [ajuint] Number of files, or zero for unnumbered.
** @param [r] cleanup [AjBool] If ajTrue, clean up temporary files after
** @return [void]
**
** @release 1.13.0
** @@
******************************************************************************/

void embDbiRmFile(const AjPStr dbname, const char* ext, ajuint nfiles,
		  AjBool cleanup)
{
#ifndef WIN32
    ajuint i;

    if(!cleanup)
	return;

    if(nfiles)
    {
	for(i=1; i<= nfiles; i++)
        {
	    ajFmtPrintS(&dbiCmdStr, "%S%03d.%s", dbname, i, ext);
            ajSysCommandRemoveS(dbiCmdStr);
        }
    }
    else
    {
	ajFmtPrintS(&dbiCmdStr, "%S.%s", dbname, ext);
        ajSysCommandRemoveS(dbiCmdStr);
    }

    return;

#else	/* WIN32 */
    static AjPStr filestr = NULL;
    ajuint i;
    
    if (!cleanup)
	return;
    
    if (nfiles)
    {
	for (i=1; i<= nfiles; i++)
	{
	    ajFmtPrintS (&filestr, "%S%03d.%s", dbname, i, ext);
	    DeleteFile(ajStrGetPtr(filestr));
	    ajDebug("Deleting file %S\n", filestr);
	}
    }
    else
    {
	ajFmtPrintS (&filestr, "%S.%s", dbname, ext);
	DeleteFile(ajStrGetPtr(filestr));
	ajDebug("Deleting file %S\n", filestr);
    }
    
    return;
#endif	/* WIN32 */
}




/* @func embDbiRmFileI ********************************************************
**
** Remove a numbered file
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] ext [const char*] Base file extension
** @param [r] ifile [ajuint] File number.
** @param [r] cleanup [AjBool] If ajTrue, clean up temporary files after
** @return [void]
**
** @release 1.13.0
******************************************************************************/

void embDbiRmFileI(const AjPStr dbname, const char* ext, ajuint ifile,
		   AjBool cleanup)
{
#ifndef WIN32
    if(!cleanup)
	return;

    ajFmtPrintS(&dbiCmdStr, "%S%03d.%s", dbname, ifile, ext);

    ajSysCommandRemoveS(dbiCmdStr);
#else
    static AjPStr filestr = NULL;
    
    if(!cleanup)
	return;
    
    ajFmtPrintS (&filestr, "%S%03d.%s", dbname, ifile, ext);
    DeleteFile(ajStrGetPtr(filestr));
    ajDebug("Deleting file %S\n", filestr);
#endif	/* WIN32 */

    return;
}




/* @func embDbiRmEntryFile ****************************************************
**
** Remove the sorted entryname file (kept until end of processing
** as it is the sorted list of all entries, used to count entries for
** field indexing.
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] cleanup [AjBool] If ajTrue, clean up temporary files after
** @return [void]
**
** @release 2.4.0
** @@
******************************************************************************/

void embDbiRmEntryFile(const AjPStr dbname,  AjBool cleanup)
{
    embDbiRmFile(dbname, "idsrt", 0, cleanup);

    return;
}




/* @func embDbiSortFile *******************************************************
**
** Sort a file, or a set of numbered files, individually
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] ext1 [const char*] Input file extension
** @param [r] ext2 [const char*] Output file extension
** @param [r] nfiles [ajuint] Number of files to sort (zero if unnumbered)
** @param [r] cleanup [AjBool] If ajTrue, clean up temporary files after
** @param [r] sortopt [const AjPStr] Extra options for the system sort
** @return [void]
**
** @release 1.13.0
** @@
******************************************************************************/

void embDbiSortFile(const AjPStr dbname, const char* ext1, const char* ext2,
		    ajuint nfiles, AjBool cleanup, const AjPStr sortopt)
{
    ajuint i;
    AjPStr dir = NULL;
    ajuint j;
    ajuint isplit;
    ajuint nsplit;
    double td;
    
#ifndef WIN32
    static const char *prog = "sort";

    dir = ajStrNewC(prog);
    ajSysFileWhich(&dir);

#else
    static const char *prog = "sort.exe";
    
    char* sortProgDir = getenv("EMBOSS_ROOT");

    if(sortProgDir == NULL)
    {
	AjPStr msg = ajStrNewC("EMBOSS_ROOT");
	ajStrAppendC(&msg, " environment variable not defined");
	ajFatal(ajStrGetPtr(msg));
    }

    dir = ajStrNewC(sortProgDir);
    ajStrAppendC(&dir,SLASH_STRING);
    ajStrAppendC(&dir,prog);

    if(!ajFilenameExistsExec(dir))
    {
        ajFmtPrintS(&dir, "%s\\apps\\release\\%s", sortProgDir, prog);
    }

    if(!ajFilenameExistsExec(dir))
    {
        ajFatal("'%s' not found in EMBOSS_ROOT or apps\\release", prog);
    }

#endif


    if(nfiles)
    {
	for(i=1; i<=nfiles; i++)
	{
	    ajFmtPrintS(&dbiInFname, "%S%03d.%s", dbname, i, ext1);
	    ajFmtPrintS(&dbiOutFname, "%S%03d.%s.srt", dbname, i, ext1);

	    if(sortopt)
		ajFmtPrintS(&dbiCmdStr, "%S -o %S %S %S",
			    dir,dbiOutFname,sortopt,dbiInFname);
	    else
		ajFmtPrintS(&dbiCmdStr, "%S -o %S %S",
			    dir,dbiOutFname,dbiInFname);

	    ajSysExecLocaleC(ajStrGetPtr(dbiCmdStr), "C");
	    embDbiRmFileI(dbname, ext1, i, cleanup);
	}

	td = sqrt(nfiles);
	nsplit = (ajuint) td;
	
	ajDebug("embDbiSortFile nfiles:%d split:%d\n", nfiles, nsplit);

	/* file merge in groups if more than 24 files ... avoids huge merges */

	if(nsplit < 2)		/* up to 3 source files */
	{
            ajFmtPrintS(&dbiCmdStr, "%S -m -o %S.%s %S",
                        dir,dbname,ext2,sortopt);

            for(i=1; i<=nfiles; i++)
                ajFmtPrintAppS(&dbiCmdStr, " %S%03d.%s.srt", dbname, i, ext1);

            ajSysExecLocaleC(ajStrGetPtr(dbiCmdStr), "C");
            ajFmtPrintS(&dbiSortExt, "%s.srt", ext1);

            for(i=1; i<=nfiles; i++)
                embDbiRmFileI(dbname, ajStrGetPtr(dbiSortExt), i, cleanup);

	}
	else
	{
            ajFmtPrintS(&dbiCmdStr2, "%S -m -o %S.%s %S",
                        dir,dbname,ext2,sortopt);
            isplit = 0;

            for(i=1; i<=nfiles; i+=nsplit)
            {
                isplit++;
                ajFmtPrintAppS(&dbiCmdStr2, " %S%03d.%s.mrg1",
                               dbname, isplit, ext2);

                /* Now we make that .mrg1 file */

                ajFmtPrintS(&dbiCmdStr, "%S -m -o %S%03d.%s.mrg1 %S",
                            dir,dbname,isplit,ext2,sortopt);

                for(j=0; j<nsplit; j++)
                    if((i+j) <= nfiles)
                        ajFmtPrintAppS(&dbiCmdStr, " %S%03d.%s.srt",
                                       dbname, i+j, ext1);

                ajSysExecLocaleC(ajStrGetPtr(dbiCmdStr),"C");
                ajFmtPrintS(&dbiSortExt, "%s.srt", ext1);

                for(j=0; j<nsplit; j++)
                    if((i+j) <= nfiles)
                        embDbiRmFileI(dbname, ajStrGetPtr(dbiSortExt), (i+j),
                                      cleanup);
            }

            ajSysExecLocaleC(ajStrGetPtr(dbiCmdStr2), "C");
            ajFmtPrintS(&dbiSortExt, "%s.mrg1", ext2);

            for(j=1; j<=isplit; j++)
                embDbiRmFileI(dbname, ajStrGetPtr(dbiSortExt), j, cleanup);
	}
    }
    else
    {
	ajFmtPrintS(&dbiInFname, "%S.%s", dbname, ext1);
	ajFmtPrintS(&dbiOutFname, "%S.%s", dbname, ext2);
	ajFmtPrintS(&dbiCmdStr, "%S -o %S %S %S",
		    dir,dbiOutFname,sortopt,dbiInFname);

	ajSysExecLocaleC(ajStrGetPtr(dbiCmdStr), "C");
	embDbiRmFile(dbname, ext1, 0, cleanup);
    }

    ajStrDel(&dir);

    return;
}




/* @func embDbiHeaderSize *****************************************************
**
** Updates the file header for an index file to include the correct file size.
**
** @param [u] file [AjPFile] Output file
** @param [r] filesize [ajuint] File size (if known, can be rewritten)
** @param [r] recordcnt [ajuint] Number of records
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiHeaderSize(AjPFile file, ajuint filesize, ajuint recordcnt)
{
    ajFileSeek(file, 0, 0);

    ajWritebinInt4(file, (ajint) filesize);	/* filesize */
    ajWritebinInt4(file, (ajint) recordcnt);	/* #records */

    return;
}




/* @func embDbiHeader *********************************************************
**
** Writes the header for an index file. Resets the file pointer to beginning
** of file, and leaves the file pointer at the start of the first record.
**
** @param [u] file [AjPFile] Output file
** @param [r] filesize [ajuint] File size (if known, can be rewritten)
** @param [r] recordcnt [ajuint] Number of records
** @param [r] recordlen [short] Record length (bytes)
** @param [r] dbname [const AjPStr] Database name (up to 20 characters used)
** @param [r] release [const AjPStr] Release as a string (up to 10
**                                   characters used)
** @param [r] date [const char[4]] Date dd,mm,yy,00
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiHeader(AjPFile file, ajuint filesize, ajuint recordcnt,
		  short recordlen, const AjPStr dbname, const AjPStr release,
		  const char date[4])
{
    ajuint i;
    static char padding[256];
    static AjBool firstcall = AJTRUE;

    if(firstcall)
    {
	for(i=0;i<256;i++)
	    padding[i] = ' ';

	firstcall = ajFalse;
    }

    ajFileSeek(file, 0, 0);

    ajWritebinInt4(file, (ajint) filesize);	/* filesize */

    ajWritebinInt4(file, (ajint) recordcnt);	/* #records */

    ajWritebinInt2(file, (ajint) recordlen);	/* recordsize */

    /* rest of the header */
    ajWritebinStr(file, dbname,  20); /* dbname */
    ajWritebinStr(file, release, 10); /* release */
    ajWritebinByte(file, date[0]);	/* release date */
    ajWritebinByte(file, date[1]);	/* release date */
    ajWritebinByte(file, date[2]);	/* release date */
    ajWritebinByte(file, date[3]);	/* release date */
    ajWritebinBinary(file, 1, 256, padding); /* padding 256 bytes */

    return;
}




/* @func embDbiFileSingle *****************************************************
**
** Builds a filename for a single temporary file to save IDs or some other
** index field, for example EMBL01.list
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] extension [const char*] Filename extension.
** @param [r] num [ajuint] Number for this file (start at 1)
** @return [AjPFile] Opened output file
**
**
** @release 2.4.0
******************************************************************************/

AjPFile embDbiFileSingle(const AjPStr dbname, const char* extension, ajuint num)
{
    AjPFile ret;

    ajFmtPrintS(&dbiOutFname, "%S%03d.%s", dbname, num, extension);
    ret = ajFileNewOutNameS(dbiOutFname);

    if(!ret)
	ajFatal("Cannot open %S for writing", dbiOutFname);

    return ret;
}




/* @func embDbiFileIn *********************************************************
**
** Builds a filename for a summary file to read IDs or some other
** index field, for example EMBL.acnum_sort
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] extension [const char*] Filename extension.
** @return [AjPFile] Opened output file
**
**
** @release 2.4.0
******************************************************************************/

AjPFile embDbiFileIn(const AjPStr dbname, const char* extension)
{
    AjPFile ret;

    ajFmtPrintS(&dbiInFname, "%S.%s", dbname, extension);
    ret = ajFileNewInNameS(dbiInFname);

    if(!ret)
	ajFatal("Cannot open %S for reading", dbiInFname);

    return ret;
}




/* @func embDbiFileOut ********************************************************
**
** Builds a filename for a summary file to save IDs or some other
** index field, for example EMBL.acnum_srt2
**
** @param [r] dbname [const AjPStr] Database name
** @param [r] extension [const char*] Filename extension.
** @return [AjPFile] Opened output file
**
**
** @release 2.4.0
******************************************************************************/

AjPFile embDbiFileOut(const AjPStr dbname, const char* extension)
{
    AjPFile ret;

    ajFmtPrintS(&dbiOutFname, "%S.%s", dbname, extension);
    ret = ajFileNewOutNameS(dbiOutFname);

    if(!ret)
	ajFatal("Cannot open %S for writing", dbiOutFname);

    return ret;
}




/* @func embDbiFileIndex ******************************************************
**
** Builds a filename for a summary file to save IDs or some other
** index field, for example EMBL.acsrt2
**
** @param [r] indexdir [const AjPStr] Index directory
** @param [r] field [const AjPStr] Field name
** @param [r] extension [const char*] Filename extension.
** @return [AjPFile] Opened output file
**
**
** @release 2.4.0
******************************************************************************/

AjPFile embDbiFileIndex(const AjPStr indexdir, const AjPStr field,
			const char* extension)
{
    AjPFile ret;

    ajFmtPrintS(&dbiOutFname, "%S.%s", field, extension);
    ret = ajFileNewOutNamePathS(dbiOutFname, indexdir);

    if(!ret)
	ajFatal("Cannot open %S for writing", dbiOutFname);

    return ret;
}




/* @func embDbiWriteDivision **************************************************
**
** Writes the division index file
**
** @param [r] indexdir [const AjPStr] Index directory
** @param [r] dbname [const AjPStr] Database name
** @param [r] release [const AjPStr] Release number as a string
** @param [r] date [const char[4]] Date
** @param [r] maxfilelen [ajuint] Max file name length
** @param [r] nfiles [ajuint] Number of files indexes
** @param [r] divfiles [AjPStr const *] Division filenames
** @param [r] seqfiles [AjPStr const *] Sequence filenames (or NULL if none)
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiWriteDivision(const AjPStr indexdir,
			 const AjPStr dbname, const AjPStr release,
			 const char date[4],  ajuint maxfilelen, ajuint nfiles,
			 AjPStr const * divfiles, AjPStr const * seqfiles)
{
    AjPFile divFile;
    AjPStr tmpfname = NULL;
    ajuint i;
    ajuint filesize;
 
    short recsize;

    ajStrAssignC(&tmpfname, "division.lkp");
    divFile = ajFileNewOutNamePathS(tmpfname, indexdir);

    filesize = 256 + 44 + (nfiles * (maxfilelen+2));
    recsize = maxfilelen + 2;

    embDbiHeader(divFile, filesize, nfiles, recsize, dbname, release, date);

    for(i=0; i<nfiles; i++)
    {
        if(seqfiles)
	    embDbiWriteDivisionRecord(divFile, maxfilelen, (short)(i+1),
				      divfiles[i], seqfiles[i]);
	else
	    embDbiWriteDivisionRecord(divFile, maxfilelen, (short)(i+1),
				      divfiles[i], NULL);
    }

    ajFileClose(&divFile);
    ajStrDel(&tmpfname);

    return;
}




/* @func embDbiWriteDivisionRecord ********************************************
**
** Writes a record to the division lookup file
**
** @param [u] file [AjPFile] Index file
** @param [r] maxnamlen [ajuint] Maximum file name length
** @param [r] recnum [short] Record number
** @param [r] datfile [const AjPStr] Data file name
** @param [r] seqfile [const AjPStr] Sequence file name (or NULL if none)
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiWriteDivisionRecord(AjPFile file, ajuint maxnamlen, short recnum,
			       const AjPStr datfile, const AjPStr seqfile)
{
    ajWritebinInt2(file, recnum);

    if(ajStrGetLen(seqfile))
    {
	ajFmtPrintS(&dbiOutRecord, "%S %S", datfile, seqfile);
	ajWritebinStr(file, dbiOutRecord, maxnamlen);
    }
    else
	ajWritebinStr(file, datfile, maxnamlen);

    return;
}




/* @func embDbiWriteEntryRecord ***********************************************
**
** Writes a record to the entryname index file
**
** @param [u] file [AjPFile] hit file
** @param [r] maxidlen [ajuint] Maximum length for an id string
** @param [r] id [const AjPStr] The id string for this entry
** @param [r] rpos [ajuint] Data file offset
** @param [r] spos [ajuint] sequence file offset
** @param [r] filenum [ajushort] file number in division file
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiWriteEntryRecord(AjPFile file, ajuint maxidlen, const AjPStr id,
			    ajuint rpos, ajuint spos, ajushort filenum)
{

    ajWritebinStr(file, id, maxidlen);
    ajWritebinInt4(file, rpos);
    ajWritebinInt4(file, spos);
    ajWritebinInt2(file, filenum);

    return;
}




/* @func embDbiWriteHit *******************************************************
**
** Writes a record to the field hit (.hit) index file
**
** @param [u] file [AjPFile] hit file
** @param [r] idnum [ajuint] Entry number (1 for the first) in the
**                          entryname file
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiWriteHit(AjPFile file, ajuint idnum)
{
    ajWritebinInt4(file, (ajint) idnum);

    return;
}




/* @func embDbiWriteTrg *******************************************************
**
** Writes a record to the field target (.trg) index file
**
** @param [u] file [AjPFile] hit file
** @param [r] maxfieldlen [ajuint] Maximum field token length
** @param [r] idnum [ajuint] First record number (1 for the first) in the
**                          field hit index file
** @param [r] idcnt [ajuint] Number of entries for this field value
**                          in the field hit index file
** @param [r] hitstr [const AjPStr] Field token string
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiWriteTrg(AjPFile file, ajuint maxfieldlen, ajuint idnum,
		    ajuint idcnt, const AjPStr hitstr)
{
    ajWritebinInt4(file, (ajint) idnum);
    ajWritebinInt4(file, (ajint) idcnt);
    ajWritebinStr(file, hitstr, maxfieldlen);

    return;
}




/* @func embDbiSortOpen *******************************************************
**
** Open sort files for entries and all fields
**
** @param [w] alistfile [AjPFile*] Sort files for each field.
** @param [r] ifile [ajuint] Input file number (used for temporary file names)
** @param [r] dbname [const AjPStr] Database name
**                                  (used for temporary file names)
** @param [r] fields [AjPStr const *] Field names (used for temporary
**                                   file names)
** @param [r] nfields [ajuint] Number of fields
** @return [AjPFile] Sort file for entries
**
** @release 2.4.0
******************************************************************************/

AjPFile embDbiSortOpen(AjPFile* alistfile,
		       ajuint ifile, const AjPStr dbname,
		       AjPStr const * fields, ajuint nfields)
{
    AjPFile elistfile;
    ajuint ifield;

    elistfile = embDbiFileSingle(dbname, "list", ifile+1);

    for(ifield=0;ifield < nfields; ifield++)
	alistfile[ifield] = embDbiFileSingle(dbname,
					     dbiFieldFile(fields[ifield]),
					     ifile+1);

    return elistfile;
}




/* @funcstatic dbiFieldFile ***************************************************
**
** Returns the index filename that relates to a USA field name
**
** @param [r] fieldname [const AjPStr] Field name
** @return [const char*] Index filename for this field
**
** @release 4.0.0
******************************************************************************/

static const char* dbiFieldFile(const AjPStr fieldname)
{
    ajuint i = 0;

    for(i=0;fieldDef[i].name;i++)
	if(ajStrMatchCaseC(fieldname, fieldDef[i].name))
	    return fieldDef[i].index;

    ajErr("Unknown query field '%S' in index filename lookup", fieldname);
    return NULL;
}




/* @func embDbiSortClose ******************************************************
**
** Close the sort files for entries and all fields
**
** @param [u] elistfile [AjPFile*] Sort file for entries
** @param [u] alistfile [AjPFile*] Sort files for each field.
** @param [r] nfields [ajuint] Number of fields
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiSortClose(AjPFile* elistfile, AjPFile* alistfile, ajuint nfields)
{
    ajuint ifield;

    ajFileClose(elistfile);

    for(ifield=0; ifield < nfields; ifield++)
	ajFileClose(&alistfile[ifield]);

    return;
}




/* @func embDbiMemEntry *******************************************************
**
** Stores data for current entry in memory by appending to lists
**
** @param [u] idlist [AjPList] List of entry IDs
** @param [u] fieldList [AjPList*] List of field tokens for each field
** @param [r] nfields [ajuint] Number of fields
** @param [u] entry [EmbPEntry] Current entry
** @param [r] ifile [ajuint] Current input file number
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiMemEntry(AjPList idlist, AjPList* fieldList, ajuint nfields,
		    EmbPEntry entry, ajuint ifile)
{
    ajuint ifield;
    ajuint i;
    EmbPField fieldData = NULL;

    entry->filenum = ifile+1;
    ajListPushAppend(idlist, entry);

    for(ifield=0; ifield < nfields; ifield++)
	for(i=0;i<entry->nfield[ifield]; i++)
	{
	    fieldData = embDbiFieldNew();
	    fieldData->entry = entry->entry;
	    fieldData->field = entry->field[ifield][i];
	    ajListPushAppend(fieldList[ifield], fieldData);
	}

    return;
}




/* @func embDbiSortWriteEntry *************************************************
**
** Write the entryname index file using data from the entry sort file.
**
** @param [u] entFile [AjPFile] Entry file
** @param [r] maxidlen [ajuint] Maximum id length
** @param [r] dbname [const AjPStr] Database name (used in temp file names)
** @param [r] nfiles [ajuint] Number of files
** @param [r] cleanup [AjBool] Cleanup temp files if true
** @param [r] sortopt [const AjPStr] Sort commandline options
** @return [ajuint] Number of entries
**
** @release 2.4.0
******************************************************************************/

ajuint embDbiSortWriteEntry(AjPFile entFile, ajuint maxidlen,
                            const AjPStr dbname, ajuint nfiles,
                            AjBool cleanup, const AjPStr sortopt)
{
    AjPFile esortfile;
    ajint rpos;
    ajint spos;
    ajint filenum;
    ajuint idcnt = 0;

    if(!dbiRegEntryIdSort)
	dbiRegEntryIdSort =
	    ajRegCompC("^([^ ]+) +([0-9]+) +([0-9]+) +([0-9]+)");

    embDbiSortFile(dbname, "list", "idsrt", nfiles, cleanup, sortopt);
    ajStrAssignC(&dbiLastId, " ");
    esortfile = embDbiFileIn(dbname, "idsrt");

    while(ajReadline(esortfile, &dbiRdLine))
    {
	ajRegExec(dbiRegEntryIdSort, dbiRdLine);
	ajRegSubI(dbiRegEntryIdSort, 1, &dbiIdStr);
	ajRegSubI(dbiRegEntryIdSort, 2, &dbiTmpStr);
	ajStrToInt(dbiTmpStr, &rpos);
	ajRegSubI(dbiRegEntryIdSort, 3, &dbiTmpStr);
	ajStrToInt(dbiTmpStr, &spos);
	ajRegSubI(dbiRegEntryIdSort, 4, &dbiTmpStr);
	ajStrToInt(dbiTmpStr, &filenum);

	if(ajStrMatchCaseS(dbiIdStr, dbiLastId))
	{
            ajDebug("Duplicate ID '%S' filenum: %d",
                    dbiIdStr, filenum);
            ajWarn("Duplicate ID skipped: '%S' "
                   "All hits will point to first ID found",
                   dbiIdStr);
            continue;
	}

	embDbiWriteEntryRecord(entFile, maxidlen, dbiIdStr,
			       rpos, spos, filenum);
	ajStrAssignS(&dbiLastId, dbiIdStr);
	idcnt++;
    }
    ajFileClose(&esortfile);

    return idcnt;
}




/* @func embDbiMemWriteEntry **************************************************
**
** Write entryname index for in-memory processing
**
** @param [u] entFile [AjPFile] entryname index file
** @param [r] maxidlen [ajuint] Maximum entry id length
** @param [r] idlist [const AjPList] List of entry IDs to be written
** @param [w] ids [void***] AjPStr* array of IDs from list
** @return [ajuint] Number of entries written (excluding duplicates)
**
** @release 2.4.0
******************************************************************************/

ajuint embDbiMemWriteEntry(AjPFile entFile, ajuint maxidlen,
			  const AjPList idlist,
			  void ***ids)
{
    ajuint idCount;
    ajuint i;
    EmbPEntry entry;
    ajuint idcnt = 0;

    idCount = (ajuint) ajListToarray(idlist, ids);
    qsort(*ids, idCount, sizeof(void*), embDbiCmpId);
    ajDebug("ids sorted\n");

    for(i = 0; i < idCount; i++)
    {
	entry = (EmbPEntry)(*ids)[i];

	if(ajStrMatchCaseC(dbiIdStr, entry->entry))
	{
	    ajErr("Duplicate ID found: '%S'", dbiIdStr);
	    continue;
	}

	ajStrAssignC(&dbiIdStr, entry->entry);
	embDbiWriteEntryRecord(entFile, maxidlen, dbiIdStr,
			       entry->rpos, entry->spos, entry->filenum);
	idcnt++;
    }

    return idcnt;
}




/* @func embDbiSortWriteFields ************************************************
**
** Write the indices for a field.
**
** @param [r] dbname [const AjPStr] Database name (used for temp file names)
** @param [r] release [const AjPStr] Release number as a string
** @param [r] date [const char[4]] Date
** @param [r] indexdir [const AjPStr] Index directory
** @param [r] fieldname [const AjPStr] Field name (used for temp file names)
** @param [r] maxFieldLen [ajuint] Maximum field token length
** @param [r] nfiles [ajuint] Number of data files
** @param [r] nentries [ajuint] Number of entries
** @param [r] cleanup [AjBool] Cleanup temp files if true
** @param [r] sortopt [const AjPStr] Sort command line options
** @return [ajuint] Number of unique field targets written
**
** @release 2.4.0
******************************************************************************/

ajuint embDbiSortWriteFields(const AjPStr dbname, const AjPStr release,
			    const char date[4], const AjPStr indexdir,
			    const AjPStr fieldname, ajuint maxFieldLen,
			    ajuint nfiles, ajuint nentries,
			    AjBool cleanup, const AjPStr sortopt)
{
    AjPFile asortfile;
    AjPFile asrt2file;
    AjPFile blistfile;
    AjPFile elistfile;
    ajuint ient;

    ajuint fieldCount=0;
    ajuint idwidth;

    AjPFile trgFile;
    AjPFile hitFile;
    short alen;
    ajuint asize;
    ajuint ahsize;
    ajuint itoken = 0;
    ajuint i;
    ajuint j;
    ajuint k;
    ajint idnum;
    ajint lastidnum;

    ajStrAssignC(&dbiFieldName, dbiFieldFile(fieldname));
    ajFmtPrintS(&dbiTmpStr, "%d", nentries);
    idwidth = ajStrGetLen(dbiTmpStr);

    if(!dbiRegFieldIdSort)
	dbiRegFieldIdSort = ajRegCompC("^([^ ]+) +");

    if(!dbiRegFieldTokSort)
	dbiRegFieldTokSort = ajRegCompC("^([^ ]+) +([^\n\r]+)");

    if(!dbiRegFieldTokIdSort)
	dbiRegFieldTokIdSort = ajRegCompC("^(.*[^ ]) +([0-9]+)[\r\n]+$");

    ajFmtPrintS(&dbiFieldId2, "%S_id2", dbiFieldName);
    ajFmtPrintS(&dbiFieldSort, "%S_sort", dbiFieldName);
    ajFmtPrintS(&dbiFieldSort2, "%S_sort2", dbiFieldName);

    trgFile = embDbiFileIndex(indexdir, dbiFieldName, "trg");
    hitFile = embDbiFileIndex(indexdir, dbiFieldName, "hit");

    embDbiSortFile(dbname, ajStrGetPtr(dbiFieldName),
		   ajStrGetPtr(dbiFieldSort),
		   nfiles, cleanup, sortopt);

    /* put in the entry numbers and remove the names */
    /* read dbname.<field>srt, for each entry, increment the count */

    elistfile = embDbiFileIn(dbname, "idsrt");
    asortfile = embDbiFileIn(dbname, ajStrGetPtr(dbiFieldSort));
    blistfile = embDbiFileOut(dbname, ajStrGetPtr(dbiFieldId2));

    fieldCount = 0;

    ient=0;
    ajStrAssignC(&dbiCurrentId, "");

    while(ajReadline(asortfile, &dbiRdLine))
    {
	ajRegExec(dbiRegFieldTokSort, dbiRdLine);
	ajRegSubI(dbiRegFieldTokSort, 1, &dbiIdStr);
	ajRegSubI(dbiRegFieldTokSort, 2, &dbiFieldStr);

	ajDebug("asortfile curr '%S' id '%S' field '%S'\n",
		dbiCurrentId, dbiIdStr, dbiFieldStr);

	while(!ajStrMatchS(dbiIdStr, dbiCurrentId))
	{
	    ajStrAssignS(&dbiFieldId, dbiCurrentId);

	    if(!ajReadline(elistfile, &dbiIdLine))
		ajFatal("Error in embDbiSortWriteFields, "
			"expected entry %S not found, last was '%S'",
			dbiIdStr, dbiCurrentId);
	    ajRegExec(dbiRegFieldIdSort, dbiIdLine);
	    ajRegSubI(dbiRegFieldIdSort, 1, &dbiCurrentId);

	    ajDebug("curr '%S' line '%S'\n", dbiCurrentId, dbiIdLine);

	    if(!ajStrMatchS(dbiFieldId, dbiCurrentId))
		ient++;
	    ajDebug("asortfile curr '%S' id '%S' ient: %u\n",
		    dbiCurrentId, dbiIdStr, ient);
	}

	ajFmtPrintF(blistfile, "%S %0*d\n", dbiFieldStr, idwidth, ient);
	fieldCount++;
    }

    ajFileClose(&asortfile);
    ajFileClose(&blistfile);
    ajFileClose(&elistfile);

    /* sort again */

    embDbiRmFile(dbname, ajStrGetPtr(dbiFieldSort), 0, cleanup);
    embDbiSortFile(dbname, ajStrGetPtr(dbiFieldId2),
		   ajStrGetPtr(dbiFieldSort2),
		   0, cleanup, sortopt);

    alen = maxFieldLen+8;
    asize = 300 + (fieldCount*(ajuint)alen); /* to be fixed later */
    embDbiHeader(trgFile, asize, fieldCount,
		 alen, dbname, release, date);

    ahsize = 300 + (fieldCount*4);
    embDbiHeader(hitFile, ahsize, fieldCount, 4,
		 dbname, release, date);

    itoken = 0;
    j = 0;
    k = 1;

    i = 0;
    lastidnum = 999999999;
    ajStrAssignC(&dbiFieldId, "");
    asrt2file = embDbiFileIn(dbname, ajStrGetPtr(dbiFieldSort2));

    while(ajReadline(asrt2file, &dbiRdLine))
    {
	ajRegExec(dbiRegFieldTokIdSort, dbiRdLine);
	ajRegSubI(dbiRegFieldTokIdSort, 1, &dbiIdStr);
	ajRegSubI(dbiRegFieldTokIdSort, 2, &dbiTmpStr);
	ajStrToInt(dbiTmpStr, &idnum);

	if(!i)
	    ajStrAssignS(&dbiFieldId, dbiIdStr);

	if(!ajStrMatchS(dbiFieldId, dbiIdStr))
	{
	    embDbiWriteHit(hitFile, idnum);
	    embDbiWriteTrg(trgFile, maxFieldLen,
			   j, k, dbiFieldId);
	    j = 1;			/* number of hits */
	    k = i+1;			/* first hit */
	    ajStrAssignS(&dbiFieldId, dbiIdStr);
	    i++;
	    itoken++;
	    lastidnum=idnum;
	}
	else if(idnum != lastidnum)	/* dbiIdStr is the same */
	{
	    embDbiWriteHit(hitFile, idnum);
	    lastidnum = idnum;
	    j++;
	    i++;
	}
    }

    ajFileClose(&asrt2file);
    embDbiRmFile(dbname, ajStrGetPtr(dbiFieldSort2), 0, cleanup);

    ajDebug("targets i:%d itoken: %d\n", i, itoken);

    if(i)
    {
	/* possibly there were no target tokens */
	embDbiWriteTrg(trgFile, maxFieldLen,
		       j, k, dbiFieldId);
	itoken++;
    }

    ajDebug("wrote %F %d\n", trgFile, itoken);

    embDbiHeaderSize(trgFile, 300+itoken*(ajuint)alen, itoken);

    ajDebug("finished...\n%7d files\n%7d %F\n%7d %F\n",
	    nfiles, itoken, trgFile,
	    fieldCount, hitFile);

    ajFileClose(&trgFile);
    ajFileClose(&hitFile);

    return itoken;
}




/* @func embDbiMemWriteFields *************************************************
**
** Write the fields indices
**
** @param [r] dbname [const AjPStr] Database name (used for temp file names)
** @param [r] release [const AjPStr] Release number as a string
** @param [r] date [const char[4]] Date
** @param [r] indexdir [const AjPStr] Index directory
** @param [r] fieldname [const AjPStr] Field name (used for file names)
** @param [r] maxFieldLen [ajuint] Maximum field token length
** @param [r] fieldList [const AjPList] List of field tokens to be written
** @param [r] ids [void**] AjPStr* array offield token s from list
** @return [ajuint] Number of unique field targets written
**
** @release 2.4.0
******************************************************************************/

ajuint embDbiMemWriteFields(const AjPStr dbname,const  AjPStr release,
			   const char date[4], const AjPStr indexdir,
			   const AjPStr fieldname, ajuint maxFieldLen,
			   const AjPList fieldList, void** ids)
{
    AjPStr field = NULL;

    ajuint fieldCount = 0;
    ajuint ient;
    ajuint fieldent;
    ajuint i;
    ajuint j;
    ajint k;
    void **fieldItems = NULL;
    AjPFile trgFile;
    AjPFile hitFile;
    short alen;
    ajuint asize;
    ajuint ahsize;
    ajuint itoken = 0;
    ajuint idup   = 0;
    EmbPField fieldData    = NULL;
    static const char* lastfd    = "";
    ajuint lastidnum = 0;

    ajStrAssignC(&field, dbiFieldFile(fieldname));
    trgFile = embDbiFileIndex(indexdir, field, "trg");
    hitFile = embDbiFileIndex(indexdir, field, "hit");

    fieldCount = (ajuint) ajListToarray(fieldList, &fieldItems);

    ajDebug("fieldItems: %d %x\n",
	    fieldCount, fieldItems);

    if(fieldCount)
    {
	qsort(fieldItems, fieldCount, sizeof(void*),
	      embDbiCmpFieldId);
	ajDebug("%S sorted by id\n", field);
	ient = 0;
	fieldent = 0;

	while(ids[ient] && fieldItems[fieldent])
	{
	    k = strcmp(((EmbPEntry)ids[ient])->entry,
		       ((EmbPField)fieldItems[fieldent])->entry);
	    if(k < 0)
		ient++;
	    else if(k > 0)
		fieldent++;
	    else
		((EmbPField)fieldItems[fieldent++])->nid = ient+1;
	}
	ajDebug("checked ids: %d fieldItems: %d %d\n",
		ient, fieldent, fieldCount);

	qsort(fieldItems, fieldCount, sizeof(void*),
	      embDbiCmpFieldField);
	ajDebug("%S sorted by %S\n", field, field);
    }

    alen = maxFieldLen+8;
    asize = 300 + (fieldCount*(ajuint)alen); /* to be fixed later */
    embDbiHeader(trgFile, asize, fieldCount,
		 alen, dbname, release, date);

    ahsize = 300 + (fieldCount*4);
    embDbiHeader(hitFile, ahsize, fieldCount, 4,
		 dbname, release, date);

    itoken = 0;
    j      = 0;
    k      = 1;
    idup   = 0;

    for(i = 0; i < fieldCount; i++)
    {
	fieldData = (EmbPField)fieldItems[i];

	if(!i)
	{
	    lastfd = fieldData->field;
	    lastidnum = 999999999;
	}

	if(strcmp(lastfd, fieldData->field))
	{
	    embDbiWriteHit(hitFile, fieldData->nid);
	    ajStrAssignC(&dbiFieldStr, lastfd);
	    embDbiWriteTrg(trgFile, maxFieldLen,
			   j, k,dbiFieldStr);
	    j = 1;
	    k = i+1-idup;
	    itoken++;
	    lastfd = fieldData->field;
	    lastidnum=fieldData->nid;
	}
	else if(fieldData->nid != lastidnum) /* lastfd is the same */
	{
	    embDbiWriteHit(hitFile, fieldData->nid);
	    lastidnum = fieldData->nid;
	    j++;
	}
	else
	    idup++;
    }

    ajStrAssignC(&dbiFieldStr, lastfd);

    if(fieldCount)
    {
	embDbiWriteTrg(trgFile, maxFieldLen, j, k, dbiFieldStr);
	itoken++;
    }

    ajDebug("wrote %F %d\n", trgFile, itoken);

    embDbiHeaderSize(trgFile, 300+itoken*(ajuint)alen, itoken);

    ajDebug("finished...\n%7d %F\n%7d %F\n",
	    itoken, trgFile,
	    fieldCount, hitFile);

    ajFileClose(&trgFile);
    ajFileClose(&hitFile);

    ajStrDel(&field);
    AJFREE(fieldItems);

    return itoken;
}




/* @func embDbiDateSet ********************************************************
**
** Sets the date as an integer array from a formatted string.
** The integer array is the internal format in database index headers
**
** @param [r] datestr [const AjPStr] Date as a string
** @param [w] date [char[4]] Data char (1 byte int) array
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiDateSet(const AjPStr datestr, char date[4])
{
    ajuint i;
    ajint j;

    if(!dbiRegDate)
	dbiRegDate = ajRegCompC("^([0-9]+).([0-9]+).([0-9]+)");

    date[3] = 0;

    if(ajRegExec(dbiRegDate, datestr))
	for(i=1; i<4; i++)
	{
	    ajRegSubI(dbiRegDate, i, &dbiTmpStr);
	    ajStrToInt(dbiTmpStr, &j);
	    date[3-i] = j;
	}

    return;
}




/* @func embDbiMaxlen *********************************************************
**
** Compares a string to a maximum string length.
**
** A negative maximum length limits the string to that absolute length.
**
** A non-negative length is updated if the string is longer
**
** @param [u] token [AjPStr*] Token string
** @param [u] maxlen [ajint*] Maximum string length
** @return [void]
**
** @release 2.4.0
******************************************************************************/

void embDbiMaxlen(AjPStr* token, ajint* maxlen)
{
    if(*maxlen < 0)
	ajStrKeepRange(token, 1, -(*maxlen));
    else
    {
	if((ajint)ajStrGetLen(*token) > *maxlen)
	    *maxlen = ajStrGetLen(*token);
    }

    return;
}




/* @func embDbiLogHeader ******************************************************
**
** Writes the header to a database indexing logfile
**
** @param [u] logfile [AjPFile] Log file
** @param [r] dbname [const AjPStr] Database name
** @param [r] release [const AjPStr] Release number, name or code
** @param [r] datestr [const AjPStr] Indexing date as a string dd/mm/yy
** @param [r] indexdir [const AjPStr] Index directory relative path
** @param [r] maxindex [ajuint] Maximum index token length (usually zero)
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogHeader(AjPFile logfile, const AjPStr dbname,
		     const AjPStr release, const AjPStr datestr,
		     const AjPStr indexdir,
		     ajuint maxindex)
{
    AjPStr dirname = NULL;
    AjPTime today = NULL;

    today =  ajTimeNewTodayFmt("report");
    ajFmtPrintF(logfile, "########################################\n");
    ajFmtPrintF(logfile, "# Program: %S\n", ajUtilGetProgram());
    ajFmtPrintF(logfile, "# Rundate: %D\n", today);
    ajFmtPrintF(logfile, "# Dbname: %S\n", dbname);
    ajFmtPrintF(logfile, "# Release: %S\n", release);
    ajFmtPrintF(logfile, "# Date: %S\n", datestr);
    ajFmtPrintF(logfile, "# CurrentDirectory: %S\n", ajFileValueCwd());
    ajFmtPrintF(logfile, "# IndexDirectory: %S\n", indexdir);
    ajStrAssignS(&dirname, indexdir);
    ajDirnameFillPath(&dirname);
    ajFmtPrintF(logfile, "# IndexDirectoryPath: %S\n", dirname);
    ajFmtPrintF(logfile, "# Maxindex: %d\n", maxindex);

    ajTimeDel(&today);
    ajStrDel(&dirname);

    return;
}




/* @func embDbiLogFields ******************************************************
**
** Writes database indexing logfile report of fields selected for indexing
**
** @param [u] logfile [AjPFile] Log file
** @param [r] fields [AjPStr const *] Field names
** @param [r] nfields [ajuint] Number of fields
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogFields(AjPFile logfile, AjPStr const * fields, ajuint nfields)
{
    ajuint i;

    ajFmtPrintF(logfile, "# Fields: %d\n", nfields+1);
    ajFmtPrintF(logfile, "#   Field 1: id\n");

    for(i=0;i<nfields;i++)
	ajFmtPrintF(logfile, "#   Field %d: %S\n", i+2, fields[i]);

    return;
}




/* @func embDbiLogSource ******************************************************
**
** Writes database indexing logfile report of source data selected for indexing
**
** @param [u] logfile [AjPFile] Log file
** @param [r] directory [const AjPStr] Data directory relative path
** @param [r] filename [const AjPStr] Selected filenames wildcard
** @param [r] exclude [const AjPStr] Excluded filenames wildcard
** @param [r] inputFiles [AjPStr const *] File names
** @param [r] nfiles [ajuint] Number of files
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogSource(AjPFile logfile, const AjPStr directory,
		     const AjPStr filename, const AjPStr exclude,
		     AjPStr const * inputFiles, ajuint nfiles)
{
    AjPStr dirname = NULL;
    ajuint i;

    ajFmtPrintF(logfile, "# Directory: %S\n", directory);
    ajStrAssignS(&dirname, directory);
    ajDirnameFillPath(&dirname);
    ajFmtPrintF(logfile, "# DirectoryPath: %S\n", dirname);
    ajFmtPrintF(logfile, "# Filenames: %S\n", filename);
    ajFmtPrintF(logfile, "# Exclude: %S\n", exclude);
    ajFmtPrintF(logfile, "# Files: %d\n", nfiles);

    for(i=0;i<nfiles;i++)
	ajFmtPrintF(logfile, "#   File %d: %S\n", i+1, inputFiles[i]);

    ajStrDel(&dirname);

    return;
}




/* @func embDbiLogCmdline *****************************************************
**
** Writes database indexing logfile report of commandline used
**
** @param [u] logfile [AjPFile] Log file
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogCmdline(AjPFile logfile)
{
    AjPStr cmdline = NULL;

    ajFmtPrintF(logfile, "########################################\n");
    ajFmtPrintF(logfile, "# Commandline: %S\n", ajUtilGetProgram());
    ajStrAssignS(&cmdline, ajUtilGetCmdline());

    if(ajStrGetLen(cmdline))
    {
	ajStrExchangeCC(&cmdline, "\n", "\1#    ");
	ajStrExchangeCC(&cmdline, "\1", "\n");
	ajFmtPrintF(logfile, "#    %S\n", cmdline);
    }

    ajStrAssignS(&cmdline, ajUtilGetInputs());

    if(ajStrGetLen(cmdline))
    {
	ajStrExchangeCC(&cmdline, "\n", "\1#    ");
	ajStrExchangeCC(&cmdline, "\1", "\n");
	ajFmtPrintF(logfile, "#    %S\n", cmdline);
    }

    ajFmtPrintF(logfile, "########################################\n\n");
    ajStrDel(&cmdline);

    return;
}




/* @func embDbiLogFile ********************************************************
**
** Writes database indexing logfile report of a single source file
**
** @param [u] logfile [AjPFile] Log file
** @param [r] curfilename [const AjPStr] Source filename
** @param [r] idCountFile [ajuint] Number of IDs in file
** @param [r] fields [AjPStr const *] Field names
** @param [r] countField [const ajuint*] Number of field tokens in this file
** @param [r] nfields [ajuint] Number of fields
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogFile(AjPFile logfile, const AjPStr curfilename,
		   ajuint idCountFile, AjPStr const * fields,
		   const ajuint* countField,
		   ajuint nfields)
{
    ajuint i;

    ajFmtPrintF(logfile, "filename: '%S'\n", curfilename);
    ajFmtPrintF(logfile, "    id: %d\n", idCountFile);

    for(i=0;i<nfields;i++)
	ajFmtPrintF(logfile, "   %3S: %d\n", fields[i], countField[i]);

    return;
}




/* @func embDbiLogFinal *******************************************************
**
** Writes database indexing logfile report of final totals
**
** @param [u] logfile [AjPFile] Log file
** @param [r] maxindex [ajuint] User defined maximum index token length
**                             (usually zero)
** @param [r] maxFieldLen [const ajint*] Maximum index token length 
**                                       for each field. Negative values
**                                       were upper limits. Positive values
**                                       are the maximum in the data
** @param [r] fields [AjPStr const *] Field names
** @param [r] fieldTot [const ajuint*] Number of unique field tokens
** @param [r] nfields [ajuint] Number of fields
** @param [r] nfiles [ajuint] Number of input files
** @param [r] idDone [ajuint] Number of unique IDs indexed
** @param [r] idCount [ajuint] Total number of IDs indexed
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiLogFinal(AjPFile logfile, ajuint maxindex,
		    const ajint* maxFieldLen,
		    AjPStr const * fields, const ajuint* fieldTot,
		    ajuint nfields, ajuint nfiles, ajuint idDone,
		    ajuint idCount)
{
    ajuint i;
    ajuint maxlen;

    ajFmtPrintF(logfile, "\n");

    for(i=0;i<nfields;i++)
    {
        if(maxindex)
	    maxlen = maxindex;
	else
	    maxlen = maxFieldLen[i];

	ajFmtPrintF(logfile, "Index %S: maxlen %d items %d\n",
		    fields[i], maxlen, fieldTot[i]);
    }

    ajFmtPrintF(logfile, "\nTotal %d files %d entries (%d duplicates)\n",
		nfiles, idCount, (idCount-idDone));
    return;
}




/* @func embDbiExit ***********************************************************
**
** Cleanup database indexing internals on exit
**
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embDbiExit(void)
{
    ajStrDel(&dbiCmdStr);
    ajStrDel(&dbiCmdStr2);
    ajStrDel(&dbiDirFix);
    ajStrDel(&dbiWildFname);
    ajStrDel(&dbiInFname);
    ajStrDel(&dbiOutFname);
    ajStrDel(&dbiOutRecord);
    ajStrDel(&dbiSortExt);
    ajStrDel(&dbiLastId);
    ajStrDel(&dbiFieldId);
    ajStrDel(&dbiIdStr);
    ajStrDel(&dbiTmpStr);
    ajStrDel(&dbiRdLine);
    ajStrDel(&dbiIdLine);
    ajStrDel(&dbiFieldSort);
    ajStrDel(&dbiFieldSort2);
    ajStrDel(&dbiFieldStr);
    ajStrDel(&dbiFieldName);
    ajStrDel(&dbiFieldId2);
    ajStrDel(&dbiCurrentId);

    ajRegFree(&dbiRegFieldIdSort);
    ajRegFree(&dbiRegFieldTokSort);
    ajRegFree(&dbiRegFieldTokIdSort);
    ajRegFree(&dbiRegEntryIdSort);
    ajRegFree(&dbiRegDate);

    return;
}