home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / BASIC / PROGNAME / PROGNAME.BAS next >
BASIC Source File  |  1994-01-17  |  4KB  |  84 lines

  1.  
  2. $COMPILE UNIT
  3. $DEBUG UNIT OFF
  4. $DEBUG PATH OFF
  5. DECLARE FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??)
  6.  
  7. 'Ray Crumrine
  8. '****************************************************************************
  9. 'DECLARE FUNCTION ProgName$()
  10.  
  11. 'This function returns the complete path to, and program name of the
  12. 'currently running program, obtained from DOS.  For example, if the
  13. 'program resides in the "MYDIRECT" directory on the D: drive and the program
  14. 'name was PROGRAM.EXE, then ProgName$ would return "D:\MYDIRECT\PROGRAM.EXE"
  15. 'The path information is useful if you need to find a configuration file
  16. 'associated with PROGRAM.EXE or something like that.
  17. 'Note that this function requires DOS V3.0 or higher to obtain the information
  18. 'If you try to use ProgName$ on a lesser version of DOS, it will return
  19. 'an empty string as an indicator.
  20. '****************************************************************************
  21. FUNCTION ProgName$ LOCAL PUBLIC
  22. LOCAL sgmt??                      'Define this for the assembler
  23.  
  24. ! Mov  ah,&h30                    ;Get DOS version
  25. ! Int  &h21
  26. ! Cmp  al,3                       ;Is it version 3 or greater?
  27. ! Jge  Label1                     ;Yes, continue at Label1
  28. ! Jmp  near Label2                ;No, return an empty string
  29.                                    
  30. Label1:
  31. ! Mov  ah,&h62                    ;Get the segment address of the PSP
  32. ! Int  &h21
  33. ! Mov  es,bx                      ;DOS returns it in bx - copy to es
  34. ! Mov  ax,es:[&h2C]               ;The offset to the environment segment is 2Ch
  35. ! Mov  sgmt??,ax                  ;Save it for PB to use
  36.  
  37.   SaveSeg?? = PbvDefSeg           'Save PB's current setting on the stack
  38.   DEF SEG = sgmt??                'Point PB to the environment segment
  39.                                    
  40.   offset?? = 0                    'start at the beginning of the environment seg
  41.   DO UNTIL PEEKI(offset??) = 0    'keep searching till we find two consecutive
  42.     INCR offset??                 'zero bytes.  Increment offset ONE byte at a
  43.   LOOP                            'time.
  44.   INCR offset??, 4                'skip over the next four bytes
  45.  
  46.   length% = StrLen%(sgmt??, offset??)  'Get the length of the string
  47.   Tstr$ = PEEK$(offset??, length%)     'Have PB copy it into it's string space
  48.   DEF SEG = SaveSeg??                  'Restore PB's segment setting
  49.  
  50.   IF ISFALSE(INSTR(Tstr$,":")) THEN    'Did we get the drive letter?
  51.     Tstr$ = LEFT$(CURDIR$,2) + Tstr$   'No, so ask PB for it
  52.   END IF
  53.  
  54. Label2:
  55. ProgName$ = Tstr$
  56. END FUNCTION
  57.  
  58. '****************************************************************************
  59. 'DECLARE FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??)
  60.  
  61. 'This function returns the length of any AsciiZ string that DOS might send to
  62. 'a PowerBASIC program.  DOS stores a string as a series of bytes that is
  63. 'terminated with a CHR$(0).  If you want to use these strings you must
  64. 'normally copy them into PowerBASIC's string space.  In order to use PEEK$ you
  65. 'need to know the length of the string.  That is what StrLen% does.  Given the
  66. 'segment and offset of the AsciiZ string, it searches DOS memory space till it
  67. 'finds the zero byte and returns the length of the string.  You can then use
  68. 'this with PEEK$ to copy the string into PowerBASIC's space.
  69. '****************************************************************************
  70. FUNCTION StrLen% (BYVAL sgmt??, BYVAL ofset??) PUBLIC
  71. LOCAL result%                  'define this for the assembler
  72. ! Push di                      ;save di
  73. ! Mov  ax,sgmt??               ;copy the segment to es using ax
  74. ! Mov  es,ax
  75. ! Mov  di,ofset??              ;the offset needs to be in di for scasb to work
  76. ! Mov  cx,&hFFFF               ;search up to 65535 bytes
  77. ! Xor  al,al                   ;search for a zero
  78. ! Repne scasb                  ;go for it
  79. ! Not  cx                      ;inverting the bits tells how many bytes it found
  80. ! Dec  cx                      ;don't count the zero byte
  81. ! Pop  di                      ;restore di
  82. ! Mov  result%,cx              ;PowerBASIC needs this for an ASM FUNCTION
  83. StrLen% = result%              'assign the function return
  84. END FUNCTION