Confdump Agent  1.4.0
EnvironmentVariable.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <cstdlib>
00019 #include <stdexcept>
00020 
00021 #include "Confdump/RuntimeError.hpp"
00022 #include "Confdump/System/Linux/EnvironmentVariable.hpp"
00023 
00024 extern char **environ;
00025 
00026 namespace Confdump
00027 {
00028 
00029 namespace System
00030 {
00031 
00032 namespace Linux
00033 {
00034 
00035 EnvironmentVariable::EnvironmentVariable( boost::shared_ptr<const LinuxDumper> driver, QString name )
00036  : name_( name )
00037 {
00038     QByteArray name_raw = name.toLocal8Bit();
00039     if ( const char* raw_value = getenv( name_raw.data() ) )
00040     {
00041         value_ = QString( raw_value );
00042     }
00043     else
00044     {
00045         throw RuntimeError( tr( "EnvironmentVariable: no such variable '%1'" ).arg( name ) );
00046     }
00047 }
00048 
00049 EnvironmentVariable::EnvironmentVariable( QString name, QString value )
00050  : name_( name ), value_( value )
00051 {
00052 }
00053 
00054 EnvironmentVariable::~EnvironmentVariable()
00055 {
00056 }
00057 
00058 std::vector< boost::shared_ptr< EnvironmentVariable > > EnvironmentVariable::allSystemInstances( boost::shared_ptr<const LinuxDumper> driver )
00059 {
00060     std::vector<boost::shared_ptr< EnvironmentVariable > > instances;
00061 
00062     for ( char *const *raw_var = environ; *raw_var; ++raw_var )
00063     {
00064         QString var( QString::fromLocal8Bit( *raw_var ) );
00065         QString name ( var.section( '=', 0, 0 ) );
00066         QString value ( var.section( '=', 1, -1 ) );
00067 
00068         boost::shared_ptr<EnvironmentVariable> instance( new EnvironmentVariable( name, value ) );
00069         instances.push_back( instance );
00070     }
00071 
00072     return instances;
00073 }
00074 
00075 QString EnvironmentVariable::name() const
00076 {
00077     return name_;
00078 }
00079 
00080 QString EnvironmentVariable::value() const
00081 {
00082     return value_;
00083 }
00084 
00085 }
00086 }
00087 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends