Last active
August 29, 2015 14:14
-
-
Save nemo-kaz/53decae924d5c4f5a7a9 to your computer and use it in GitHub Desktop.
Remove Trigraph from given source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Removing trigraph notation in the source code | |
//---------------------------------------------------------------------------- | |
//| trigraph | replacement | trigraph | replacement | trigraph | replacement | | |
//---------------------------------------------------------------------------- | |
//| ??= | # | ??( | [ | ??< | { | | |
//| ??/ | \ | ??) | ] | ??> | } | | |
//| ??’ | ? | ??! | | | ??- | ? | | |
//---------------------------------------------------------------------------- | |
// From To | |
// ??= # | |
// ??( [ | |
// ??) ] | |
// ??< { | |
// ??> } | |
// ??/ \ | |
// ??' ^ | |
// ??! | | |
// ??- ~ | |
String.metaClass.delTrigraph = { -> | |
ans = delegate | |
if (ans =~/\?\?</) { | |
ans = ans.replaceAll(/\?\?</) {m0 -> "{"} | |
} // { | |
if (ans =~/\?\?>/) { | |
ans = ans.replaceAll(/\?\?>/) {m0 -> "}"} | |
} // } | |
if (ans =~/\?\?=/) { | |
ans = ans.replaceAll(/\?\?=/) {m0 -> "#"} | |
} // # | |
if (ans =~/\?\?\(/) { | |
ans = ans.replaceAll(/\?\?\(/) {m0 -> "["} | |
} // [ | |
if (ans =~/\?\?\)/) { | |
ans = ans.replaceAll(/\?\?\)/) {m0 -> "]"} | |
} // ] | |
if (ans =~/\?\?\//) { | |
ans = ans.replaceAll(/\?\?\//) {m0 -> "\\"} | |
} // \ | |
if (ans =~/\?\?'/) { | |
ans = ans.replaceAll(/\?\?'/) {m0 -> "^"} | |
} // ^ | |
if (ans =~/\?\?!/) { | |
ans = ans.replaceAll(/\?\?!/) {m0 -> "|"} | |
} // | | |
if (ans =~/\?\?-/) { | |
ans = ans.replaceAll(/\?\?-/) {m0 -> "~"} | |
} // ~ | |
return ans | |
} | |
assert "??<".delTrigraph() == "{" | |
assert "??>".delTrigraph() == "}" | |
assert "??=".delTrigraph() == "#" | |
assert "??/".delTrigraph() == """\\""" | |
assert "??(".delTrigraph() == "[" | |
assert "??)".delTrigraph() == "]" | |
assert "??'".delTrigraph() == "^" | |
assert "??!".delTrigraph() == "|" | |
assert "??-".delTrigraph() == "~" | |
new File(".").eachFileRecurse { file -> | |
// if(file.isFile() && file.name.matches("[0-9@_#A-Z]+")) { | |
if(file.isFile() && (file.name.endsWith(".c") || | |
file.name.endsWith(".h")) | |
) { | |
println file.getName() | |
curName = file.getPath().replaceAll(/.\\(.*)/) {m0,m1 -> m1} | |
newName = new File(curName+"_convd") | |
convert2(curName, newName) | |
} | |
} | |
def convert2(from, too) { | |
try { | |
inpfile = new File(curName.toString()).getText('Shift_JIS') | |
outfile = new File(newName.toString()) | |
inpfile.eachLine { line -> | |
outl = line.delTrigraph().delTrigraph() | |
if ((outl.toString() =~/\\$/)) { // if terminated with \ | |
outfile << outl.toString().substring(0, outl.length()-1) | |
} else { | |
outfile << outl.toString() | |
outfile << "\n" | |
} | |
} | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment