All - anyone opened and read a text file recently with the new asset system. Been playing around with this but can’t get it right… general approach I am looking for is to open a file and read it into an array then I can interpret each line separately, with Lua seems to be:
local file = io.open("your-file-name", "r");
local data = file:read("*a")
local file = io.open("file-name", "r");
local arr = {}
for line in file:lines() do
table.insert (arr, line);
end
Anyone tried this in Codea? Every time I get into io.open etc I struggle to get the syntax correctly. The other thing is that Codea treats. File.txt as file_txt in the asset system.
@Bri_G Here’s some code to do a hex dump. Change the file name in readText().
-- hex dump
viewer.mode=FULLSCREEN
function setup()
dy=0
font("Courier")
textMode(CORNER)
fill(255)
tab={}
str=readText(asset.documents.Dropbox.text1)
end
function draw()
background(0)
for z=1,#str,16 do
text(string.format("%5d:",z-1),30,HEIGHT-25*(z//16+1)+dy)
for s=1,16 do
v=str:byte(z+s-1)
if v~=nil then
ch=""
if v>31 and v<128 then
ch=string.char(v)
end
if s<9 then
a=string.format("%02x ",v)
text(a,25*s+100,HEIGHT-25*(z//16+1)+dy )
text(ch,12*s+550,HEIGHT-25*(z//16+1)+dy )
if s==8 then
text("-",25*s+123,HEIGHT-25*(z//16+1)+dy )
end
else
a=string.format("%02x ",v)
text(a,25*s+110,HEIGHT-25*(z//16+1)+dy )
text(ch,12*s+550,HEIGHT-25*(z//16+1)+dy )
end
end
end
end
end
function touched(t)
dy=dy+t.deltaY
if dy<0 then
dy=0
end
end
function setup()
tab={}
file=os.getenv("HOME").."/Documents/Dropbox.assets/text1.txt"
readBinary()
print(table.concat(tab," "))
print("done")
end
function draw()
background(40,40,50)
end
function readBinary()
local bFd = io.open(file,"rb")
local bData = ""
if bFd then
while true do
local data = bFd:read(512) -- Read 512 bytes at a time
if not data then
break
end
bData = bData..data
end
bFd:close()
end
for i=1,string.len(bData) do
x=bData:byte(i)
if x>32 and x<128 then
table.insert(tab,string.format("%02x-%s ",x,string.char(x)))
else
table.insert(tab,string.format("%02x-- ",x))
end
end
end
@dave1707 - wow, pretty impressive but not quite what I am seeking. But, the addressing in setup() was interesting. I was looking at io.lines and that may be the key I needed. Also, I have managed to read the file line by line by using readText(). Now looking at deciphering the text in each line trying to re-write it in another format. Thanks for that.