XrdOucEnv.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                          X r d O u c E n v . c c                           */
00004 /*                                                                            */
00005 /* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010   
00011 //         $Id: XrdOucEnv.cc 38011 2011-02-08 18:35:57Z ganis $
00012 
00013 const char *XrdOucEnvCVSID = "$Id: XrdOucEnv.cc 38011 2011-02-08 18:35:57Z ganis $";
00014 
00015 #include "string.h"
00016 #include "stdio.h"
00017 #include <stdlib.h>
00018 
00019 #include "XrdOuc/XrdOucEnv.hh"
00020   
00021 /******************************************************************************/
00022 /*                           C o n s t r u c t o r                            */
00023 /******************************************************************************/
00024   
00025 XrdOucEnv::XrdOucEnv(const char *vardata, int varlen, 
00026                      const XrdSecEntity *secent)
00027                     : env_Hash(8,13), secEntity(secent)
00028 {
00029    char *vdp, varsave, *varname, *varvalu;
00030 
00031    if (!vardata) {global_env = 0; global_len = 0; return;}
00032 
00033 // Copy the the global information (don't rely on its being correct)
00034 //
00035    if (!varlen) varlen = strlen(vardata);
00036    global_env = (char *)malloc(varlen+2); global_len = varlen;
00037    if (*vardata == '&') vdp = global_env;
00038       else {*global_env = '&'; vdp = global_env+1;}
00039    memcpy((void *)vdp, (const void *)vardata, (size_t)varlen);
00040    *(vdp+varlen) = '\0';
00041    vdp = global_env;
00042 
00043 // scan through the string looking for '&'
00044 //
00045    if (vdp) while(*vdp)
00046         {if (*vdp != '&') {vdp++; continue;}    // &....
00047          varname = ++vdp;
00048 
00049          while(*vdp && *vdp != '=') vdp++;  // &....=
00050          if (!*vdp) break;
00051          *vdp = '\0';
00052          varvalu = ++vdp;
00053 
00054          while(*vdp && *vdp != '&') vdp++;  // &....=....&
00055          varsave = *vdp; *vdp = '\0';
00056 
00057          if (*varname && *varvalu)
00058             env_Hash.Rep(varname, strdup(varvalu), 0, Hash_dofree);
00059 
00060          *vdp = varsave; *(--varvalu) = '=';
00061         }
00062    return;
00063 }
00064 
00065 /******************************************************************************/
00066 /*                               D e l i m i t                                */
00067 /******************************************************************************/
00068 
00069 char *XrdOucEnv::Delimit(char *value)
00070 {
00071      while(*value) if (*value == ',') {*value = '\0'; return ++value;}
00072                       else value++;
00073      return (char *)0;
00074 }
00075  
00076 /******************************************************************************/
00077 /*                                E x p o r t                                 */
00078 /******************************************************************************/
00079 
00080 int XrdOucEnv::Export(const char *Var, const char *Val)
00081 {
00082    int vLen = strlen(Var);
00083    char *eBuff;
00084 
00085 // If this is a null value then substitute a null string
00086 //
00087    if (!Val) Val = "";
00088 
00089 // Allocate memory. Note that this memory will appear to be lost.
00090 //
00091    eBuff = (char *)malloc(vLen+strlen(Val)+2); // +2 for '=' and '\0'
00092 
00093 // Set up envar
00094 //
00095    strcpy(eBuff, Var);
00096    *(eBuff+vLen) = '=';
00097    strcpy(eBuff+vLen+1, Val);
00098    return putenv(eBuff);
00099 }
00100 
00101 /******************************************************************************/
00102 
00103 int XrdOucEnv::Export(const char *Var, int Val)
00104 {
00105    char buff[32];
00106    sprintf(buff, "%d", Val);
00107    return Export(Var, buff);
00108 }
00109 
00110 
00111 /******************************************************************************/
00112 /*                                I m p o r t                                 */
00113 /******************************************************************************/
00114 bool XrdOucEnv::Import( const char *var, char *&val )
00115 {
00116   char *value = getenv( var );
00117   if( !value || !*value )
00118      return false;
00119 
00120   val = value;
00121   return true;
00122 }
00123 
00124 /******************************************************************************/
00125 /*                                I m p o r t                                 */
00126 /******************************************************************************/
00127 bool XrdOucEnv::Import( const char *var, long  &val )
00128 {
00129   char *value;
00130   if( !Import( var, value ) )
00131     return false;
00132 
00133   char *status;
00134   val = strtol( value, &status, 0 );
00135 
00136   if( *status != 0 )
00137     return false;
00138   return true;
00139 }
00140 
00141 /******************************************************************************/
00142 /*                                G e t I n t                                 */
00143 /******************************************************************************/
00144 
00145 long XrdOucEnv::GetInt(const char *varname) 
00146 {
00147 // Retrieve a char* value from the Hash table and convert it into a long.
00148 // Return -999999999 if the varname does not exist
00149 //
00150   if (env_Hash.Find(varname) == NULL) {
00151     return -999999999;
00152   } else {
00153     return atol(env_Hash.Find(varname));
00154   }
00155 }
00156 
00157 
00158 /******************************************************************************/
00159 /*                                P u t I n t                                 */
00160 /******************************************************************************/
00161 
00162 void XrdOucEnv::PutInt(const char *varname, long value) 
00163 {
00164 // Convert the long into a char* and the put it into the hash table
00165 //
00166   char stringValue[24];
00167   sprintf(stringValue, "%ld", value);
00168   env_Hash.Rep(varname, strdup(stringValue), 0, Hash_dofree);
00169 }

Generated on Tue Jul 5 14:46:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1