{\em Resources\/} are arbitrary texts that are associated with
applications. Resources can be bundled into an application
using the m3bundle
facility. They may also be found in the
file system.
This interface supports retrieval of resources using a {\em search
path}. A search path is a list of elements; each element is either
a Pathname.T
that refers to a directory, or a Bundle.T
,
typically created by m3bundle
.
INTERFACERsrc ; IMPORT RefList, Rd, Thread; TYPE Path = RefList.T; (* of Pathname.T or Bundle.T *) EXCEPTION NotFound; PROCEDURE Open (name: TEXT; path: Path): Rd.T RAISES {NotFound};
Ifname
is an absolute pathname, then look forname
in the file system: A reader is returned ifFileRd.Open(name)is successeful; otherwise an exception is raised. Ifname
is not an absolute pathname, then search each element ofpath
, from front to back, for the first occurrence of the resource calledname
and return a reader on the resource. If the path element is a pathnamep
, then a reader is returned ifFileRd.Open(Pathname.Join (p, name, NIL))is successful. If the path element is a bundleb
, a reader is returned ifTextRd.New(Bundle.Get(b, name))is successful. TheNotFound
exception is raised if no element ofpath
yields a successful reader onname
. It is a checked runtime error ifpath
contains an element that is neither a pathname nor a bundle.
PROCEDURE Get (name: TEXT; path: Path): TEXT RAISES {NotFound, Rd.Failure, Thread.Alerted};
A convenience procedure to retrieve the contents of the resourcename
as aTEXT
.
The procedure
Get
is logically equivalent to
VAR rd := Open(name, path); BEGIN TRY RETURN Rd.GetText(rd, LAST(CARDINAL)) FINALLY Rd.Close(rd) END END;The implementation is slightly more efficient, because it takes advantage of
Bundle.Get
procedure which returns the
contents of the bundle element as a TEXT
. The Rd.Failure
exception is raised if Rd.GetText
or Rd.Close
report a
problem. The Thread.Alerted
can be raised by the call to
Rd.GetText
.
PROCEDURE BuildPath (a1, a2, a3, a4: REFANY := NIL): Path;
Build aPath
from the non-NIL
elements. Each element must be either aBundle.T
or aTEXT
. If it is aTEXT
, it is assumed to be the pathname of a directory, unless it starts with a dollar sign, in which case it is assumed to be an environment variable whose value is the name of a directory; the value is retrieved usingEnv.Get
. It is a checked runtime error if the pathname is not valid.
END Rsrc.