mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-04 20:24:19 +01:00
util/cfdict_helpers: code improvements
1. simplify CFString usage 2. support get string from CFDataRef
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user