blingfire-sys 1.0.1

Bindings to the BlingFire C++ library
Documentation
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
/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */


#include "FAConfig.h"
#include "FAUtils.h"
#include "FASecurity.h"


class SortBytes {

protected:
    static int fReverse;
    static int fNumeric;
    static int fUnique;

private:
    typedef struct MERGEDATA
    {
        int iFile;              // number of temp file this string came from
        unsigned char *pwsz;          // pointer to the string
        FILE *pFile;            // handle to the open file
    } MERGEDATA;

    typedef struct SORTDATA
    {
        char pszTempDir[PATH_MAX];      // temporary directory for intermediate files
        char * pszTempFileName; // Space to hold a temporary file name
        unsigned char *pwszCoreLoad;  // buffer to hold the data to be sorted
        int cwchCoreLoadMax;    // absolute size of coreload buffer
        int cwchCoreLoadMac;    // currently used amount of coreload buffer
        int cwchCoreLoadLim;    // number of bytes of whole strings in coreload
        unsigned char **ppwszCorePointers; // buffer of pointers to beginning of strings
        int cCorePointersMax;   // size of pointers array
        int cCorePointersMac;   // number of pointers currently in use

        int cwchStringMac;      // length of longest string

        MERGEDATA rgMergeData[FOPEN_MAX];   // array of strings being merged.
        int cOpenFilesMax;      // Max number of input files we can have open at one time
        int cFilesMac;          // Total number of output files so far
        int cFilesMic;          // Lowest valid temporary file (we delete old ones)
    } SORTDATA;


public:
    static void Sort(FILE* pInput, int cMegsCoreLoad, const char* pszOutputFile);
    static void SetFReverse(int fR);
    static int GetFReverse();
    static void SetFNumeric(int fN);
    static int GetFNumeric();
    static void SetFUnique(int fU);
    static int GetFUnique();

protected:
    static char* MakeSortName(int iFile, SORTDATA *pSortData);

    static int KeyCmp(const unsigned char *pwsz1, const unsigned char *pwsz2) {
        int cmpValue;
        int strlen1;
        int strlen2;
        int minlen;

        if (fNumeric) {
            // note that if fNumeric, results are undefined unless all characters are the same signed or not
            cmpValue = strtol((const char*)pwsz1,NULL,10) - strtol((const char*)pwsz2,NULL,10);
        } else {
            // at this point the strings should be guaranteed null-terminated - compare their beginnings
            strlen1 = (int) strlen((const char*)pwsz1);
            strlen2 = (int) strlen((const char*)pwsz2);
            if (strlen2 < strlen1)
            {
                minlen = strlen2;
            }
            else {
                minlen = strlen1;
            }
            cmpValue = memcmp(pwsz1,pwsz2,minlen);

            if (0 == cmpValue) {
                if (strlen2 < strlen1)
                {
                    // strlen2 is a prefix of strlen1
                    cmpValue = 1;
                }
                else if (strlen1 < strlen2)
                {
                    // strlen1 is a prefix of strlen2
                    cmpValue = -1;
                }
                else
                {
                    // do nothing, these strings really are the same
                }
            }
        }

        if (fReverse) {
            cmpValue = -cmpValue;
        }

        return cmpValue;
    }

    static int __cdecl MergeCmp(const MERGEDATA *pMergeData1, const MERGEDATA *pMergeData2)
    {
        return KeyCmp(pMergeData1->pwsz, pMergeData2->pwsz);
    }


    static int __cdecl SortCmp(const unsigned char **ppwsz1, const unsigned char **ppwsz2)
    {
        return KeyCmp(*ppwsz1, *ppwsz2);
    }

    static void SortDump(const char *pszError, SORTDATA *pSortData);
    static int SortCoreLoad(FILE *pInput, SORTDATA *pSortData);
    static int SortPhase(FILE *pInput, SORTDATA *pSortData);
    static int MergeNextSet(const char *pszOutputFileName, SORTDATA *pSortData);
    static int MergePhase(const char *pOutputFileName, SORTDATA *pSortData);
    static void CountMaxOpenFiles(SORTDATA *pSortData);
    static unsigned char* GetLineInUChar(
        __out_ecount(cchMaxLen) unsigned char* pStr0,
        int cchMaxLen,
        FILE *pFile);
    static void PutLineInUChar(const unsigned char* pString, FILE *pFile);

};

int SortBytes::fReverse = 0;
int SortBytes::fNumeric = 0;
int SortBytes::fUnique = 0;

void SortBytes::SetFReverse(int fR)
{
    fReverse = fR;
}
int SortBytes::GetFReverse()
{
    return fReverse;
}
void SortBytes::SetFNumeric(int fN)
{
    fNumeric = fN;
}
int SortBytes::GetFNumeric()
{
    return fNumeric;
}
void SortBytes::SetFUnique(int fU)
{
    fUnique = fU;
}
int SortBytes::GetFUnique()
{
    return fUnique;
}

/* Convert an integer (a file number) into a temp file name, using the sort directory and the process
id to make a unique name.  \directory\sortPID.INDEX The space is allocated in the sort data structure
already, and this routine simply puts the name there.  It also returns a pointer to it, as a convenience.
This means you'd better not call this again until you're through with the last name! */

char* SortBytes::MakeSortName(int iFile, SORTDATA *pSortData)
{
#if _MSC_VER < 1400
    sprintf (pSortData->pszTempFileName, "%s\\sort%04X.%03X", pSortData->pszTempDir, _getpid(), iFile);
#else
    const int TmpBuffSize = (int) strlen (pSortData->pszTempDir) + 14; // includes space for the terminating zero
    sprintf_s (pSortData->pszTempFileName, TmpBuffSize, "%s\\sort%04X.%03X", pSortData->pszTempDir, _getpid(), iFile);
#endif
    return pSortData->pszTempFileName;
}

void SortBytes::SortDump(const char *pszError, SORTDATA *pSortData)
{
    int i;
    fprintf(stderr,"%s: cCorePointersMac/Max = %d/%d\n",pszError, pSortData->cCorePointersMac, pSortData->cCorePointersMax);

    for (i = 0; i < pSortData->cCorePointersMac; ++i) {
        fprintf(stderr,"\t%3d: '%s'\n",i,pSortData->ppwszCorePointers[i]);
    }
}


// Read a coreload, sort it, and write it to the output

int SortBytes::SortCoreLoad(FILE *pInput, SORTDATA *pSortData)
{
    FILE *pOutputFile;
    int i, len;

    // if there's data left over at end of buffer, copy it down

    if (pSortData->cwchCoreLoadMac > pSortData->cwchCoreLoadLim) {
        pSortData->cwchCoreLoadMac -= pSortData->cwchCoreLoadLim;
        memmove(pSortData->pwszCoreLoad,
            pSortData->pwszCoreLoad+pSortData->cwchCoreLoadLim,
            pSortData->cwchCoreLoadMac*sizeof(unsigned char));
        pSortData->cwchCoreLoadLim = 0;
    }
    pSortData->cwchCoreLoadMac -= pSortData->cwchCoreLoadLim;
    pSortData->cwchCoreLoadLim = 0;
    pSortData->ppwszCorePointers[0] = pSortData->pwszCoreLoad;
    pSortData->cCorePointersMac = 0;

    // now read strings until buffer full or EOF
    // actually, buffer needs 3 empty slots: char, nl, and null

    while (pSortData->cwchCoreLoadMax - pSortData->cwchCoreLoadMac > 2 &&
        GetLineInUChar(pSortData->pwszCoreLoad + pSortData->cwchCoreLoadMac,
            pSortData->cwchCoreLoadMax - pSortData->cwchCoreLoadMac,
            pInput)) {

        /* If there are no available pointer slots, double the allocation */

        if (pSortData->cCorePointersMac == pSortData->cCorePointersMax) {

            /* double the allocation */

            pSortData->cCorePointersMax *= 2;

            void * const ptr = realloc (pSortData->ppwszCorePointers, \
                pSortData->cCorePointersMax*sizeof(unsigned char *));

            if (NULL == ptr) {
                fprintf(stderr,"fa_sortbytes: out of memory\n");
                exit(-1);
            }

            pSortData->ppwszCorePointers = (unsigned char**) ptr;
        }

        /* Record the pointer to the beginning of the string we just read, unless
        this is the first string (possibly a continuation from the last coreload) */

        if (pSortData->cCorePointersMac) {
            pSortData->ppwszCorePointers[pSortData->cCorePointersMac] =
                pSortData->pwszCoreLoad + pSortData->cwchCoreLoadMac;
        }
        ++pSortData->cCorePointersMac;

        /* see how many characters we got, including the terminal \n, if any */

        while (pSortData->cwchCoreLoadMax > pSortData->cwchCoreLoadMac &&
            pSortData->pwszCoreLoad[pSortData->cwchCoreLoadMac]) {
            ++pSortData->cwchCoreLoadMac;
        }

        /* If we only got part of a string, bail out now */

        if (pSortData->pwszCoreLoad[pSortData->cwchCoreLoadMac-1] != L'\n') {
            --pSortData->cCorePointersMac;
            break;
        };

        pSortData->pwszCoreLoad[pSortData->cwchCoreLoadMac-1] = L'\x00';

        /* Okay, so we read an entire string. */

        pSortData->cwchCoreLoadLim = pSortData->cwchCoreLoadMac;

    }

    //  SortDump("Outside of the loop",pSortData);

    /* If we hit EOF with no data, return an indication that we are done */

    if (!pSortData->cwchCoreLoadLim) {
        return false;
    }

    /* At this point, cwchCoreLoadMac is the number of bytes read into the buffer, possibly the
    same as cwchCoreLoadMax, and cwchCoreLoadLim is the number of bytes of complete strings
    in the buffer. cCorePointersMac is the number of complete strings. */

    // Sort the pointers to the strings. A more advanced sort might do more here

    qsort(pSortData->ppwszCorePointers, pSortData->cCorePointersMac, sizeof(unsigned char *), reinterpret_cast<int(__cdecl *)(const void *, const void *)>(SortBytes::SortCmp));

    // SortDump("After the sort",pSortData);
    // Open an output file

    int res = fopen_s (&pOutputFile, MakeSortName(pSortData->cFilesMac,pSortData),"wb");
    if (NULL == pOutputFile || 0 != res) {
        fprintf(stderr,"Error opening coreload file %d\n",pSortData->cFilesMac);
        exit(-1);
    }

    pSortData->cFilesMac++;

    // Write the strings in order, tracking the max length

    for (i = 0; i < pSortData->cCorePointersMac; ++i) {
        PutLineInUChar(pSortData->ppwszCorePointers[i],pOutputFile);
        fwrite("\n", 1, sizeof(char), pOutputFile);
        len = (int) strlen((char*)(pSortData->ppwszCorePointers[i]));
        if (len > pSortData->cwchStringMac) {
            pSortData->cwchStringMac = len;
        }
    }

    /* Close our file and clean up */

    fclose(pOutputFile);

    return true;
}

int SortBytes::SortPhase(FILE *pInput, SORTDATA *pSortData)
{

    /* Allocate space for coreLoad */

    pSortData->pwszCoreLoad = (unsigned char*)malloc(pSortData->cwchCoreLoadMax*sizeof(unsigned char));
    pSortData->ppwszCorePointers = (unsigned char**)malloc(sizeof(unsigned char *));
    pSortData->cCorePointersMax = 1;
    if (!pSortData->pwszCoreLoad || !pSortData->ppwszCorePointers) {
        fprintf(stderr,"Out of memory in sort initialization\n");
        exit(-1);
    }

    while (SortCoreLoad(pInput,pSortData))
        ;

    free(pSortData->pwszCoreLoad);
    free(pSortData->ppwszCorePointers);

    return (true);

} // SortPhase

/* merge as many files as possible into a new one.  If ALL files are merged, write to
the output file instead of to a temp. The logic here is that every temporary file is sorted,
so any set of them can be merged into a new temporary file.  Since it doesn't matter much
which order we do them in, we start from the beginning.  */

int SortBytes::MergeNextSet(const char *pszOutputFileName, SORTDATA *pSortData)
{
    int i, cFiles, lastPass, cFiles0, CmpEntries, fSkip;
    MERGEDATA *pMergeData, mergeDataTmp;
    unsigned char *pwszCoreLoad;
    FILE *pOutputFile;

    // Open pSortData->cOpenFilesMax files for reading.  If there aren't that many, note that this is the last pass
    cFiles = cFiles0 = (pSortData->cOpenFilesMax <= pSortData->cFilesMac - pSortData->cFilesMic) ? \
        pSortData->cOpenFilesMax : \
        pSortData->cFilesMac - pSortData->cFilesMic ;

    lastPass = false;
    if (cFiles + pSortData->cFilesMic == pSortData->cFilesMac) {
        lastPass = true;
    }

    /* Now set up the mergeData structure.  Each string points to a space in the coreload area,
    the temporary file number is set, the temp file is opened, and the first string is read.
    Note that there must be at least one string in each file; there are no empty coreloads */

    pMergeData = pSortData->rgMergeData;
    pwszCoreLoad = pSortData->pwszCoreLoad;
    for (i = 0; i < cFiles; ++i) {
        pMergeData->iFile = pSortData->cFilesMic + i;
        pMergeData->pwsz = pwszCoreLoad;
        int res = fopen_s (&(pMergeData->pFile), MakeSortName(pMergeData->iFile,pSortData), "rb");
        if (NULL == pMergeData->pFile || 0 != res) {
            perror("Cannot open a temporary file.");
            exit(-1);
        }
        GetLineInUChar(pMergeData->pwsz,pSortData->cwchStringMac+1,pMergeData->pFile);
        ++pMergeData;
        pwszCoreLoad += pSortData->cwchStringMac+1;
    }

    // Open the output file.  Just another temp file, unless this is the last merge

    int res = 0;

    if (lastPass) {

        if (pszOutputFileName) {
            res = fopen_s (&pOutputFile, pszOutputFileName, "wb");
        } else {
            pOutputFile = stdout;
        }

    } else {
        res = fopen_s (&pOutputFile, MakeSortName(pSortData->cFilesMac,pSortData), "wb");
    }

    if (NULL == pOutputFile || 0 != res) {
        perror("Can't open merge output file");
        exit(-1);
    }

    // Sort the Merge list
    qsort(pSortData->rgMergeData,cFiles,sizeof(MERGEDATA),reinterpret_cast<int(__cdecl *)(const void *, const void *)>(SortBytes::MergeCmp));

    // While there are still input files,
    pMergeData = pSortData->rgMergeData;
    fSkip = false;
    while (cFiles) {

        // Write out the top string.

        if (!fSkip)
        {
            PutLineInUChar(pMergeData->pwsz,pOutputFile);
        }
        fSkip = false;

        // Read a replacement from the file it came from

        if (GetLineInUChar(pMergeData->pwsz,pSortData->cwchStringMac+1,pMergeData->pFile)) {

            // see where the new string fits alphabetically

            for (i = 1; i < cFiles; ++i)
            {
                CmpEntries = MergeCmp(pMergeData,pMergeData+i);
                if (0 > CmpEntries) break;
                else if (0 == CmpEntries)
                {
                    fSkip = fUnique;
                    break;
                }
            }

            // rearrange the array, if need be

            if (i > 1) {
                mergeDataTmp = *pMergeData;
                memcpy(pMergeData,pMergeData+1,(i-1)*sizeof(MERGEDATA));
                pMergeData[i-1] = mergeDataTmp;

            }
        } else {
            // We finished with the file.  Decrement the count, close and delete the file,
            // and shift the array up.

            --cFiles;
            res = fclose(pMergeData->pFile);
            res |= _unlink(MakeSortName(pMergeData->iFile,pSortData));
            DebugLogAssert (0 == res);
            memcpy(pMergeData,pMergeData+1,cFiles*sizeof(MERGEDATA));
        }
    }

    if (pOutputFile != stdout) {
        fclose(pOutputFile);
    }

    if (lastPass) {
        return (false);
    }

    // Adjust all the counts
    pSortData->cFilesMic += cFiles0;
    ++pSortData->cFilesMac;

    return (true);
} // MergeNextSet




int SortBytes::MergePhase(const char *pOutputFileName, SORTDATA *pSortData)
{
    // Set up memory allocations

    pSortData->pwszCoreLoad = (unsigned char*)malloc((pSortData->cwchStringMac+1)*(pSortData->cOpenFilesMax)*sizeof(unsigned char));
    if (!pSortData->pwszCoreLoad) {
        fprintf(stderr,"Out of memory in merge initialization\n");
        exit(-1);
    }

    // Merge until no files remain

    while (MergeNextSet(pOutputFileName,pSortData))
        ;

    free(pSortData->pwszCoreLoad);

    return (true);
} // MergePhase

void SortBytes::CountMaxOpenFiles(SORTDATA *pSortData)
{
    int i;
    FILE *rgFile[FOPEN_MAX];

    for (i = 0; i < FOPEN_MAX; ++i) {
        int res = fopen_s (&(rgFile[i]), MakeSortName(i,pSortData), "w");
        if (NULL == rgFile[i] || 0 != res) {
            break;
        }
    }

    // Now i is the total number of files we can open, but we must allow for an output file

    if (i < 3) {
        fprintf(stderr,"Can't open at least 3 files!\n");
        exit(-1);
    }

    pSortData->cOpenFilesMax = i-1;

    /* Now clean up after ourselves */

    while (i--) {
        int res = fclose(rgFile[i]);
        res |= _unlink(MakeSortName(i,pSortData));
        DebugLogAssert (0 == res);
    }
} // CountMaxOpenFiles

unsigned char* SortBytes::GetLineInUChar(
    __out_ecount(cchMaxLen) unsigned char* pStr0,
    int cchMaxLen,
    FILE *pFile)
{
    char ch;
    char* pStr;

    if (cchMaxLen <= 0)
        return NULL;

    pStr = (char*)pStr0;
    cchMaxLen--; // reserve room for 0
    while (cchMaxLen > 0)
    {
        ch = (char)fgetc(pFile);
        if (feof(pFile))
        {
            if (pStr == (char*)pStr0)
                return NULL;
            break;
        }

        switch (ch)
        {
        case '\r':
            break;

        default:
            *pStr++ = ch;
            cchMaxLen--;
            break;
        }

        if (ch == '\n')
            break;
    }

    *pStr = '\0';
    return(pStr0);
}

void SortBytes::PutLineInUChar (const unsigned char* pString, FILE *pFile)
{
    size_t len = strlen ((char*) pString);

    bool fNeedNewLine = false;

    if (len && pString [len - 1] == '\n') {
        --len;
        fNeedNewLine = true;
    }
    if (len && pString [len - 1] == '\r') {
        --len;
        fNeedNewLine = true;
    }

    fwrite (pString, len, sizeof (char), pFile);
    if (fNeedNewLine) {
        fwrite ("\n", 1, sizeof (char), pFile);
    }
}


int
__cdecl
main(int argc, char** argv)
{
    FILE *pInput;
    int i;
    int fError = 0;
    const char *pszOutputFile;
    int cMegsCoreLoad = 1;   // Multiply by 2 to get byte size of WCHAR working buffer
    int fNumeric = 0;
    int fReverse = 0;
    int fUnique = 0;

    ::FAIOSetup ();

    /* Loop through the argument list looking for flags */

    while (argc > 1) {
        if (*argv[1] != '-' && *argv[1] != '/') {
            break;
        }
        for (i = 1; argv[1][i] != '\0'; ++i) {
            switch (argv[1][i]) {
            case 'n': fNumeric = 1;
                break;
            case 'r': fReverse = 1;
                break;
            case 'u': fUnique = 1;
                break;
            case 'm': cMegsCoreLoad = 250;
                break;
            default:
                fError = 1;
            }
        }
        --argc;
        ++argv;
    }

    /* We allow no more than two non-flag arguments */

    if (argc > 3) {
        fError = 1;
    }

    /* Abend here if we found any errors */

    if (fError) {
        fprintf(stderr,"Usage: fa_sortbytes <-nrm> <input-file <output-file>>\n");
        fprintf(stderr,"       -n sort in numeric order\n");
        fprintf(stderr,"       -r sort in reverse order\n");
        fprintf(stderr,"       -u remove duplicate entries - each resulting entry will be unique\n");
        fprintf(stderr,"          (not implemented yet)\n");
        fprintf(stderr,"       -m use ~512 MB of RAM during sort phase (default ~2)\n");
        exit(-1);
    }

    pInput = stdin;
    int res = 0;

    if (argc > 1) {
        res = fopen_s (&pInput, argv[1], "rb");
    }

    if (NULL == pInput || 0 != res) {
          perror("Cannot open the input file.");
          exit(-1);
    }

    pszOutputFile = argc > 2 ? argv[2] : NULL;

    /* Suppress conversion to text mode */
    ::FAInputIOSetup ();

    SortBytes::SetFReverse(fReverse);
    SortBytes::SetFNumeric(fNumeric);
    SortBytes::SetFNumeric(fUnique);
    SortBytes::Sort(pInput,cMegsCoreLoad,pszOutputFile);

    exit (0);
}

/* pszOutputFile should be either the name of a file or NULL for stdout */

void SortBytes::Sort(FILE* pInput, int cMegsCoreLoad, const char* pszOutputFile)
{
    SORTDATA sortData;
    int cchFileNames;

    /* Algorithm: read file into memory, one coreload at a time.  Sort each coreload with qsort,
    then write each into a separate file, using names like sort0097.000, sort0097.001, etc.  Then merge
    the files.  Create the next sequential name for the output, then read the first element from each input
    file.  qsort the list, and write the best one out to the output.  Replace it from the file it
    came from (unless that file is at EOT).  And resort.  Repeat until all files are at EOT.  Then
    delete them.  Then repeat the merge operation on the next set of files.  When there is only
    one file left, that is the desired output file. */

    memset(&sortData,0,sizeof(sortData));
    sortData.cwchCoreLoadMax = cMegsCoreLoad * 1024*1024;

    // and use the current directory for now
    sortData.pszTempDir [0] = '.';
    sortData.pszTempDir [1] = 0;

    // dir plus backslash plus 8 letter name plus dot plus 3 letter ext plus null
    cchFileNames = (int) strlen(sortData.pszTempDir) + 14;
    sortData.pszTempFileName = (char*)malloc(cchFileNames*sizeof(char));

    CountMaxOpenFiles(&sortData);   // find out how many files can be open at once

    SortPhase(pInput, &sortData);
    if (pInput != stdin) {
        fclose(pInput);
    }
    MergePhase(pszOutputFile,&sortData);
}