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 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 path val str = loadIO (io, "") val () = TextIO.closeIn io in case Parser.parse str of SOME (canvasWidth, canvasHeight, tree) => Mailbox.send ( inputMailbox , USE_LAYERS { tree = tree , canvasWidth = canvasWidth , canvasHeight = canvasHeight } ) | NONE => () end fun saveString (filename, toSaveString) = let val io = TextIO.openOut filename val () = TextIO.output (io, toSaveString) in TextIO.closeOut io end fun run (fileMailbox, inputMailbox) = let val _ = case Mailbox.recv fileMailbox of SAVE_SQUARES {filepath, output} => saveString (filepath, output) | EXPORT_SQUARES {filepath, output} => saveString (filepath, output) | EXPORT_COLLISIONS {filepath, output} => saveString (filepath, output) | LOAD_SQUARES {filepath} => loadSquares (filepath, inputMailbox) in run (fileMailbox, inputMailbox) end end