home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WHEREIS.ZIP / IPARSEFN.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-10-27  |  6.4 KB  |  159 lines

  1.  
  2. ;   FILENAME: IPARSEFN.ASM
  3. ;
  4. ;   Copyright (c) 1988, 1992 by Borland International, Inc.
  5. ;
  6. ;   DESCRIPTION:  This module implements a routine that parses a file spec.
  7. ;   into its drive, path and file name.
  8. ;
  9. ;   ASSEMBLY INSTRUCTIONS: To assemble this module use the following
  10. ;   TASM command line.
  11. ;
  12. ;       TASM /dMDL=memorymodel iparsefn
  13. ;
  14. ;   'memorymodel' in the above command line may be replaced by TINY, SMALL,
  15. ;   MEDIUM, COMPACT, LARGE or HUGE.
  16.  
  17. %tabsize 4
  18.  
  19. ifndef  MDL
  20.     display "Error: This module requires that you provide a memory model"
  21.     display "    definition on the command line. I.E. /dMDL=SMALL."
  22.     err ; Force a fatal error
  23. else
  24.  
  25.     ideal                   ; Use TASM's Ideal mode
  26. %   model   MDL             ; Define the memory model
  27.  
  28.     global  ParseFilename:proc      ; Declare a public proc
  29.  
  30.     global  FindBytePos:proc        ; Declare extrn proc's
  31.     global  ByteCopy:proc
  32.     global  GetASCIIZStrLen:proc
  33.  
  34.     codeseg
  35.  
  36.  
  37.     proc    ParseFilename
  38.  
  39.     ;   This routine takes a pointer to a FileSpecification and returns three
  40.     ;   pointers to the filenames drive, path and FileSpecification. Note
  41.     ;   that the space to store the drive, path and FName must be allocated
  42.     ;   by the calling routine. The three strings generated are stored as
  43.     ;   pascal strings. That is, with a leading length byte. The original
  44.     ;   FileSpecification spec. is assumed to be stored as an ASCIIZ string.
  45.     ;
  46.     ;   Input
  47.     ;       FileSpecification - Far pointer to the complete FileSpecification
  48.     ;       Drive    - Far pointer to location to store the drive
  49.     ;       Path     - Far pointer to location to store the path
  50.     ;       FName    - Far pointer to location to store the FileSpecification
  51.     ;   Output
  52.     ;       The var parameters Drive, Path and FName point to the parsed
  53.     ;       components of the FileSpecification.
  54.     ;   Calling conventions
  55.     ;       Pascal
  56.     ;   Registers modified
  57.     ;       ax, bl, cx, es, di
  58.  
  59.     arg FName:dword, Path:dword, Drive:dword, FileSpecification:dword
  60.  
  61.         push    bp
  62.         mov     bp, sp
  63.  
  64.         ; First initialize the length bytes of each of the drive and path
  65.  
  66.         les     di, [Drive]
  67.         mov     [byte es:di], 0
  68.         les     di, [Path]
  69.         mov     [byte es:di], 0
  70.         mov     al, ':'             ; Search for the colon indicating a drive
  71.         mov     cx, 3               ; definition
  72.         push    [word FileSpecification+2]   ; Push the address of the FileSpecification
  73.         push    [word FileSpecification]
  74.         mov     si, sp
  75.         inc     [word ss:si]
  76.         call    FindBytePos         ; Look for the ':'
  77.         jcxz    FindEndOfPath
  78.         les     di, [Drive]
  79.         mov     [byte es:di], al    ; Store the length of the drive definition
  80.         xor     ah, ah
  81.         push    [word FileSpecification+2]   ; Push the address of the FileSpecification
  82.         push    [word FileSpecification]
  83.         mov     si, sp
  84.         inc     [word ss:si]
  85.         push    [word Drive+2]      ; Push the address to store the drive
  86.         push    [word Drive]        ; definition in
  87.         inc     [word ss:si-4]
  88.         call    ByteCopy            ; Copy the drive spec.
  89.     FindEndOfPath:
  90.         les     di, [Drive]         ; Get the address of the drive description
  91.         xor     bx, bx
  92.         mov     bl, [byte es:di]    ; Get the length of the drive spec.
  93.         les     di, [FileSpecification] ; Get the address of the FileSpecification
  94.         mov     cl, [byte es:di]    ; Get the length of FileSpecification
  95.         inc     di                  ; Skip length byte
  96.         add     di, bx              ; Move the pointer past the drive spec.
  97.         sub     cl, bl              ; subtract the length of the drive spec.
  98.         push    es                  ; Save the pointer to the start of the path
  99.         push    di
  100.     NextBackSlash:
  101.         push    es                  ; Save the current pointer
  102.         push    di
  103.         push    es                  ; Pass the current pointer to FindBytePos
  104.         push    di
  105.         mov     al, '\'             ; Look for backslash
  106.         call    FindBytePos         ; Find the next '\' character
  107.         jcxz    CopyPath            ; Didn't find a '\'
  108.         pop     bx                  ; Remove the pointer to the last '\' from
  109.         pop     bx                  ; the stack.
  110.         jmp     NextBackSlash
  111.     CopyPath:
  112.         mov     si, sp
  113.         mov     ax, [ss:si]         ; Get offset to last character in path
  114.         mov     bx, [ss:si+4]       ; Get offset to first position of path
  115.         sub     ax, bx              ; Determine the length of the string to copy
  116.         jz      NoPath
  117.         push    [ss:si+6]           ; Push the address of the start of the
  118.         push    [ss:si+4]           ; path for the call to ByteCopy
  119.         push    [word Path+2]       ; Push the destination to copy the path to.
  120.         push    [word Path]
  121.         mov     si, sp
  122.         inc     [word ss:si]        ; Point past the length byte
  123.         call    ByteCopy
  124.         les     di, [Path]
  125.         mov     [byte es:di], al    ; Store the length byte
  126.     NoPath:
  127.  
  128.         ; At this point a pointer to the start of the path and a pointer to
  129.         ; the end of the path(i.e. the start of the FileSpecification) are still
  130.         ; on the stack.
  131.  
  132.         les     di, [FileSpecification] ; Determine the length of the filename
  133.         mov     al, [byte es:di]        ; part of FileSpecification
  134.         les     di, [Path]
  135.         sub     al, [byte es:di]
  136.         les     di, [Drive]
  137.         sub     al, [byte es:di]
  138.  
  139.         ; Now use the pointer to the filename(still on the stack) in the call
  140.         ; to ByteCopy and copy the filename to its storage location.
  141.  
  142.         push    [word FName+2]      ; Push the address to store the filename
  143.         push    [word FName]
  144.         mov     si, sp              ; Skip the length byte in the destination
  145.         inc     [byte ss:si]        ; string
  146.         les     di, [FName]
  147.         mov     [byte es:di], al    ; Store the length byte
  148.         call    ByteCopy
  149.         pop     bx                  ; Claen up the stack
  150.         pop     bx
  151.         pop     bp
  152.         ret                         ; Don't remove any of the parameters from
  153.                                     ; the stack as they are var parameters.
  154.     endp    ParseFilename
  155.  
  156. endif   ; ifndef MDL
  157.  
  158. end
  159.