13#pragma comment(lib, "psapi.lib")
14#pragma warning(disable : 4996)
21#include <sys/utsname.h>
33 wchar_t buffer [MAX_COMPUTERNAME_LENGTH + 1];
34 DWORD nSize = MAX_COMPUTERNAME_LENGTH + 1;
35 GetComputerNameW(buffer, &nSize);
38 char buffer[HOST_NAME_MAX + 1];
39 if (gethostname(buffer,
sizeof(buffer)) == 0) {
51 ::GetUserNameW(buffer, &nSize);
54 struct passwd *pPw = getpwuid(getuid());
58 const char* pUser = getenv(
"USER");
59 return pUser ? pUser :
"";
67 GetSystemInfo(&sysInfo);
68 return (
int)sysInfo.dwNumberOfProcessors;
70 return (
int)sysconf(_SC_NPROCESSORS_ONLN);
86 wchar_t buffer [MAX_PATH];
87 ::GetCurrentDirectoryW(MAX_PATH, buffer);
90 char buffer[PATH_MAX];
91 if (getcwd(buffer,
sizeof(buffer))) {
101 wchar_t buffer [MAX_PATH];
102 ::GetSystemDirectoryW(buffer, MAX_PATH);
105 return String(
"/usr/lib");
112 OSVERSIONINFOEXW info;
113 ZeroMemory(&info,
sizeof(OSVERSIONINFOEXW));
114 info.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEXW);
115 GetVersionExW((LPOSVERSIONINFOW)&info);
117 std::stringstream ss;
118 ss << info.dwMajorVersion <<
"." << info.dwMinorVersion <<
"." << info.dwBuildNumber;
119 return String(ss.str().c_str());
122 if (uname(&name) == 0) {
123 return String(name.release);
135 static int64_t ReadLinuxWorkingSet() {
136 std::ifstream statusFile(
"/proc/self/status");
138 while (std::getline(statusFile, sLine)) {
139 if (sLine.compare(0, 6,
"VmRSS:") == 0) {
140 size_t nStart = sLine.find_first_of(
"0123456789");
141 size_t nEnd = sLine.find_first_not_of(
"0123456789", nStart);
142 if (nStart != std::string::npos)
return std::stoll(sLine.substr(nStart, nEnd - nStart)) * 1024;
152 PROCESS_MEMORY_COUNTERS pmc;
153 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
sizeof(pmc)))
return (int64_t)pmc.WorkingSetSize;
156 return ReadLinuxWorkingSet();
168 DWORD nSize = ::ExpandEnvironmentStringsW(sWName.c_str(), NULL, 0);
169 if (nSize == 0)
return sName;
170 std::vector<wchar_t> buffer(nSize);
171 ::ExpandEnvironmentStringsW(sWName.c_str(), buffer.data(), nSize);
176 return pVal ?
String(pVal) : sName;
183 std::vector<String> tempArgs;
186 LPWSTR* pSzArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
187 if (pSzArglist != NULL) {
188 for (
int iIndex = 0; iIndex < nArgs; iIndex++) tempArgs.push_back(
String(
WCharToUtf8(pSzArglist[iIndex]).c_str()));
189 LocalFree(pSzArglist);
192 std::ifstream cmdline(
"/proc/self/cmdline", std::ios::binary);
194 while (std::getline(cmdline, sArg,
'\0')) tempArgs.push_back(
String(sArg.c_str()));
202 for (
int iIndex = 0; iIndex < (int)tempArgs.size(); iIndex++) result[iIndex] = tempArgs[iIndex];
209 DWORD nSize = ::GetEnvironmentVariableW(sWVar.c_str(), NULL, 0);
210 if (nSize == 0)
return String(
"");
211 std::vector<wchar_t> buffer(nSize);
212 ::GetEnvironmentVariableW(sWVar.c_str(), buffer.data(), nSize);
221 int iEqIdx = sLine.
IndexOf(
"=");
230 wchar_t* pLpvEnv = GetEnvironmentStringsW();
231 for (
wchar_t* pLpszVariable = pLpvEnv; *pLpszVariable; pLpszVariable += wcslen(pLpszVariable) + 1) {
234 FreeEnvironmentStringsW(pLpvEnv);
236 for (
char** ppEnv = environ; *ppEnv; ++ppEnv) {
247 case SF::ApplicationData:
return CSIDL_APPDATA;
248 case SF::CommonApplicationData:
return CSIDL_COMMON_APPDATA;
249 case SF::CommonProgramFiles:
return CSIDL_PROGRAM_FILES_COMMON;
250 case SF::Cookies:
return CSIDL_COOKIES;
251 case SF::Desktop:
return CSIDL_DESKTOP;
252 case SF::Favorites:
return CSIDL_FAVORITES;
253 case SF::History:
return CSIDL_HISTORY;
254 case SF::InternetCache:
return CSIDL_INTERNET_CACHE;
255 case SF::LocalApplicationData:
return CSIDL_LOCAL_APPDATA;
256 case SF::MyComputer:
return CSIDL_DRIVES;
257 case SF::MyDocuments:
return CSIDL_MYDOCUMENTS;
258 case SF::MyMusic:
return CSIDL_MYMUSIC;
259 case SF::MyPictures:
return CSIDL_MYPICTURES;
260 case SF::MyVideos:
return CSIDL_MYVIDEO;
261 case SF::ProgramFiles:
return CSIDL_PROGRAM_FILES;
262 case SF::Programs:
return CSIDL_PROGRAMS;
263 case SF::Recent:
return CSIDL_RECENT;
264 case SF::SendTo:
return CSIDL_SENDTO;
265 case SF::StartMenu:
return CSIDL_STARTMENU;
266 case SF::Startup:
return CSIDL_STARTUP;
267 case SF::System:
return CSIDL_SYSTEM;
268 case SF::Templates:
return CSIDL_TEMPLATES;
269 case SF::UserProfile:
return CSIDL_PROFILE;
277 wchar_t buffer[MAX_PATH];
278 if (SHGetFolderPathW(NULL,
GetCsidlForFolder(eFolder), NULL, 0, buffer) == S_OK) {
290 std::vector<String> tempDrives;
293 for (
int iIndex = 0; iIndex < 26; iIndex++) {
294 if ((nDrives >> iIndex) & 1) {
295 char chDriveName[4] = { (char)(
'A' + iIndex),
':',
'\\', 0 };
296 tempDrives.push_back(
String(chDriveName));
300 tempDrives.push_back(
String(
"/"));
303 for (
int iIndex = 0; iIndex < (int)tempDrives.size(); iIndex++) result[iIndex] = tempDrives[iIndex];
311 ::SetEnvironmentVariableW(sWVar.c_str(), sWVal.c_str());
319 OSVERSIONINFOEXW info;
320 ZeroMemory(&info,
sizeof(OSVERSIONINFOEXW));
321 info.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEXW);
322 GetVersionExW((LPOSVERSIONINFOW)&info);
Provides information about, and means to manipulate, the current environment and platform.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Represents a collection of keys and values.
void Add(const TKey &key, const TValue &value)
static String GetSystemDirectory()
Gets the fully qualified path of the system directory.
static String GetUserName()
Gets the user name of the person who is currently logged on to the OS.
static String GetUserDomainName()
Gets the network domain name associated with the current user.
static Array< String > GetCommandLineArgs()
Returns a string array containing the command-line arguments for the current process.
static OperatingSystem GetOperatingSystem()
Gets an OperatingSystem object that contains the current platform identifier and version.
static int GetProcessorCount()
Gets the number of logical processors available on the current machine.
static String ExpandEnvironmentVariables(const String &sName)
Replaces the name of each environment variable embedded in the specified string with the string equiv...
SpecialFolder
Specifies enumerated constants used to retrieve directory paths to system special folders.
static void Exit(int iExitCode)
Terminates this process and returns an exit code to the operating system.
static int64_t GetWorkingSet()
Gets the amount of physical memory mapped to the process context (Working Set in bytes).
static void SetEnvironmentVariable(const String &sVariable, const String &sValue)
Creates, modifies, or deletes an environment variable stored in the current process.
static String GetFolderPath(SpecialFolder eFolder)
Gets the path to the system special folder that is identified by the specified enumeration.
static String GetEnvironmentVariable(const String &sVariable)
Retrieves the value of an environment variable from the current process.
static Collections::Generic::Dictionary< String, String > GetEnvironmentVariables()
Retrieves all environment variable names and their values from the current process.
static Array< String > GetLogicalDrives()
Returns an array of string containing the names of the logical drives on the current computer.
static String GetCurrentDirectory()
Gets the fully qualified path of the current working directory.
static String GetMachineName()
Gets the NetBIOS / network name of this local computer.
static String GetNewLine()
Gets the newline string defined for this environment ("\r\n" on Windows, "\n" on POSIX).
static String GetOSVersion()
Gets an operating system version string representation.
Represents information about an operating system, such as the version and platform identifier.
OperatingSystem(PlatformID platform, const Version &version)
Initializes a new instance of the OperatingSystem class, using the specified platform identifier and ...
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
String Substring(int iStartIndex) const
bool StartsWith(const String &sPrefix) const
bool EndsWith(char ch, bool bIgnoreCase) const
String()
Initializes a new instance of the String class to an empty string.
int IndexOf(const String &sSubstring) const
const char * GetRawString() const
Version()
Initializes a new instance of the Version class to 0.0.0.0.
std::string WCharToUtf8(const wchar_t *pWStr)
std::wstring Utf8ToWChar(const char *pUtf8Str)
static void ParseAndAddEnvEntry(const String &sLine, Collections::Generic::Dictionary< String, String > &result)
static int GetCsidlForFolder(Environment::SpecialFolder eFolder)
static std::vector< String > ReadCommandLineTokens()
@ Unix
Unix or Linux platform.
@ Win32NT
Windows NT, 2000, XP, Vista, 7, 8, 10, 11, or Windows Server.