home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PPOS2.ZIP / RLINE.ASM < prev    next >
Assembly Source File  |  1989-03-16  |  4KB  |  123 lines

  1.         title   RLINE --- read text file
  2.         page    55,132
  3.     .286
  4.  
  5. ; RLINE.ASM --- read line from text file (OS/2 version)
  6. ; by Ray Duncan, (C) 1988 Ziff Davis Communications
  7. ;
  8. ; Call with:    DS:DX = buffer address
  9. ;               CX    = buffer size
  10. ;               BX    = text file handle
  11. ;               
  12. ;               Buffer should be larger than any line that
  13. ;               will be encountered in the text file.
  14. ;
  15. ; Returns:      AX    = length of line including new-line
  16. ;                       delimiter character(s), or 0 if 
  17. ;                       end of file or no delimiter found.
  18. ;               DS:DX = text address
  19. ;
  20. ;               Other registers preserved.      
  21.  
  22.     extrn    DosRead:far
  23.     extrn    DosChgFilePtr:far
  24.  
  25. DGROUP  group   _DATA
  26.  
  27. _DATA   segment word public 'DATA'
  28.  
  29. nl      db      0dh,0ah                 ; OS/2 logical new-line
  30. nl_len  equ     $-nl
  31.  
  32. rlen    dw    0            ; receives count from DosRead
  33. newfp    dd    0            ; receives absolute filepointer
  34.  
  35. _DATA   ends
  36.  
  37. _TEXT   segment word public 'CODE'
  38.  
  39.         extrn   strndx:near             ; string search utility
  40.  
  41.         assume  cs:_TEXT,ds:DGROUP,es:DGROUP
  42.  
  43.         public  rline
  44. rline   proc    near
  45.  
  46.         push    bx                      ; save registers
  47.         push    cx
  48.         push    dx
  49.         push    si
  50.         push    di
  51.         push    es
  52.  
  53.                                         ; read chunk from file...
  54.     push    bx            ; file handle
  55.         push    ds            ; buffer address
  56.         push    dx
  57.     push    cx            ; length to read
  58.         push    ds                ; receives actual length
  59.         push    offset DGROUP:rlen    
  60.         call    DosRead                ; transfer to OS/2
  61.     or    ax,ax                ; read successful?
  62.         jnz    rline1                  ; jump if read error
  63.         cmp    rlen,0            ; end of file?
  64.         jz      rline1                  ; yes, jump
  65.  
  66.         push    bx                      ; save input file handle
  67.         push    dx                      ; save buffer base address
  68.         push    ds
  69.  
  70.         push    ds                      ; set up for delimiter search
  71.         pop     es                      ; ES:DI = string to search
  72.         push    dx
  73.         pop     di
  74.         mov     dx,rlen            ; DX = string length
  75.         mov     si,DGROUP               ; DS:SI = delimiter address
  76.         mov     ds,si
  77.         mov     si,offset DGROUP:nl
  78.         mov     bx,nl_len               ; BX = delimiter length 
  79.         call    strndx                  ; search for delimiter
  80.  
  81.         pop     ds                      ; restore buffer base address
  82.         pop     dx
  83.         pop     bx                      ; restore input file handle
  84.         jc      rline1                  ; jump if no delimiter found
  85.  
  86.         add     di,nl_len               ; calculate line length
  87.         sub     di,dx
  88.  
  89.     mov    ax,rlen            ; calculate read excess
  90.         sub     ax,di
  91.         neg     ax
  92.         cwd
  93.  
  94.                     ; now back up file pointer
  95.     push    bx            ; file handle
  96.         push    dx            ; number of bytes
  97.         push    ax
  98.         push    1            ; method = rel. to current FP
  99.     push    ds               ; receives absolute filepointer
  100.         push    offset DGROUP:newfp
  101.     call    DosChgFilePtr
  102.  
  103.         mov     ax,di                   ; return line length in AX
  104.         jmp     rline2
  105.  
  106. rline1: xor     ax,ax                   ; end of file or other error,
  107.                                         ; set line length = 0
  108.  
  109. rline2: pop     es                      ; restore registers
  110.         pop     di
  111.         pop     si
  112.         pop     dx
  113.         pop     cx
  114.         pop     bx
  115.         ret                             ; return line length
  116.  
  117. rline   endp
  118.  
  119. _TEXT   ends
  120.         
  121.         end
  122.  
  123.