Page 1 of 1

[wxLua] wxArchiveFSHandler

Posted: Sat May 27, 2017 6:31 am
by django013
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?

Re: [wxLua] wxArchiveFSHandler

Posted: Sat May 27, 2017 7:58 am
by doublemax
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.

Re: [wxLua] wxArchiveFSHandler

Posted: Sat May 27, 2017 3:19 pm
by django013
Thank you for your assistance!

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
but that's lua specific so may be I have to ask for help at different location.

Re: [wxLua] wxArchiveFSHandler

Posted: Sat May 27, 2017 6:32 pm
by doublemax
Can you show the code you used?

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 3:33 am
by django013
sure!

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
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.

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 8:19 am
by doublemax
I'm not sure about the syntax to call methods in Lua, but this at least didn't throw any error:

Code: Select all

local ah = wx.wxArchiveFSHandler()
wx.wxFileSystem.AddHandler( ah ) 
wxFileSystem::AddHandler() is a static method.

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 9:34 am
by django013
I got a bit further.
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
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?

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 12:58 pm
by doublemax
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.

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 2:04 pm
by django013
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?

Re: [wxLua] wxArchiveFSHandler

Posted: Sun May 28, 2017 3:41 pm
by doublemax
...or who takes care about the resources?
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.

Re: [wxLua] wxArchiveFSHandler

Posted: Mon May 29, 2017 3:04 pm
by django013
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:

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 :D

Woohoo - thanks a lot for your help :)