mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
macOS: improve IO service related code generally
This commit is contained in:
+29
-28
@@ -19,26 +19,42 @@ const char* ffCfNumGetInt64(CFTypeRef cf, int64_t* result)
|
||||
return "TypeID is neither 'CFNumber' nor 'CFData'";
|
||||
}
|
||||
|
||||
const char* ffCfStrGetString(CFStringRef str, FFstrbuf* result)
|
||||
const char* ffCfStrGetString(CFTypeRef cf, FFstrbuf* result)
|
||||
{
|
||||
if (!str)
|
||||
if (!cf)
|
||||
{
|
||||
ffStrbufClear(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t length = (uint32_t)CFStringGetLength(str);
|
||||
//CFString stores UTF16 characters, therefore may require larger buffer to convert to UTF8 string
|
||||
ffStrbufEnsureFree(result, length * 2);
|
||||
if(!CFStringGetCString(str, result->chars, result->allocated, kCFStringEncodingUTF8))
|
||||
if (CFGetTypeID(cf) == CFStringGetTypeID())
|
||||
{
|
||||
ffStrbufEnsureFree(result, length * 4);
|
||||
if(!CFStringGetCString(str, result->chars, result->allocated, kCFStringEncodingUTF8))
|
||||
return "CFStringGetCString() failed";
|
||||
CFStringRef cfStr = (CFStringRef)cf;
|
||||
uint32_t length = (uint32_t)CFStringGetLength(cfStr);
|
||||
//CFString stores UTF16 characters, therefore may require larger buffer to convert to UTF8 string
|
||||
ffStrbufEnsureFree(result, length * 2);
|
||||
if (!CFStringGetCString(cfStr, result->chars, result->allocated, kCFStringEncodingUTF8))
|
||||
{
|
||||
ffStrbufEnsureFree(result, length * 4);
|
||||
if(!CFStringGetCString(cfStr, result->chars, result->allocated, kCFStringEncodingUTF8))
|
||||
return "CFStringGetCString() failed";
|
||||
}
|
||||
// CFStringGetCString ensures the buffer is NUL terminated
|
||||
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
|
||||
result->length = (uint32_t) strnlen(result->chars, (uint32_t)result->allocated);
|
||||
}
|
||||
// CFStringGetCString ensures the buffer is NUL terminated
|
||||
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
|
||||
result->length = (uint32_t) strnlen(result->chars, (uint32_t)result->allocated);
|
||||
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 "TypeID is neither 'CFString' nor 'CFData'";
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -48,22 +64,7 @@ const char* ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* r
|
||||
if(cf == NULL)
|
||||
return "CFDictionaryGetValue() failed";
|
||||
|
||||
if(CFGetTypeID(cf) == CFStringGetTypeID())
|
||||
{
|
||||
return ffCfStrGetString((CFStringRef)cf, result);
|
||||
}
|
||||
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';
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return "TypeID is neither 'CFString' nor 'CFData'";
|
||||
return ffCfStrGetString(cf, result);
|
||||
}
|
||||
|
||||
const char* ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result)
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <IOKit/IOKitLib.h>
|
||||
|
||||
//Return error info if failed, NULL otherwise
|
||||
const char* ffCfStrGetString(CFStringRef str, FFstrbuf* result);
|
||||
const char* ffCfStrGetString(CFTypeRef cf, FFstrbuf* result);
|
||||
const char* ffCfNumGetInt64(CFTypeRef cf, int64_t* result);
|
||||
const char* ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* result);
|
||||
const char* ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result);
|
||||
@@ -29,4 +30,12 @@ static inline void cfReleaseWrapper(void* type)
|
||||
|
||||
#define FF_CFTYPE_AUTO_RELEASE __attribute__((__cleanup__(cfReleaseWrapper)))
|
||||
|
||||
static inline void wrapIoObjectRelease(io_service_t* service)
|
||||
{
|
||||
assert(service);
|
||||
if (*service)
|
||||
IOObjectRelease(*service);
|
||||
}
|
||||
#define FF_IOOBJECT_AUTO_RELEASE __attribute__((__cleanup__(wrapIoObjectRelease)))
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user