Skip to content

Commit 1da3a2d

Browse files
committed
basic refactor so the sysinfo() code is legible
1 parent d75cf8c commit 1da3a2d

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/PlatformUtils-win.cc

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,11 @@ static BOOL IsWow64()
121121
return bIsWow64;
122122
}
123123

124-
std::string PlatformUtils::sysinfo(bool extended)
124+
std::string platformid_sysinfo(OSVERSIONINFOEX &osinfo)
125125
{
126-
std::string result;
127-
128-
OSVERSIONINFOEX osinfo;
129-
osinfo.dwOSVersionInfoSize = sizeof(osinfo);
130-
131-
if (GetVersionExEx(&osinfo) == 0) {
132-
result += "Unknown Windows";
133-
} else {
134-
unsigned int version = osinfo.dwMajorVersion * 1000 + osinfo.dwMinorVersion;
135-
if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
136-
switch (version) {
126+
std::string result("");
127+
unsigned int version = osinfo.dwMajorVersion * 1000 + osinfo.dwMinorVersion;
128+
switch (version) {
137129
case 5000:
138130
result += "Windows 2000";
139131
break;
@@ -159,52 +151,67 @@ std::string PlatformUtils::sysinfo(bool extended)
159151
default:
160152
result += "Unknown Windows";
161153
break;
154+
}
155+
156+
if (osinfo.wServicePackMajor > 0) {
157+
boost::format fmtServicePack;
158+
if (osinfo.wServicePackMinor == 0) {
159+
fmtServicePack = boost::format(" SP%d");
160+
fmtServicePack % osinfo.wServicePackMajor;
161+
} else {
162+
fmtServicePack = boost::format(" SP%d.%d");
163+
fmtServicePack % osinfo.wServicePackMajor % osinfo.wServicePackMinor;
162164
}
163-
164-
if (osinfo.wServicePackMajor > 0) {
165-
boost::format fmtServicePack;
166-
if (osinfo.wServicePackMinor == 0) {
167-
fmtServicePack = boost::format(" SP%d");
168-
fmtServicePack % osinfo.wServicePackMajor;
169-
} else {
170-
fmtServicePack = boost::format(" SP%d.%d");
171-
fmtServicePack % osinfo.wServicePackMajor % osinfo.wServicePackMinor;
172-
}
173-
result += fmtServicePack.str();
174-
}
175-
176-
boost::format fmt(" (v%d.%d.%d.%d)");
177-
fmt % osinfo.dwMajorVersion % osinfo.dwMinorVersion % osinfo.wServicePackMajor % osinfo.wServicePackMinor;
178-
result += fmt.str();
165+
result += fmtServicePack.str();
166+
}
167+
168+
boost::format fmt(" (v%d.%d.%d.%d)");
169+
fmt % osinfo.dwMajorVersion % osinfo.dwMinorVersion % osinfo.wServicePackMajor % osinfo.wServicePackMinor;
170+
result += fmt.str();
171+
return result;
172+
}
173+
174+
std::string PlatformUtils::sysinfo(bool extended)
175+
{
176+
std::string result;
177+
178+
OSVERSIONINFOEX osinfo;
179+
osinfo.dwOSVersionInfoSize = sizeof(osinfo);
180+
181+
if (GetVersionExEx(&osinfo) == 0) {
182+
result += "Unknown Windows";
179183
} else {
180-
boost::format fmt("Unknown Windows (dwPlatformId = %d, dwMajorVersion = %d, dwMinorVersion = %d");
181-
fmt % osinfo.dwPlatformId % osinfo.dwMajorVersion % osinfo.dwMinorVersion;
182-
result += fmt.str();
183-
}
184+
if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
185+
result += platformid_sysinfo( osinfo );
186+
} else {
187+
boost::format fmt("Unknown Windows (dwPlatformId = %d, dwMajorVersion = %d, dwMinorVersion = %d");
188+
fmt % osinfo.dwPlatformId % osinfo.dwMajorVersion % osinfo.dwMinorVersion;
189+
result += fmt.str();
190+
}
184191
}
185-
192+
186193
SYSTEM_INFO systeminfo;
187194
bool isWow64 = IsWow64();
188195
if (isWow64) {
189196
GetNativeSystemInfo(&systeminfo);
190197
} else {
191-
GetSystemInfo(&systeminfo);
198+
GetSystemInfo(&systeminfo);
192199
}
193-
200+
194201
if (extended) {
195202
int numcpu = systeminfo.dwNumberOfProcessors;
196203
boost::format fmt(" %d CPU%s%s");
197204
fmt % numcpu % (numcpu > 1 ? "s" : "") % (isWow64 ? " WOW64" : "");
198205
result += fmt.str();
199-
206+
200207
MEMORYSTATUSEX memoryinfo;
201208
memoryinfo.dwLength = sizeof(memoryinfo);
202209
if (GlobalMemoryStatusEx(&memoryinfo) != 0) {
203210
result += " ";
204211
result += PlatformUtils::toMemorySizeString(memoryinfo.ullTotalPhys, 2);
205212
result += " RAM";
206213
}
207-
}
214+
}
208215

209216
return result;
210217
}

src/PlatformUtils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ namespace PlatformUtils {
4545
* OS type is reported based on what platform the application was
4646
* built for.
4747
*
48-
* Extended sysinfo will return more info, like CPUs and RAM
49-
*
48+
* Extended sysinfo will return more info, like CPUs and RAM
5049
* @return system information.
5150
*/
5251
std::string sysinfo(bool extended = true);

0 commit comments

Comments
 (0)