#ident "$Id: slice.c,v 1.4 2006/07/13 19:46:48 craig Exp $"
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * This program produces a new file by taking a slice from an existing
 * file.
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#else
#define fseeko(file, where, whence)     fseeko64(file, where, whence)
#define ftello(file)                    ftello64(file)
#endif

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          // for off_t definition
#include <unistd.h>
#include <getopt.h>

/* ------------------------------------------------------------- */
void
usage(const char* const progName)
{
    printf ("Usage: %s -i input -o output [-s starting offset] [-e end offset]\n",
             progName);
}
/* ------------------------------------------------------------- */

static const char *optString = "e:hi:o:s:";

int
main(int argc, char *argv[])
{
    char        *inFileName = NULL, *outFileName = NULL;
    FILE        *inFile, *outFile;
    off_t       startOff = 0, endOff = 0, curPosition;
    int         optChar;
    uint8_t     buffer[16 * 1024];
    size_t      numToRead, numRead, numWritten;
    bool        done;

    while ((optChar = getopt(argc, argv, optString)) != -1) {
        switch (optChar) {

        case 'e':
            endOff = strtoll(optarg, NULL, 0);
            break;

        case 'h':
            usage(argv[0]);
            exit(0);
            break;

        case 'i':
            inFileName = optarg;
            break;

        case 'o':
            outFileName = optarg;
            break;

        case 's':
            startOff = strtoll(optarg, NULL, 0); 
            break;

        default:
            usage(argv[0]);
            exit(1);
            break;
        }
    }

    if (inFileName == NULL) {
        fprintf (stderr, "(%s) Error - missing input file name\n", argv[0]);
        usage(argv[0]);
        exit(1);
    }

    if (outFileName == NULL) {
        fprintf (stderr, "(%s) Error - missing output file name\n", argv[0]);
        exit(1);
    }

    if (endOff != 0 && endOff <= startOff) {
        fprintf (stderr, "(%s) Error - start offset must\n", argv[0]);
        exit(1);
    }

    if ( (inFile = fopen(inFileName, "rb")) == NULL) {
        fprintf (stderr, "Can't open file '%s' for read - exit\n",
                 inFileName);
        exit(1);
    }

    if ( (outFile = fopen(outFileName, "wb")) == NULL) {
        fprintf (stderr, "Can't open file '%s' for write - exit\n",
                 outFileName);
        exit(1);
    }
    
    if (fseeko(inFile, startOff, SEEK_SET) == -1) {
        fprintf (stderr, "fseeko to %llu failed - exit\n", startOff);
        exit(1);
    }

    curPosition = startOff;

    done = false;
    do {
        numToRead = sizeof(buffer);
        if (endOff != 0) {
            if (curPosition + sizeof(buffer) > endOff) {
                numToRead = (size_t) (endOff - curPosition);
                done = true;
            }
        }
        numRead = fread(buffer, 1, numToRead, inFile);
        if (numRead != numToRead) {
            done = true;
            if (ferror(inFile)) {
                fprintf(stderr, "Error on file read - exit\n");
                exit(1);
            }
        }
        numWritten = fwrite(buffer, 1, numRead, outFile);
        if (numWritten != numRead) {
            fprintf (stderr, "Error on file write - exit\n");
            exit(1);
        }
        curPosition += numWritten;
    } while (!done);

    fclose(inFile);
    fclose(outFile);
}
