Printf.fprintf stderr "Automate inconnu: `%s' !\n%!" auto;
(estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ()))))
| ["write"; f] ->
- (try Out_channel.with_open_text f (Automata.serialise (module M) st.board)
+ (try
+ Out_channel.with_open_text f (fun f ->
+ Printf.fprintf f "%s\n" M.name;
+ Automata.serialise (module M) st.board f)
with Sys_error _ -> Printf.fprintf stderr "Failed to write file `%s' !\n%!" f);
(EditorState (m, st), Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())))
| ["read"; f] -> (
try
- ( EditorState
- (m, { st with board = In_channel.with_open_text f (Automata.deserialise (module M)) }),
- Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())) )
+ In_channel.with_open_text f (fun f ->
+ let name = Option.get (In_channel.input_line f) in
+ let auto = List.find (fun m -> get_name m = name) Automata.automata in
+ let module M = (val auto) in
+ ( EditorState
+ ( (module M),
+ {
+ board = Automata.deserialise (module M) f;
+ pos = st.pos;
+ current = M.default;
+ size = st.size;
+ } ),
+ Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ())) ))
with Sys_error _ ->
Printf.fprintf stderr "Failed to write file `%s' !\n%!" f;
(estate, Either.right (ModeAux.ModeAndState ((module Normal), Normal.initial ()))))