RE: [ROOT] finding of a configuration file (second call for help)

From: Philippe Canal (pcanal@fnal.gov)
Date: Wed Jan 30 2002 - 18:49:24 MET


Hi Jacek,

> Now, if it makes anything simpler - assume that this will be done while
> loading the library - just in the constructor of a "global static instance
> of some class". If it makes things easier - assume the OS is Linux.

If you are going to be that restrictive, it is 'easy' :)
Here is a simple class that is able to detect with library it
is being loaded as a global static instance:

#ifndef __CINT__
#include "link.h"
#endif

#include <stdio.h>

#ifndef __CINT__
class ResourceLoader {
public:

  const char* find(void *what) {

    struct link_map *current = _r_debug.r_map;

    while( current ) {
      if (current->l_name && strlen(current->l_name) && 
          (void*)current->l_map_start<=what && 
           what<(void*)current->l_map_end) {
         return current->l_origin;
      }
      current = current->l_next;
    }
    return 0;
  }

  ResourceLoader() {
     const char * directory = find(this);
     if ( directory ) {
        fprintf(stderr,"Load configuration from %s\n",directory);
     }
  }
};

ResourceLoader t;
#endif

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Jacek M. Holeczek
Sent: Wednesday, January 30, 2002 2:17 AM
To: RootTalk
Subject: [ROOT] finding of a configuration file (second call for help)


Assume you have a shared library somewhere ... you load it ... now assume
you need to "read" a configuration file which should be placed in the same
subdirectory that the library comes from. Is there any easy way to learn
from which subdirectory the shared library has been loaded ?
Now, typically, you don't even know the name of the library - the only
assumption is that - this is the same library that provides the function
which tries to "read" the configuration file - so, first the function
should find from which shared library it comes, then from which
subdirectory it has been loaded, then find the configuration file there.
Now, if it makes anything simpler - assume that this will be done while
loading the library - just in the constructor of a "global static instance
of some class". If it makes things easier - assume the OS is Linux.
One should not rely on any "environment variables", nor that one will 
define the "directory" during compilation.
(Everything happens in ROOT, of course.)
Thanks in advance,
Best regards,
Jacek.
P.S. Well, I was told that "Microsoft does provide a function: the
     GetModuleFileName function retrieves the full path and file name for
     the file containing the specified module", but it doesn't seem to me
     to be a sufficient reason to get in love with Windoze. Jacek.



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET