home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n05.zip / RLINE.ASM < prev    next >
Assembly Source File  |  1988-12-12  |  4KB  |  103 lines

  1. ; RLINE.ASM --- read line from text file (MS-DOS version)
  2. ; Copyright (c) 1989 Ziff Communications Co.
  3. ; PC Magazine * Ray Duncan
  4. ;
  5. ; Call with:    DS:DX = buffer address
  6. ;               CX    = buffer size
  7. ;               BX    = text file handle
  8. ;               
  9. ;               Buffer should be larger than any line that
  10. ;               will be encountered in the text file.
  11. ;
  12. ; Returns:      AX    = length of line including new-line
  13. ;                       delimiter character(s), or 0 if 
  14. ;                       end of file or no delimiter found.
  15. ;               DS:DX = text address
  16. ;
  17. ;               Other registers preserved.      
  18.  
  19. DGROUP  group   _DATA
  20.  
  21. _DATA   segment word public 'DATA'
  22.  
  23. nl      db      0dh,0ah                 ; MS-DOS logical new-line
  24. nl_len  equ     $-nl
  25.  
  26. _DATA   ends
  27.  
  28. _TEXT   segment word public 'CODE'
  29.  
  30.         extrn   strndx:near             ; string search utility
  31.  
  32.         assume  cs:_TEXT
  33.  
  34.         public  rline
  35. rline   proc    near
  36.  
  37.         push    bx                      ; save registers
  38.         push    cx
  39.         push    dx
  40.         push    si
  41.         push    di
  42.         push    es
  43.  
  44.                                         ; read chunk from file...
  45.         mov     ah,3fh                  ; Fxn 3FH = read
  46.         int     21h                     ; transfer to MS-DOS
  47.         jc      rline8                  ; jump if read error
  48.         or      ax,ax                   ; end of file?
  49.         jz      rline9                  ; yes, jump
  50.  
  51.         push    ax                      ; save actual data length
  52.         push    bx                      ; save input file handle
  53.         push    dx                      ; save buffer base address
  54.         push    ds
  55.  
  56.         push    ds                      ; set up for delimiter search
  57.         pop     es                      ; ES:DI = string to search
  58.         push    dx
  59.         pop     di
  60.         mov     dx,ax                   ; DX = string length
  61.         mov     si,DGROUP               ; DS:SI = delimiter address
  62.         mov     ds,si
  63.         mov     si,offset DGROUP:nl
  64.         mov     bx,nl_len               ; BX = delimiter length 
  65.         call    strndx                  ; search for delimiter
  66.  
  67.         pop     ds                      ; restore buffer base address
  68.         pop     dx
  69.         pop     bx                      ; restore input file handle
  70.         pop     ax                      ; restore original read length
  71.         jc      rline8                  ; jump if no delimiter found
  72.  
  73.         add     di,nl_len               ; calculate line length
  74.         sub     di,dx
  75.         push    di                      ; and save it
  76.  
  77.         sub     ax,di                   ; calculate read excess
  78.         neg     ax
  79.         cwd
  80.         mov     cx,ax                   ; CX:DX = amount to back
  81.         xchg    dx,cx                   ; up file pointer
  82.         mov     ax,4201h                ; Function 42H = seek
  83.         int     21h                     ; transfer to MS-DOS
  84.  
  85.         pop     ax                      ; get back line length
  86.         jmp     rline9                  ; and return it to caller
  87.  
  88. rline8: xor     ax,ax                   ; end of file or other error,
  89.                                         ; set line length = 0
  90.  
  91. rline9: pop     es                      ; restore registers
  92.         pop     di
  93.         pop     si
  94.         pop     dx
  95.         pop     cx
  96.         pop     bx
  97.         ret                             ; return line length
  98.  
  99. rline   endp
  100.  
  101. _TEXT   ends
  102.         end
  103.