| # Gaudi Framework, version v23r1 |

Home |  | Generated: Wed Feb 29 2012

# Environment.cpp

Go to the documentation of this file.

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines