FileUtil.Mesa
Last Modified By kap&wsh February 22, 1980 1:24 PM
Last Modified By Paul Rovner On October 12, 1983 0:13 am
DIRECTORY
Ascii,
Rope USING[ROPE, Concat, Fetch, Size, Equal, Substr, FromChar],
IO USING[STREAM, GetChar],
FileUtilDefs,
FS USING [FileInfo, Error];
FileUtil: PROGRAM
IMPORTS IO, FS, Rope
EXPORTS FileUtilDefs
= BEGIN
Exported procedures (public)
UpperCase: PUBLIC PROC[ch: CHARACTER] RETURNS[CHARACTER] =
{SELECT ch FROM
IN ['a..'z] => RETURN[ch - 'a + 'A];
ENDCASE => RETURN[ch]};
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FileExists: PUBLIC PROC [fileName: Rope.ROPE] RETURNS [ans: BOOLTRUE] = {
[] ← FS.FileInfo[fileName ! FS.Error => {ans ← FALSE; CONTINUE}];
};
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
If name has an extension, and it is not the desired one, result is
FALSE. If the extension is the one desired, result is TRUE. If the
name has no extension, tack the desired one on and return TRUE.
ExtendFileName: PUBLIC PROC [name, extension: Rope.ROPE]
RETURNS [extendedName: Rope.ROPE, extended: BOOLEAN] =
{s: INT = Rope.Size[name];
FOR i: INT IN [0 .. s)
DO IF Rope.Fetch[name,i] = '. THEN -- the name has an extension already
RETURN[name, Rope.Equal[Rope.Substr[name, i + 1, s - (i + 1)],
extension]];
ENDLOOP;
here if the name has no extension; tack on the desired one
IF name = NIL
THEN RETURN[NIL, FALSE]
ELSE RETURN[Rope.Concat[Rope.Concat[name, "."], extension], TRUE]};
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Snarf "words" (terminated by SP, TAB or CR) from s until either
one is found which has (or can be extended with) the
indicated extension or s is exhausted. '/...' word suffixes are stuck into 'switches'
name = NIL => none found. Otherwise, name is the
(extended) file name.
GetExtendedFileName: PUBLIC PROC [s: IO.STREAM,
extensionName: Rope.ROPE,
extensionRequired: BOOLEANTRUE]
RETURNS[name, switches: Rope.ROPE] =
{ letter: CHARACTER;
name ← switches ← NIL;
letter ← IO.GetChar[s ! ANY => GOTO failure];
WHILE letter = Ascii.SP
OR letter = Ascii.TAB
OR letter = Ascii.CR
DO letter ← IO.GetChar[s ! ANY => GOTO failure] ENDLOOP;
skip leading spaces, tabs, and CRs
WHILE letter # Ascii.SP
AND letter # Ascii.TAB
AND letter # Ascii.CR
DO -- get the next name
IF letter = '/ THEN
{letter ← IO.GetChar[s ! ANY => GOTO out];
UNTIL letter = Ascii.SP
OR letter = Ascii.TAB
OR letter = Ascii.CR
DO
switches ← Rope.Concat[switches, Rope.FromChar[letter]];
letter ← IO.GetChar[s ! ANY => GOTO out];
ENDLOOP;
EXIT};
name ← Rope.Concat[name, Rope.FromChar[letter]];
letter ← IO.GetChar[s ! ANY => GOTO out];
ENDLOOP;
GOTO out;
EXITS
out => {extended: BOOLEANFALSE;
IF extensionName # NIL
THEN [name, extended] ← ExtendFileName[name, extensionName];
IF extensionRequired AND NOT extended THEN name ← switches ← NIL};
failure => NULL}; -- no filename found
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IsSwitch: PUBLIC PROC [ch: CHARACTER, switches: Rope.ROPE] RETURNS[BOOLEAN] =
{FOR i: INT IN [0..Rope.Size[switches]) DO
IF UpperCase[ch] = UpperCase[Rope.Fetch[switches,i]] THEN RETURN[TRUE] ENDLOOP;
RETURN[FALSE]};
END.