home *** CD-ROM | disk | FTP | other *** search
- { LINES BETWEEN ******* ARE A FRAME FOR TESTING -- INCLUDE "VALIDFIL.BOX" IN YOUR PROGRAM }
- {***********************************************************************************************************}
-
- program TESTIT ;
-
- type
- TempStr = String [14] ;
- var
- FileFlag : Boolean ;
- TempName : TempStr ;
-
- {***********************************************************************************************************}
-
- { function CheckFileName (Name : NameString) : Boolean ; }
-
- (* A TURBO PASCAL function to parse a DOS filename for validity. The function accepts a string of
- maximum length 14 containing a file name with (optional) extension and/or (optional) drive
- designator. The function returns a Boolean value, TRUE if the file name is valid, FALSE if
- it is not. Example:
-
- FileFlag := CheckFileName ('B:$TEMP_FL.{%}') ;
- FileFlag := CheckName (TempName) ;
-
- where FileFlag is a variable defined as Boolean. Note that the string length of the parameter passed to
- the function (in this case "TempName") need not match the string length of the formal parameter of the
- function. The parameter is passed by value rather than by variable. However, it the string passed exceeds
- 40 characters in length, it will be truncated upon passage.
-
- If there is an error an explanatory message is presented on the screen.
-
- This function is valid under all TURBO Versions.
-
- Bruce Cameron
- 4067 Rose Hill
- Cincinnati OH 45229
-
- (On FIDO 108/10) *)
-
- type
- NameString = String [40] ; { String to contain input file name }
-
- const
- HighestValidDrive : Char = 'B' ; { The function checks for validity of the drive designator if one
- is included, a test which also tests if the drive is in the system.
- This test compares the drive designator specified with the constant
- "HighestValidDrive" as defined here. Before use in your system,
- this constant must be set correctly, either by changing this line,
- or by assigning the correct value to this typed constant within
- your code }
-
- function CheckFileName (Name : NameString) : Boolean ;
- { Check a file name for validity; i.e., characters are valid for a file
- name, drive and/or name and/or extension all individually of valid
- length. Valid characters for a filename are the complete set as
- defined under DOS 2.x. If you wish to use this routine for a more
- restricted set of valid characters, redefine the constant "FileChars"
- to your specifications. NOTE: this function examines file names only
- and does not parse a PATH for validity. }
-
- type
- Valid = Set of Char ;
-
- const
- Colon = ':' ;
- Period = '.' ;
- FileChars : Valid = ['@'..'Z', '0'..'9', '!', '#'..')', '-', '_', '`', '{', '}'] ;
- { Define complete set of valid characters in a DOS filename, as specified for DOS 2.x }
- Blank = ' ' ;
-
- type
- GenStr = String [60] ; { String for error messages }
-
- var
- NameChars : Valid ; { Set of characters in the name passed to the function }
- OK : Boolean ; { Result flag of test }
- I : Byte ; { Index }
- AllBlanks : Boolean ; { Flag, name string all blanks }
-
- procedure BlankStrip (var Str : NameString) ; { Strips leading and/or trailing blanks }
-
- var
- I, J : Byte ; { Indices }
-
- begin
- repeat { Close up over any leading blanks }
- for I := 1 to Length (Str) do if Str [I] = Blank then for J := (I + 1) to Length (Str) do Str [J - 1] := Str [J] ;
- until Str [1] <> Blank ;
- J := Length (Str) ; { Copy leaving out any trailing blanks }
- if J > 14 then for I := J downto 14 do if Str [I] = Blank then Str := Copy (Str, 1, (I - 1)) ;
- end ;
-
- procedure FormSet (var ChSet : Valid ; Name : NameString) ; { Form set of characters in the filename }
-
- var
- I : 1..14 ; { Index }
- Ch : Char ; { Individual character in the name }
-
- begin
- ChSet := [] ; { Initialize the set empty }
- for I := 1 to Length (Name) do begin { For number of characters in the name }
- Ch := Name[I] ;
- ChSet := ChSet + [Ch] ; { Place them into the name character set }
- end ;
- end ;
-
-
- procedure NameErr (var NameErr : Boolean ; Errms : GenStr) ; { Error message if filename in error }
-
- begin
- NameErr := False ; { Set function return flag, error }
- Writeln (Output) ;
- Write (Output, ^G) ; { Beep the speaker }
- Writeln (Output, Errms) ;
- end ;
-
- procedure ParseName (ChSet : Valid ; Name : NameString ; var Flag : Boolean) ;
-
- var
- TStr : NameString ; { Temporary name string }
-
- procedure CheckExt (Name : NameString ; var Errf : Boolean) ; { Check name and extension lengths }
-
- var
- I, Len : Byte ; { Position / length parameters of name }
- MSvar : GenStr ; { Specific error message identifying text }
-
- begin
- I := Pos (Period, Name) ; { Where is '.' in the name }
- Len := Length (Name) ; { How long is the name }
- { If '.' is beyond the 9th character then the name is too long; if '.' is
- closer to the end than 3 then the extension is too long }
- if ((Len - I) > 3) or (I > 9) then begin
- if I > 9 then MSvar := 'name' else MSvar := 'extension' ;
- NameErr (Errf, 'Invalid length, ' + MSvar) ;
- end ;
- end ;
-
- begin { PROC ParseName }
- Flag := True ; { Set file name flag valid }
- if Colon in ChSet then begin { Name includes a drive designator }
- if Name[2] <> Colon then NameErr (Flag, 'Invalid drive specifier') ; { The ':' must be in position 2 }
- { Check that drive requested is actually in the system }
- if not (Name[1] in ['A'..HighestValidDrive]) then NameErr (Flag, 'Drive specified not in system') ;
- Tstr := Copy (Name, 3, 14) ; { Copy, stripping out drive ID }
- end
- else Tstr := Copy (Name, 1, 14) ; { No drive, copy entire name }
- if Period in ChSet then CheckExt (Tstr, Flag) { Name includes an extension }
- { If no extension, name must not exceed 8 characters }
- else if Length (Tstr) > 8 then NameErr (Flag, 'Invalid length, name') ;
- end ;
-
-
- begin { FUNC CheckFileName }
- AllBlanks := True ; { Set flag, default name is all blanks }
- { Check name, if any character not a blank then flag reset }
- for I := 1 to Length (Name) do if Name [I] <> Blank then AllBlanks := False ;
- if not AllBlanks then begin { Name exists, process }
- BlankStrip (Name) ; { Clear leading and/or trailing blanks }
- if Length (Name) > 14 then NameErr (OK, 'Input name string too long')
- else begin { Within maximum possible valid length of 14 }
- { Add to valid set the characters for drive and extension separators, ':' and '.' }
- FileChars := FileChars + [Colon] + [Period] ;
- FormSet (NameChars, Name) ; { Form set of characters in the name }
- if NameChars - FileChars <> [] then NameErr (OK, 'Invalid characters in name') { Any invalid, error }
- else ParseName (NameChars, Name, OK) ; { All valid, check for length, valid drive }
- end ;
- end
- else NameErr (OK, 'Non-existent name, all blanks') ;
- CheckFileName := OK ; { Return result }
- end ;
- { The return flag is set TRUE (valid) within the procedure "ParseName"; it there is
- any error, it is set FALSE (invalid) by execution of "NameErr" }
-
- {***********************************************************************************************************}
-
- begin
- HighestValidDrive := 'D' ;
- FileFlag := CheckFileName ('B:$TEMP_FL.{%}') ;
- Writeln (FileFlag) ;
- TempName := 'E:VARFIL.1' ;
- FileFlag := CheckFileName (TempName) ;
- Writeln (FileFlag) ;
- FileFlag := CheckFileName ('A:PATH\FIL.EXT') ;
- Writeln (FileFlag) ;
- FileFlag := CheckFileName ('FILE.EXTBAD') ;
- Writeln (FileFlag) ;
- FileFlag := CheckFileName (' B:MAXIFILE.EXT ') ;
- Writeln (FileFlag) ;
- FileFlag := CheckFileName (' ') ;
- Writeln (FileFlag) ;
- end .
-
- {***********************************************************************************************************}