util/cf_helpers: rename; return error info if failed

This commit is contained in:
李通洲
2022-09-20 16:50:15 +08:00
parent c282384dab
commit 482e5a5eff
8 changed files with 85 additions and 81 deletions
+1 -1
View File
@@ -272,7 +272,7 @@ endif()
if(APPLE)
list(APPEND LIBFASTFETCH_SRC
src/util/apple/cfdict_helpers.c
src/util/apple/cf_helpers.c
src/util/apple/osascript.m
src/detection/host/host_apple.c
src/detection/os/os_apple.c
+6 -6
View File
@@ -1,6 +1,6 @@
#include "fastfetch.h"
#include "battery.h"
#include "util/apple/cfdict_helpers.h"
#include "util/apple/cf_helpers.h"
#include <IOKit/IOKitLib.h>
@@ -31,14 +31,14 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
BatteryResult* battery = ffListAdd(results);
ffStrbufInit(&battery->capacity);
int currentCapacity, maxCapacity;
if(ffCfDictGetInt(properties, CFSTR("CurrentCapacity"), &currentCapacity) &&
ffCfDictGetInt(properties, CFSTR("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, CFSTR("built-in"), &boolValue) && boolValue)
if (!ffCfDictGetBool(properties, CFSTR("built-in"), &boolValue) && boolValue)
{
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
ffStrbufAppendS(&battery->modelName, "Builtin");
@@ -52,9 +52,9 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results)
}
ffStrbufInit(&battery->status);
if (ffCfDictGetBool(properties, CFSTR("FullyCharged"), &boolValue) && boolValue)
if (!ffCfDictGetBool(properties, CFSTR("FullyCharged"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Fully charged");
else if (ffCfDictGetBool(properties, CFSTR("IsCharging"), &boolValue) && boolValue)
else if (!ffCfDictGetBool(properties, CFSTR("IsCharging"), &boolValue) && boolValue)
ffStrbufAppendS(&battery->status, "Charging");
else
ffStrbufAppendS(&battery->status, "");
+3 -3
View File
@@ -1,7 +1,7 @@
#include "gpu.h"
#include "common/library.h"
#include "detection/cpu/cpu.h"
#include "util/apple/cfdict_helpers.h"
#include "util/apple/cf_helpers.h"
#include <IOKit/graphics/IOGraphicsLib.h>
@@ -34,7 +34,7 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
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, CFSTR("model"), &gpu->name))
if(ffCfDictGetString(properties, CFSTR("model"), &gpu->name))
{
CFRelease(properties);
@@ -49,7 +49,7 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance)
ffCfDictGetString(properties, CFSTR("model"), &gpu->name);
}
if(!ffCfDictGetInt(properties, CFSTR("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;
+1 -1
View File
@@ -1,7 +1,7 @@
#include "fastfetch.h"
#include "detection/media/media.h"
#include "common/library.h"
#include "util/apple/cfdict_helpers.h"
#include "util/apple/cf_helpers.h"
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
+59
View File
@@ -0,0 +1,59 @@
#include "cf_helpers.h"
const char* ffCfStrGetString(CFStringRef str, FFstrbuf* result)
{
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))
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);
return NULL;
}
const char* ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* result)
{
CFTypeRef cf = (CFTypeRef)CFDictionaryGetValue(dict, key);
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'";
}
const char* ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)CFDictionaryGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID())
return "TypeID is not 'CFBoolean'";
*result = CFBooleanGetValue(cf);
return NULL;
}
const char* ffCfDictGetInt(CFDictionaryRef dict, CFStringRef key, int* result)
{
CFNumberRef cf = (CFNumberRef)CFDictionaryGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return "TypeID is not 'CFNumber'";
if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result))
return "Number type is not SInt32";
return NULL;
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#ifndef FASTFETCH_INCLUDED_cf_helpers
#define FASTFETCH_INCLUDED_cf_helpers
#include "fastfetch.h"
#include <CoreFoundation/CoreFoundation.h>
//Return error info if failed, NULL otherwise
const char* ffCfStrGetString(CFStringRef str, FFstrbuf* result);
const char* ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* result);
const char* ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result);
const char* ffCfDictGetInt(CFDictionaryRef dict, CFStringRef key, int* result);
#endif
-57
View File
@@ -1,57 +0,0 @@
#include "cfdict_helpers.h"
bool ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* result)
{
CFTypeRef cf = (CFTypeRef)CFDictionaryGetValue(dict, key);
if(cf == NULL)
return false;
if(CFGetTypeID(cf) == CFStringGetTypeID())
{
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))
{
// 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 false;
}
return true;
}
bool ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)CFDictionaryGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFBooleanGetTypeID())
return false;
*result = CFBooleanGetValue(cf);
return true;
}
bool ffCfDictGetInt(CFDictionaryRef dict, CFStringRef key, int* result)
{
CFNumberRef cf = (CFNumberRef)CFDictionaryGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return false;
if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result))
return false;
return true;
}
-13
View File
@@ -1,13 +0,0 @@
#pragma once
#ifndef FASTFETCH_INCLUDED_cfdict_helpers
#define FASTFETCH_INCLUDED_cfdict_helpers
#include "fastfetch.h"
#include <CoreFoundation/CoreFoundation.h>
bool ffCfDictGetString(CFDictionaryRef dict, CFStringRef key, FFstrbuf* result);
bool ffCfDictGetBool(CFDictionaryRef dict, CFStringRef key, bool* result);
bool ffCfDictGetInt(CFDictionaryRef dict, CFStringRef key, int* result);
#endif