{============================================================================} {= =} {= 'GetFiles.Pas' =} {= =} {= Procedures to set up the input and output files. =} {= =} {= 11/29/86 - Bruce Tomlin =} {= =} {============================================================================} {$R+} { Turn on range checking in this module } UNIT GetFiles; INTERFACE USES MacDefs, Files; FUNCTION GetFiles(input, output: Text): BOOLEAN; IMPLEMENTATION {============================================================================} {= =} {= PASFILEFILTER =} {= =} {= Filter procedure to allow only files with '.PAS' as the =} {= last four characters of their filenames to be selected. =} {= =} {= Parameters: =} {= =} {= PB I/O parameter block with information about a file. =} {= =} {= Returns: =} {= =} {= TRUE to skip the file, FALSE to allow the file to be selected =} {= =} {============================================================================} FUNCTION PasFileFilter(pb: ParmBlkPtr): BOOLEAN; VAR extension: Str255; nameLength: INTEGER; BEGIN WITH pb^ DO BEGIN { Get the length of the file name } nameLength := Length(ioNamePtr^); { If the file name has less than four characters, skip it } IF nameLength<4 THEN PasFileFilter := TRUE ELSE BEGIN { Otherwise, get the last four characters of the file name, } { convert them to uppercase, and compare with '.PAS'. } extension := Copy(ioNamePtr^,nameLength-3,4); UprString(extension,TRUE); PasFileFilter := (extension <> '.PAS'); END; END; END; {============================================================================} {= =} {= COPYFILE =} {= =} {= Copies the contents of one file variable to another. =} {= =} {= The reason for this kludge is that TML Pascal does not allow files =} {= to be assigned to each other as in "F1 := F2;" because it might be =} {= unsafe. In the case of the files set up for input from the =} {= keyboard and output to the screen, however, it is safe. =} {= =} {============================================================================} PROCEDURE CopyFile(srcFile: Text; VAR destFile: Text); BEGIN BlockMove(@srcFile,@destFile,SizeOf(Text)); END; {============================================================================} {= =} {= GETFILES =} {= =} {= Allows the user to select an input and an output file, and =} {= redirects the appropriate I/O paths to the selected file(s). =} {= =} {= Parameters: =} {= =} {= INPUT Default keyboard input file =} {= OUTPUT Default screen output file =} {= =} {= Returns TRUE if everything is O.K. =} {= =} {============================================================================} FUNCTION GetFiles(input, output: Text): BOOLEAN; VAR reply: SFReply; { File selection information } oldVol: INTEGER; { Used to save default volume reference number } where: Point; { Where on the screen to call up the SF dialogs } typeList: SFTypeList; { Used to specify which types of files to select } err: OSErr; { Error code returned from GetVol and SetVol } outName: Str255; { Default name of listing output file } BEGIN GetFiles := TRUE; { Nothing has gone wrong yet } { Set up the 'inFile' and 'outFile' to be the keyboard and screen and } { also set up 'kbd' and 'screen' to be the keyboard and the screen. } CopyFile(input,inFile); CopyFile(input,kbd); CopyFile(output,outFile); CopyFile(output,screen); screenOut := TRUE; { Let the user select a file to input from } SetPt(where,75,75); typeList[0] := 'TEXT'; SFGetFile(where,'',@PasFileFilter,1,typeList,NIL,reply); WITH reply DO IF good THEN BEGIN { If a file was selected, open the file for input. } err := GetVol(NIL,oldVol); err := SetVol(NIL,vRefNum); Reset(inFile,fName); { Nasty things happen if the input file can't be opened } err := IOResult; IF err<>noErr THEN BEGIN WriteLn(screen,'Error opening input file, ID = ',err); GetFiles := FALSE; SysBeep(8); END; err := SetVol(NIL,oldVol); { Now that input has been redirected, maybe } { the user wants to redirect the output. } { The output file name should default to } { 'filename.lis' for a file 'filename.pas'. } outName := Concat(Copy(fName,1,Length(fName)-4),'.Lis'); SFPutFile(where,'',outName,NIL,reply); IF good THEN BEGIN { An output file was selected, so open } { it and redirect the output to it. } err := GetVol(NIL,oldVol); err := SetVol(NIL,vRefNum); Rewrite(outFile,fName); screenOut := FALSE; err := SetVol(NIL,oldVol); END; END ELSE GetFiles := FALSE; { The user clicked 'Cancel' } END; END.