| 1 |
structure GenFile : GENERATE_FILE = |
| 2 |
struct |
| 3 |
|
| 4 |
open MDLError |
| 5 |
|
| 6 |
val bufsize = 1024*1024 |
| 7 |
|
| 8 |
fun gen {trans, program, fileSuffix} (_, [infile]) = |
| 9 |
(let val _ = init() |
| 10 |
val {base, ext} = OS.Path.splitBaseExt infile |
| 11 |
val outfile = OS.Path.joinBaseExt{base=base, ext=SOME fileSuffix} |
| 12 |
val _ = if infile = outfile then |
| 13 |
fail("input and output file the same name!") |
| 14 |
else |
| 15 |
() |
| 16 |
val text = trans infile |
| 17 |
fun changed() = |
| 18 |
let val s = TextIO.openIn outfile |
| 19 |
val t = TextIO.inputN(s, bufsize) |
| 20 |
in TextIO.closeIn s; |
| 21 |
t <> text |
| 22 |
end handle _ => true |
| 23 |
in if !errorCount > 0 then |
| 24 |
(print("[Result not written to "^outfile^"]\n"); 1) |
| 25 |
else if changed() then |
| 26 |
(print("[Generating "^outfile^"]\n"); |
| 27 |
let val s = TextIO.openOut outfile |
| 28 |
in TextIO.output(s, text); |
| 29 |
TextIO.closeOut s; |
| 30 |
0 |
| 31 |
end |
| 32 |
) |
| 33 |
else |
| 34 |
(print("[No change to "^outfile^"]\n"); 0) |
| 35 |
end |
| 36 |
handle Error => 1 |
| 37 |
| exn => fail("Uncaught exception "^exnName exn) |
| 38 |
) |
| 39 |
| gen {program,...} _ = fail("usage: "^program^" <filename>") |
| 40 |
|
| 41 |
|
| 42 |
end |