From 93e7ac7b5565695af9d52020761c7c460c837c29 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 24 Sep 2012 23:03:58 -0400 Subject: [PATCH] Support quoting in property and category names for almost all possible characters allowed --- .../minecraftforge/common/Configuration.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/common/net/minecraftforge/common/Configuration.java b/common/net/minecraftforge/common/Configuration.java index 7da8b7a4a..379e0f15e 100644 --- a/common/net/minecraftforge/common/Configuration.java +++ b/common/net/minecraftforge/common/Configuration.java @@ -20,6 +20,7 @@ import java.util.Locale; import java.util.Map; import java.util.TreeMap; +import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; import com.google.common.collect.Maps; @@ -50,6 +51,8 @@ public class Configuration private boolean caseSensitiveCustomCategories; public static final String ALLOWED_CHARS = "._-"; + private static final CharMatcher allowedProperties = CharMatcher.JAVA_LETTER_OR_DIGIT.or(CharMatcher.anyOf(ALLOWED_CHARS)); + /** * Create a configuration file for the file given in parameter. */ @@ -217,10 +220,10 @@ public class Configuration int nameStart = -1, nameEnd = -1; boolean skip = false; - + boolean quoted = false; for (int i = 0; i < line.length() && !skip; ++i) { - if (Character.isLetterOrDigit(line.charAt(i)) || ALLOWED_CHARS.indexOf(line.charAt(i)) != -1) + if (Character.isLetterOrDigit(line.charAt(i)) || ALLOWED_CHARS.indexOf(line.charAt(i)) != -1 || (quoted && line.charAt(i) != '"')) { if (nameStart == -1) { @@ -241,6 +244,16 @@ public class Configuration skip = true; continue; + case '"': + if (quoted) + { + quoted = false; + } + if (!quoted && nameStart == -1) + { + quoted = true; + } + break; case '{': String scopeName = line.substring(nameStart, nameEnd + 1); @@ -279,6 +292,10 @@ public class Configuration } } } + if (quoted) + { + throw new RuntimeException("unmatched quote"); + } } } } @@ -338,7 +355,12 @@ public class Configuration } buffer.write("####################\r\n\r\n"); - buffer.write(category.getKey() + " {\r\n"); + String catKey = category.getKey(); + if (!allowedProperties.matchesAllOf(catKey)) + { + catKey = '"'+catKey+'"'; + } + buffer.write(catKey + " {\r\n"); writeProperties(buffer, category.getValue().values()); buffer.write("}\r\n\r\n"); } @@ -372,8 +394,12 @@ public class Configuration buffer.write(" # " + commentLine + "\r\n"); } } - - buffer.write(" " + property.getName() + "=" + property.value); + String propName = property.getName(); + if (!allowedProperties.matchesAllOf(propName)) + { + propName = '"'+propName+'"'; + } + buffer.write(" " + propName + "=" + property.value); buffer.write("\r\n"); } }