From e8b6d48ad038089e37730990212676e50a8d2352 Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sat, 14 Jan 2012 01:06:47 -0800 Subject: [PATCH] Introduce env_var_t to replace empty string as missing environment variable --- env.cpp | 25 +++++++++++++++++++------ env.h | 14 +++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/env.cpp b/env.cpp index 1233a87db..e0d316868 100644 --- a/env.cpp +++ b/env.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -300,7 +301,7 @@ static int is_locale( const wchar_t *key ) */ static void handle_locale() { - const wcstring lc_all = env_get_string( L"LC_ALL" ); + const env_var_t lc_all = env_get_string( L"LC_ALL" ); wcstring lang; int i; wchar_t *old = wcsdup(wsetlocale( LC_MESSAGES, NULL )); @@ -321,7 +322,7 @@ static void handle_locale() } ; - if( !lc_all.empty() ) + if( !lc_all.missing() ) { wsetlocale( LC_ALL, lc_all.c_str() ); } @@ -669,7 +670,8 @@ void env_init() /* Set up SHLVL variable */ - const wchar_t *shlvl = env_get_string( L"SHLVL" ).empty()?NULL:env_get_string( L"SHLVL" ).c_str(); + const wcstring shlvl_str = env_get_string( L"SHLVL" ); + const wchar_t *shlvl = shlvl_str.empty() ? NULL : shlvl_str.c_str(); if ( shlvl ) { @@ -1136,7 +1138,18 @@ int env_remove( const wchar_t *key, int var_mode ) return !erased; } -wcstring env_get_string( const wchar_t *key ) +env_var_t env_var_t::missing_var(void) { + env_var_t result(L""); + result.is_missing = true; + return result; +} + +const wchar_t *env_var_t::c_str(void) const { + assert(! is_missing); + return wcstring::c_str(); +} + +env_var_t env_get_string( const wchar_t *key ) { scoped_lock lock(env_lock); @@ -1231,7 +1244,7 @@ wcstring env_get_string( const wchar_t *key ) if( !item || (wcscmp( item, ENV_NULL )==0)) { - return wcstring(L""); + return env_var_t::missing_var(); } else { @@ -1772,7 +1785,7 @@ char **env_export_arr( int recalc ) hash_table_t vals; int prev_was_null=1; int pos=0; - int i; + size_t i; debug( 4, L"env_export_arr() recalc" ); diff --git a/env.h b/env.h index fec0e9055..435cf22aa 100644 --- a/env.h +++ b/env.h @@ -95,10 +95,22 @@ int env_set( const wchar_t *key, */ wchar_t *env_get( const wchar_t *key ); +class env_var_t : public wcstring { +private: + bool is_missing; +public: + static env_var_t missing_var(void); + env_var_t(const wcstring & x) : wcstring(x), is_missing(false) { } + env_var_t(const wchar_t *x) : wcstring(x), is_missing(false) { } + env_var_t() : wcstring(L""), is_missing(false) { } + bool missing(void) const { return is_missing; } + bool missing_or_empty(void) const { return missing() || empty(); } + const wchar_t *c_str(void) const; +}; /** Gets the variable with the specified name, or an empty string if it does not exist. */ -wcstring env_get_string( const wchar_t *key ); +env_var_t env_get_string( const wchar_t *key ); /** Returns 1 if the specified key exists. This can't be reliably done