home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
spoc88
/
tbcoml
/
param.bas
< prev
next >
Wrap
BASIC Source File
|
1988-05-08
|
4KB
|
126 lines
'
' Author: Duke Kamstra
' Mod. Date: 5/8/88
'
'
' To use these routines in your own program, keep them in an
' include file. When you need to manage command line parameters
' in a program include these routines by inserting the
' metastatement:
' $INCLUDE "PARAM"
' in your program. Be sure to set the named constant
' %MAXPARAMETERS appropriately for your application. If the
' number of parameters given on the command line is larger
' than %MAXPARAMETERS the extras are ignored.
%MAXPARAMETERS = 25 ' Maximum # of parameters that can be read by the
' program. Should never be larger than 64 since
' DOS only allows a 127 character command line.
DIM Parameters$(0:%MAXPARAMETERS) ' String array used to store
' parameters
%TRUE = 1 ' Named constant representing boolean value
DEF FNParamCount%
' Return the number of command line parameters passed to the program.
' Store each of the parameters in the SHARED string array
' Parameters$(). Note the function will only process up to
' %MAXPARAMETERS command line parameters.
'
' The first time the function is called is processes the parameter
' list and sets a flag Initialized% to indicate that the command
' line doesn't need to be processed again. Any subsequent calls to
' the function will return the value stored in Result%.
STATIC Initialized% ' Flag indicating parameters have been read
' and data structure has been initialized.
STATIC Result% ' Store result after calling the function
' the first time
SHARED Parameters$() ' Global variable to store parameter data
LOCAL I%, J%, Count%, ParamPos%(), SearchChar$
%L = 0 ' Named constants used to reference ParamPos%
%R = 1
DIM ParamPos%(0:%MAXPARAMETERS, %L:%R) ' Make room for position
' information
IF Initialized% <> %TRUE THEN ' We haven't parsed the command
' line yet
' Set flag indicating we've parsed the command line
Initialized% = %TRUE
IF COMMAND$ = "" THEN ' No command line parameters specified
FNParamCount% = 0 ' Return 0 for parameter count
Result% = 0 ' Save parameter count in static variable
EXIT DEF ' Leave the function
ELSE ' At least one command line parameter was specified
' First we need to determine the number of parameters
I% = 1
WHILE (I% <= LEN(COMMAND$)) AND (Count% < %MAXPARAMETERS)
Count% = Count% + 1 ' Increment parameter counter
ParamPos%(Count%, %L) = I% ' Store left position of parameter
' Determine what to search for as the end of the current
' parameter
IF MID$(COMMAND$,I%,1) = CHR$(34) THEN
' Parameter is enclosed in double quotes
SearchChar$ = CHR$(34)
ParamPos%(Count%, %L) = _ ' we don't want the "
ParamPos%(Count%, %L) + 1
ELSE
SearchChar$ = " " ' look for a space
END IF
' Check if the next character in the command line terminates
' the current parameter
IF INSTR(I%+1,COMMAND$,SearchChar$) <> 0 THEN
' find end of parameter
I% = INSTR(I%+1,COMMAND$,SearchChar$)
' Store right position of parameter
ParamPos%(Count%, %R) = I%
' Advance past the "
IF SearchChar$ = CHR$(34) THEN I% = I% + 1
ELSE
' Store right position of parameter
ParamPos%(Count%, %R) = LEN(COMMAND$) + 1
EXIT LOOP
END IF
WHILE MID$(COMMAND$,I%,1) = " " AND I% < LEN(COMMAND$)
I% = I% + 1 ' now find the start of the next parameter
WEND
WEND
' next we need to store the parameters in our SHARED string
' array
FOR J% = 1 TO Count% ' Store each of the parameters
Parameters$(J%) = MID$(COMMAND$, ParamPos%(J%, %L), _
ParamPos%(J%, %R) - ParamPos%(J%, %L))
NEXT J%
FNParamCount% = Count%
Result% = Count%
END IF ' COMMAND$ = ""
ELSE ' The function has already been called once
FNParamCount% = Result%
END IF
END DEF ' FNParamCount%
DEF FNParamStr$(Count%)
' Return the command line parameter indexed by Count%. The function
' verifies that the parameter exists by calling FNParamCount%(). If
' the parameter exists it is read from the global SHARED array
' Parameter$() and returned.
SHARED Parameters$() ' Global variable to store parameter data
LOCAL ParmCount%
IF Count% <= FNParamCount% THEN ' Check to make sure parameter
FNParamStr$ = Parameters$(Count%) ' exists
ELSE
FNParamStr$ = ""
END IF
END DEF ' FNParamStr$()