PlatformSpecific::getCurrentUserName


#1

What about adding a function like this to the Platform stuff (or in a common place) ?

[code]
const String getCurrentUserName()
{
String userName;

#if JUCE_LINUX || JUCE_MAC
userName = String(getenv(“USER”)).trim();

if (userName.isEmpty())
{
  char text[255];
  memset(text, 0, sizeof(text));

  FILE* whoami = popen("whoami", "r");
  char* retVal = fgets(text, sizeof(text), whoami);
  (void*) retVal;
  pclose(whoami);

  userName = String(text);
}

#elif JUCE_WIN32
{
wchar_t text[250];
::ExpandEnvironmentStrings(L"%userprofile%", text, sizeof(text));

  userName = String(text);
}

#else
jassertfalse // Unknown platform ! cannot determine user
#endif

return userName.trim();

}[/code]


#2

That’s quite a coincidence - I needed exactly the same thing for something I was writing yesterday, and you’ve saved me the trouble of looking up how to do it! Thanks, I’ll probably put it into the SystemStats class.


#3

well, i’ve updated it cause on some win systems it make the use of wchar_t instead of char… don’t know if the fix works ok for most platforms there…


#4

I think the JUCE_WIN32 is a bit too discriminatory here, cause it should fail on Win64.
I know under visual C++, _WINDOWS is defined all the time (for 32 and 64 bit programs), maybe you could use it ? (Don’t know if it’s defined by default on other IDEs though)


#5

I’ve already implemented it properly and added it to the build, so don’t worry about the code in this thread.


#6

cool thanx man :slight_smile: