Minor fix

- Fixed some bugs surrounding escaped characters
This commit is contained in:
Gabriel Tofvesson 2016-11-16 00:23:01 +01:00
parent 53524c5438
commit 3bc6405458

View File

@ -25,11 +25,25 @@ public class Language {
try { try {
InputStream i = new FileInputStream(f); InputStream i = new FileInputStream(f);
readLine(i); readLine(i);
String s, s1; String s, s1="";
while(!(s1=getKey(s=readLine(i))).equals(key)) while(i.available()>0){
if(i.available()<=0) break; if((s1=readLine(i)).equals(key)){
char[] c = s1.toCharArray();
char n, m=0, k=0;
for(int o = 0; o<c.length; ++o){
n=m;
m=k;
k=c[o];
if(n!='\\' && m=='/' && k=='/'){
s1 = s1.substring(0, o-1);
break;
}else if(n=='\\' && m=='/' && k=='/') s1 = s1.substring(0, o-2) + s1.substring(o-1, s1.length());
}
if(s1.equals(key)) break;
}
}
if(s1.equals(key)){ if(s1.equals(key)){
data.put(s1, s=getValue(s)); data.put(s1, s=getValue(s1));
return s; return s;
} }
} catch (IOException ignored) {} } catch (IOException ignored) {}
@ -125,18 +139,27 @@ public class Language {
String subVerify; String subVerify;
boolean firstLine = true; boolean firstLine = true;
char read; char read;
int lineCount = 1; int lineCount = 0;
while(i.available()>0){ while(i.available()>0){
read = (char) i.read(); subVerify = truncateLeadingSpaces(readLine(i));
if(read==10) { if(firstLine){
if (firstLine) firstLine = false; ++lineCount;
firstLine = false;
continue; continue;
} }
if(read==' ') subVerify = ""; char[] c = subVerify.toCharArray();
else subVerify = ""+read; char n, m=0, k=0;
subVerify += truncateLeadingSpaces(readLine(i)); for(int o = 0; o<c.length; ++o){
n=m;
m=k;
k=c[o];
if(n!='\\' && m=='/' && k=='/'){
subVerify = subVerify.substring(0, o-1);
break;
}else if(n=='\\' && m=='/' && k=='/') subVerify = subVerify.substring(0, o-2) + subVerify.substring(o-1, subVerify.length());
}
if(subVerify.length()==0 || subVerify.toCharArray().length==0 || subVerify.toCharArray()[0]=='\n') continue;
++lineCount; ++lineCount;
if(subVerify.toCharArray().length==0) continue;
if(!isValidKVPair(subVerify)) throw new MalformedLanguageException("Error found at line "+lineCount if(!isValidKVPair(subVerify)) throw new MalformedLanguageException("Error found at line "+lineCount
+" of "+f.getAbsolutePath()+". Invalid key-value pair detected! Note that ':' in the keys or values must be escaped with '\'"); +" of "+f.getAbsolutePath()+". Invalid key-value pair detected! Note that ':' in the keys or values must be escaped with '\'");
String s1 = getKey(subVerify); String s1 = getKey(subVerify);
@ -162,8 +185,10 @@ public class Language {
private static String readLine(InputStream i){ private static String readLine(InputStream i){
String s = ""; String s = "";
try{
char j; char j;
try{ while(i.available()>0 && (j=(char)i.read())!='\n' && j!=13) s+=j; }catch(IOException ignored){} while(i.available()>0 && (j=(char)i.read())!='\n' && j!=13) s+=j;
}catch(IOException ignored){}
return s; return s;
} }
private static boolean isValidKVPair(String data){ private static boolean isValidKVPair(String data){
@ -197,13 +222,23 @@ public class Language {
private static String getValue(String data){ private static String getValue(String data){
char[] str = truncateLeadingSpaces(data).toCharArray(); char[] str = truncateLeadingSpaces(data).toCharArray();
char prev = 0; char prev = 0;
String p1="", p2="";
for(int i = str.length-1; i>0; --i) { for(int i = str.length-1; i>0; --i) {
if (i != str.length-1){ if (i != str.length-1){
if(str[i]!='\\' && prev==':') if(str[i]!='\\' && prev==':') {
return data.substring(i+2, str.length); p1 = data.substring(i + 2, data.length());
break;
}
} }
prev = str[i]; prev = str[i];
} }
return data; str = p1.toCharArray();
for(int i = 0; i<p1.length(); ++i){
if(i != 0)
if(!(str[i]==':' && prev=='\\')) p2+=prev;
prev = str[i];
}
p2+=prev;
return p2;
} }
} }