| 1 | structure Util :> UTIL = |
| 2 | struct |
| 3 | |
| 4 | datatype 'a flat_element = |
| 5 | BEGIN |
| 6 | | END |
| 7 | | ITEM of 'a |
| 8 | type 'a flat_tree = 'a flat_element list |
| 9 | |
| 10 | fun printInt n = |
| 11 | Web.print (if n < 0 then |
| 12 | "-" ^ Int.toString (~n) |
| 13 | else |
| 14 | Int.toString n) |
| 15 | |
| 16 | fun printReal n = |
| 17 | Web.print (if n < 0.0 then |
| 18 | "-" ^ Real.fmt (StringCvt.FIX (SOME 2)) (~n) |
| 19 | else |
| 20 | Real.fmt (StringCvt.FIX (SOME 2)) n) |
| 21 | |
| 22 | fun id x = x |
| 23 | |
| 24 | fun makeSet f items = |
| 25 | case items of |
| 26 | [] => "()" |
| 27 | | [usr] => "(" ^ f usr ^ ")" |
| 28 | | usr::rest => foldl (fn (usr, s) => s ^ ", " ^ f usr) ("(" ^ f usr) rest ^ ")" |
| 29 | |
| 30 | fun neg (r : real) = ~r |
| 31 | |
| 32 | fun isIdent ch = Char.isLower ch orelse Char.isDigit ch |
| 33 | |
| 34 | fun validHost s = |
| 35 | size s > 0 andalso size s < 20 andalso List.all isIdent (String.explode s) |
| 36 | |
| 37 | fun validDomain s = |
| 38 | size s > 0 andalso size s < 100 andalso List.all validHost (String.fields (fn ch => ch = #".") s) |
| 39 | |
| 40 | fun validUser s = |
| 41 | size s > 0 andalso size s < 50 andalso List.all |
| 42 | (fn ch => isIdent ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+") |
| 43 | (String.explode s) |
| 44 | |
| 45 | fun validEmailUser s = |
| 46 | size s > 0 andalso size s < 50 andalso List.all |
| 47 | (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+") |
| 48 | (String.explode s) |
| 49 | |
| 50 | fun validEmail s = |
| 51 | (case String.fields (fn ch => ch = #"@") s of |
| 52 | [user, host] => validEmailUser user andalso validDomain host |
| 53 | | _ => false) |
| 54 | |
| 55 | fun whoisUrl dom = String.concat ["http://reports.internic.net/cgi/whois?whois_nic=", dom, "&type=domain"] |
| 56 | |
| 57 | val rnd = ref (Random.rand (0, 0)) |
| 58 | |
| 59 | fun init () = rnd := Random.rand (SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())), |
| 60 | SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ()))) |
| 61 | |
| 62 | fun randomPassword () = Int.toString (Int.abs (Random.randInt (!rnd))) |
| 63 | |
| 64 | end |