Skip to content

Instantly share code, notes, and snippets.

@nemo-kaz
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemo-kaz/53decae924d5c4f5a7a9 to your computer and use it in GitHub Desktop.
Save nemo-kaz/53decae924d5c4f5a7a9 to your computer and use it in GitHub Desktop.
Remove Trigraph from given source code
// 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