1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : environmentconfig.h
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#ifndef __environmentconfig__
#define __environmentconfig__
#include "configurationtoolbase.h"
#include "archive.h"
#include "evnvarlist.h"
#include <wx/utils.h>
#include "macros.h"
#include <wx/thread.h>
class EnvSetter;
/// A helper class that makes sure that escpaed $ signs
/// are kept unharmed
class WXDLLIMPEXP_SDK DollarEscaper
{
wxString &m_str;
public:
DollarEscaper(wxString &str) ;
~DollarEscaper() ;
};
class WXDLLIMPEXP_SDK EnvironmentConfig : public ConfigurationToolBase
{
// Allow access to Apply/UnApply Env
friend class EnvSetter;
wxStringMap_t m_envSnapshot;
wxCriticalSection m_cs;
int m_envApplied;
protected:
wxString DoExpandVariables(const wxString &in);
void ApplyEnv(wxStringMap_t *overrideMap, const wxString &project);
void UnApplyEnv();
public:
static EnvironmentConfig* Instance();
static void Release();
bool Load();
wxString ExpandVariables(const wxString &in, bool applyEnvironment);
EvnVarList GetSettings();
void SetSettings(EvnVarList &vars);
/**
* @brief return a list of the environment variabels as defined by the user
* in the global environment table + workspace + project (this function only return the names of the variable, not its value)
*/
wxArrayString GetActiveSetEnvNames(bool includeWorkspace = true, const wxString &project = wxEmptyString);
private:
EnvironmentConfig();
virtual ~EnvironmentConfig();
public:
virtual wxString GetRootName();
};
class EnvSetter
{
EnvironmentConfig *m_env;
wxString m_envName;
wxString m_oldEnvValue;
bool m_restoreOldValue;
public:
EnvSetter(wxStringMap_t *om = NULL)
: m_env(EnvironmentConfig::Instance())
, m_restoreOldValue(false)
{
m_env->ApplyEnv(om, wxEmptyString);
}
EnvSetter(EnvironmentConfig *conf, wxStringMap_t *om = NULL)
: m_env(EnvironmentConfig::Instance())
, m_restoreOldValue(false)
{
wxUnusedVar(conf);
m_env->ApplyEnv(om, wxEmptyString);
}
EnvSetter(EnvironmentConfig *conf, wxStringMap_t *om, const wxString &project)
: m_env(EnvironmentConfig::Instance())
, m_restoreOldValue(false)
{
wxUnusedVar(conf);
m_env->ApplyEnv(om, project);
}
EnvSetter(const wxString &var, const wxString &value)
: m_env(NULL)
{
m_envName = var;
// keep old value
m_restoreOldValue = ::wxGetEnv(m_envName, &m_oldEnvValue);
::wxSetEnv(m_envName, value);
}
~EnvSetter() {
if (m_env) {
m_env->UnApplyEnv();
m_env = NULL;
}
if ( m_restoreOldValue ) {
// restore old env variable value
::wxSetEnv(m_envName, m_oldEnvValue);
} else if ( !m_envName.IsEmpty() ) {
// we applied a single evn variable without old value
// uset it
::wxUnsetEnv( m_envName );
}
}
};
#endif // __environmentconfig__
|