Template language overhaul & misc. improvements
[bpt/mlt.git] / src / lib / web.sml
index 4b256a5..6d81d5b 100644 (file)
 
 structure Web :> WEB =
 struct
-    val params : string StringMap.map ref = ref StringMap.empty
+    fun for f (r1, r2) =
+       if r1 < r2 then
+           let
+               fun loop i =
+                   if i > r2 then
+                       ()
+                   else
+                       (f i;
+                        loop (i+1))
+           in
+               loop r1
+           end
+       else
+           let
+               fun loop i =
+                   if i < r2 then
+                       ()
+                   else
+                       (f i;
+                        loop (i-1))
+           in
+               loop r1
+           end
 
-    val paramStack : string StringMap.map list ref = ref []
+    val params : string list StringMap.map ref = ref StringMap.empty
+
+    val paramStack : string list StringMap.map list ref = ref []
 
     fun setParam (n, v) = params := StringMap.insert (!params, n, v)
+    fun setSingleParam (n, v) = setParam (n, [v])
 
     fun getParam v =
        (case StringMap.find (!params, v) of
             NONE => ""
-          | SOME s => s)
+          | SOME [] => ""
+          | SOME (s::_) => s)
+
+    fun getMultiParam v =
+       (case StringMap.find (!params, v) of
+            NONE => []
+          | SOME l => l)
 
     fun pushParams nvs =
        (paramStack := (!params) :: (!paramStack);
@@ -54,4 +85,47 @@ struct
     fun output () =
        (TextIO.print "Status: 200\nContent-type: text/html\n\n";
         TextIO.print (String.concat (List.rev (!text))))
+
+    val getCgi = OS.Process.getEnv
+
+    fun html s =
+       let
+           fun xch #"<" = "&lt;"
+             | xch #">" = "&gt;"
+             | xch #"&" = "&amp;"
+             | xch #"\"" = "&quot;"
+             | xch ch = str ch
+       in
+           foldr op^ "" (map xch (String.explode s))
+       end
+
+    fun htmlNl s =
+       let
+           fun xch #"<" = "&lt;"
+             | xch #">" = "&gt;"
+             | xch #"&" = "&amp;"
+             | xch #"\"" = "&quot;"
+             | xch #"\n" = "<br />"
+             | xch ch = str ch
+       in
+           foldr op^ "" (map xch (String.explode s))
+       end
+
+    exception Format of string
+
+    fun stoiOpt s = Int.fromString s
+    fun stoi s =
+       (case Int.fromString s of
+            NONE => raise Format s
+          | SOME i => i)
+
+    fun storOpt s = Real.fromString s
+    fun stor s =
+       (case Real.fromString s of
+            NONE => raise Format s
+          | SOME r => r)
+
+    fun summary () =
+       StringMap.foldli (fn (n, vs, s) => foldl (fn (v, s) => s ^ " VALUE: " ^ v) (s ^ " NAME: " ^ n) vs)
+       "" (!params)
 end
\ No newline at end of file