libyui  3.10.0
YEnvVar.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YEnvVar.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <stdlib.h> // getenv()
27 #include <string.h> // strcmp(), strcasecmp()
28 #include <ctype.h> // tolower()
29 
30 #define YUILogComponent "ui"
31 #include "YUILog.h"
32 
33 #include <YEnvVar.h>
34 
35 using std::string;
36 
37 
38 YEnvVar::YEnvVar( const string & name )
39  : _name( name )
40  , _isSet( false )
41 {
42  if ( ! _name.empty() )
43  {
44  const char * val = getenv( _name.c_str() );
45 
46  if ( val )
47  {
48  _isSet = true;
49  _value = val;
50  }
51  }
52 }
53 
54 
55 bool
56 YEnvVar::isEqual( const string & str, bool caseSensitive ) const
57 {
58  if ( ! _isSet )
59  return false;
60 
61  if ( caseSensitive )
62  return strcmp( _value.c_str(), str.c_str() ) == 0;
63  else
64  return strcasecmp( _value.c_str(), str.c_str() ) == 0;
65 }
66 
67 bool
68 YEnvVar::contains( const string & str, bool caseSensitive ) const
69 {
70  if ( ! _isSet )
71  return false;
72 
73  if ( caseSensitive )
74  {
75  return _value.find( str ) != string::npos;
76  }
77  else
78  {
79  return tolower( _value ).find( tolower( str ) ) != string::npos;
80  }
81 }
82 
83 
84 string tolower( const string & str )
85 {
86  string lowStr;
87  lowStr.reserve( str.size() );
88 
89  for ( string::const_iterator it = str.begin();
90  it != str.end();
91  ++it )
92  {
93  lowStr += ::tolower( *it );
94  }
95 
96  return lowStr;
97 }
98 
99 
100 std::ostream &
101 operator<<( std::ostream & stream, const YEnvVar env )
102 {
103  if ( env.name().empty() )
104  {
105  stream << "<unnamed environment variable>";
106  }
107  else
108  {
109  if ( env.isSet() )
110  stream << "$" << env.name() << "=\"" << env.value() << "\"";
111  else
112  stream << "$" << env.name() << ": <not set>";
113  }
114 
115  return stream;
116 }
YEnvVar::contains
bool contains(const std::string &str, bool caseSensitive=false) const
Return 'true' if the environment variable is set and the value contains 'str'.
Definition: YEnvVar.cc:68
YEnvVar
Helper class to represent an environment variable and its value.
Definition: YEnvVar.h:37
YEnvVar::isSet
bool isSet() const
Return 'true' if the environment variable is set.
Definition: YEnvVar.h:54
YEnvVar::isEqual
bool isEqual(const std::string &str, bool caseSensitive=false) const
Return 'true' if the environment variable is set and the value is 'str'.
Definition: YEnvVar.cc:56
YEnvVar::name
std::string name() const
Return the name of the environment variable.
Definition: YEnvVar.h:49
YEnvVar::YEnvVar
YEnvVar(const std::string &name=std::string())
Constructor: Retrieve the environment variable 'name' and store the value (unless 'name' is empty).
Definition: YEnvVar.cc:38
YEnvVar::value
std::string value() const
Return the value of the environment variable.
Definition: YEnvVar.h:59