Files
sml-projects/imperative-shell/file-thread.sml

117 lines
3.4 KiB
Standard ML

signature FILE_THREAD =
sig
val run: FileMessage.t Mailbox.mbox * InputMessage.t Mailbox.mbox -> unit
end
structure FileThread :> FILE_THREAD =
struct
open FileMessage
open InputMessage
val structureName = "Green"
val filename = "green.dsc"
val exportFilename = "green.sml"
fun ndcToLerpX num =
let
val num = (num + 1.0) / 2.0
val num = Real32.toString num
val num =
(* Problem: It seems that Real32.toString may sometimes return a string
* that is recognised as an integer, like "1" instead of "1.0".
* If that happens, we just add a ".0" to the end
* so it's recognised as a real. *)
if String.isSubstring "." num then num
else num ^ ".0"
in
" (((startX * (1.0 - " ^ num ^ ")) + (endX * " ^ num
^ ")) / windowWidth) - 1.0"
end
fun ndcToLerpY num =
let
val num = (num + 1.0) / 2.0
val num = Real32.toString num
val num = if String.isSubstring "." num then num else num ^ ".0"
in
" (((startY * (1.0 - " ^ num ^ ")) + (endY * " ^ num
^ ")) / windowHeight) - 1.0"
end
fun colToString col =
let val col = Real32.toString col
in if String.isSubstring "." col then col else col ^ ".0"
end
fun exportSquares squares = ()
fun parse (io, acc) = ()
fun loadIO (io, str) =
case TextIO.inputLine io of
SOME line => loadIO (io, str ^ line)
| NONE => str
fun loadSquares (path, inputMailbox) =
let
val io = TextIO.openIn filename
val str = loadIO (io, "")
val () = TextIO.closeIn io
in
case Parser.parse str of
SOME (canvasWidth, canvasHeight, grid) => ()
| NONE => ()
end
fun saveSquares squaresString =
let
val io = TextIO.openOut filename
val () = TextIO.output (io, squaresString)
in
TextIO.closeOut io
end
fun getDirList (dir, acc, rootPath) =
case OS.FileSys.readDir dir of
SOME path =>
let
val folderPath = String.concat [rootPath, "/", path]
in
if OS.FileSys.isDir folderPath then
getDirList (dir, AppType.IS_FOLDER path :: acc, rootPath)
else if OS.FileSys.isLink folderPath then
getDirList (dir, acc, rootPath)
else
getDirList (dir, AppType.IS_FILE path :: acc, rootPath)
end
| NONE => let val acc = List.rev acc in Vector.fromList acc end
fun loadFiles (path, inputMailbox) =
let
val path = if String.size path = 0 then OS.FileSys.getDir () else path
val dir = OS.FileSys.openDir path
val dirList = getDirList (dir, [], path)
val _ = OS.FileSys.closeDir dir
val inputMsg = FILE_BROWSER_AND_PATH {fileBrowser = dirList, path = path}
in
Mailbox.send (inputMailbox, inputMsg)
end
fun selectPath (path, inputMailbox) =
if OS.FileSys.isDir path then loadFiles (path, inputMailbox)
else loadSquares (path, inputMailbox)
fun run (fileMailbox, inputMailbox) =
let
val _ =
case Mailbox.recv fileMailbox of
SAVE_SQUARES str => saveSquares str
| LOAD_SQUARES => loadSquares (filename, inputMailbox)
| EXPORT_SQUARES triangles => exportSquares triangles
| LOAD_FILES path => loadFiles (path, inputMailbox)
| SELECT_PATH path => selectPath (path, inputMailbox)
in
run (fileMailbox, inputMailbox)
end
end