perm filename FILSCN.SAI[LIB,AIL] blob sn#408157 filedate 1979-01-09 generic text, type T, neo UTF8
entry FileScan; 	 	comment LoaderAliases;
 
begin "Filscn"
 
define library!entry=true;
require "SYS:STD.HDR" source!file;
 
 
 
 
!*** FILESCAN: parses TOPS10 filenames and returns true if the name makes
	      sense. If Wild is true then wildcard filespecs are accepted.
	Note: Device is always set to something. "DSK" or whatever the inp
		string specifies so that one can directly use it for OPEN.
	      All blanks in the input str are ignored;
 
internal simple boolean procedure FileScan(string FileName;
		reference string Device,Name,Ext,Path;
		boolean Wild(false));
begin "FileScan"
 
  integer Brk,BrChar,I;
  string Str;
  label GetName,GetExt,GetPath;
 
  Device←Name←Ext←Path←Null;
 
  if length(FileName)=0 then
    if Wild then
      begin
	Device←"DSK";
	return(true);
      end
    else
      return(false);
 

  Brk←getbreak;
  setbreak(brk, null, " " & tab, "IS");
  FileName←scan(FileName, Brk, BrChar);         ! squeeze out tabs and spaces;

  Str←"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!#%$";
  if Wild then Str←Str & "?*";
  setbreak(Brk,Str,null,"SX");  
 
  Device←scan(FileName,Brk,BrChar);
 
  if length(Device)=0 then
    begin
      if Wild then
	begin
	  Device←"DSK";
	  if BrChar="." then goto GetExt
	  else if BrChar="[" then goto GetPath
	  else if BrChar=0 then 		! filename="  ";
	    begin
	      relbreak(Brk);
	      return(true);
	    end;
	end;
      relbreak(Brk);
      return(false);
    end;
 
! if length(Device) > 6 then Device←Device[1 for 6];
 
  if length(FileName)=0 then
    begin
      relbreak(Brk);
      if BrChar=":" then
	return(Wild);			! OK only if Wild is true;
 
 
      if BrChar neq 0 and BrChar neq "." then
	return(false);
 
      Name←Device;
      Device←"DSK";
      return(true);
    end;
 
  ! More filename to come;
 
  if BrChar="." then
    begin
      Name←Device;
      Device←"DSK";
      goto GetExt;
    end;
 
  if BrChar="[" then
    begin
      Name←Device;
      Device←"DSK";
      goto GetPath;
    end;
 
  if BrChar Neq ":" then
    return(false);
 
GetName:
  Name←scan(FileName,Brk,BrChar);
 
  if length(Name)=0 and not Wild then
    begin
      relbreak(Brk);
      return(false);
    end;
 
  if length(FileName)=0 then
    begin
      relbreak(Brk);
      return(BrChar=0 or BrChar=".");
    end;
 
  if BrChar="." then
    goto GetExt;
 
  if BrChar="[" then
    goto GetPath;
 
  ! Illegal break character;
 
  relbreak(Brk);
  return(false);
 
GetExt:
  Ext←scan(FileName,Brk,BrChar);
 
  if length(FileName)=0 then		! can move relbreak out;
    begin
      relbreak(Brk);
      return(BrChar=0);
    end;
 
  if BrChar="[" then goto GetPath;
 
  relbreak(Brk);
  return(false);
 
 
GetPath:
  Path←"[";
  for I←1 step 1 until 16 do
    begin
      Str←scan(FileName,Brk,BrChar);
 
      if length(FileName)=0 then
	begin
	  relbreak(Brk);

          if I > 1 and          !*** I > 1 disallows "[", "[]", "[170", "170]" etc.;
             (BrChar=0 Or BrChar="]") then
	    begin
	      Path←Path & Str & "]";
	      return(true);
	    end;
 
	  if BrChar="," then
	    begin
	      Path←Path & Str & ",]";
	      return(true);
	    end;
 
	  return(false);
	end;
 
      if BrChar Neq "," then
	begin
	  relbreak(Brk);
	  return(false);
	end;
 
      Path←Path & Str & ",";
    end;
 
  ! Too many SFDs		Bet you   no one ever gets here;
 
  relbreak(Brk);
  return(false);
 
end "FileScan";
 
end "Filscn";