LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices


Rate this Entry

GTK Resource Compilation Utility

Posted 04-09-2015 at 02:30 PM by rainbowsally
Updated 04-22-2015 at 01:41 PM by rainbowsally (text formatting probs)

GTK Resource Compilation.

There's a utility called 'glib-compile-resources' that takes an xml file that is basically a list of resources such as glade ui files, and it outputs c code to embed the data in the application. It's messy to use, but it works.

There may be another messy gtk/glib utility to create these xml files that 'glib-compile-resources' can use, but if there is here's another one that may or may not be easier to use.

file: src/gtk.mkres.cpp
purpose: utility to create gtk <name>gresource.xml files for gtk ui files.
Code:
// main.cpp -- skeleton created by new.main

#include <stdio.h>
#include <malloc.h>
#include <string.h>

void dbg(){} // for a non-moving breakpoint

#define streq(a, b) (strcmp(a, b) == 0)
#define VERSION_MAJOR 1
#define VERSION_MINOR 0

int usage(int errcode);
int print_version();
void write_file(FILE* fp, int nargs, char** args, char* prefix, char* appname);

int main(int argc, char** argv)
{
  dbg();
  
  char appname[64];
  char prefix[64];
  char outfile[64];
  
  // usage checks
  if(argc == 2)
  {
    if(streq(argv[1], "--help"))
      return usage(0);
    if(streq(argv[1], "--version"))
      return print_version();
    else
      return usage(1);
  }
  if(argc < 4)
    return usage(1);
  
  //////////////////////////
  // args checks
  strcpy(appname,argv[1]);
  strcpy(prefix, argv[2]);
  
  // appname is not a file name
  if((strchr(appname, '.'))||strchr(appname, '/'))
    return usage(1);
  if(prefix[0] != '/')
    return usage(1);
  if(prefix[strlen(prefix)-1] == '/')
    prefix[strlen(prefix)-1] = 0;
  
  /////////////////////////
  // write output file
  
  sprintf(outfile, "%s.gresource.xml", appname);
  
  FILE* fp = fopen(outfile, "w");
  if(!fp)
  {
    fprintf(stderr, "Can't open output file %s\n", outfile);
    return 1;
  }
  
  write_file(fp, argc-3, argv+3, prefix, appname);
  fclose(fp);
  
  return 0;
}

int usage(int errcode)
{
#include "usage_str.dat"
  fprintf( errcode == 0 ? stdout : stderr, "%s", usage_str);
  return errcode;
}

int print_version()
{
  printf("Version %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
  return 0;
}

void write_file(FILE* fp, int nargs, char** args, char* prefix, char* appname)
{
  ///////////////////////////////
  // write header
  fprintf(fp,
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  "<gresources>\n"
  "  <gresource prefix=\"%s/%s\">\n",
  prefix, appname
  );
  
  // write body
  for(int i = 0; i < nargs; i++)
    fprintf(fp, 
  "    <file preprocess=\"xml-stripblanks\">%s</file>\n", args[i]
    );

  // write footer
  fprintf(fp, "%s",
  "  </gresource>\n"
  "</gresources>\n"
  );
}

file: src/usage_str.dat
purpose: usage text.
Code:
// This file was created with txt2str
const char* usage_str =
  "Usage: mk.res <appname> <respath> <input_file(s).ui>\n"
  "  Where \n"
  "    appname \n"
  "      This is the name of the application iself\n"
  "    respath \n"
  "      This is the path starting with '/' for the resource,\n"
  "      for example /org/gtk. The resouces will be addressed \n"
  "      as <respath>/<appname>, for example using the above\n"
  "      example the address will be /org/gtk/<appname>\n"
  "      in the resulting resouce file.\n"
  "    input_file(s) \n"
  "      These may be *.ui or individually listed.  There must \n"
  "      be at least one inputfile.\n"
  "\n"
  ;

file: mc2.def
purpose: Makefile definitions (see mc2).
Code:
# mc2.def template created with Makefile Creator 'mc2'

## sandbox path and other new variables
PREFIX = $(HOME)/usr
BUILDDIR := $(PWD)

# output name override
OUTNAME = gtk.mkres

SRCDIR = src
OBJDIR = o
BINDIR = .

# what COMPILE should do
COMPILE = g++ -c -o # COMPILE <output_file> ...
# CFLAGS = -Wall -g3 # debug
CFLAGS = -Wall -O2 # optimized
INCLUDE = -I $(SRCDIR) -I$(PREFIX)/include -I /usr/include 

# what LINK should do
LINK = g++ -o # LINK <output_file> ...
LDFLAGS = 
LIB = -L/usr/lib -L$(PREFIX)/lib

# additional targets
#mc2-update:
#  @mc2 -update

semiclean: $(EXT_SEMICLEAN)
  @rm -f $(OBJ)
  @rm -f *.kdevelop.pcs *.kdevses
  @rm -f *~ */*~ */*/*~ */*/*/*~

strip:
  @strip $(MAIN)
  @make semiclean

clean: $(EXT_CLEAN)
  @rm -f $(MAIN)
  @rm -f $(OBJ)
  @rm -f *.kdevelop.pcs *.kdevses
  @rm -f *~ */*~ */*/*~ */*/*/*~ tmp.mak

Makefile: mc2.def
  mc2 -update
  sleep 2

mc2.def:

force: # used to force execution
In case you don't have or can't use mc2, here's the Makefile compressed as Makefile.tar.xz
and encoded in base64.

To extract it type first create the tar.xz file like so.
Code:
base64 -d Makefile.tar.xz.base64 > Makefile.tar.xz
And then
Code:
tar -xaf Makefile.tar.xz
to get the Makefile.

file: Makefile.tar.xz.base64
purpose: compressed Makefile
Code:
/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4A//BA5dACaYSWZp2dmN5MBFtRePdUOjo4dtjvAfsZp8
PfHcI/1gP+f6yepgUmfwNbUOOPFeZNCCiGWQUbEEGoQMPy1KaWpVnoa+LpgJC7nikbHZBnICRkGO
+kr8xiFXK8FoxI9rZV9nzERg+p3XIDeU5280YCtr8SIfCICsC3RL95S/dSyQR/hu0stQiKfOqb18
gPDfOw3NKmKm7DgiV0bmaL1KYi0xs+DpwtIown+DAGf+uHEJ7aHNGYeNoK9Qd3/YeXs5WEx8IPaZ
rEB03izu93NTbarKT81Taw2viMW6XNc1+I9q1Mq27yYMT/8rm3AuiwcEdQloXccnEcvCRx7x26iW
kJsbp+3B9skSq9N9bV7TBFxrPzta6VBVW3oIS5Zt3ENr0kG41M0aHe5S6FLeHoDC5aHBvzi60pzm
EgcuclV7pL/XzjfJyIw3TT/dUITzHR+oN/bNXdEYuzCxj6beCIoir1ZiabDoiGSFHdo+Eqd2MLUZ
TZZwkAC1qxn0O/CQ7OJgysbVbjKIKTgTIVFDpavLZC1JHyiGWei4eSfEfSbpVcssuupS/wXCfofG
zJx4d3ZLOHZvt5xaDO6E6WDAYEpbv8qdFKi8awlytFiXOgh5YiDy5a1+iBjYVmizSFiGypTRZOI8
gWiMArzbWt0uFCzUfZ87TC5llyVN9tvwsJiKELqaWXDRaedHkIkXoQEQtEI8O0O98x9JIzHpajQJ
QnD+ubnbfrV/B2JBOCdpJ4XN6GvyWylUJkmyS73YhScirXKom5zGf6fHe3M/Gr3XfRT5JWPzm1E0
MU4j8l0VDEqtjIu1sR1QEq4J/iElHN5hdn+uVPBJmpechCz8v7h43P7WrDx/lhA0xEbw9DO2AAI1
4JQ5Np2HM9db5SkKBuNVVb1P2h0JuJZRyG57pG3JusIrZ5c09a5TxCV4EXBOpwiXSmY+xtYTeDj+
6t8I6K03oNFoxoco87JaaDDzuMD1+hKSfaKN2b0P1fgsmmp1xz/7SRfb0NoHpKfNFRZLFpk6dXMQ
v201hBvNJ/5rN9KZ35xm2GTXg5F3DMByDpvAzdxWOHBACEuEzb3X0K5fmj34jje6QkhRtmD1xVhj
Lx7iiO/l68Va7A/gdVzytuKMFmAkp7FLdzZPmVy3duRjsQum/RhG3UneX6/jAARfDz7dnHHPCjlj
LFzwpJB1Z69fa0vw7dCMMKjFEKI0S1tSDoJjYmdgFQlSGRGfrNbZ3cibvXuRGYSzLilKm9SodgsJ
Qb+f266mhSFULcStHqQYyhb4Ns+eJffJhACioyn47AVnpa6DU4gqUYq1TDPxAgM80ecEpaTQV/WW
fhBlMXwafQl1ycAIDcTltXwZnMWNnPk4/gOQIKhbNLmRZd/KHBjLtXYOAAAAAISeQyrNSF5WAAGq
CIAgAABPXbaescRn+wIAAAAABFla
Posted in Uncategorized
Views 2282 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration