[GRASS5] A GRASS user configuration directory...

Eric G. Miller egm2 at jps.net
Fri Feb 23 21:02:13 EST 2001


On Fri, Feb 23, 2001 at 06:56:51PM +0700, Justin Hickey wrote:
> Hi Eric
> 
> "Eric G. Miller" wrote:
> > 
> > I've done a bit of the work to handle a user's configuration directory
> > in $HOME/.grass.  I don't know if this is something we want to mess 
> > with now, or wait until later.  Basically, I have one callable
> > function so far: G_rc_path(char *elems, char *item).  It works similar
> > to the G_element() types of functions, but it does several things.
> 
> I'm not familiar with G_element(). Could you please provide a brief
> overview of how G_rc_path() works? Maybe with an example?

Well, there really isn't a G_element() function, I was kind of
generalizing the concept of mapset elements used in various open/find
type routines.  There is the G__make_mapset_element()...

Anyway, for a short example.  Say I wanted to get the path for the x0
socket file, that lives or will live in $HOME/.grass/com.  I'd just
do something like:

  char *path;
  ...
  path = G_rc_path ("com", "x0");

If $HOME/.grass  didn't exist, it would be created.
If $HOME/.grass/com didn't exist, it would be created.
No test for existence is done for the file (since it's not known if it
will be a new file or what).

I've already tested it out here with the XDRIVER (using sockets
obviously) and it works.  A similar approach could be done for temporary
files by having G_tempfile() do something like:

char * 
G_tempfile(void)
{
  int i, status;
  pid_t pid;
  char *path, name[30];
  struct stat buf;

  pid = getpid();
  i = 0;
  path = NULL;
  do {
    snprintf (name, 30, "%d.%d", pid, i);
    G_free (path);
    path = G_rc_path ("tmp", name); /* should check for NULL return */
    status = lstat (path, &buf);  
    i++;
  } while (status == 0); /* file exists... */
  
  return path;
}
  
At some point, it would return a path like $HOME/.grass/tmp/12345.7

All in all, it's pretty simple.  Most the code I wrote for this spends
time doing sanity testing on input and making directories as needed.

-- 
Eric G. Miller <egm2 at jps.net>

---------------------------------------- 
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo at geog.uni-hannover.de with
subject 'unsubscribe grass5'



More information about the grass-dev mailing list