Page 1 of 1

looking for wxTextInputStream replacement for binary files

Posted: Mon Apr 20, 2020 7:29 pm
by maximand
Hello there,

I am looking a piece of code to replace wxTextInputStream to something for binary data.
I don't understand how to use wxMemoryBuffer. Please advice.
In resources there are the following files: ChartNew.js, format.js, home_page.htt, kaching.wav, master.css, mmex.ico, sorttable.js
Thanks in advance

Code: Select all

    //Read files from resources
    const wxString res_dir = mmex::GetResourceDir().GetPathWithSep();
    wxArrayString files_array;
    wxDir::GetAllFiles(res_dir, &files_array);
    for (const auto& source_file : files_array)
    {
        wxString data;
        if (wxFileName::FileExists(source_file))
        {
            wxFileInputStream input(source_file);
            wxTextInputStream text(input);

            while (input.IsOk() && !input.Eof()) {
                data += text.ReadLine() + "\n";
            }
            const auto file_name = wxFileName(source_file).GetFullName();
            wxLogDebug("File: %s has been copied to VFS", file_name);
            wxMemoryFSHandler::AddFile(file_name, data);
        }
    }

Re: looking for wxTextInputStream replacement for binary files

Posted: Mon Apr 20, 2020 7:55 pm
by New Pagodi
You should be able to use wxMemoryOutputStream. I haven't tested this, but this should work:

Code: Select all

if (wxFileName::FileExists(source_file))
{
    wxFileInputStream input(source_file);

    wxMemoryOutputStream memOut(NULL);
    input.Read(memOut);
    wxStreamBuffer* buffer = memOut.GetOutputStreamBuffer();

    const auto file_name = wxFileName(source_file).GetFullName();
    wxLogDebug("File: %s has been copied to VFS", file_name);

    wxMemoryFSHandler::AddFile(file_name, buffer->GetBufferStart(),
                               buffer->GetBufferSize());
}
By initializing wxMemoryOutputStream with NULL, the stream will clean up its buffer on its own. Make sure to add "#include <wx/mstream.h>" at the top of the file to use the class.

Re: looking for wxTextInputStream replacement for binary files

Posted: Mon Apr 20, 2020 8:41 pm
by maximand
Great. It's working. Thank you very much.