Re: Checking the existence of a file

From: silverdr_at_wfmh.org.pl
Date: Tue, 1 Mar 2016 19:03:05 +0100
Message-Id: <383782B5-44B7-4DEB-B4FA-80B78A12A4E7@wfmh.org.pl>
> On 2016-03-01, at 15:37, silverdr@wfmh.org.pl wrote:
> 
>> I tested the routine by entering a non-existing name but after OPEN the Carry is clear (thus file exists IMHO),
> 
> [...] I need to fetch my sources to recall how I eventually did it in a simple and reliable way.

Huh, simple it was - one call:

jsr open_file

but that call is to my library routine, which isn't /that/ simple (and it calls a few more):


;// Open File - open a file
; INPUTS:	X - device number. A/Y - pointer to null-terminated filename
; RETURNS:	A - logical file number of the opened file and CARRY clear on success
;			A/Y - pointer to null-terminated error message and CARRY set on failure
;
; NOTE:	Status channel is open at the end to verify whether OPEN() was successful.
;		due to a bug in DOS, it can't be closed until the actual file is closed
;		or it will interfere with the actual file :-( REMEMBER to close status channel
;		once done with the file...

.export open_file

.import select_filenumber, strout_no_basic, read_status_channel

.include "kernal.i"
.include "c64.i"

MAX_FILENAME_LENGTH = 20

open_file:
	pha
	lda FREEZP
	sta storage_freezp
	lda FREEZP + 1
	sta storage_freezp + 1
	pla
	sta FREEZP
	sty FREEZP + 1
	stx storage_device_number
	
; verify device number
	lda storage_device_number
	cmp #$00
	beq devnum_error
	cmp #31	; maximal device number plus 1
	bcs devnum_error
	cmp #$03
	beq devnum_error
	cmp #$04
	beq devnum_error
	cmp #$05
	beq devnum_error
	cmp #$06
	beq devnum_error

; verify filename length
	ldy #$00
@loop0:
	lda (FREEZP),y
	beq end_of_filename
	iny
	cpy #MAX_FILENAME_LENGTH
	bcc @loop0
	bcs filename_error
end_of_filename:
	tya
	sta storage_filename_length

	jsr select_filenumber	; returns available file number in A
	bcs lfn_error
	sta storage_logical_file_number
	ldx storage_device_number
	; TODO - implement a "select secondary address routine"" and use it here too.. ?
	; TODO - actually to implement read and write mode for opened files.
	ldy #$07	; 0 and 1 are used for LOAD and SAVE, 15 is for command channel, 2 - 14 are available
	jsr SETLFS
	lda storage_filename_length	; stored there when checking params
	ldx FREEZP
	ldy FREEZP + 1
	jsr SETNAM
	jsr OPEN
	ldx storage_device_number
	sec
	jsr read_status_channel
	bcs restore_freezp
	lda storage_logical_file_number
restore_freezp:
	ldx storage_freezp
	stx FREEZP
	ldx storage_freezp + 1
	stx FREEZP + 1
	rts

lfn_error:
	lda #<lfn_error_message
	ldy #>lfn_error_message
	sec
	bcs restore_freezp

devnum_error:
	lda #<devnum_error_message
	ldy #>devnum_error_message
	sec
	bcs restore_freezp

filename_error:
	lda #<filename_error_message
	ldy #>filename_error_message
	sec
	bcs restore_freezp

.segment "DATA"
storage_freezp:
	.res 2
storage_device_number:
	.res 1
storage_logical_file_number:
	.res 1
storage_filename_length:
	.res 1
lfn_error_message:
	.byte "97, can't assign lfn, 00, 00", $00
filename_error_message:
	.byte "98, invalid filename, 00, 00", $00	 ;TODO - present the invalid length here?
devnum_error_message:
	.byte "99, invalid device number, 00, 00", $00	;TODO - present the invalid device number here?
;/

If you are interested I can put up or send you the remaining parts.
-- 
SD!


       Message was sent through the cbm-hackers mailing list
Received on 2016-03-01 19:00:12

Archive generated by hypermail 2.2.0.