From a5ad6edf3c71e82155c79bbb18b947476fda1f07 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 2 Feb 2017 19:24:16 -0800 Subject: [PATCH] Bias pair parsing toward key in digest.txt. In that file we have: filename = hex_encoded_hash The hex_encoded_hash will never contain = but the filename could conceivably contain =, so for that file we split on the last = instead of the first. Everywhere else we continue to split on the first = because that makes more sense. --- .../threerings/getdown/data/Application.java | 4 ++-- .../com/threerings/getdown/data/Digest.java | 4 +++- .../threerings/getdown/util/ConfigUtil.java | 24 ++++++++++++------- .../getdown/util/ConfigUtilTest.java | 16 ++++++------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/threerings/getdown/data/Application.java b/src/main/java/com/threerings/getdown/data/Application.java index c6dd522..1b226c2 100644 --- a/src/main/java/com/threerings/getdown/data/Application.java +++ b/src/main/java/com/threerings/getdown/data/Application.java @@ -765,7 +765,7 @@ public class Application File pairFile = getLocalPath(pairLocation); if (pairFile.exists()) { try { - List args = ConfigUtil.parsePairs(pairFile, false); + List args = ConfigUtil.parsePairs(pairFile, false, false); for (String[] pair : args) { if (pair[1].length() == 0) { collector.add(pair[0]); @@ -1231,7 +1231,7 @@ public class Application try { in = ConnectionUtil.open(_latest).getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); - for (String[] pair : ConfigUtil.parsePairs(bin, false)) { + for (String[] pair : ConfigUtil.parsePairs(bin, false, false)) { if (pair[0].equals("version")) { _targetVersion = Math.max(Long.parseLong(pair[1]), _targetVersion); if (fileVersion != -1 && _targetVersion > fileVersion) { diff --git a/src/main/java/com/threerings/getdown/data/Digest.java b/src/main/java/com/threerings/getdown/data/Digest.java index 7eb74d2..69a14f2 100644 --- a/src/main/java/com/threerings/getdown/data/Digest.java +++ b/src/main/java/com/threerings/getdown/data/Digest.java @@ -113,7 +113,9 @@ public class Digest String filename = digestFile(version); StringBuilder data = new StringBuilder(); File dfile = new File(appdir, filename); - for (String[] pair : ConfigUtil.parsePairs(dfile, false)) { + // bias = toward key: the key is the filename and could conceivably contain = signs, value + // is the hex encoded hash which will not contain = + for (String[] pair : ConfigUtil.parsePairs(dfile, false, true)) { if (pair[0].equals(filename)) { _metaDigest = pair[1]; break; diff --git a/src/main/java/com/threerings/getdown/util/ConfigUtil.java b/src/main/java/com/threerings/getdown/util/ConfigUtil.java index 0a8bf2b..945735a 100644 --- a/src/main/java/com/threerings/getdown/util/ConfigUtil.java +++ b/src/main/java/com/threerings/getdown/util/ConfigUtil.java @@ -32,28 +32,32 @@ public class ConfigUtil * * @param checkPlatform if true, platform qualifiers will be used to filter out pairs that do * not match the current platform; if false, all pairs will be returned. + * @param biasToKey if true the splitting on {@code =} will be key-biased meaning we split on + * the last {@code =} in the string (extra equals go in the key). If false, we bias toward the + * value: split on the first equals, put extra equals in the value. * * @return a list of String[] instances containing the key/value pairs in the * order they were parsed from the file. */ - public static List parsePairs (File config, boolean checkPlatform) + public static List parsePairs (File config, boolean checkPlatform, boolean biasToKey) throws IOException { // annoyingly FileReader does not allow encoding to be specified (uses platform default) - return parsePairs( - new InputStreamReader(new FileInputStream(config), "UTF-8"), checkPlatform); + InputStreamReader input = new InputStreamReader(new FileInputStream(config), "UTF-8"); + return parsePairs(input, checkPlatform, biasToKey); } /** * See {@link #parsePairs(File,boolean)}. */ - public static List parsePairs (Reader config, boolean checkPlatform) - throws IOException + public static List parsePairs (Reader config, boolean checkPlatform, + boolean biasToKey) throws IOException { return parsePairs( config, checkPlatform ? StringUtil.deNull(System.getProperty("os.name")).toLowerCase() : null, - checkPlatform ? StringUtil.deNull(System.getProperty("os.arch")).toLowerCase() : null); + checkPlatform ? StringUtil.deNull(System.getProperty("os.arch")).toLowerCase() : null, + biasToKey); } /** @@ -71,7 +75,7 @@ public class ConfigUtil // I thought that we could use HashMap and put new String[] {pair[1]} for // the null case, but it mysteriously dies on launch, so leaving it as HashMap for now - for (String[] pair : parsePairs(config, checkPlatform)) { + for (String[] pair : parsePairs(config, checkPlatform, false)) { Object value = data.get(pair[0]); if (value == null) { data.put(pair[0], pair[1]); @@ -104,7 +108,8 @@ public class ConfigUtil } /** A helper function for {@link #parsePairs(Reader,boolean}. */ - protected static List parsePairs (Reader config, String osname, String osarch) + protected static List parsePairs (Reader config, String osname, String osarch, + boolean biasToKey) throws IOException { List pairs = new ArrayList(); @@ -123,7 +128,8 @@ public class ConfigUtil // parse our key/value pair String[] pair = new String[2]; - int eidx = line.indexOf("="); + // if we're biasing toward key, put all the extra = in the key rather than the value + int eidx = biasToKey ? line.lastIndexOf("=") : line.indexOf("="); if (eidx != -1) { pair[0] = line.substring(0, eidx).trim(); pair[1] = line.substring(eidx+1).trim(); diff --git a/src/test/java/com/threerings/getdown/util/ConfigUtilTest.java b/src/test/java/com/threerings/getdown/util/ConfigUtilTest.java index 9772cfd..986cc3c 100644 --- a/src/test/java/com/threerings/getdown/util/ConfigUtilTest.java +++ b/src/test/java/com/threerings/getdown/util/ConfigUtilTest.java @@ -38,7 +38,7 @@ public class ConfigUtilTest @Test public void testSimplePairs () throws IOException { - List pairs = ConfigUtil.parsePairs(toReader(SIMPLE_PAIRS), true); + List pairs = ConfigUtil.parsePairs(toReader(SIMPLE_PAIRS), true, false); for (int ii = 0; ii < SIMPLE_PAIRS.length; ii++) { assertEquals(SIMPLE_PAIRS[ii].key, pairs.get(ii)[0]); assertEquals(SIMPLE_PAIRS[ii].value, pairs.get(ii)[1]); @@ -57,7 +57,7 @@ public class ConfigUtilTest Pair notWin = new Pair("fifteen", "[!windows] sixteen"); Pair[] pairs = { linux, mac, linuxAndMac, linux64, linux64s, mac64, win64, notWin }; - List parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "i386"); + List parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "i386", false); assertTrue(exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(exists(parsed, linuxAndMac.key)); @@ -67,7 +67,7 @@ public class ConfigUtilTest assertTrue(!exists(parsed, win64.key)); assertTrue(exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "x86_64"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "x86_64", false); assertTrue(exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(exists(parsed, linuxAndMac.key)); @@ -77,7 +77,7 @@ public class ConfigUtilTest assertTrue(!exists(parsed, win64.key)); assertTrue(exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "amd64"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "linux", "amd64", false); assertTrue(exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(exists(parsed, linuxAndMac.key)); @@ -87,7 +87,7 @@ public class ConfigUtilTest assertTrue(!exists(parsed, win64.key)); assertTrue(exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "mac os x", "x86_64"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "mac os x", "x86_64", false); assertTrue(!exists(parsed, linux.key)); assertTrue(exists(parsed, mac.key)); assertTrue(exists(parsed, linuxAndMac.key)); @@ -97,7 +97,7 @@ public class ConfigUtilTest assertTrue(!exists(parsed, win64.key)); assertTrue(exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "i386"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "i386", false); assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, linuxAndMac.key)); @@ -107,7 +107,7 @@ public class ConfigUtilTest assertTrue(!exists(parsed, win64.key)); assertTrue(!exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "x86_64"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "x86_64", false); assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, linuxAndMac.key)); @@ -117,7 +117,7 @@ public class ConfigUtilTest assertTrue(exists(parsed, win64.key)); assertTrue(!exists(parsed, notWin.key)); - parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "amd64"); + parsed = ConfigUtil.parsePairs(toReader(pairs), "windows", "amd64", false); assertTrue(!exists(parsed, linux.key)); assertTrue(!exists(parsed, mac.key)); assertTrue(!exists(parsed, linuxAndMac.key));