completed diskio file lister

This commit is contained in:
Irmen de Jong 2020-12-08 02:16:41 +01:00
parent ecbd9d739e
commit df20467e03
4 changed files with 79 additions and 28 deletions

View File

@ -59,13 +59,13 @@ io_error:
} }
sub listfiles(ubyte drivenumber, uword pattern, ubyte matchSuffix, sub listfiles(ubyte drivenumber, uword pattern, ubyte suffixmatch,
uword filenamesbufferptr, uword blocksizesptr, ubyte max_files) -> ubyte { uword namesarray, uword blocksarray, uword namesbuffer, ubyte maxnum) -> ubyte {
; -- returns a list of files in the directory matching the given prefix or suffix (optional) ; -- returns a list of files in the directory matching the given prefix or suffix (optional)
; their blocksizes will be put into the uword array given by blocksizesptr ; their blocksizes will be put into the blocksarray
; their names will be concatenated into the filenamesbuffer, separated by a 0-byte ; pointers to their names will be put into the namesarray
; The buffer should be at least 17 * max_files and the block sizes array should be that large as well. ; The namesbuffer should be at least 17 * maxnum and both arrays should be at least maxnum size as well.
if max_files==0 return 0 if maxnum==0 return 0
ubyte num_files = 0 ubyte num_files = 0
ubyte pattern_size = 0 ubyte pattern_size = 0
@ -86,11 +86,11 @@ io_error:
} }
ubyte disk_name = true ubyte disk_name = true
uword last_filename_ptr = filenamesbufferptr uword last_filename_ptr = namesbuffer
while not c64.READST() { while not c64.READST() {
@(blocksizesptr) = c64.CHRIN() @(blocksarray) = c64.CHRIN()
@(blocksizesptr+1) = c64.CHRIN() @(blocksarray+1) = c64.CHRIN()
; read until the filename starts after the first " ; read until the filename starts after the first "
while c64.CHRIN()!='\"' { while c64.CHRIN()!='\"' {
@ -98,6 +98,7 @@ io_error:
goto close_end goto close_end
} }
; append the filename to the buffer
repeat { repeat {
ubyte char = c64.CHRIN() ubyte char = c64.CHRIN()
if char==0 if char==0
@ -106,31 +107,37 @@ io_error:
; break ; TODO fix generated code for this jump ; break ; TODO fix generated code for this jump
if char=='\"' if char=='\"'
break break
if not disk_name { @(namesbuffer) = char
@(filenamesbufferptr) = char namesbuffer++
filenamesbufferptr++
}
} }
if not disk_name { if disk_name
@(filenamesbufferptr) = 0 namesbuffer = last_filename_ptr
filenamesbufferptr++ else {
@(namesbuffer) = 0
namesbuffer++
ubyte matches = true ubyte matches = true
if pattern_size { if pattern_size {
if matchSuffix ; do filename matching
if suffixmatch
rightstr(last_filename_ptr, filename, pattern_size) rightstr(last_filename_ptr, filename, pattern_size)
else else
leftstr(last_filename_ptr, filename, pattern_size) leftstr(last_filename_ptr, filename, pattern_size)
matches = strcmp(filename, pattern)==0 matches = strcmp(filename, pattern)==0
} }
if matches { if matches {
blocksizesptr += 2 ; enter the details into the arrays and increment
num_files++ num_files++
last_filename_ptr = filenamesbufferptr @(namesarray) = lsb(last_filename_ptr)
if num_files>=max_files @(namesarray+1) = msb(last_filename_ptr)
last_filename_ptr = namesbuffer
namesarray += 2
blocksarray += 2
if num_files>=maxnum
goto close_end goto close_end
} else { } else {
filenamesbufferptr = last_filename_ptr ; no match, reset buffer to overwrite previous
namesbuffer = last_filename_ptr
} }
} }

Binary file not shown.

43
examples/dirlist.p8 Normal file
View File

@ -0,0 +1,43 @@
%import textio
%import diskio
%zeropage basicsafe
%import test_stack
%option no_sysinit
main {
sub start() {
const ubyte max_files = 8
uword[max_files] blocks
uword[max_files] names
str filenamesbuffer = "?????????????????" * max_files
ubyte num_files=0
txt.print("\nfiles starting with 'cub':\n")
num_files = diskio.listfiles(8, "cub", false, names, blocks, filenamesbuffer, max_files)
print_listing()
txt.print("\nfiles ending with 'gfx':\n")
num_files = diskio.listfiles(8, "gfx", true, names, blocks, filenamesbuffer, max_files)
print_listing()
txt.print("\nfiles, no filtering (limited number):\n")
num_files = diskio.listfiles(8, 0, false, names, blocks, filenamesbuffer, max_files)
print_listing()
;test_stack.test()
sub print_listing() {
if num_files==0
txt.print("no files found.\n")
else {
ubyte i
for i in 0 to num_files-1 {
txt.print_ub(i+1)
txt.print(": ")
txt.print(names[i])
txt.print(" = ")
txt.print_uw(blocks[i])
txt.print(" blocks\n")
}
}
}
}
}

View File

@ -33,6 +33,8 @@ errors {
; char = c64.CHRIN() ; TODO fix undefined symbol error, should refer to 'char' above in the subroutine's scope ; char = c64.CHRIN() ; TODO fix undefined symbol error, should refer to 'char' above in the subroutine's scope
; } until char==0 ; } until char==0
; TODO fix compiler crash:
; str[max_files] names
} }
} }
@ -51,17 +53,18 @@ main {
; txt.chrout('\n') ; txt.chrout('\n')
; test_stack.test() ; test_stack.test()
const ubyte max_files = 10 const ubyte max_files = 8
uword[max_files] blocks uword[max_files] blocks
str filenames = "?????????????????" * max_files uword[max_files] names
str filenamesbuffer = "?????????????????" * max_files
ubyte num_files=0 ubyte num_files=0
txt.print("files starting with 'cub':\n") txt.print("files starting with 'cub':\n")
num_files = diskio.listfiles(8, "cub", false, filenames, blocks, max_files) num_files = diskio.listfiles(8, "cub", false, names, blocks, filenamesbuffer, max_files)
print_listing() print_listing()
txt.chrout('\n') txt.chrout('\n')
txt.print("files ending with 'gfx':\n") txt.print("files ending with 'gfx':\n")
num_files = diskio.listfiles(8, "gfx", true, filenames, blocks, max_files) num_files = diskio.listfiles(8, "gfx", true, names, blocks, filenamesbuffer, max_files)
print_listing() print_listing()
;test_stack.test() ;test_stack.test()
@ -70,15 +73,13 @@ main {
txt.print("no files found.") txt.print("no files found.")
else { else {
ubyte i ubyte i
uword filenameptr = &filenames
for i in 0 to num_files-1 { for i in 0 to num_files-1 {
txt.print_ub(i+1) txt.print_ub(i+1)
txt.print(": ") txt.print(": ")
txt.print(filenameptr) txt.print(names[i])
txt.print(" = ") txt.print(" = ")
txt.print_uw(blocks[i]) txt.print_uw(blocks[i])
txt.print(" blocks\n") txt.print(" blocks\n")
filenameptr += strlen(filenameptr) + 1
} }
} }
} }