/*
 * Copyright 2008-2011 Nathan Fiedler. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 */
package org.burstsort4j;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Command-line interface to the various sort implementations.
 *
 * @author Nathan Fiedler
 */
public class Main {

    private static final String BURSTSORT = "--burstsort";
    private static final String FUNNELSORT = "--funnelsort";
    private static final String MULTIKEY = "--multikey";

    private Main() {
    }

    /**
     * Main entry point for the sorter.
     *
     * @param  args  command line arguments.
     */
    public static void main(String[] args) {
        if (args.length < 2 || args.length > 3) {
            usage();
        }
        String input = null;
        String output = null;
        String sort = null;
        if (args.length == 3) {
            sort = args[0];
            input = args[1];
            output = args[2];
        } else {
            sort = BURSTSORT;
            input = args[0];
            output = args[1];
        }

        // Read in the input file.
        long r1 = System.currentTimeMillis();
        String[] data = readFile(input);
        long r2 = System.currentTimeMillis();
        System.out.format("Read time: %dms\n", r2 - r1);

        // Sort the data using the selected sort.
        long s1 = System.currentTimeMillis();
        if (sort.equals(FUNNELSORT)) {
            LazyFunnelsort.sort(data);
        } else if (sort.equals(BURSTSORT)) {
            Burstsort.sort(data);
        } else if (sort.equals(MULTIKEY)) {
            MultikeyQuicksort.sort(data);
        } else {
            usage();
        }
        long s2 = System.currentTimeMillis();
        System.out.format("Sort time: %dms\n", s2 - s1);

        // Write the results to the output file.
        long w1 = System.currentTimeMillis();
        writeFile(output, data);
        long w2 = System.currentTimeMillis();
        System.out.format("Write time: %dms\n", w2 - w1);
    }

    /**
     * Display usage information and exit.
     */
    private static void usage() {
        System.out.println("Usage: Main [options] <input> <output>\n");
        System.out.println("\t<input> is the name of the (unsorted) input file.");
        System.out.println("\t<output> is the name for the (sorted) output file.\n");
        System.out.println("\t--burstsort");
        System.out.println("\t\tSort using the original Burstsort algorithm.");
        System.out.println("\t\tThis is the default sort if none is specified.\n");
        System.out.println("\t--funnelsort");
        System.out.println("\t\tSort using the Lazy Funnelsort algorithm.\n");
        System.out.println("\t--multikey");
        System.out.println("\t\tSort using the Multikey Quicksort algorithm.");
        System.exit(0);
    }

    /**
     * Reads the contents of the named file into an array of strings.
     *
     * @param  name  file to be read.
     * @return  the lines of text from the file.
     */
    private static String[] readFile(String name) {
        List<String> data = new ArrayList<String>();
        try {
            FileReader fr = new FileReader(name);
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
                data.add(line);
                line = br.readLine();
            }
            br.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return data.toArray(new String[data.size()]);
    }

    /**
     * Write the given array of strings to the named file.
     *
     * @param  name  file name to write to.
     * @param  data  the strings to be written.
     */
    private static void writeFile(String name, String[] data) {
        try {
            FileWriter fw = new FileWriter(name);
            BufferedWriter bw = new BufferedWriter(fw);
            for (String output : data) {
                bw.write(output);
                bw.newLine();
            }
            bw.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

@@ -386,16 +386,274 @@
 .List;%0A%0A
+import java.io.InputStreamReader;%0Aimport java.io.OutputStreamWriter;%0Aimport java.util.zip.GZIPInputStream;%0Aimport java.util.zip.GZIPOutputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.Reader;%0Aimport java.io.Writer;%0A%0A
 /**%0A * C
@@ -2812,16 +2812,115 @@
 ut file.
+%22);%0A        System.out.println(%22%5CtGzip compression will be used on any file having a %5C%22.gz%5C%22 suffix
 %5Cn%22);%0A  
@@ -3717,26 +3717,294 @@
         
-File
 Reader
+ fr = null;%0A            if (name.toLowerCase().endsWith(%22.gz%22)) %7B%0A              FileInputStream fis = new FileInputStream(name);%0A              GZIPInputStream gis = new GZIPInputStream(fis);%0A              fr = new InputStreamReader(gis);%0A            %7D else %7B%0A             
  fr = ne
@@ -4015,32 +4015,46 @@
 leReader(name);%0A
+            %7D%0A
             Buff
@@ -4683,26 +4683,299 @@
         
-File
 Writer
+ fw = null;%0A            if (name.toLowerCase().endsWith(%22.gz%22)) %7B%0A              FileOutputStream fos = new FileOutputStream(name);%0A              GZIPOutputStream gos = new GZIPOutputStream(fos);%0A              fw = new OutputStreamWriter(gos);%0A            %7D else %7B%0A             
  fw = ne
@@ -4986,32 +4986,46 @@
 leWriter(name);%0A
+            %7D%0A
             Buff

support gzip'd input and output in Main