Skip to content

Commit

Permalink
Factorize file pointer check
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleroy committed Jan 30, 2024
1 parent ee8c8df commit eb1741d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
28 changes: 28 additions & 0 deletions libsrc/common/checkferror.s
Original file line number Diff line number Diff line change
@@ -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
11 changes: 3 additions & 8 deletions libsrc/common/fgetc.s
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions libsrc/common/fputc.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

.export _fputc
.importzp ptr1
.import _write
.import _write, checkferror
.import pushax, pusha0, popax, incsp2
.import pushptr1, popptr1, returnFFFF

Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions libsrc/common/fputs.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

.export _fputs
.importzp ptr1, ptr2
.import _write, _strlen
.import _write, _strlen, checkferror
.import swapstk, pushax, returnFFFF

.include "stdio.inc"
Expand All @@ -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
Expand Down

0 comments on commit eb1741d

Please sign in to comment.