Battery: add dirty support for macOS

This commit is contained in:
李通洲
2022-09-16 17:41:24 +08:00
committed by Carter Li
parent 9eeb216b70
commit 413d126bcc
4 changed files with 124 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
#include "cfdict_helpers.h"
const void* ffCfDictGetValue(CFMutableDictionaryRef dict, const char* key)
{
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())
return false;
uint32_t length = (uint32_t)CFStringGetLength(cf);
ffStrbufEnsureFree(result, length + 1);
if(CFStringGetCString(cf, result->chars, length + 1, kCFStringEncodingASCII))
{
result->length = length;
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
}
return true;
}
bool ffCfDictGetBool(CFMutableDictionaryRef dict, const char* key, bool* result)
{
CFBooleanRef cf = (CFBooleanRef)ffCfDictGetValue(dict, key);
if(cf == NULL || CFGetTypeID(cf) != CFNumberGetTypeID())
return false;
*result = CFBooleanGetValue(cf);
return true;
}
bool ffCfDictGetInt(CFMutableDictionaryRef dict, const char* key, int* result)
{
CFNumberRef cf = (CFNumberRef)ffCfDictGetValue(dict, key);
if (cf == NULL || CFGetTypeID(cf) != CFStringGetTypeID())
return false;
if(!CFNumberGetValue(cf, kCFNumberSInt32Type, result))
return false;
return true;
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#ifndef FASTFETCH_INCLUDED_cfdict_helpers
#define FASTFETCH_INCLUDED_cfdict_helpers
#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);
#endif