diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index fa1563cf3..341af63d4 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -59,13 +59,13 @@ io_error: } - sub listfiles(ubyte drivenumber, uword pattern, ubyte matchSuffix, - uword filenamesbufferptr, uword blocksizesptr, ubyte max_files) -> ubyte { + sub listfiles(ubyte drivenumber, uword pattern, ubyte suffixmatch, + uword namesarray, uword blocksarray, uword namesbuffer, ubyte maxnum) -> ubyte { ; -- 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 names will be concatenated into the filenamesbuffer, separated by a 0-byte - ; The buffer should be at least 17 * max_files and the block sizes array should be that large as well. - if max_files==0 return 0 + ; their blocksizes will be put into the blocksarray + ; pointers to their names will be put into the namesarray + ; The namesbuffer should be at least 17 * maxnum and both arrays should be at least maxnum size as well. + if maxnum==0 return 0 ubyte num_files = 0 ubyte pattern_size = 0 @@ -86,11 +86,11 @@ io_error: } ubyte disk_name = true - uword last_filename_ptr = filenamesbufferptr + uword last_filename_ptr = namesbuffer while not c64.READST() { - @(blocksizesptr) = c64.CHRIN() - @(blocksizesptr+1) = c64.CHRIN() + @(blocksarray) = c64.CHRIN() + @(blocksarray+1) = c64.CHRIN() ; read until the filename starts after the first " while c64.CHRIN()!='\"' { @@ -98,6 +98,7 @@ io_error: goto close_end } + ; append the filename to the buffer repeat { ubyte char = c64.CHRIN() if char==0 @@ -106,31 +107,37 @@ io_error: ; break ; TODO fix generated code for this jump if char=='\"' break - if not disk_name { - @(filenamesbufferptr) = char - filenamesbufferptr++ - } + @(namesbuffer) = char + namesbuffer++ } - if not disk_name { - @(filenamesbufferptr) = 0 - filenamesbufferptr++ + if disk_name + namesbuffer = last_filename_ptr + else { + @(namesbuffer) = 0 + namesbuffer++ ubyte matches = true if pattern_size { - if matchSuffix + ; do filename matching + if suffixmatch rightstr(last_filename_ptr, filename, pattern_size) else leftstr(last_filename_ptr, filename, pattern_size) matches = strcmp(filename, pattern)==0 } if matches { - blocksizesptr += 2 + ; enter the details into the arrays and increment num_files++ - last_filename_ptr = filenamesbufferptr - if num_files>=max_files + @(namesarray) = lsb(last_filename_ptr) + @(namesarray+1) = msb(last_filename_ptr) + last_filename_ptr = namesbuffer + namesarray += 2 + blocksarray += 2 + if num_files>=maxnum goto close_end } else { - filenamesbufferptr = last_filename_ptr + ; no match, reset buffer to overwrite previous + namesbuffer = last_filename_ptr } } diff --git a/examples/compiled/dirlist.prg b/examples/compiled/dirlist.prg new file mode 100644 index 000000000..735ce69cb Binary files /dev/null and b/examples/compiled/dirlist.prg differ diff --git a/examples/dirlist.p8 b/examples/dirlist.p8 new file mode 100644 index 000000000..bd0f1fdda --- /dev/null +++ b/examples/dirlist.p8 @@ -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") + } + } + } + } +} diff --git a/examples/test.p8 b/examples/test.p8 index acb5d7738..25967f6fb 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -33,6 +33,8 @@ errors { ; char = c64.CHRIN() ; TODO fix undefined symbol error, should refer to 'char' above in the subroutine's scope ; } until char==0 + ; TODO fix compiler crash: + ; str[max_files] names } } @@ -51,17 +53,18 @@ main { ; txt.chrout('\n') ; test_stack.test() - const ubyte max_files = 10 + const ubyte max_files = 8 uword[max_files] blocks - str filenames = "?????????????????" * max_files + uword[max_files] names + str filenamesbuffer = "?????????????????" * max_files ubyte num_files=0 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() txt.chrout('\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() ;test_stack.test() @@ -70,15 +73,13 @@ main { txt.print("no files found.") else { ubyte i - uword filenameptr = &filenames for i in 0 to num_files-1 { txt.print_ub(i+1) txt.print(": ") - txt.print(filenameptr) + txt.print(names[i]) txt.print(" = ") txt.print_uw(blocks[i]) txt.print(" blocks\n") - filenameptr += strlen(filenameptr) + 1 } } }