Browse Source

Fiddled the way ConfigFile does escaping to allow = in keys

git-svn-id: http://svn.dmdirc.com/trunk@3787 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
5bf62d37f6
1 changed files with 39 additions and 7 deletions
  1. 39
    7
      src/com/dmdirc/util/ConfigFile.java

+ 39
- 7
src/com/dmdirc/util/ConfigFile.java View File

@@ -92,12 +92,14 @@ public class ConfigFile {
92 92
         int offset;
93 93
 
94 94
         for (String line : file.getLines()) {
95
-            final String tline = unescape(line.trim());
95
+            final String tline = line.trim();
96 96
 
97 97
             if (tline.indexOf('#') == 0 || tline.isEmpty()) {
98 98
                 continue;
99
-            } else if (tline.endsWith(":") && tline.indexOf('=') == -1) {
100
-                domain = tline.substring(0, tline.length() - 1);
99
+            } else if (
100
+                    (tline.endsWith(":") && !tline.endsWith("\\:"))
101
+                    && findEquals(tline) == -1) {
102
+                domain = unescape(tline.substring(0, tline.length() - 1));
101 103
 
102 104
                 domains.add(domain);
103 105
 
@@ -109,13 +111,14 @@ public class ConfigFile {
109 111
                 } else if (!keydomain && !flatdomains.containsKey(domain)) {
110 112
                     flatdomains.add(domain);
111 113
                 }
112
-            } else if (domain != null && keydomain && (offset = tline.indexOf('=')) != -1) {
113
-                final String key = tline.substring(0, offset);
114
-                final String value = tline.substring(offset + 1);
114
+            } else if (domain != null && keydomain
115
+                    && (offset = findEquals(tline)) != -1) {
116
+                final String key = unescape(tline.substring(0, offset));
117
+                final String value = unescape(tline.substring(offset + 1));
115 118
 
116 119
                 keydomains.get(domain).put(key, value);
117 120
             } else if (domain != null && !keydomain) {
118
-                flatdomains.add(domain, tline);
121
+                flatdomains.add(domain, unescape(tline));
119 122
             } else {
120 123
                 throw new InvalidConfigFileException("Unknown or unexpected" +
121 124
                         " line encountered: " + tline);
@@ -288,8 +291,37 @@ public class ConfigFile {
288 291
         return temp.toString();
289 292
     }
290 293
     
294
+    /**
295
+     * Escapes the specified input string by prefixing all occurances of
296
+     * \, \n, \r, = and : with backslashes.
297
+     * 
298
+     * @param input The string to be escaped
299
+     * @return A backslash-armoured version of the string
300
+     */
291 301
     protected static String escape(final String input) {
292 302
         return input.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n")
293 303
                 .replaceAll("\r", "\\\\r").replaceAll("=", "\\\\=").replaceAll(":", "\\\\:");
294 304
     }
305
+    
306
+    /**
307
+     * Finds the first non-escaped instance of '=' in the specified string.
308
+     * 
309
+     * @param input The string to be searched
310
+     * @return The offset of the first non-escaped instance of '=', or -1.
311
+     */
312
+    protected static int findEquals(final String input) {
313
+        boolean escaped = false;
314
+        
315
+        for (int i = 0; i < input.length(); i++) {
316
+            if (escaped) {
317
+                escaped = false;
318
+            } else if (input.charAt(i) == '\\') {
319
+                escaped = true;
320
+            } else if (input.charAt(i) == '=') {
321
+                return i;
322
+            }
323
+        }
324
+        
325
+        return -1;
326
+    }
295 327
 }

Loading…
Cancel
Save