1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-08 15:29:37 +00:00

Factorize file pointer check

This commit is contained in:
Colin Leroy-Mira 2024-01-30 17:11:10 +01:00
parent ee8c8dfeda
commit eb1741dbcc
4 changed files with 35 additions and 24 deletions

View File

@ -0,0 +1,28 @@
;
; Colin Leroy-Mira, 2024
;
; Helper to check for file opened, not eof, not ferror
; Expects file pointer in ptr, returns 0 if everything is OK, -1 otherwise
; Destroys A, X, Y
;
.export checkferror
.importzp ptr1
.include "_file.inc"
checkferror:
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
bne ret_eof
lda #$00
rts
ret_eof:
lda #$01
rts

View File

@ -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,13 +17,7 @@ _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

View File

@ -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

View File

@ -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