Search code examples
c++gtkmm

gtkmm load from resource plain text file


I have a simple resource xml file looks like

<?xml version="1.0" encoding="UTF-8" ?>
<gresources>
    <gresource prefix="ui"> 
        <file preprocess="xml-stripblanks">ui.glade</file>
    </gresource>
    <gresource prefix="text-data">
        <file>definitions.txt</file>
    </gresource>
</gresources> 

And UI loaded without problem

refBuilder->add_from_resource("/ui/ui.glade");

But I do not know how to load and read the text file that is defined in the resource file as "definition.txt".


Solution

  • You are able to retrieve the file contents with one of two static methods of Gio::Resource class: open_stream_global or lookup_data_global. Here are their signatures:

    • Glib::RefPtr< InputStream > open_stream_global (const std::string& path, ResourceLookupFlags lookup_flags=RESOURCE_LOOKUP_FLAGS_NONE)
    • Glib::RefPtr< const Glib::Bytes > lookup_data_global (const std::string& path, ResourceLookupFlags lookup_flags=RESOURCE_LOOKUP_FLAGS_NONE)

    As you can see, the difference between them is how you access data: via a Gio stream class or Glib::Bytes (a "data array": byte contents + length).