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 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) => Mailbox.send (inputMailbox, USE_SQUARES { squares = grid , canvasWidth = canvasWidth , canvasHeight = canvasHeight }) | NONE => () end fun saveSquares squaresString = let val io = TextIO.openOut filename val () = TextIO.output (io, squaresString) in TextIO.closeOut io end fun exportSquares squaresString = let val io = TextIO.openOut exportFilename 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 str => exportSquares str | LOAD_FILES path => loadFiles (path, inputMailbox) | SELECT_PATH path => selectPath (path, inputMailbox) in run (fileMailbox, inputMailbox) end end