/**/
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#ifdef __STDC__
#include <string.h>
#else
#include <strings.h>
#endif

#ifndef NULL
#define NULL 0
#endif

#define BUFSIZ 1024

#ifndef APPNAME
#define APPNAME "vtwm."
#endif

#ifndef APPCLASS
#define APPCLASS "Twm."
#endif

char *getenv();

XrmDatabase GetResourceDatabase(dpy)
     Display *dpy;
{
   /* resources get searched in the following order:

     - command line specified
     - RESOURCE_MANAGER user resources
     - XENVIRONMENT file
     - app-defaults

     since we're not dealing with the first or the last, this is easy
     to deal with */

   XrmDatabase rmdb, xedb;
   char *filename;

   if (dpy->xdefaults != NULL)
      rmdb = XrmGetStringDatabase(dpy->xdefaults);
   else
      rmdb = NULL;

   if (filename = getenv("XENVIRONMENT")) {
      xedb = XrmGetFileDatabase(filename);
      XrmMergeDatabases(xedb, &rmdb);
   }

   return(rmdb);
}

char *GetStringResource(db, name, class)
     XrmDatabase db;
     char *name;
     char *class;
{
   char *type;
   XrmValue val;
   char fname[BUFSIZ], fclass[BUFSIZ];

   strcpy(fname, APPNAME);
   strcat(fname, name);
   strcpy(fclass, APPCLASS);
   strcat(fclass, class);

   if (XrmGetResource(db, fname, fclass, &type, &val)) {
      if (strcmp(type, "String") == 0)
	 return(val.addr);
   }

   return(NULL);
}
