| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# Environment.cpp

Go to the documentation of this file.

    00001 #include "GaudiKernel/Environment.h"
    00002 #include <cstdlib>
    00003
    00004 namespace {
    00005   std::string i_resolve(std::string& source, int recursions)  {
    00006     if ( recursions > 0 )  {
    00007       int lvl = 0, mx_lvl = 0;
    00008       for(const char* c=source.c_str(), *beg=0; *c != 0; ++c)  {
    00009         switch ( *c )  {
    00010           case '$':
    00011             if ( *(c+1) == '{' )  {
    00012               lvl++;
    00013               if ( lvl > mx_lvl )  {
    00014                 mx_lvl = lvl;
    00015                 beg = c;
    00016               }
    00017               c += 2;
    00018             }
    00019             break;
    00020           case '}':
    00021             if ( lvl == mx_lvl )  {
    00022               std::string env(beg+2,c-beg-2);
    00023               const char* res = ::getenv(env.c_str());
    00024               std::string rep = (res) ? std::string(res) : i_resolve(env,--recursions);
    00025               if ( rep.length() )  {
    00026                 std::string e(beg,c-beg+1);
    00027                 size_t idx=std::string::npos;
    00028                 while((idx=source.find(e)) != std::string::npos)  {
    00029                   source.replace(idx, e.length(), rep);
    00030                 }
    00031                 return i_resolve(source, --recursions);
    00032               }
    00033               else  {
    00034                 // error: environment cannot be reolved....
    00035                 // Try to continue, but there is not too much hope.
    00036               }
    00037             }
    00038             lvl--;
    00039             break;
    00040           default:
    00041             break;
    00042         }
    00043       }
    00044     }
    00045     return source;
    00046   }
    00047 }
    00048
    00049 StatusCode System::resolveEnv(const std::string& var, std::string& res, int recursions)  {
    00050   std::string source = var;
    00051   res = i_resolve(source, recursions);
    00052   if ( res.find("${") == std::string::npos ) {
    00053     return StatusCode::SUCCESS;
    00054   }
    00055   return StatusCode::FAILURE;
    00056 }
    00057
    00058 std::string System::homeDirectory()  {
    00059    // Return the user's home directory.
    00060   std::string home_dir = "./";
    00061   const char *h = 0;
    00062   if ( 0 == (h = ::getenv("home")))  {
    00063     h = ::getenv("HOME");
    00064   }
    00065   if ( h ) {
    00066     home_dir = h;
    00067   }
    00068   else {
    00069     // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
    00070     //                                         or     $(HOMEDRIVE)/$(HOMEPATH)
    00071     h = ::getenv("HOMESHARE");
    00072     if ( 0 == h )  {
    00073       h = ::getenv("HOMEDRIVE");
    00074     }
    00075     if (h) {
    00076       home_dir = h;
    00077       h = ::getenv("HOMEPATH");
    00078       if( h )  {
    00079         home_dir += h;
    00080       }
    00081     }
    00082   }
    00083   return home_dir;
    00084 }
    00085
    00086 std::string System::tempDirectory()   {
    00087    // Return a user configured or systemwide directory to create
    00088    // temporary files in.
    00089    const char *dir = ::getenv("TEMP");
    00090    if (!dir)   dir = ::getenv("TEMPDIR");
    00091    if (!dir)   dir = ::getenv("TEMP_DIR");
    00092    if (!dir)   dir = ::getenv("TMP");
    00093    if (!dir)   dir = ::getenv("TMPDIR");
    00094    if (!dir)   dir = ::getenv("TMP_DIR");
    00095    if (!dir) return homeDirectory();
    00096    return dir;
    00097 }