[wxLua] wxArchiveFSHandler
[wxLua] wxArchiveFSHandler
Hi,
I'm new to wx and lua - and I'm looking for a way to access files from archive.
Now I happened to read about wxArchiveFSHandler - I found the section in the docs, but I did not find any sample with context and I have no idea on how to use filesystems or their handlers.
Can anybody please gimme a hand on how to enumerate files from archive and how to extract a file from archive?
I'm new to wx and lua - and I'm looking for a way to access files from archive.
Now I happened to read about wxArchiveFSHandler - I found the section in the docs, but I did not find any sample with context and I have no idea on how to use filesystems or their handlers.
Can anybody please gimme a hand on how to enumerate files from archive and how to extract a file from archive?
Re: [wxLua] wxArchiveFSHandler
Here's an example in C++ that reads a file from a ZIP archive.
viewtopic.php?p=173631#p173631
To enumerate files use wxFileSystem::FindFirst/FindNext.
Hopefully that's enough so you can translate it to Lua.
viewtopic.php?p=173631#p173631
To enumerate files use wxFileSystem::FindFirst/FindNext.
Hopefully that's enough so you can translate it to Lua.
Use the source, Luke!
Re: [wxLua] wxArchiveFSHandler
Thank you for your assistance!
I failed on adding the handler:
but that's lua specific so may be I have to ask for help at different location.
I failed on adding the handler:
Code: Select all
archive.wx.lua:31: wxLua: Expected a 'wxFileSystemHandler' for parameter 1, but got a 'userdata'.
Function called: 'AddHandler(wxFileSystem, wxArchiveFSHandler)'
01. wxFileSystem::AddHandler(wxFileSystemHandler) - static
Re: [wxLua] wxArchiveFSHandler
sure!
For my usecase I have no need to save the extracted file to disk. I only need a way to use it as bitmap for wxBitmapButtons.
Code: Select all
function extract(archive, archiveEntry, newName)
local fs = wx:wxFileSystem()
local ah = wx:wxArchiveFSHandler()
local archiveType = '#zip:'
fs:AddHandler(ah)
local ae = fs:OpenFile(archive .. archiveType .. archiveEntry)
if (ae ~= nil) then
local bitmap = ae:read('*all')
local nbm = io.open(newName, 'wb')
if (nbm ~= nil) then
nbm:write(bitmap)
nbm:close()
end
ae:close()
end
end
Re: [wxLua] wxArchiveFSHandler
I'm not sure about the syntax to call methods in Lua, but this at least didn't throw any error:
wxFileSystem::AddHandler() is a static method.
Code: Select all
local ah = wx.wxArchiveFSHandler()
wx.wxFileSystem.AddHandler( ah )
Use the source, Luke!
Re: [wxLua] wxArchiveFSHandler
I got a bit further.
This code works now:
but I have hardcoded the size of the extracted entry.
Didn't find a way yet to retrieve the size.
What's the next step?
When I have a variable bitmap with the extracted image, how can I pass that bitmap to a bitmap-button at creation time?
This code works now:
Code: Select all
function extract(archive, archiveEntry, newName)
local fs = wx:wxFileSystem()
local ah = wx:wxArchiveFSHandler()
local archiveType = '#zip:'
wx.wxFileSystem.AddHandler(ah)
local ae = fs:OpenFile(archive .. archiveType .. archiveEntry)
if (ae ~= nil) then
local stream = ae:GetStream()
-- local size = ae:Length()
local size = 9172 -- extracted size
local bitmap = stream:Read(size)
-- local bitmap = stream:ReadAll() -- unknown method :(
if (bitmap ~= nil) then
local nbm = io.open(newName, 'wb')
if (nbm ~= nil) then
nbm:write(bitmap)
nbm:close()
end
else
print('failed to read from archive!!!')
end
-- ae:Close()
end
end
Didn't find a way yet to retrieve the size.
What's the next step?
When I have a variable bitmap with the extracted image, how can I pass that bitmap to a bitmap-button at creation time?
Re: [wxLua] wxArchiveFSHandler
You don't need the length.
A wxImage can be contructed and loaded from an input stream.
http://docs.wxwidgets.org/trunk/classwx ... 820342d91a
Afterwards convert the wxImage to wxBitmap.
A wxImage can be contructed and loaded from an input stream.
http://docs.wxwidgets.org/trunk/classwx ... 820342d91a
Afterwards convert the wxImage to wxBitmap.
Use the source, Luke!
Re: [wxLua] wxArchiveFSHandler
Thanks a lot for your help! Sounds great!
I'm a bit alienated about resource usage. I found no way to close stream or archive.
I also tried to read the archive-entry in chunks, but there's no feedback on success or size ...
The stream continues to read past the end of entry (I wonder, what it reads so far?) without error and the length of the stream is always the chunksize ...
Nothing at all like coding with real languages like c++ ...
So if I read 10 bitmaps from an archive - do I have 10 open streams as busy resource, or who takes care about the resources?
I'm a bit alienated about resource usage. I found no way to close stream or archive.
I also tried to read the archive-entry in chunks, but there's no feedback on success or size ...
The stream continues to read past the end of entry (I wonder, what it reads so far?) without error and the length of the stream is always the chunksize ...
Nothing at all like coding with real languages like c++ ...
So if I read 10 bitmaps from an archive - do I have 10 open streams as busy resource, or who takes care about the resources?
Re: [wxLua] wxArchiveFSHandler
It would be wxLua's job to destroy the objects that are not referenced any longer. But i don't know anything about (wx)Lua memory management....or who takes care about the resources?
Use the source, Luke!
Re: [wxLua] wxArchiveFSHandler
Hi,
bad news from here
First: wxLua does not have the constructor for loading images from streams (at least the version I'm bound to).
Then I followed your advice from your footer and I searched the wxLua sources. There I discovered the call LoadFile, which takes a stream argument.
With that I got a bit further, but no success.
Now it fails with:
// Edith says:
giving the image type to LoadFile is the key
Woohoo - thanks a lot for your help
bad news from here
First: wxLua does not have the constructor for loading images from streams (at least the version I'm bound to).
Then I followed your advice from your footer and I searched the wxLua sources. There I discovered the call LoadFile, which takes a stream argument.
With that I got a bit further, but no success.
Now it fails with:
Code: Select all
Can't automatically determine the image format for non-seekable input
// Edith says:
giving the image type to LoadFile is the key
Woohoo - thanks a lot for your help