home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
BASIC
/
PROGNAME
/
PROGNAME.BAS
next >
Wrap
BASIC Source File
|
1994-01-17
|
4KB
|
84 lines
$COMPILE UNIT
$DEBUG UNIT OFF
$DEBUG PATH OFF
DECLARE FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??)
'Ray Crumrine
'****************************************************************************
'DECLARE FUNCTION ProgName$()
'This function returns the complete path to, and program name of the
'currently running program, obtained from DOS. For example, if the
'program resides in the "MYDIRECT" directory on the D: drive and the program
'name was PROGRAM.EXE, then ProgName$ would return "D:\MYDIRECT\PROGRAM.EXE"
'The path information is useful if you need to find a configuration file
'associated with PROGRAM.EXE or something like that.
'Note that this function requires DOS V3.0 or higher to obtain the information
'If you try to use ProgName$ on a lesser version of DOS, it will return
'an empty string as an indicator.
'****************************************************************************
FUNCTION ProgName$ LOCAL PUBLIC
LOCAL sgmt?? 'Define this for the assembler
! Mov ah,&h30 ;Get DOS version
! Int &h21
! Cmp al,3 ;Is it version 3 or greater?
! Jge Label1 ;Yes, continue at Label1
! Jmp near Label2 ;No, return an empty string
Label1:
! Mov ah,&h62 ;Get the segment address of the PSP
! Int &h21
! Mov es,bx ;DOS returns it in bx - copy to es
! Mov ax,es:[&h2C] ;The offset to the environment segment is 2Ch
! Mov sgmt??,ax ;Save it for PB to use
SaveSeg?? = PbvDefSeg 'Save PB's current setting on the stack
DEF SEG = sgmt?? 'Point PB to the environment segment
offset?? = 0 'start at the beginning of the environment seg
DO UNTIL PEEKI(offset??) = 0 'keep searching till we find two consecutive
INCR offset?? 'zero bytes. Increment offset ONE byte at a
LOOP 'time.
INCR offset??, 4 'skip over the next four bytes
length% = StrLen%(sgmt??, offset??) 'Get the length of the string
Tstr$ = PEEK$(offset??, length%) 'Have PB copy it into it's string space
DEF SEG = SaveSeg?? 'Restore PB's segment setting
IF ISFALSE(INSTR(Tstr$,":")) THEN 'Did we get the drive letter?
Tstr$ = LEFT$(CURDIR$,2) + Tstr$ 'No, so ask PB for it
END IF
Label2:
ProgName$ = Tstr$
END FUNCTION
'****************************************************************************
'DECLARE FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??)
'This function returns the length of any AsciiZ string that DOS might send to
'a PowerBASIC program. DOS stores a string as a series of bytes that is
'terminated with a CHR$(0). If you want to use these strings you must
'normally copy them into PowerBASIC's string space. In order to use PEEK$ you
'need to know the length of the string. That is what StrLen% does. Given the
'segment and offset of the AsciiZ string, it searches DOS memory space till it
'finds the zero byte and returns the length of the string. You can then use
'this with PEEK$ to copy the string into PowerBASIC's space.
'****************************************************************************
FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??) PUBLIC
LOCAL result% 'define this for the assembler
! Push di ;save di
! Mov ax,sgmt?? ;copy the segment to es using ax
! Mov es,ax
! Mov di,ofset?? ;the offset needs to be in di for scasb to work
! Mov cx,&hFFFF ;search up to 65535 bytes
! Xor al,al ;search for a zero
! Repne scasb ;go for it
! Not cx ;inverting the bits tells how many bytes it found
! Dec cx ;don't count the zero byte
! Pop di ;restore di
! Mov result%,cx ;PowerBASIC needs this for an ASM FUNCTION
StrLen% = result% 'assign the function return
END FUNCTION