libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YEnvVar.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 00026 #include <stdlib.h> // getenv() 00027 #include <string.h> // strcmp(), strcasecmp() 00028 #include <ctype.h> // tolower() 00029 00030 #define YUILogComponent "ui" 00031 #include "YUILog.h" 00032 00033 #include <YEnvVar.h> 00034 00035 00036 YEnvVar::YEnvVar( const std::string & name ) 00037 : _name( name ) 00038 , _isSet( false ) 00039 { 00040 if ( ! _name.empty() ) 00041 { 00042 const char * val = getenv( _name.c_str() ); 00043 00044 if ( val ) 00045 { 00046 _isSet = true; 00047 _value = val; 00048 } 00049 } 00050 } 00051 00052 00053 bool 00054 YEnvVar::isEqual( const std::string & str, bool caseSensitive ) const 00055 { 00056 if ( ! _isSet ) 00057 return false; 00058 00059 if ( caseSensitive ) 00060 return strcmp( _value.c_str(), str.c_str() ) == 0; 00061 else 00062 return strcasecmp( _value.c_str(), str.c_str() ) == 0; 00063 } 00064 00065 bool 00066 YEnvVar::contains( const std::string & str, bool caseSensitive ) const 00067 { 00068 if ( ! _isSet ) 00069 return false; 00070 00071 if ( caseSensitive ) 00072 { 00073 return _value.find( str ) != std::string::npos; 00074 } 00075 else 00076 { 00077 return tolower( _value ).find( tolower( str ) ) != std::string::npos; 00078 } 00079 } 00080 00081 00082 std::string tolower( const std::string & str ) 00083 { 00084 std::string lowStr; 00085 lowStr.reserve( str.size() ); 00086 00087 for ( std::string::const_iterator it = str.begin(); 00088 it != str.end(); 00089 ++it ) 00090 { 00091 lowStr += ::tolower( *it ); 00092 } 00093 00094 return lowStr; 00095 } 00096 00097 00098 std::ostream & 00099 operator<<( std::ostream & stream, const YEnvVar env ) 00100 { 00101 if ( env.name().empty() ) 00102 { 00103 stream << "<unnamed environment variable>"; 00104 } 00105 else 00106 { 00107 if ( env.isSet() ) 00108 stream << "$" << env.name() << "=\"" << env.value() << "\""; 00109 else 00110 stream << "$" << env.name() << ": <not set>"; 00111 } 00112 00113 return stream; 00114 }