home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / progjour / 1991 / 06 / alib / read_ef.asm < prev    next >
Assembly Source File  |  1991-08-21  |  1KB  |  68 lines

  1.     include    asm.inc
  2.  
  3.     public    read_entire_file
  4.  
  5.     .const
  6.     public    ertx_file_too_big
  7. ertx_file_too_big    db    'File too big',0
  8.  
  9.     .code
  10.     extn    open_input_file,current_file_size,malloc,rewind
  11.     extn    read_from_file,close_file,set_strerror
  12.  
  13.  
  14. ;;    read entire file
  15. ;
  16. ;    entry    DS:SI    asciiz file name
  17. ;    exit    DS:SI    ptr to storage containing file (NULL delimited)
  18. ;        CX    file byte count
  19. ;        Cf    if file not found or too big
  20. ;    uses    AX
  21. ;
  22. read_entire_file proc
  23.     pushm    bx,dx,di,es
  24.     call    open_input_file
  25.     jc    ref2            ; if file not found
  26.  
  27.     call    current_file_size
  28.     jc    ref1
  29.  
  30.     or    dx,dx            ; check file size
  31.     jnz    ref3            ;  if file too big
  32.     cmp    ax,0FFF0h
  33.     jae    ref3            ;  if file too big
  34.  
  35.     mov    cx,ax            ;  allocate storage for file contents
  36.     inc    cx            ;   (account for delimiter)
  37.     call    malloc
  38.     jc    ref1            ;  if no memory
  39.  
  40.     call    rewind
  41.     jc    ref1
  42.  
  43.     mov    cx,-1
  44.     call    read_from_file
  45.     jc    ref1            ;  if unexpected file i/o error
  46.  
  47.     mov    cx,ax            ; return file byte count
  48.     mov    si,di            ;  and storage pointer
  49.     push    es
  50.     pop    ds
  51.  
  52.     add    di,ax            ; delimit file with NULL
  53.     mov    al,NULL_CHAR
  54.     stosb
  55.  
  56. ref1:    pushf
  57.     call    close_file
  58.     popf
  59. ref2:    popm    es,di,dx,bx
  60.     ret
  61.  
  62. ref3:    lea    ax,ertx_file_too_big    ; *file too big*
  63.     call    set_strerror
  64.     jmp    ref1
  65. read_entire_file endp
  66.  
  67.     end
  68.