/**Driver that allows running of algorithms on text input
*
* Samantha Fitzpatrick
*/
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Driver_jgrasp{
/**Main driver to open files
*
*@param args[] Holds command line arguments: filename.
*/
// Variables to hold all command-line inputs and switches.
// input and output text Lists
ArrayList<String> text = new ArrayList<String>();
ArrayList<String> outText = new ArrayList<String>();
// regex pattern match for command-line arguments
Pattern p = Pattern.compile("(^-{1,2}[a-zA-Z]+)[0-9]*$");
// Loop through command-line arguments
for (int i=0; i<args.length; i++) {
Matcher m = p.matcher(args[i]);
if (m.find()) {
inputArg = m.group(1);
}
// Handle each found Argument. Only arguments starting with -- or -
// will be parsed as arguments. Everything else will only be parsed
// if there is a rule to take in the value with a given option.
switch (inputArg) {
case "--help":
case "-h":
System.
out.
println("Usage: "); System.
out.
println("--help, -h: Print this helpfile and exit."); System.
out.
println("--input, -i: Filename to read input text from."); System.
out.
println("--readbytes, -rb: Read input as binary."); System.
out.
println("--output, -o: Filename to send output to. If omitted, STDOUT will be used."); System.
out.
println("--writebytes, -wb: Write output as binary."); System.
out.
println("--compress, -c: Flag to run compression."); System.
out.
println(" -c huffman: to run huffman compression"); System.
out.
println(" -c lzw: to run LZW compression."); System.
out.
println("--huffmanfiles: Files from which to build huffman tree."); System.
out.
println(" only used if huffman compression is run."); System.
out.
println("--extract, -x: Flag to decompress the text."); System.
out.
println(" -x huffman: decompress with Huffman"); System.
out.
println(" -x lzw: decompress with LZW."); System.
out.
println("--encrypt, -e: Flag to encrypt the text."); System.
out.
println("--decrypt, -d: Flag to decrypt the text."); return;
// File to take input text from
case "--input":
case "-i":
inputFile = args[i+1];
break;
// Flag to read input as binary instead of ascii
case "--readbytes":
case "-rb":
readAsBytes = true;
break;
// File to output changed text to
case "--output":
case "-o":
outputFile = args[i+1];
break;
// Flag to write output as binary instead of ascii
case "--writebytes":
case "-wb":
writeAsBytes = true;
break;
// Flag to compress input
// Will parse the following argument to specify compression algorithm
case "--compress":
case "-c":
break;
// Specify input text files to build huffman tree.
// all strings following argument until next optional parameter is found
// specified by string starting with '-'.
case "--huffmanfiles":
break;
// Extract (decompress) input
// Immediately following argument will specify algorithm to use
case "--extract":
case "-x":
break;
// Flag to encrypt output before writing.
// Following argument will specify algorithm to use
case "--encrypt":
case "-e":
break;
// Flag to decrypt output after reading.
// Following argument will specify algorithm to use
case "--decrypt":
case "-d":
break;
// File to specify keys for encryption algorithms
case "--keyfile":
case "-k":
break;
}
}
// Read text from input file and store in ArrayList
if (inputFile != "") {
if (buffer.exists() && !buffer.isDirectory()) {
if (readAsBytes) {
text = readFileInputStream(inputFile);
} else {
text = ReadFile(inputFile);
}
} else {
System.
out.
println("Input file not found on disk: " + inputFile
); }
}
// Compute raw size of text input
int rawsize = 0;
rawsize = rawsize + s.length();
}
// Compute size of text output
int outsize = 0;
outsize = outsize + s.length();
}
System.
out.
println("Raw character length: " + rawsize
);
//Output information: either to stdout or a filename
if (outputFile != "") {
System.
out.
println("#-----------------------#"); System.
out.
println("Writing file " + outputFile
); System.
out.
println("#-----------------------#"); if (writeAsBytes) {
WriteToFile(outText, outputFile, writeAsBytes);
} else {
WriteToFile(outText, outputFile);
}
} else {
System.
out.
println("No output file supplied, redirecting to STDOUT"); for (int i=0; i<outText.size(); i++) {
if (writeAsBytes) {
System.
out.
println(outText.
get(i
).
getBytes()); } else {
System.
out.
println(outText.
get(i
)); }
}
}
}
/** Read from input File
*/
Scanner scanner
= new Scanner
(new File(filename
));
ArrayList<String> words = new ArrayList<String>();
System.
out.
println("#-----------------------#"); System.
out.
println("Reading file " + filename
); System.
out.
println("#-----------------------#");
while(scanner.hasNextLine()){
words.add(scanner.nextLine());
}
return words;
}
/** Read from input file as an
*/
byte[] buffer =null;
try {
int length = (int)a_file.length();
buffer = new byte [length];
fis.read(buffer);
fis.close();
e.printStackTrace();
}
sContent
= new String(buffer
);
ArrayList<String> text = new ArrayList<String>();
text.add(sContent);
return text;
}
/** Write to file as ascii
*/
try {
for (int i=0; i<text.size(); i++) {
pw.println(text.get(i));
}
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
}
/** Write to file as binary
*/
private static void WriteToFile
(ArrayList
<String
> text,
String outName,
Boolean bytes
) {
try {
for (int i=0; i<text.size(); i++) {
byte[] outBytes = text.get(i).getBytes();
output.write(outBytes);
}
e.printStackTrace();
} finally {
try{
if (output != null) {
output.close();
}
e.printStackTrace();
}
}
}
}