home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / iparsefn.asm < prev    next >
Assembly Source File  |  1988-08-28  |  7KB  |  158 lines

  1.  
  2. ;   FILENAME: IPARSEFN.ASM
  3. ;
  4. ;   Copyright (c) 1988 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.     codeseg
  29.  
  30.     global  ParseFilename:proc      ; Declare a public proc
  31.  
  32.     global  FindBytePos:proc        ; Declare an extrn proc's
  33.     global  ByteCopy:proc
  34.     global  GetASCIIZStrLen:proc
  35.  
  36.     proc    ParseFilename
  37.  
  38.     ;   This routine takes a pointer to a FileSpecification and returns three
  39.     ;   pointers to the filenames drive, path and FileSpecification. Note
  40.     ;   that the space to store the drive, path and FName must be allocated
  41.     ;   by the calling routine. The three strings generated are stored as
  42.     ;   pascal strings. That is, with a leading length byte. The original
  43.     ;   FileSpecification spec. is assumed to be stored as an ASCIIZ string.
  44.     ;
  45.     ;   Input
  46.     ;       FileSpecification - Far pointer to the complete FileSpecification
  47.     ;       Drive    - Far pointer to location to store the drive
  48.     ;       Path     - Far pointer to location to store the path
  49.     ;       FName    - Far pointer to location to store the FileSpecification
  50.     ;   Output
  51.     ;       The var parameters Drive, Path and FName point to the parsed
  52.     ;       components of the FileSpecification.
  53.     ;   Calling conventions
  54.     ;       Pascal
  55.     ;   Registers modified
  56.     ;       ax, bl, cx, es, di
  57.  
  58.     arg FName:dword, Path:dword, Drive:dword, FileSpecification:dword
  59.  
  60.         push    bp
  61.         mov     bp, sp
  62.  
  63.         ; First initialize the length bytes of each of the drive and path
  64.  
  65.         les     di, [Drive]
  66.         mov     [byte es:di], 0
  67.         les     di, [Path]
  68.         mov     [byte es:di], 0
  69.         mov     al, ':'             ; Search for the colon indicating a drive
  70.         mov     cx, 3               ; definition
  71.         push    [word FileSpecification+2]   ; Push the address of the FileSpecification
  72.         push    [word FileSpecification]
  73.         mov     si, sp
  74.         inc     [word ss:si]
  75.         call    FindBytePos         ; Look for the ':'
  76.         jcxz    FindEndOfPath
  77.         les     di, [Drive]
  78.         mov     [byte es:di], al    ; Store the length of the drive definition
  79.         xor     ah, ah
  80.         push    [word FileSpecification+2]   ; Push the address of the FileSpecification
  81.         push    [word FileSpecification]
  82.         mov     si, sp
  83.         inc     [word ss:si]
  84.         push    [word Drive+2]      ; Push the address to store the drive
  85.         push    [word Drive]        ; definition in
  86.         inc     [word ss:si-4]
  87.         call    ByteCopy            ; Copy the drive spec.
  88.     FindEndOfPath:
  89.         les     di, [Drive]         ; Get the address of the drive description
  90.         xor     bx, bx
  91.         mov     bl, [byte es:di]    ; Get the length of the drive spec.
  92.         les     di, [FileSpecification] ; Get the address of the FileSpecification
  93.         mov     cl, [byte es:di]    ; Get the length of FileSpecification
  94.         inc     di                  ; Skip length byte
  95.         add     di, bx              ; Move the pointer past the drive spec.
  96.         sub     cl, bl              ; subtract the length of the drive spec.
  97.         push    es                  ; Save the pointer to the start of the path
  98.         push    di
  99.     NextBackSlash:
  100.         push    es                  ; Save the current pointer
  101.         push    di
  102.         push    es                  ; Pass the current pointer to FindBytePos
  103.         push    di
  104.         mov     al, '\'             ; Look for backslash
  105.         call    FindBytePos         ; Find the next '\' character
  106.         jcxz    CopyPath            ; Didn't find a '\'
  107.         pop     bx                  ; Remove the pointer to the last '\' from
  108.         pop     bx                  ; the stack.
  109.         jmp     NextBackSlash
  110.     CopyPath:
  111.         mov     si, sp
  112.         mov     ax, [ss:si]         ; Get offset to last character in path
  113.         mov     bx, [ss:si+4]       ; Get offset to first position of path
  114.         sub     ax, bx              ; Determine the length of the string to copy
  115.         jz      short NoPath
  116.         push    [ss:si+6]           ; Push the address of the start of the
  117.         push    [ss:si+4]           ; path for the call to ByteCopy
  118.         push    [word Path+2]       ; Push the destination to copy the path to.
  119.         push    [word Path]
  120.         mov     si, sp
  121.         inc     [word ss:si]        ; Point past the length byte
  122.         call    ByteCopy
  123.         les     di, [Path]
  124.         mov     [byte es:di], al    ; Store the length byte
  125.     NoPath:
  126.  
  127.         ; At this point a pointer to the start of the path and a pointer to
  128.         ; the end of the path(i.e. the start of the FileSpecification) are still
  129.         ; on the stack.
  130.  
  131.         les     di, [FileSpecification] ; Determine the length of the filename
  132.         mov     al, [byte es:di]        ; part of FileSpecification
  133.         les     di, [Path]
  134.         sub     al, [byte es:di]
  135.         les     di, [Drive]
  136.         sub     al, [byte es:di]
  137.  
  138.         ; Now use the pointer to the filename(still on the stack) in the call
  139.         ; to ByteCopy and copy the filename to its storage location.
  140.  
  141.         push    [word FName+2]      ; Push the address to store the filename
  142.         push    [word FName]
  143.         mov     si, sp              ; Skip the length byte in the destination
  144.         inc     [byte ss:si]        ; string
  145.         les     di, [FName]
  146.         mov     [byte es:di], al    ; Store the length byte
  147.         call    ByteCopy
  148.         pop     bx                  ; Claen up the stack
  149.         pop     bx
  150.         pop     bp
  151.         ret                         ; Don't remove any of the parameters from
  152.                                     ; the stack as they are var parameters.
  153.     endp    ParseFilename
  154.  
  155. endif   ; ifndef MDL
  156.  
  157. end
  158.