mirror of
https://github.com/cc65/cc65.git
synced 2024-12-27 00:29:31 +00:00
Factorize file pointer check
This commit is contained in:
parent
1a5a7d67a7
commit
2b97735d5d
24
libsrc/common/checkferror.s
Normal file
24
libsrc/common/checkferror.s
Normal file
@ -0,0 +1,24 @@
|
||||
;
|
||||
; Colin Leroy-Mira, 2024
|
||||
;
|
||||
; Helper to check for file opened, not eof, not ferror
|
||||
; Expects file pointer in ptr1,
|
||||
; Returns with Z flag set if everything is OK,
|
||||
; Destroys A, X, Y,
|
||||
; Sets file flags in A
|
||||
;
|
||||
|
||||
.export checkferror
|
||||
.importzp ptr1
|
||||
|
||||
.include "_file.inc"
|
||||
|
||||
checkferror:
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
tax
|
||||
and #(_FOPEN|_FERROR|_FEOF); Check for file open, error/eof
|
||||
tay
|
||||
txa
|
||||
cpy #_FOPEN
|
||||
rts
|
@ -5,7 +5,8 @@
|
||||
;
|
||||
|
||||
.export _fgetc
|
||||
.import _read, pusha0, pushax, popptr1, incsp2, returnFFFF
|
||||
.import _read, checkferror
|
||||
.import pusha0, pushax, popptr1, incsp2, returnFFFF
|
||||
.importzp ptr1
|
||||
|
||||
.include "stdio.inc"
|
||||
@ -16,16 +17,10 @@ _fgetc:
|
||||
stx ptr1+1
|
||||
jsr pushax ; Backup our ptr
|
||||
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
tax
|
||||
and #_FOPEN ; Check for file open
|
||||
beq ret_eof
|
||||
txa
|
||||
and #(_FERROR|_FEOF); Check for error/eof
|
||||
jsr checkferror
|
||||
bne ret_eof
|
||||
|
||||
txa
|
||||
tax
|
||||
and #_FPUSHBACK ; Check for pushed back char
|
||||
beq do_read
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
.export _fputc
|
||||
.importzp ptr1
|
||||
.import _write
|
||||
.import _write, checkferror
|
||||
.import pushax, pusha0, popax, incsp2
|
||||
.import pushptr1, popptr1, returnFFFF
|
||||
|
||||
@ -21,13 +21,7 @@ _fputc:
|
||||
sta c ; to return it anyway
|
||||
stx c+1
|
||||
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
tax
|
||||
and #_FOPEN ; Check for file open
|
||||
beq ret_eof
|
||||
txa
|
||||
and #(_FERROR|_FEOF); Check for error/eof
|
||||
jsr checkferror
|
||||
bne ret_eof
|
||||
|
||||
jsr pushptr1 ; Backup fp pointer
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
.export _fputs
|
||||
.importzp ptr1, ptr2
|
||||
.import _write, _strlen
|
||||
.import _write, _strlen, checkferror
|
||||
.import swapstk, pushax, returnFFFF
|
||||
|
||||
.include "stdio.inc"
|
||||
@ -16,13 +16,7 @@ _fputs:
|
||||
sta ptr1
|
||||
stx ptr1+1
|
||||
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
tax
|
||||
and #_FOPEN ; Check for file open
|
||||
beq ret_eof
|
||||
txa
|
||||
and #(_FERROR|_FEOF); Check for error/eof
|
||||
jsr checkferror
|
||||
bne ret_eof
|
||||
|
||||
; Push _write parameters
|
||||
|
Loading…
Reference in New Issue
Block a user