From df20467e036386dc7476320d04c119a6395bd120 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 8 Dec 2020 02:16:41 +0100 Subject: [PATCH] completed diskio file lister --- compiler/res/prog8lib/diskio.p8 | 49 ++++++++++++++++++-------------- examples/compiled/dirlist.prg | Bin 0 -> 1816 bytes examples/dirlist.p8 | 43 ++++++++++++++++++++++++++++ examples/test.p8 | 15 +++++----- 4 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 examples/compiled/dirlist.prg create mode 100644 examples/dirlist.p8 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 0000000000000000000000000000000000000000..735ce69cb937be941d5fba2db701d33d770424c2 GIT binary patch literal 1816 zcmd5-O>7%Q6rSC6QhV1KH-JP6IZQ*PCLv8tK@dU_G|ivgZ4&Y$KuBmMk~ZZ|;XrSm z3Ps-1_7EcF&s3SjN%==0MYWy5^b$7Nkpr&_`uh|a(P!BqPx^<}&Udw$`K>?QO za>#`)%G8PD^xKdNF9wzvJV(!mWK06n^bGr*2u#vd#*GHXn64D9Dp*FVPWt6;=y)Mi z*kkBNyP*It;=Qe@t~!Zq$h6-MR^M{5Od*Cu9tWu(eil;e3m~QYTm*7|JMW_}WZG@( z36#V0=jHbKkLBQh^KP-|yjM{~Unbla^%d7`(a+t<;Cs#V`gYS&)Y4tksDrlFcM4k6($UOC zZ~J=N(P4%kX7tIvrX9Mc_e6K^u7ELI@W%gSuugjXz+ylOfw*XuqIe;`Rjl%)*N_s%UyMpH-`LcsSG|`Dd5sUW zKpJO8B7Xm`Gv@>>$^&B9&qriLmgS0p49*M#$1C9o={uXGnSj)JTz}SnKkJN#?>Q%A6yaxa>$IHWJDH%Ti*a|G)FxD9jU_ zQ}nnpdqdFz8TDlaG`O3AAW9CWOIFnRWOKXEe@e|)(c8GUa|NW-kSNe7(CeUn1d>I=L6$6;EKV(<4USx@fmFCw z8-i#dH(Qf;r{f`jZWUa#oO3dw`m&^!bNZTqzRx3bkElcMax)CAaB-^3ZTx8ofC_p& zB%@Pf*byi}U+4JkLPsILZPx9gIs~pv6rFZ5!Bk{&9IWt*Xh_?_3fJT80$9P@gG`*I zRah(r+!ZZ_O>Oa{)bMT&c*~H~-`!*@SyeDt=%_hrh~}7j#~rJOmO>m;CH&i@u9Z=+ zOH%<4$BsbjESa+q*XV07cpuW6Aw@S~@Bw6`z7A0os@-KaKLdl|JWpXTV)NG_x{!wy zPtfh*b=J!~LFhpq4+VB5j@y;M!(`af9@RnOU?~t%(oP63!Sv%wuv1i%u>DLI-V@B8 zH0?Jj#bT@u-WBb`V*A_KYO!wks(F=MO$x@tB2+Y3lhi}?eY==H7MlTm$4xe?1`hG& zTM$x~NClrTE~K;tF}2E$YabQXxl4hy32-`)VK+H#l*Y40rF9^lZTKVY-EWkeX_jfm z%o@L$+r}ytG@8?5GcX#4JS0Y8Gy-upupPY)DLe0FUL}+O8dK!sr7#Is^jJlo(H&;*@kHYf4FASoz?c#4Pre*c#DdWu7SX@C9RvC+ha7 OU68MV{;~fKw0{A>WzE3= literal 0 HcmV?d00001 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 } } }