Created
May 4, 2015 06:52
-
-
Save 1995eaton/c511d2eab6a3ae6a2e05 to your computer and use it in GitHub Desktop.
CSC-151 Project 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Argument parser for the FileUtilities class | |
* | |
* @author Jake Eaton | |
*/ | |
public class ArgumentParser { | |
private static String helpLine = | |
"Usage: java FileUtilities COMMAND SHIFT FILE [-verbose]\n" + | |
"Encrypt/decrypt files using the shift cipher.\n\n" + | |
" COMMAND encrypt or decrypt\n" + | |
" SHIFT how many letters to shift\n" + | |
" FILE the file to be encrypted/decrypted\n" + | |
" -h, --help show this message\n" + | |
" -v, --verbose write the encrypted/decrypted text to stdout in addition to FILE"; | |
private static final int VERBOSE = 1, | |
FILE_NAME = 2, | |
SHIFT = 4, | |
TYPE = 8; | |
public boolean verbose = false; | |
public String fileName; | |
public int shift; | |
public boolean encrypt; | |
public static void printHelp() { | |
System.out.println(helpLine); | |
} | |
public static void fail() { | |
System.err.println("Try 'java FileUtilities --help' for more information."); | |
System.exit(-1); | |
} | |
public ArgumentParser(final String[] args) { | |
int mask = 0; | |
for (final String e: args) { | |
if (e.equals("-h") || e.equals("--help")) { | |
printHelp(); | |
System.exit(0); | |
} | |
} | |
for (final String e: args) { | |
if (e.equals("-v") || e.equals("--verbose")) { | |
verbose = true; | |
mask |= VERBOSE; | |
} else if ((mask & SHIFT) == 0 && | |
e.matches("^\\s*-?(0|[1-9][0-9]*)\\s*$")) { | |
shift = Integer.valueOf(e); | |
mask |= SHIFT; | |
} else if ((mask & TYPE) == 0 && | |
e.matches("^(de|en)crypt$")) { | |
encrypt = e.equals("encrypt"); | |
mask |= TYPE; | |
} else { | |
if (e.indexOf('-') == 0) { | |
System.err.printf("Unexpected option: %s\n", e); | |
fail(); | |
} | |
if ((mask & FILE_NAME) != 0) { | |
System.err.printf("Unexpected argument: %s\n", e); | |
fail(); | |
} | |
mask |= FILE_NAME; | |
fileName = e; | |
} | |
} | |
shift *= encrypt ? -1 : 1; | |
if ((mask & ~VERBOSE) != (FILE_NAME | SHIFT | TYPE)) { | |
System.err.println("Invalid usage."); | |
fail(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.FileOutputStream; | |
import java.nio.file.Paths; | |
import java.nio.file.Files; | |
public class FileUtilities { | |
public static byte encodeLetter(byte letter, int shift) { | |
return (letter < 'A' || letter > 'z' || letter > 'Z' && letter < 'a') ? (letter) : | |
(byte) ('A' + (letter & 32) + (letter - 39 + shift % 26 - (letter & 32)) % 26); | |
} | |
public static void main(String[] args) { | |
final ArgumentParser parser = new ArgumentParser(args); | |
byte[] data = null; | |
try { | |
data = Files.readAllBytes(Paths.get(parser.fileName)); | |
} catch (IOException e) { | |
System.err.printf("Error reading file: %s\n", parser.fileName); | |
System.exit(-1); | |
} | |
for (int i = 0; i < data.length; i++) { | |
data[i] = encodeLetter(data[i], parser.shift); | |
} | |
if (parser.verbose) { | |
System.out.println(new String(data).replaceFirst("\n+$", "")); | |
} | |
FileOutputStream fileOut = null; | |
try { | |
fileOut = new FileOutputStream(parser.fileName); | |
} catch (IOException e) { | |
System.err.printf("Error opening file for output: %s\n", | |
parser.fileName); | |
System.exit(-1); | |
} | |
try { | |
fileOut.write(data); | |
fileOut.close(); | |
} catch (IOException e) { | |
System.err.printf("Error writing file: %s\n", | |
parser.fileName); | |
System.exit(-1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment