| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

# Environment.cpp

Go to the documentation of this file.

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