2024-08-28 20:42:52 +01:00
|
|
|
signature FILE_THREAD =
|
|
|
|
|
sig
|
2024-08-29 05:38:58 +01:00
|
|
|
val run: FileMessage.t Mailbox.mbox * InputMessage.t Mailbox.mbox -> unit
|
2024-08-28 20:42:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
structure FileThread :> FILE_THREAD =
|
|
|
|
|
struct
|
|
|
|
|
open FileMessage
|
2024-08-29 05:38:58 +01:00
|
|
|
open InputMessage
|
2024-08-28 20:42:52 +01:00
|
|
|
|
2024-12-29 20:18:29 +00:00
|
|
|
val structureName = "Green"
|
|
|
|
|
val filename = "green.dsc"
|
|
|
|
|
val exportFilename = "green.sml"
|
2024-08-30 02:22:02 +01:00
|
|
|
|
2025-07-12 07:03:09 +01:00
|
|
|
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
|
2025-07-12 07:17:52 +01:00
|
|
|
SOME (canvasWidth, canvasHeight, grid) =>
|
|
|
|
|
Mailbox.send (inputMailbox, USE_SQUARES
|
|
|
|
|
{ squares = grid
|
|
|
|
|
, canvasWidth = canvasWidth
|
|
|
|
|
, canvasHeight = canvasHeight
|
|
|
|
|
})
|
2025-07-12 07:03:09 +01:00
|
|
|
| NONE => ()
|
|
|
|
|
end
|
2024-08-30 02:22:02 +01:00
|
|
|
|
2025-07-12 06:03:59 +01:00
|
|
|
fun saveSquares squaresString =
|
|
|
|
|
let
|
|
|
|
|
val io = TextIO.openOut filename
|
|
|
|
|
val () = TextIO.output (io, squaresString)
|
|
|
|
|
in
|
|
|
|
|
TextIO.closeOut io
|
|
|
|
|
end
|
2024-08-29 00:05:30 +01:00
|
|
|
|
2025-07-12 19:34:52 +01:00
|
|
|
fun exportSquares squaresString =
|
|
|
|
|
let
|
|
|
|
|
val io = TextIO.openOut exportFilename
|
|
|
|
|
val () = TextIO.output (io, squaresString)
|
|
|
|
|
in
|
|
|
|
|
TextIO.closeOut io
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-29 22:26:07 +01:00
|
|
|
fun getDirList (dir, acc, rootPath) =
|
2024-09-25 08:08:15 +01:00
|
|
|
case OS.FileSys.readDir dir of
|
|
|
|
|
SOME path =>
|
2024-09-29 22:26:07 +01:00
|
|
|
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
|
2024-09-25 08:08:15 +01:00
|
|
|
| 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
|
2024-09-29 22:26:07 +01:00
|
|
|
val dirList = getDirList (dir, [], path)
|
2024-09-25 08:08:15 +01:00
|
|
|
val _ = OS.FileSys.closeDir dir
|
2024-09-27 10:06:21 +01:00
|
|
|
val inputMsg = FILE_BROWSER_AND_PATH {fileBrowser = dirList, path = path}
|
2024-09-25 08:08:15 +01:00
|
|
|
in
|
2024-09-27 10:06:21 +01:00
|
|
|
Mailbox.send (inputMailbox, inputMsg)
|
2024-09-25 08:08:15 +01:00
|
|
|
end
|
|
|
|
|
|
2024-09-29 22:26:07 +01:00
|
|
|
fun selectPath (path, inputMailbox) =
|
|
|
|
|
if OS.FileSys.isDir path then loadFiles (path, inputMailbox)
|
2025-07-06 15:06:00 +01:00
|
|
|
else loadSquares (path, inputMailbox)
|
2024-09-29 22:26:07 +01:00
|
|
|
|
2024-08-29 05:38:58 +01:00
|
|
|
fun run (fileMailbox, inputMailbox) =
|
2024-08-28 20:42:52 +01:00
|
|
|
let
|
|
|
|
|
val _ =
|
|
|
|
|
case Mailbox.recv fileMailbox of
|
2025-07-12 06:03:59 +01:00
|
|
|
SAVE_SQUARES str => saveSquares str
|
2025-07-06 15:06:00 +01:00
|
|
|
| LOAD_SQUARES => loadSquares (filename, inputMailbox)
|
2025-07-12 19:34:52 +01:00
|
|
|
| EXPORT_SQUARES str => exportSquares str
|
2024-09-25 08:08:15 +01:00
|
|
|
| LOAD_FILES path => loadFiles (path, inputMailbox)
|
2024-09-29 22:26:07 +01:00
|
|
|
| SELECT_PATH path => selectPath (path, inputMailbox)
|
2024-08-28 20:42:52 +01:00
|
|
|
in
|
2024-08-29 05:38:58 +01:00
|
|
|
run (fileMailbox, inputMailbox)
|
2024-08-28 20:42:52 +01:00
|
|
|
end
|
|
|
|
|
end
|