Wednesday, September 23, 2009

GtkBuilder XML to .h file

Here is a little perl script to convert a GtkBuilder .xml file as generated by libglade3 to a .h file. Kind of in the style of gdk-pixbuf-csource.
#!/usr/bin/perl
$var = $ARGV[0];
open IN, "<$var";
$var =~ s/\./_/g;
print "static const char $var\[\] =\n";

while() {
$line = $_;
chomp $line;
$line =~ s/\"/\\\"/g;
if (/<\/interface>/) {
print "\"$line\";\n";
} else {
print "\"$line\"\n";
}
}
close IN;

Use it like this
xmltoh.pl GtkBuilder.xml > GtkBuilder.h
And then #include "GtkBuilder.h" and you should have a variable name GtkBuilder_xml that is a string that holds the xml that gtk_builder_add_from_string can use.

No comments: