Category : Pascal Source Code
Archive   : STDFILES.ZIP
Filename : STDFILES.PAS
{--
Written Jan 1988 by Paul Heitner, Bramalea, Ontario.
Placed in the Public Domain. The author disclaims any
responsibility for possible adverse consequences resulting
from the use of this code.
This routine provides functions to create DOS filespecs with
standard defaults. Use of these functions simplifies the coding
while allowing the end user maximum convenience in specifying
file arguments to programs. Since these functions are only string
manipulators they can be used regardless of how the given string
was obtained by the application (from the command line, typed in
in response to a prompt, selected from a menu, read from a file,
etc.
The returned value is a filespec composed of the concatenation
of path, base, and extension. The default values are used but
any portion specified in the given string replaces the default.
Function secondary_file_name substitutes first the defaults, then
the primary, and then the given.
Examples:
A program JOB normally finds its input file in subdirectory SPECS of
the current directory, uses another input files from subdirectory
CONFIG of the root directory, and writes its output file in subdirectory
RESULTS of the current directory. The usual extension for the first
input file is .SPC; the usual extension for the second input file is
.CFG; the usual extension for the output file is .RES. The default
input filename is TESTJOB. A fully-specified command line for the
standard setup is
JOB specs\testjob.spc \config\testjob.cfg results\testjob.res
The application obtains the first filespec and calls
fname1 := primary_file_name(fspec,'SPECS\','TESTJOB','SPC');
fspec = '' => fname1 = "specs\testjob.spc"
fspec = myjob => fname1 = "specs\myjob.spc"
fspec = d:\mydir\ => fname1 = "d:\mydir\testjob.spc"
fspec = .xxx => fname1 = "specs\testjob.xxx"
secondary_file_name replaces the defaults with the parts of the
specification in the "primary" parameter string. The fully-specified
example could be produced by calling primary_file_name with a null
string for given, as "fspec" in the example above, and then calling
secondary_file_name twice with a null string for given and primary
and appropriate defaults.
The most frequent usage is when the user supplies just the base name,
and the routines are used to generate complete filespecs. A simple
example:
USAGE: convert oldfile [newfile]
The application assumes that the files will be in the current directory,
and that the new file will have the same base name as the old file, and
that the extensions .OLD and .NEW are standard. The code fragment:
CONST
defpath = '';
defbase = '';
defoldx = '.OLD';
defnewx = '.NEW';
VAR
given_infile, given_out_file, infile, outfile : string;
BEGIN
given_infile := paramstr(1);
given_outfile := paramstr(2);
infile := primary_file_name(given_infile, defpath, defbase, defoldx);
outfile := secondary_file_name(given_outfile, defpath, defbase,
defnewx, infile);
If the command line is "convert thing" then infile would end up as
"thing.OLD" and outfile would be "thing.NEW". If the command line
is "convert \sam\joe\table.xxx" then infile would be as specified
and outfile would be "\sam\joe\table.NEW".
NOTE: The default_path parameters need not have a trailing \ since
the functions will supply it if omitted; similarly the default_ext
parameters need not have the leading period. A given or primary
parameter which is ONLY a path, however, must have the trailing \
to distinguish it from a base; similarly for ONLY an extension and
its leading period.
--}
INTERFACE
FUNCTION primary_file_name(given, default_path, default_base,
default_ext:STRING):STRING;
FUNCTION secondary_file_name(given, default_path, default_base,
default_ext, primary:STRING):STRING;
IMPLEMENTATION
FUNCTION primary_file_name(given, default_path, default_base,
default_ext:STRING):STRING;
VAR
path, base, ext : STRING;
j : ShortInt;
BEGIN
path := default_path;
base := default_base;
ext := default_ext;
{allow laxity in provinding \ and . in path and ext}
IF (path <> '') AND (path[Length(path)] <> '\') THEN path := path + '\';
IF ext[1] <> '.' THEN ext := '.' + ext;
IF Length(ext) > 4 THEN ext := Copy(ext,1,4);
{If given has an ext, replace the default}
j := Pos('.',given);
IF j <> 0 THEN BEGIN
ext := Copy(given,j,Length(given) - j + 1);
Delete(given, j, Length(given) - j + 1);
END;
{if given has a path, replace the default}
IF (Pos(':',given) <> 0) OR (Pos('\',given) <> 0) THEN BEGIN
{scan from right for end of path}
j := Length(given);
WHILE (given[j] <> ':') AND (given[j] <> '\') DO Dec(j);
path := Copy(given,1,j);
Delete(given,1,j);
END;
{If given has a base, replace default}
IF given <> '' THEN base := given;
IF ext = '.' THEN ext := ''; {delete trailing period}
primary_file_name := path + base + ext;
END;
FUNCTION secondary_file_name(given, default_path, default_base,
default_ext, primary:STRING):STRING;
VAR
path, base, ext : STRING;
j : ShortInt;
BEGIN
path := default_path;
base := default_base;
ext := default_ext;
{allow laxity in provinding \ and . in path and ext}
IF (path <> '') AND (path[Length(path)] <> '\') THEN path := path + '\';
IF ext[1] <> '.' THEN ext := '.' + ext;
IF Length(ext) > 4 THEN ext := Copy(ext,1,4);
{If primary has an ext, discard it}
j := Pos('.',primary);
IF j <> 0 THEN BEGIN
Delete(primary, j, Length(primary) - j + 1);
END;
{if primary has a path, replace the default}
IF (Pos(':',primary) <> 0) OR (Pos('\',primary) <> 0) THEN BEGIN
{scan from right for end of path}
j := Length(primary);
WHILE (primary[j] <> ':') AND (primary[j] <> '\') DO Dec(j);
path := Copy(primary,1,j);
Delete(primary,1,j);
END;
{If primary has a base, replace default}
IF primary <> '' THEN base := primary;
IF ext = '.' THEN ext := ''; {delete trailing period}
{If given has an ext, replace the default}
j := Pos('.',given);
IF j <> 0 THEN BEGIN
ext := Copy(given,j,Length(given) - j + 1);
Delete(given, j, Length(given) - j + 1);
END;
{if given has a path, replace the default}
IF (Pos(':',given) <> 0) OR (Pos('\',given) <> 0) THEN BEGIN
{scan from right for end of path}
j := Length(given);
WHILE (given[j] <> ':') AND (given[j] <> '\') DO Dec(j);
path := Copy(given,1,j);
Delete(given,1,j);
END;
{If given has a base, replace default}
IF given <> '' THEN base := given;
IF ext = '.' THEN ext := ''; {delete trailing period}
secondary_file_name := path + base + ext;
END;
END.
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/