Skip to content

Commit

Permalink
iStats Chris911#86: CFStringGetCStringPtr usually returns null. Added…
Browse files Browse the repository at this point in the history
… fallback

with CFStringGetCString. This is consistent and in compliance with Apple
documentation.
  • Loading branch information
zenenwjaimes committed Dec 10, 2019
1 parent 0b86af3 commit 89a1a9c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions ext/osx_stats/smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,23 @@ const char* getBatteryHealth() {
if(powerSourceInformation == NULL)
return "Unknown";

CFStringRef batteryHealthRef = (CFStringRef) CFDictionaryGetValue(powerSourceInformation, CFSTR("BatteryHealth"));
CFStringRef batteryHealthRef = (CFStringRef) CFDictionaryGetValue(powerSourceInformation, CFSTR(kIOPSBatteryHealthKey));

const char *batteryHealth = CFStringGetCStringPtr(batteryHealthRef, // CFStringRef theString,
kCFStringEncodingMacRoman); //CFStringEncoding encoding);
if(batteryHealth == NULL)
return "unknown";
const char *batteryHealth = CFStringGetCStringPtr(batteryHealthRef, kCFStringEncodingMacRoman);

if(batteryHealth == NULL) {
CFIndex length = CFStringGetLength(batteryHealthRef);
CFIndex bufSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingMacRoman) + 1;
char *buffer = (char *)malloc(bufSize);

if (CFStringGetCString(batteryHealthRef, buffer, bufSize, kCFStringEncodingMacRoman)) {
return buffer;
}

free(buffer);

return "Unknown";
}

return batteryHealth;
}
Expand Down

0 comments on commit 89a1a9c

Please sign in to comment.