prog8/examples/cx16/fileseek.p8
2023-05-02 19:33:49 +02:00

95 lines
2.8 KiB
Lua

; this program shows the use of the f_seek function to seek to a position in an opened file.
; (this only works on Commander X16 DOS. on sdcard, not on host filesystem.)
%import diskio
%import textio
%zeropage basicsafe
%option no_sysinit
main {
uword megabuffer = memory("megabuffer", 20000, 256)
sub start() {
txt.print("writing data file (drive 8)...\n")
uword total=0
if diskio.f_open_w("@:seektestfile.bin") {
repeat 100 {
str text = "hello world.*"
void diskio.f_write(text, string.length(text))
total += string.length(text)
}
diskio.f_close_w()
txt.print("written size=")
txt.print_uw(total)
txt.nl()
} else {
txt.print("error: ")
txt.print(diskio.status())
sys.exit(1)
}
read_last_bytes()
; NOTE: f_seek_w() doesn't work reliably right now. I only manage to corrupt the fat32 filesystem on the sdcard with it...
; txt.print("\nseeking to 1292 and writing a few bytes...\n")
; if diskio.f_open_w("seektestfile.bin,p,a") {
; diskio.f_seek_w(0, 1292)
; void diskio.f_write("123", 3)
; diskio.f_close_w()
; } else {
; txt.print("error: ")
; txt.print(diskio.status())
; sys.exit(1)
; }
read_last_bytes()
}
sub read_last_bytes() {
; read the last 10 bytes of the 1300 bytes file
uword total = 0
uword size
txt.print("\nreading...\n")
if diskio.f_open("seektestfile.bin") {
size = diskio.f_read_all(megabuffer)
diskio.f_close()
txt.print("size read:")
txt.print_uw(size)
txt.nl()
} else {
txt.print("error!\n")
sys.exit(1)
}
txt.print("\nseeking to 1290 and reading...\n")
if diskio.f_open("seektestfile.bin") {
diskio.f_seek(0, 1290)
uword ptr = megabuffer
do {
size = diskio.f_read(ptr, 255)
total += size
ptr += size
} until size==0
diskio.f_close()
txt.print("size read=")
txt.print_uw(total)
txt.nl()
megabuffer[lsb(total)] = 0
txt.print("buffer read=")
ubyte idx
for idx in 0 to lsb(total-1) {
txt.print_ubhex(megabuffer[idx], false)
txt.spc()
}
txt.print("\nas text: \"")
txt.print(megabuffer)
txt.print("\"\n")
} else {
txt.print("error: ")
txt.print(diskio.status())
sys.exit(1)
}
}
}