--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 12b / External Resources
--
--------------------------------------------------- 
--
-- You can add also external resources that won't be packed inside the internal one data file
-- This tutorial shows how to handle them
--
-- Debug notes: every time you launch your application, all external resources files will be
-- copied inside debug running directory (usually fba_win32), so everytime you launch your application
-- from the windows IDE, all resources files will be overwritten with the original one.
-- This means you can't use them to store persistent data between debug sessions.
--------------------------------------------------- 


-- open and read/write a value from an external resource file
openconfigext("myextconfig.ini")

times = readconfignumber("options", "myvalue", 0)
debug("Value inside external resource config file is: " .. times )

debug("now I increment it....")
writeconfignumber("options", "myvalue", times+1)

-- debug note: every time we launch this application, this resource will be overwritten
-- and the counter will start from 0
times = readconfignumber("options", "myvalue", 0)
debug("New value inside external resource config file is: " .. times )

closeconfig()



-- open a resource file from external resources. 
openresext("testfile.txt", true)

-- print each splitted line
--
for l=0, getresline()-1 do
        debug("Line " .. l+1 .. " " .. getresline(l))
end
        
closeres()



-- try to open a binary file from a not existent external resource (this will fail)
handle = openfile("my_not_existant_resource.dat", "read", true)

-- now open a binary file from an existant external resource
handle = openfile("binarytest.dat", "write", true)

-- tell use the file size
posfile(handle, 0, "e")
debug("File size is: " .. posfile(handle) )

-- close and reopen file
closefile(handle)
handle = openfile("binarytest.dat", "write", true)

-- append 2 bytes
writefile(handle, "12")

-- read the new file size
posfile(handle, 0, "e")
debug("New file size is: " .. posfile(handle) )


-- finally close, reopen the file and read all content!
closefile(handle)
handle = openfile("binarytest.dat", "read", true)
-- read size
posfile(handle, 0, "e")
filesize = posfile(handle)
-- set file pointer to first byte
posfile(handle, 0, "s")
-- read the file
content = readfile(handle, filesize)
closefile(handle)

debug("File content is: " .. content)

function onmainloop()
end
