libyui  3.4.2
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 
36 YEnvVar::YEnvVar( const std::string & name )
37  : _name( name )
38  , _isSet( false )
39 {
40  if ( ! _name.empty() )
41  {
42  const char * val = getenv( _name.c_str() );
43 
44  if ( val )
45  {
46  _isSet = true;
47  _value = val;
48  }
49  }
50 }
51 
52 
53 bool
54 YEnvVar::isEqual( const std::string & str, bool caseSensitive ) const
55 {
56  if ( ! _isSet )
57  return false;
58 
59  if ( caseSensitive )
60  return strcmp( _value.c_str(), str.c_str() ) == 0;
61  else
62  return strcasecmp( _value.c_str(), str.c_str() ) == 0;
63 }
64 
65 bool
66 YEnvVar::contains( const std::string & str, bool caseSensitive ) const
67 {
68  if ( ! _isSet )
69  return false;
70 
71  if ( caseSensitive )
72  {
73  return _value.find( str ) != std::string::npos;
74  }
75  else
76  {
77  return tolower( _value ).find( tolower( str ) ) != std::string::npos;
78  }
79 }
80 
81 
82 std::string tolower( const std::string & str )
83 {
84  std::string lowStr;
85  lowStr.reserve( str.size() );
86 
87  for ( std::string::const_iterator it = str.begin();
88  it != str.end();
89  ++it )
90  {
91  lowStr += ::tolower( *it );
92  }
93 
94  return lowStr;
95 }
96 
97 
98 std::ostream &
99 operator<<( std::ostream & stream, const YEnvVar env )
100 {
101  if ( env.name().empty() )
102  {
103  stream << "<unnamed environment variable>";
104  }
105  else
106  {
107  if ( env.isSet() )
108  stream << "$" << env.name() << "=\"" << env.value() << "\"";
109  else
110  stream << "$" << env.name() << ": <not set>";
111  }
112 
113  return stream;
114 }
bool isEqual(const std::string &str, bool caseSensitive=false) const
Return &#39;true&#39; if the environment variable is set and the value is &#39;str&#39;.
Definition: YEnvVar.cc:54
bool isSet() const
Return &#39;true&#39; if the environment variable is set.
Definition: YEnvVar.h:54
std::string value() const
Return the value of the environment variable.
Definition: YEnvVar.h:59
YEnvVar(const std::string &name=std::string())
Constructor: Retrieve the environment variable &#39;name&#39; and store the value (unless &#39;name&#39; is empty)...
Definition: YEnvVar.cc:36
Helper class to represent an environment variable and its value.
Definition: YEnvVar.h:36
bool contains(const std::string &str, bool caseSensitive=false) const
Return &#39;true&#39; if the environment variable is set and the value contains &#39;str&#39;.
Definition: YEnvVar.cc:66
std::string name() const
Return the name of the environment variable.
Definition: YEnvVar.h:49