From: Adam Chlipala Date: Sun, 10 Dec 2006 18:21:14 +0000 (+0000) Subject: Serial number processing X-Git-Tag: release_2010-11-19~327 X-Git-Url: https://git.hcoop.net/hcoop/domtool2.git/commitdiff_plain/3f07a40e5a17c2bce4d90e1a154f8a73d476aaaa Serial number processing --- diff --git a/src/plugins/bind.sml b/src/plugins/bind.sml index 41bc1d8..9a05dce 100644 --- a/src/plugins/bind.sml +++ b/src/plugins/bind.sml @@ -116,6 +116,34 @@ fun readLine inf = fun readILine inf = valOf (Int.fromString (readLine inf)) +val monthToInt = fn Date.Jan => 1 + | Date.Feb => 2 + | Date.Mar => 3 + | Date.Apr => 4 + | Date.May => 5 + | Date.Jun => 6 + | Date.Jul => 7 + | Date.Aug => 8 + | Date.Sep => 9 + | Date.Oct => 10 + | Date.Nov => 11 + | Date.Dec => 12 + +fun padBy ch amt s = + if size s < amt then + CharVector.tabulate (amt, fn _ => ch) ^ s + else + s + +fun dateString () = + let + val date = Date.fromTimeUniv (Time.now ()) + in + padBy #"0" 4 (Int.toString (Date.year date)) + ^ padBy #"0" 2 (Int.toString (monthToInt (Date.month date))) + ^ padBy #"0" 2 (Int.toString (Date.day date)) + end + val () = Slave.registerFileHandler (fn fs => let val {dir, file} = OS.Path.splitDirFile (#file fs) @@ -149,6 +177,39 @@ val () = Slave.registerFileHandler (fn fs => val min = readILine inf val () = TextIO.closeIn inf + 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 + val dns = OS.Path.joinDirFile {dir = #dir fs, file = "dns"} @@ -167,7 +228,7 @@ val () = Slave.registerFileHandler (fn fs => TextIO.output (outf, ".\thostmaster."); TextIO.output (outf, #domain fs); TextIO.output (outf, ". ( "); - TextIO.output (outf, Int.toString 123456789); + TextIO.output (outf, newSerial); TextIO.output (outf, " "); TextIO.output (outf, Int.toString rf); TextIO.output (outf, " ");