Lua file:seek() doesn't work properly with file larger than 16MB(16777216bytes)

, ,
  • Platform: SDK 1.11.1 on Linux/Windows10
    (not tested on device, because I haven't it yet)

Strange inconsistent behavior

When a file is 16 MB or larger, seek() does not seem to move the file pointer to the correct location.
The location specified by seek() does not match the result of tell().
This problem occurs only when the location specified by seek() is an odd number of bytes.

Reproduction Ex

  • testfile.txt (newline: LF)
[LF] (firstline is newline only:1byte)
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
.
.
.
Over16MB
  • testfile.txt structure
0
[LF]
1                                                    54
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ[LF]
55                                                   108
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ[LF]
109                                                  162
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ[LF]
.
.
.
  • code(pseudo code)
file = playdate.file.open(testfile.txt, playdate.file.kFileRead)

LOOP begin
	linenum = linenum+1
	offset = [give position of "a" at eachline:0, 1, 55, 109, 163, 217, 271...]

	file:seek(offset)
		print("seek".."["..linenum.."]:"..offset)
	location = file:tell()
		print("tell:"..location)

	[file:readline() & drawText()]
LOOP end
  • console output
seek[1]:0
tell:0
seek[2]:1
tell:1
seek[3]:55
tell:55
seek[4]:109
tell:109
seek[5]:163
tell:163
seek[6]:217
tell:217
seek[7]:271
tell:271
.
.
.

seek[310687]:16776991
tell:16776991
seek[310688]:16777045
tell:16777045
seek[310689]:16777099
tell:16777099
seek[310690]:16777153
tell:16777153
seek[310691]:16777207
tell:16777207
seek[310692]:16777261 <--Over16MB(16777216) & odd byte
tell:16777260 <- * doesn't match to seek() value (-1)
seek[310693]:16777315
tell:16777316 <- * doesn't match to seek() value (+1)
seek[310694]:16777369
tell:16777368 <- * doesn't match to seek() value (-1)
seek[310695]:16777423
tell:16777424 <- * doesn't match to seek() value (+1)
.
.
.
  • top of file
    playdate-20220603-171655

  • location around 16MB
    playdate-20220603-181401

Ugggggh.

int offset = luaL_checknumber(L, 2);

I'm converting the argument to a float there, losing accuracy when it gets large. :man_facepalming: Well, at least it's an easy fix, just need to use checkinteger() instead. ..though I think that will return 0 if the number happens to actually be a float for whatever reason, so I should check for that, too.

4 Likes

OK, I understand.
Thanks for getting back to me.
I'll wait for future release.

Thank you for the fix in SDK version 1.12.0.
My boring test stuff now works!

playdate-20220617-133607

3 Likes

Text viewer looks great!

1 Like