[wxLua] wxArchiveFSHandler

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

[wxLua] wxArchiveFSHandler

Post 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?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxLua] wxArchiveFSHandler

Post 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.
Use the source, Luke!
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

Re: [wxLua] wxArchiveFSHandler

Post 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.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxLua] wxArchiveFSHandler

Post by doublemax »

Can you show the code you used?
Use the source, Luke!
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

Re: [wxLua] wxArchiveFSHandler

Post 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.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxLua] wxArchiveFSHandler

Post 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.
Use the source, Luke!
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

Re: [wxLua] wxArchiveFSHandler

Post 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?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxLua] wxArchiveFSHandler

Post 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.
Use the source, Luke!
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

Re: [wxLua] wxArchiveFSHandler

Post 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?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: [wxLua] wxArchiveFSHandler

Post 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.
Use the source, Luke!
django013
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sat May 27, 2017 6:15 am

Re: [wxLua] wxArchiveFSHandler

Post 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 :)
Post Reply