home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 364b.lha / PCQ_v1.1 / Runtime / Writers / flush.asm next >
Encoding:
Assembly Source File  |  1990-04-08  |  3.1 KB  |  109 lines

  1.  
  2. *    Flush.asm (of the PCQ Pascal runtime library)
  3. *    Copyright (c) 1989 Patrick Quaid
  4.  
  5. *    Write the contents of the file buffer to the DOS file.
  6.  
  7. *    Algorithm for FlushBuffer
  8. *
  9. *    Write CURRENT - BUFFER bytes to HANDLE
  10. *    set CURRENT to BUFFER
  11. *
  12.  
  13. *    Upon entry, this routine expects the file record in a0.
  14. *    a0 is preserved.
  15.  
  16.     SECTION FlushBuffer,CODE
  17.  
  18.     XREF    _LVOWrite
  19.     XREF    _LVOIoErr
  20.  
  21.     XREF    _p%DOSBase
  22.     XREF    _p%IOResult
  23.  
  24.     INCLUDE    ":runtime/FileRec.i"
  25.  
  26.     XDEF    _p%FlushBuffer
  27. _p%FlushBuffer
  28.  
  29.     move.l    HANDLE(a0),d1        ; get the file handle
  30.     bne.s    1$            ; are we OK
  31.     jsr    _p%MayOpenOutput    ; it's not open.  Try to get Output
  32.     move.l    HANDLE(a0),d1        ; if we got here, it must be OK
  33. 1$    move.l    BUFFER(a0),d2        ; get buffer address
  34.     move.l    CURRENT(a0),d3        ; get current address
  35.     sub.l    d2,d3            ; get number of bytes to write
  36.     ble.s    3$            ; if <= 0, skip the write
  37.     move.l    HANDLE(a0),d1        ; get the proper handle
  38.     move.l    _p%DOSBase,a6        ; get the library base
  39.     move.l    d3,-(sp)        ; save bytes to write
  40.     move.l    a0,-(sp)        ; save file record ptr
  41.     jsr    _LVOWrite(a6)        ; and write it
  42.     move.l    (sp)+,a0        ; retrieve it
  43.     cmp.l    (sp)+,d0        ; did we write proper number?
  44.     beq.s    2$            ; if so, go ahead
  45.     jsr    _LVOIoErr(a6)        ; get the error number
  46.     move.l    d0,_p%IOResult        ; set IOResult
  47.     bra.s    3$            ; do NOT update file rec
  48. 2$    move.l    BUFFER(a0),a1        ; get buffer address
  49.     move.l    a1,CURRENT(a0)        ; reset current ptr
  50. 3$    rts
  51.  
  52. *    MayOpenOutput
  53. *
  54. *    This routine opens a Standard Output window for programs that
  55. *    may have started from the Workbench.  It gets the window spec
  56. *    from _StdOutName, which is defined either in the User program
  57. *    or, by default, in this library.  If the Input file is already
  58. *    open, and it's interactive, that already open file is used.
  59. *
  60. *    Algorithm for MayOpenOutput:
  61. *
  62. *    if a0 <> Output then
  63. *        generate a runtime error
  64. *    if Input is open and interactive then
  65. *        Output.Handle := Input.Handle
  66. *    else
  67. *        Open(StdOutName, Output)
  68. *        if it did not open OK
  69. *        generate a runtime error
  70. *
  71.  
  72. *    Upon entry to this routine, a0 holds the address of the
  73. *    File Record, which may or may not be Output.
  74.  
  75.     XREF    _p%ExitWithAddr
  76.     XREF    _p%Open
  77.     XREF    _StdOutName
  78.     XREF    _Input
  79.     XREF    _Output
  80.  
  81. _p%MayOpenOutput
  82.     cmpa.l    #_Output,a0    ; is it Output?
  83.     beq.s    1$        ; if so, skip this
  84.     move.l    #56,d0        ; runtime error
  85.     jsr    _p%ExitWithAddr    ; generate the error
  86. 1$    move.l    #_Input,a1    ; get Input ptr
  87.     tst.l    HANDLE(a1)    ; is it open?
  88.     beq    2$        ; if not, open a new one
  89.     tst.b    INTERACTIVE(a1)    ; is it interactive?
  90.     bne    2$        ; if not, open a new file
  91.     move.l    HANDLE(a1),a1    ; get the file handle
  92.     move.l    a1,HANDLE(a0)    ; and copy it over
  93.     rts            ; and try that one
  94. 2$    move.l    #80,MAX(a0)    ; set up for Open call
  95.     move.l    #1,RECSIZE(a0)    ; Text file
  96.     move.w    #1006,ACCESS(a0)    ; it's an output file (ModeNewFile)
  97.     move.l    _StdOutName,-(sp)    ; push the file name
  98.     move.l    a0,-(sp)    ; push the file record address
  99.     jsr    _p%Open        ; try to open this file
  100.     move.l    (sp)+,a0    ; get file record ptr back
  101.     addq.l    #4,sp        ; pop other arg
  102.     tst.b    d0        ; did it go OK?
  103.     bne.s    3$        ; if so, go on
  104.     move.l    #57,d0        ; if not, generate an error
  105.     jsr    _p%ExitWithAddr    ; goodbye
  106. 3$    rts            ; return to sender
  107.  
  108.     END
  109.