From 28356a7bf097269ff02edcab05b4dac38e73e7dc Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 20 Oct 2012 09:29:30 -0700 Subject: Presets are now stored in a single file. Presets are read from presets.txt one per line, listing an input for each output, followed by an optional name prefixed with a #. Also added net.tuschhcm.routercontrol.PrintPresets to parse and print a presets file. --- src/net/tuschhcm/routercontrol/Preset.java | 123 +++++++++++++++-------- src/net/tuschhcm/routercontrol/PrintPresets.java | 25 +++++ src/net/tuschhcm/routercontrol/SwitcherApp.java | 24 ++--- 3 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 src/net/tuschhcm/routercontrol/PrintPresets.java diff --git a/src/net/tuschhcm/routercontrol/Preset.java b/src/net/tuschhcm/routercontrol/Preset.java index 9da1de3..2a408e9 100644 --- a/src/net/tuschhcm/routercontrol/Preset.java +++ b/src/net/tuschhcm/routercontrol/Preset.java @@ -4,13 +4,18 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; + +import java.text.ParseException; + import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; /** * Class to represent a router preset. */ public class Preset { - private static final String UNNAMED = ""; + private static final String UNNAMED = ""; /** * Name of the preset. @@ -24,56 +29,92 @@ public class Preset { private final ArrayList mInputs; /** - * Load a preset from the given file. + * Load the presets from a given file. * * @param file The file to load. - * @return the Preset object - * @throws IllegalArgumentException if the preset can't be loaded. + * @return A list of Presets + * @throws ParseException if the presets file can't be parsed. + * @throws IOException if the file can't be read. */ - public static Preset loadPresetFile(File file) throws IllegalArgumentException { + public static Map loadPresetsFile(File file) + throws ParseException, IOException { + + final Map map = new HashMap(); + final BufferedReader in = new BufferedReader(new FileReader(file)); + try { - Preset preset = new Preset(); + int lineno = 0; + String line; - BufferedReader in = new BufferedReader(new FileReader(file)); - int lineno = 1; + while ((line = in.readLine()) != null) { + lineno++; // Inc. the line count immediately after the read. - String line = in.readLine(); - if (line == null) { - throw new IllegalArgumentException("Empty preset file: " + file); - } - - // The name is optional and starts with a # - if (line.charAt(0) == '#') { - preset.mName = line.substring(1).trim(); - line = in.readLine(); - lineno++; - } + line = line.trim(); + if (line.equals("")) { + continue; + } - // Read the inputs. - int output = 1; - while (line != null && !line.equals("")) { try { - final Integer input = Integer.valueOf(line.trim()); - preset.mInputs.add(input); - output++; - - } catch (final NumberFormatException nfe) { - throw new IllegalArgumentException( - String.format("%s: Input set for output %d on line %d is not a number.", - file, output, lineno)); - } + map.put(lineno, Preset.fromString(line.trim())); - line = in.readLine(); - lineno++; + } catch (IllegalArgumentException e) { + throw new ParseException("Malformed preset on line " + lineno + + ": " + e.getMessage(), lineno); + } } + return map; + + } finally { in.close(); + } + } - return preset; + /** + * Load a preset from a String. + * @param line The string to parse. + * @return The Preset defined by the string. + * @throws IllegalArgumentException if the string is malformed. + */ + public static Preset fromString(String line) { + final Preset p = new Preset(); + + boolean inInput = false; + int start = 0; + for (int i = 0; i < line.length(); i++) { + char c = line.charAt(i); + + if (inInput && Character.isWhitespace(c)) { + // End of current input + Integer input = Integer.valueOf(line.substring(start, i)); + p.mInputs.add(input); + inInput = false; + start = i + 1; + + } else if (!inInput && c == '#') { + // Start comment + // Ignore empty comments. + if ((i+1) < line.length()) { + p.mName = line.substring(i+1).trim(); + } + break; // end loop + + } else if (Character.isDigit(c)) { + inInput = true; + + } else { + throw new IllegalArgumentException("Unexpected character '" + c + "' at position " + + (i+1) + ". (inInput = " + inInput +")"); + } + } - } catch (final IOException e) { - throw new IllegalArgumentException("Could not read " + file, e); + if (inInput) { + // One last input at the end of the line. + Integer input = Integer.valueOf(line.substring(start)); + p.mInputs.add(input); } + + return p; } /** @@ -121,12 +162,12 @@ public class Preset { */ public String toString() { String str = ""; - if (!mName.equals(UNNAMED)) { - str = "# " + mName + "\n"; - } - for (Integer in : mInputs) { - str += in + "\n"; + str += in + " "; + } + + if (!mName.equals(UNNAMED)) { + str += "# " + mName; } return str; diff --git a/src/net/tuschhcm/routercontrol/PrintPresets.java b/src/net/tuschhcm/routercontrol/PrintPresets.java new file mode 100644 index 0000000..1fcab17 --- /dev/null +++ b/src/net/tuschhcm/routercontrol/PrintPresets.java @@ -0,0 +1,25 @@ +package net.tuschhcm.routercontrol; + +import java.io.File; +import java.util.Map; + +/** + * Utility to print out the preset file in a nice readable form. + */ +public class PrintPresets { + public static void main(String... args) throws Exception { + Map ps = Preset.loadPresetsFile(new File("presets.txt")); + + for (Map.Entry entry : ps.entrySet()) { + final Preset p = entry.getValue(); + + System.out.printf("Preset %3d. %-20s", + entry.getKey(), p.getName()); + + for (int i = 1; i <= p.getNumberOfOutputs(); i++) { + System.out.printf("%3d", p.getInputForOutput(i)); + } + System.out.println(); + } + } +} diff --git a/src/net/tuschhcm/routercontrol/SwitcherApp.java b/src/net/tuschhcm/routercontrol/SwitcherApp.java index 00127e8..9a4b8ae 100644 --- a/src/net/tuschhcm/routercontrol/SwitcherApp.java +++ b/src/net/tuschhcm/routercontrol/SwitcherApp.java @@ -29,7 +29,7 @@ public class SwitcherApp { /** * My list of presets. */ - private final Map mPresets; + private Map mPresets; /** * Create a switcher app. @@ -74,23 +74,19 @@ public class SwitcherApp { private void loadPresets() { try { - File presetDir = new File("presets"); - if (!presetDir.isDirectory()) { - System.err.println("Could not find presets directory!"); + final String dir = SwitcherApp.class.getProtectionDomain(). + getCodeSource().getLocation().getPath(); + File presets = new File(dir, "presets.txt"); + + if (!presets.isFile()) { + System.err.println("Could not find presets.txt in " + dir); System.exit(1); } - int i = 1; - for (File file : presetDir.listFiles()) { - if (file.isFile()) { - Preset p = Preset.loadPresetFile(file); - mPresets.put(i, p); - i++; - } - } + mPresets = Preset.loadPresetsFile(presets); - } catch (IllegalArgumentException e) { - System.err.println("Problem loading preset: " + e.getMessage()); + } catch (Exception e) { + System.err.println("Problem loading preset file: " + e.getMessage()); System.exit(1); } } -- cgit v1.2.3