メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TEnvironmentVariableTable.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TEnvironmentVariableTable.cc,v 1.4 2004/03/07 10:30:34 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TEnvironmentVariableTable.cc,v $
00005 //  Revision 1.4  2004/03/07 10:30:34  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.3  2003/10/06 16:38:26  goiwai
00010 //  *** empty log message ***
00011 //
00012 //  Revision 1.2  2003/07/30 16:21:20  goiwai
00013 //  ファイルにコミットログをつけることにしました.
00014 //
00015 // =====================================================================
00016 #include "TEnvironmentVariableTable.hh"
00017 #include "TRunCommandEnvironmentVariable.hh"
00018 
00019 TEnvironmentVariableTable::TEnvironmentVariableTable()
00020   : theItemList(), theValueList(), theNumberOfEnvironments( 0 )
00021 {
00022   ResetEnvironment();
00023 }
00024 
00025 TEnvironmentVariableTable::TEnvironmentVariableTable( const Tstring& rcfile )
00026   : theItemList(), theValueList(), theNumberOfEnvironments( 0 )
00027 {
00028   ResetEnvironment();
00029   TRunCommandEnvironmentVariable rc( rcfile );
00030   TstringList readbuf;
00031   while ( rc.IsSuccess() ) {
00032     readbuf = rc.ReadLine();
00033     if ( rc.IsExecutable( readbuf ) ) {
00034       SetEnvironment( readbuf[ 0 ], readbuf[ 1 ] );
00035     }
00036   }
00037   rc.Close();
00038 }
00039 
00040 TEnvironmentVariableTable::TEnvironmentVariableTable( const TstringList& itemlist )
00041   : theItemList(), theValueList(), theNumberOfEnvironments( 0 )
00042 {
00043   ResetEnvironment();
00044   for ( Tsize_t i = 0; i < itemlist.size(); i ++ ) {
00045     SetEnvironment( itemlist[ i ], "" );
00046   }
00047 }
00048 
00049 TEnvironmentVariableTable::TEnvironmentVariableTable( const TstringList& itemlist, const TstringList& valuelist )
00050   : theItemList(), theValueList(), theNumberOfEnvironments( 0 )
00051 {
00052   ResetEnvironment();
00053   SetEnvironment( itemlist, valuelist );
00054 }
00055 
00056 TEnvironmentVariableTable::TEnvironmentVariableTable( const TEnvironmentVariableTable& right )
00057   : theItemList( right.theItemList ),
00058     theValueList( right.theValueList ),
00059     theNumberOfEnvironments( right.theNumberOfEnvironments )
00060 {
00061   SetEnvironment( theItemList, theValueList );
00062 }
00063 
00064 TEnvironmentVariableTable::~TEnvironmentVariableTable()
00065 {;}
00066 
00067 const TEnvironmentVariableTable& TEnvironmentVariableTable::operator=( const TEnvironmentVariableTable& right )
00068 {
00069   theItemList = right.theItemList;
00070   theValueList = right.theValueList;
00071   theNumberOfEnvironments = right.theNumberOfEnvironments;
00072   SetEnvironment( theItemList, theValueList );
00073   return *this;
00074 }
00075 
00076 Tbool TEnvironmentVariableTable::operator==( const TEnvironmentVariableTable& right ) const
00077 {
00078   Tbool retval = Ttrue;
00079   retval &= ( theItemList == right.theItemList );
00080   retval &= ( theValueList == right.theValueList );
00081   return retval;
00082 }
00083 
00084 Tbool TEnvironmentVariableTable::operator!=( const TEnvironmentVariableTable& right ) const
00085 {
00086   Tbool retval = Tfalse;
00087   retval |= ( theItemList != right.theItemList );
00088   retval |= ( theValueList != right.theValueList );
00089   return retval;
00090 }
00091 
00092 Tostream& operator<<( Tostream& tos, const TEnvironmentVariableTable& right )
00093 {
00094   Tint nenv = right.theNumberOfEnvironments;
00095   for ( Tint i = 0; i < nenv; i ++ ) {
00096     tos << right.theItemList[ i ] << "=" << right.theValueList[ i ];
00097     if ( i != nenv - 1 )
00098       tos << Tendl;
00099   }
00100   tos << Tflush;
00101   return tos;
00102 }
00103 
00104 Tstring TEnvironmentVariableTable::GetEnvironmentValue( const Tstring& item ) const
00105 {
00106   Tstring value = "";
00107   Tint pos = FindEnvironmentItem( item );
00108   if ( pos >= 0 ) {
00109     value = theValueList[ pos ];
00110   }
00111   return value;
00112 }
00113 
00114 Tstring TEnvironmentVariableTable::GetEnvironmentValue( Tint row ) const
00115 {
00116   Tstring value = "";
00117   if ( ! theValueList.empty() && row < (Tint)theValueList.size() ) {
00118     value = theValueList[ row ];
00119   }
00120   return value;
00121 }
00122 
00123 Tstring TEnvironmentVariableTable::GetEnvironmentItem( Tint row ) const
00124 {
00125   Tstring item = "";
00126   if ( ! theItemList.empty() && row < (Tint)theItemList.size() ) {
00127     item = theItemList[ row ];
00128   }
00129   return item;
00130 }
00131 
00132 Tint TEnvironmentVariableTable::FindEnvironmentItem( const Tstring& item ) const
00133 {
00134   for ( Tint i = 0; i < (Tint)theItemList.size(); i ++ ) {
00135     if ( theItemList[ i ] == item ) {
00136       return i;
00137     }
00138   }
00139   return tNotFound;
00140 }
00141 
00142 Tint TEnvironmentVariableTable::FindEnvironmentValue( const Tstring& value ) const
00143 {
00144   for ( Tint i = 0; i < (Tint)theValueList.size(); i ++ ) {
00145     if ( theValueList[ i ] == value ) {
00146       return i;
00147     }
00148   }
00149   return tNotFound;
00150 }
00151 
00152 Tbool TEnvironmentVariableTable::HasEnvironmentItem( const Tstring& item ) const
00153 {
00154   if ( FindEnvironmentItem( item ) == tNotFound ) {
00155     return Tfalse;
00156   } else {
00157     return Ttrue;
00158   }
00159 }
00160 
00161 Tbool TEnvironmentVariableTable::HasEnvironmentValue( const Tstring& value ) const
00162 {
00163   if ( FindEnvironmentValue( value ) == tNotFound ) {
00164     return Tfalse;
00165   } else {
00166     return Ttrue;
00167   }
00168 }
00169 
00170 Tvoid TEnvironmentVariableTable::UnsetEnvironment( const Tstring& item )
00171 {
00172   Tint pos = FindEnvironmentItem( item );
00173   if ( pos != tNotFound ) {
00174     unsetenv( theItemList[ pos ].c_str() );
00175     theItemList.erase( theItemList.begin() + pos );
00176     theValueList.erase( theValueList.begin() + pos );
00177     theNumberOfEnvironments --;
00178   }
00179   return;
00180 }
00181 
00182 Tvoid TEnvironmentVariableTable::UnsetEnvironment( const TstringList& item )
00183 {
00184   for ( Tsize_t i = 0; i < item.size(); i ++ ) {
00185     UnsetEnvironment( item[ i ] );
00186   }
00187   return;
00188 }
00189 
00190 Tvoid TEnvironmentVariableTable::SetEnvironment( const Tstring& item, const Tstring& value )
00191 {
00192   Tint pos = FindEnvironmentItem( item );
00193   if ( pos == tNotFound ) {
00194     theItemList.push_back( item );
00195     theValueList.push_back( value );
00196     theNumberOfEnvironments ++;
00197   } else {
00198     theValueList[ pos ] = value;
00199   }
00200   if ( setenv( item.c_str(), value.c_str(), 1 ) < 0 ) {
00201     Tcerr << "TEnvironmentVariableTable::SetEnvironment: no space." << Tendl;
00202   }
00203   return;
00204 }
00205 
00206 Tvoid TEnvironmentVariableTable::SetEnvironment( const TstringList& item, const TstringList& value )
00207 {
00208   if ( item.size() != value.size() ) {
00209     Tcerr << "TEnvironmentVariableTable::SetEnvironment: not match." << Tendl;
00210     return;
00211   }
00212   for ( Tsize_t i = 0; i < item.size(); i ++ ) {
00213     SetEnvironment( item[ i ], value[ i ] );
00214   }
00215   return;
00216 }
00217 
00218 Tvoid TEnvironmentVariableTable::SetEnvironment( const Tstring& rcfile )
00219 {
00220   TRunCommandEnvironmentVariable rc( rcfile );
00221   TstringList readbuf;
00222   while ( rc.IsSuccess() ) {
00223     readbuf = rc.ReadLine();
00224     if ( rc.IsExecutable( readbuf ) ) {
00225       SetEnvironment( readbuf[ 0 ], readbuf[ 1 ] );
00226     }
00227   }
00228   rc.Close();
00229   return;
00230 }
00231 
00232 Tvoid TEnvironmentVariableTable::ClearEnvironment()
00233 {
00234   for ( Tsize_t i = 0; i < theItemList.size(); i ++ ) {
00235     unsetenv( theItemList[ i ].c_str() );
00236   }
00237   theItemList.clear();
00238   theValueList.clear();
00239   theNumberOfEnvironments = 0;
00240   return;
00241 }
00242 
00243 Tvoid TEnvironmentVariableTable::ResetEnvironment()
00244 {
00245   theItemList.clear();
00246   theValueList.clear();
00247   Tchar** env = environ;
00248   while ( *env != 0 ) {
00249     Tstring item = *env;
00250     Tsize_t eqpos = item.find( "=" );
00251     Tstring value = item.substr( eqpos + 1, item.size() - eqpos );
00252     item.erase( item.begin() + eqpos, item.end() );
00253     theItemList.push_back( item );
00254     theValueList.push_back( value );
00255     env ++;
00256   }
00257   if ( theItemList.size() != theValueList.size() ) {
00258     Tcerr << "TEnvironmentVariableTable::ResetEnvironment: invalid table." << Tendl;
00259   }
00260   theNumberOfEnvironments = (Tint)theItemList.size();
00261   return;
00262 }
00263 
00264 Tint TEnvironmentVariableTable::GetIntegerValue( const Tstring& item ) const
00265 {
00266   // should check string.
00267   // isdigit()
00268   // isxdigit()
00269   return strtol( GetEnvironmentValue( item ).c_str(), 0, 0 );
00270 }
00271 
00272 Tdouble TEnvironmentVariableTable::GetDoubleValue( const Tstring& item ) const
00273 {
00274   // should check string.
00275   // isdigit()
00276   // isxdigit()
00277   return strtod( GetEnvironmentValue( item ).c_str(), 0 );
00278 }
00279 
00280 #ifdef __CLDAQ_ROOT_DLL
00281     ClassImp(TEnvironmentVariableTable)
00282 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp