util/cfdict_helpers: code improvements

1. simplify CFString usage
2. support get string from CFDataRef
This commit is contained in:
Carter Li
2022-09-17 02:17:27 +08:00
parent 3d4ce07892
commit d46f9ab4dc
4 changed files with 41 additions and 30 deletions
+5 -4
View File
@@ -31,13 +31,14 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
BatteryResult* battery = ffListAdd(results);
ffStrbufInit(&battery->capacity);
int currentCapacity, maxCapacity;
if(ffCfDictGetInt(properties, "CurrentCapacity", &currentCapacity) && ffCfDictGetInt(properties, "MaxCapacity", &maxCapacity))
if(ffCfDictGetInt(properties, CFSTR("CurrentCapacity"), &currentCapacity) &&
ffCfDictGetInt(properties, CFSTR("MaxCapacity"), &maxCapacity))
ffStrbufAppendF(&battery->capacity, "%.0f", currentCapacity * 100.0 / maxCapacity);
ffStrbufInit(&battery->manufacturer);
ffStrbufInit(&battery->modelName);
ffStrbufInit(&battery->technology);
if (ffCfDictGetBool(properties, "built-in", &boolValue) && boolValue)
if (ffCfDictGetBool(properties, CFSTR("built-in"), &boolValue) && boolValue)
{
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
ffStrbufAppendS(&battery->modelName, "Builtin");
@@ -51,9 +52,9 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
}
ffStrbufInit(&battery->status);
if (ffCfDictGetBool(properties, "FullyCharged", &boolValue) && boolValue)
if (ffCfDictGetBool(properties, CFSTR("FullyCharged"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Fully charged");
else if (ffCfDictGetBool(properties, "IsCharging", &boolValue) && boolValue)
else if (ffCfDictGetBool(properties, CFSTR("IsCharging"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Charging");
else
ffStrbufAppendS(&battery->status, "");
+3 -3
View File
@@ -29,15 +29,15 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
ffStrbufInitA(&gpu->vendor, 0);
ffStrbufInit(&gpu->driver);
ffCfDictGetString(properties, "CFBundleIdentifier", &gpu->driver);
ffCfDictGetString(properties, CFSTR("CFBundleIdentifier"), &gpu->driver);
ffStrbufInit(&gpu->name);
//IOAccelerator returns model property for Apple Silicon, but not for Intel Iris GPUs.
//Still needs testing for AMD's
if(!ffCfDictGetString(properties, "model", &gpu->name) && gpu->driver.length > 0)
if(!ffCfDictGetString(properties, CFSTR("model"), &gpu->name) && gpu->driver.length > 0)
ffStrbufAppendS(&gpu->name, gpu->driver.chars + ffStrbufLastIndexC(&gpu->driver, '.') + 1);
if(!ffCfDictGetInt(properties, "gpu-core-count", &gpu->coreCount))
if(!ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount))
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
+30 -19
View File
@@ -1,31 +1,42 @@
#include "cfdict_helpers.h"
const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* key)
bool ffCfDictGetString(CFMutableDictionaryRef dict, CFStringRef key, FFstrbuf* result)
{
CFStringRef cfKey = CFStringCreateWithCStringNoCopy(NULL, key, kCFStringEncodingASCII, kCFAllocatorNull);
return CFDictionaryGetValue(dict, cfKey);
}
bool ffCfDictGetString(CFMutableDictionaryRef dict, const char* key, FFstrbuf* result)
{
CFStringRef cf = (CFStringRef)ffCfDictGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID())
CFTypeRef cf = (CFTypeRef)CFDictionaryGetValue(dict, key);
if(cf == NULL)
return false;
uint32_t length = (uint32_t)CFStringGetLength(cf);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cf, result->chars, length + 1, kCFStringEncodingASCII))
if(CFGetTypeID(cf) == CFStringGetTypeID())
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
CFStringRef cfStr = (CFStringRef)cf;
uint32_t length = (uint32_t)CFStringGetLength(cfStr);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cfStr, result->chars, length + 1, kCFStringEncodingASCII))
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
}
}
else if(CFGetTypeID(cf) == CFDataGetTypeID())
{
CFDataRef cfData = (CFDataRef)cf;
uint32_t length = (uint32_t)CFDataGetLength(cfData);
ffStrbufEnsureFree(result, length + 1);
CFDataGetBytes(cfData, CFRangeMake(0, length), (uint8_t*)result->chars);
result->length = (uint32_t)strnlen(result->chars, length);
result->chars[result->length] = '\0';
}
else
{
return false;
}
return true;
}
bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result)
bool ffCfDictGetBool(CFMutableDictionaryRef dict, CFStringRef key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)ffCfDictGetValue(dict, key);
CFBooleanRef cf = (CFBooleanRef)CFDictionaryGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID())
return false;
@@ -33,9 +44,9 @@ bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result)
return true;
}
bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result)
bool ffCfDictGetInt(CFMutableDictionaryRef dict, CFStringRef key, int* result)
{
CFNumberRef cf = (CFNumberRef)ffCfDictGetValue(dict, key);
CFNumberRef cf = (CFNumberRef)CFDictionaryGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return false;
+3 -4
View File
@@ -6,9 +6,8 @@
#include "fastfetch.h"
#include <CoreFoundation/CoreFoundation.h>
const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* str);
bool ffCfDictGetString(CFMutableDictionaryRef dict, const char* key, FFstrbuf* result);
bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result);
bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result);
bool ffCfDictGetString(CFMutableDictionaryRef dict, CFStringRef key, FFstrbuf* result);
bool ffCfDictGetBool(CFMutableDictionaryRef dict, CFStringRef key, bool* result);
bool ffCfDictGetInt(CFMutableDictionaryRef dict, CFStringRef key, int* result);
#endif