MODULE FileHandlers;
IMPORT
Files, Configuration, Strings, Commands, Streams;
PROCEDURE OpenFile* (CONST path : ARRAY OF CHAR; err: Streams.Writer; caller: OBJECT);
VAR config,filehandler: ARRAY 128 OF CHAR; name,filename, ext : Files.FileName; index, res : LONGINT;
context: Commands.Context; arg: Streams.StringReader; in: Streams.Reader; out: Streams.Writer;
BEGIN
Files.SplitExtension(path, name, ext);
index := Strings.Find (ext, 0, '@');
IF index >= 0 THEN Strings.Truncate (ext, index); END;
Strings.LowerCase(ext);
config := "Filehandlers.";
Strings.Append(config, ext);
Strings.Append(config, ".Open");
Configuration.Get(config, filehandler, res);
IF (res # Configuration.Ok) THEN
IF err # NIL THEN err.String ("Opening '"); err.String (ext); err.String ("' files not specified in Configuration.XML."); err.Ln END;
RETURN
END;
context := Commands.GetContext ();
IF context = NIL THEN
in := NIL; out := NIL
ELSE
in := context.in; out := context.out
END;
filename := '"';
Strings.Append(filename,path);
Strings.Append(filename,'"');
IF index >= 0 THEN ext[index] := '@'; Strings.Move (ext, index, Strings.Length (ext) - index, ext, 0); Strings.Append (filename, ext); END;
NEW (arg, LEN (filename)); arg.Set (filename);
NEW (context, in, arg, out, err, caller);
Commands.Activate (filehandler, context, {}, res, name);
IF res # Commands.Ok THEN
IF err # NIL THEN err.String (name); err.Ln END
END;
END OpenFile;
PROCEDURE Open* (context: Commands.Context);
VAR path: ARRAY Files.NameLength OF CHAR;
BEGIN
context.arg.Ln (path);
Strings.TrimWS (path);
OpenFile (path, context.error, context.caller);
END Open;
END FileHandlers.
SystemTools.Free FileHandlers ~