+ val oldSerial = let
+ val inf = TextIO.openIn (OS.Path.joinDirFile {dir = #dir fs,
+ file = "serial"})
+ in
+ SOME (readLine inf)
+ before TextIO.closeIn inf
+ end handle IO.Io _ => NONE
+
+ val newSerial =
+ case serial of
+ SOME n => Int.toString n
+ | NONE =>
+ let
+ val prefix = dateString ()
+ in
+ prefix
+ ^ (case oldSerial of
+ NONE => "00"
+ | SOME old =>
+ if size old >= 8 andalso
+ String.substring (old, 0, 8) = prefix then
+ case Int.fromString (String.extract (old, 8, NONE)) of
+ NONE => "00"
+ | SOME old => padBy #"0" 2 (Int.toString (old+1))
+ else
+ "00")
+ end
+
+ val outf = TextIO.openOut (OS.Path.joinDirFile {dir = #dir fs,
+ file = "serial"})
+ val _ = TextIO.output (outf, newSerial)
+ val _ = TextIO.closeOut outf
+