From d44ee7b66bbc6c62d7c86b1dee1747a8dc1d35d6 Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Thu, 5 Mar 2009 18:38:33 +0000 Subject: [PATCH] 2009-03-05 19:44 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/include/hbapi.h * harbour/include/hbwince.h * harbour/source/common/hbgete.c * harbour/source/common/hbwince.c + added BOOL hb_setenv( const char * szName, const char * szValue ) It sets or deletes (szValue==NULL) environment variable. Please add support for other compilers/OS-es or define unsupported platforms to eliminate TODO warning. For sure OS2 version have to be updated. * harbour/source/rtl/gete.c + added .prg function: HB_SETENV( , [] [, ] ) -> which sets or deletes (when is NIL) process environment variable . controls character translation to OS encoding. By default is enabled. TOFIX: OS character encoding is missing in [HB_]_GETENV() --- harbour/ChangeLog | 19 ++++++++++++ harbour/include/hbapi.h | 1 + harbour/include/hbwince.h | 1 + harbour/source/common/hbgete.c | 51 +++++++++++++++++++++++++++++++++ harbour/source/common/hbwince.c | 8 ++++++ harbour/source/rtl/gete.c | 40 +++++++++++++++++++------- 6 files changed, 110 insertions(+), 10 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 6afab828d3..3574d2f032 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,25 @@ 2009-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2009-03-05 19:44 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/include/hbapi.h + * harbour/include/hbwince.h + * harbour/source/common/hbgete.c + * harbour/source/common/hbwince.c + + added BOOL hb_setenv( const char * szName, const char * szValue ) + It sets or deletes (szValue==NULL) environment variable. + Please add support for other compilers/OS-es or define + unsupported platforms to eliminate TODO warning. + For sure OS2 version have to be updated. + + * harbour/source/rtl/gete.c + + added .prg function: + HB_SETENV( , [] [, ] ) -> + which sets or deletes (when is NIL) process environment + variable . controls character translation to + OS encoding. By default is enabled. + TOFIX: OS character encoding is missing in [HB_]_GETENV() + 2009-03-05 18:10 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/source/common/hbprintf.c * do not use modfl() in WinCE POCC builds. If MSVC also does not diff --git a/harbour/include/hbapi.h b/harbour/include/hbapi.h index ff54d2a5cf..8c2c519ea8 100644 --- a/harbour/include/hbapi.h +++ b/harbour/include/hbapi.h @@ -1044,6 +1044,7 @@ extern HB_EXPORT BYTE * hb_osDecode( BYTE * szFileName, BOOL * pfFree ); /* Conv extern BOOL hb_getenv_buffer( const char * szName, char * szBuffer, int nSize ); /* WARNING: This returned pointer must be freed if not NULL using hb_xfree( ( void * ) ptr ); */ extern char * hb_getenv( const char * name ); +extern BOOL hb_setenv( const char * szName, const char * szValue ); /* set or delete (szValue==NULL) environment variable */ extern char * hb_netname( void ); extern char * hb_username( void ); diff --git a/harbour/include/hbwince.h b/harbour/include/hbwince.h index d38bb4b77f..aa485da0d6 100644 --- a/harbour/include/hbwince.h +++ b/harbour/include/hbwince.h @@ -95,6 +95,7 @@ extern char *strerror( int errnum ); #endif DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size ); + BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value ); LPSTR WINAPI GetEnvironmentStringsA( void ); BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, diff --git a/harbour/source/common/hbgete.c b/harbour/source/common/hbgete.c index 41d068295a..2dd48021f5 100644 --- a/harbour/source/common/hbgete.c +++ b/harbour/source/common/hbgete.c @@ -146,3 +146,54 @@ BOOL hb_getenv_buffer( const char * szName, char * szBuffer, int nSize ) return bRetVal; } + +/* set current process environment variable, if szValue is NULL delete + * environment variable + */ +BOOL hb_setenv( const char * szName, const char * szValue ) +{ +#if defined(HB_OS_WIN) + + return SetEnvironmentVariableA( szName, szValue ) != 0; + +#elif defined( _BSD_SOURCE ) || _POSIX_C_SOURCE >= 200112L || \ + _XOPEN_SOURCE >= 600 || defined( __WATCOMC__ ) || defined( __DJGPP__ ) + + if( szValue ) + return setenv( szName, szValue, 1 ) == 0; + else + { +# if defined( __DJGPP__ ) && ( __DJGPP__ < 2 || ( __DJGPP__ == 2 && __DJGPP_MINOR__ < 4 ) ) + szValue = getenv( szName ); + if( szValue && *szValue ) + return setenv( szName, "", 1 ) == 0; + else + return TRUE; +# else + return unsetenv( szName ) == 0; +# endif + } + +#elif defined( _HB_NO_SETENV_ ) + + HB_SYMBOL_UNUSED( szName ); + HB_SYMBOL_UNUSED( szValue ); + + return FALSE; + +#else + /* please add support for other C compilers + * if such functionality does not exists for given platform/C compiler + * then please simply added C compiler with necessary OS/version checking + * to the above #elif ... to eliminate warning [druzus] + */ + + int TODO; + + HB_SYMBOL_UNUSED( szName ); + HB_SYMBOL_UNUSED( szValue ); + + return FALSE; + +#endif +} diff --git a/harbour/source/common/hbwince.c b/harbour/source/common/hbwince.c index 8d70c7457f..9c5359f637 100644 --- a/harbour/source/common/hbwince.c +++ b/harbour/source/common/hbwince.c @@ -228,6 +228,14 @@ DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size ) return size; } +BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value ) +{ + HB_SYMBOL_UNUSED( name ); + HB_SYMBOL_UNUSED( value ); + + /* TODO: */ +} + BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime ) diff --git a/harbour/source/rtl/gete.c b/harbour/source/rtl/gete.c index 16e8e56499..517ca47ef3 100644 --- a/harbour/source/rtl/gete.c +++ b/harbour/source/rtl/gete.c @@ -112,13 +112,9 @@ HB_FUNC( GETENV ) BOOL fFree; char * pbyResult = ( char * ) hb_osDecode( ( BYTE * ) szValue, &fFree ); + hb_retc_buffer( pbyResult ); if( fFree ) - { - hb_retc_buffer( pbyResult ); hb_xfree( szValue ); - } - else - hb_retc_buffer( szValue ); } } else @@ -198,13 +194,9 @@ HB_FUNC( HB_GETENV ) BOOL fFree; char * pbyResult = ( char * ) hb_osDecode( ( BYTE * ) szValue, &fFree ); + hb_retc_buffer( pbyResult ); if( fFree ) - { - hb_retc_buffer( pbyResult ); hb_xfree( szValue ); - } - else - hb_retc_buffer( szValue ); } else hb_retc_buffer( szValue ); @@ -224,3 +216,31 @@ HB_FUNC( HB_GETENV ) else hb_retc( NULL ); } + +HB_FUNC( HB_SETENV ) +{ + char * pszName = hb_parc( 1 ); + BOOL fResult = FALSE; + + if( pszName ) + { + char * pszValue = hb_parc( 2 ); + BOOL fFreeName = FALSE, fFreeVal = FALSE; + + if( ( ! ISLOG( 3 ) || hb_parl( 3 ) ) ) + { + pszName = ( char * ) hb_osEncode( ( BYTE * ) pszName, &fFreeName ); + if( pszValue ) + pszValue = ( char * ) hb_osEncode( ( BYTE * ) pszValue, &fFreeVal ); + } + + fResult = hb_setenv( pszName, pszValue ); + + if( fFreeName ) + hb_xfree( pszName ); + if( fFreeVal ) + hb_xfree( pszValue ); + } + + hb_retl( fResult ); +}