]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Fix quoting in config file
authorJP <jp@here>
Tue, 13 Mar 2012 11:51:09 +0000 (12:51 +0100)
committerJP <jp@here>
Tue, 13 Mar 2012 11:51:09 +0000 (12:51 +0100)
tv_grab_nl_java/src/org/vanbest/xmltv/Config.java

index 666acac8e3559617526d125d698de9c11a2245b1..45a2687d258c7aa569dd981472c3d03923039dec 100644 (file)
@@ -37,75 +37,43 @@ public class Config {
                out.close();
        }
        
-       public static String unescape(String s) {
-               String result = s.trim();
-               if (result.charAt(0)=='"') {
-                       result = result.substring(1, result.length()-1);
-                       result = result.replaceAll("\\:",":").replaceAll("\\\"", "\"");
-               }
-               return result;
-       }
-       
        public static String escape(String s) {
-               return "\"" + s.replaceAll("\"", "\\\"").replaceAll(":", "\\:") + "\"";
+               return "\"" + s.replaceAll("\\\"", "\\\\\"") + "\"";
        }
 
-
-       public static String[] old_splitLine(String s) {
-               int colon = 0;
-               while (true) {
-                       colon  = s.indexOf(':', colon+1);
-                       if (colon<0 || s.charAt(colon-1) != '\\') {
-                               break;
-                       }
-               }
-               if (colon<0) {
-                       String[] parts = {s};
-                       return parts;
-               } 
-               String id = s.substring(0,  colon).trim();
-               int firstColon = colon;
-               while (true) {
-                       colon  = s.indexOf(':', colon+1);
-                       if (colon<0 || s.charAt(colon-1) != '\\') {
-                               break;
-                       }
-               }
-               if (colon<0) {
-                       String name = unescape(s.substring(firstColon+1));
-                       
-                       String[] parts = {id, name};
-                       return parts;
-               } 
-               String name = unescape(s.substring(firstColon+1, colon));
-               String icon = unescape(s.substring(colon+1));
-               String[] parts = {id,name,icon};
-               return parts;
-       }
        public static List<String> splitLine(String s) {
-               int colon = 0;
-               int prev = 0;
                List<String> parts = new ArrayList<String>(5);
+               int pos=0;
                while (true) {
-                       // Find first unescaped colon   
-                       while (true) {
-                               colon  = s.indexOf(':', prev);
-                               if (colon<0 || s.charAt(colon-1) != '\\') {
+                       // Find first colon outside quotes
+                       boolean quoted=false;
+                       int quoteStart=-1;
+                       StringBuffer buf = new StringBuffer();
+                       for (; pos<s.length(); pos++) {
+                               if (s.charAt(pos)=='"') {
+                                       if (quoted) {
+                                               buf.append(s.substring(quoteStart, pos).replaceAll("\\\\\"", "\\\""));
+                                       } else {
+                                               quoteStart = pos+1;
+                                       }
+                                       quoted=!quoted;
+                                       continue;
+                               }
+                               if(s.charAt(pos)=='\\') pos++;
+                               if(quoted) continue;
+                               if(s.charAt(pos)==':') {
                                        break;
                                }
+                               buf.append(s.charAt(pos));
                        }
-                       if (colon<0) {
-                               // Last part
-                               parts.add(unescape(s.substring(prev)));
+                       parts.add(buf.toString().trim());
+                       if (pos>=s.length()) {
                                break;
-                       } else {
-                               parts.add(unescape(s.substring(prev,colon)));
-                               prev = colon+1;
                        }
+                       pos++;
                }
                return parts;
        }
-       
        public static Config readConfig(File file) {
                Config result = new Config();
                try {