From 2d9312ca3ef80fc92a02f8eb359084a79294fcf2 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 18 Jul 2012 16:17:36 +0100 Subject: [PATCH] Added function: SystemStats::getEnvironmentVariable() --- modules/juce_core/native/juce_posix_SharedCode.h | 11 +++++++++++ .../juce_core/native/juce_win32_SystemStats.cpp | 14 ++++++++++++++ modules/juce_core/system/juce_SystemStats.h | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index fd683ae4e9..cf9ce96e41 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -525,6 +525,17 @@ Result FileOutputStream::truncate() return getResultForReturnValue (ftruncate (getFD (fileHandle), (off_t) currentPosition)); } +//============================================================================== +String SystemStats::getEnvironmentVariable (const String& name, const String& defaultValue) +{ + const char* s = ::getenv (name.toUTF8()); + + if (s != nullptr) + return String::fromUTF8 (s); + + return defaultValue; +} + //============================================================================== MemoryMappedFile::MemoryMappedFile (const File& file, MemoryMappedFile::AccessMode mode) : address (nullptr), diff --git a/modules/juce_core/native/juce_win32_SystemStats.cpp b/modules/juce_core/native/juce_win32_SystemStats.cpp index 34add677df..4014504aca 100644 --- a/modules/juce_core/native/juce_win32_SystemStats.cpp +++ b/modules/juce_core/native/juce_win32_SystemStats.cpp @@ -198,6 +198,20 @@ int SystemStats::getMemorySizeInMegabytes() return (int) (mem.ullTotalPhys / (1024 * 1024)) + 1; } +//============================================================================== +String SystemStats::getEnvironmentVariable (const String& name, const String& defaultValue) +{ + DWORD len = GetEnvironmentVariableW (name.toWideCharPointer(), nullptr, 0); + if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) + return String (defaultValue); + + HeapBlock buffer (len); + len = GetEnvironmentVariableW (name.toWideCharPointer(), buffer, len); + + return String (CharPointer_wchar_t (buffer), + CharPointer_wchar_t (buffer + len)); +} + //============================================================================== uint32 juce_millisecondsSinceStartup() noexcept { diff --git a/modules/juce_core/system/juce_SystemStats.h b/modules/juce_core/system/juce_SystemStats.h index b95918f4c2..2cceab8985 100644 --- a/modules/juce_core/system/juce_SystemStats.h +++ b/modules/juce_core/system/juce_SystemStats.h @@ -94,6 +94,11 @@ public: static int getOSXMinorVersionNumber(); #endif + /** Returns an environment variable. + If the named value isn't set, this will return the defaultValue string instead. + */ + static String getEnvironmentVariable (const String& name, const String& defaultValue); + //============================================================================== /** Returns the current user's name, if available. @see getFullUserName()