looking for wxTextInputStream replacement for binary files Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
maximand
Experienced Solver
Experienced Solver
Posts: 79
Joined: Fri Nov 11, 2011 5:44 pm
Location: Russia

looking for wxTextInputStream replacement for binary files

Post 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);
        }
    }
M$, VS2017, C++
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 469
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: looking for wxTextInputStream replacement for binary files

Post 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.
maximand
Experienced Solver
Experienced Solver
Posts: 79
Joined: Fri Nov 11, 2011 5:44 pm
Location: Russia

Re: looking for wxTextInputStream replacement for binary files

Post by maximand »

Great. It's working. Thank you very much.
M$, VS2017, C++
Post Reply