mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Global: major FFlist related code refactor
This commit is contained in:
parent
6d002c175f
commit
39582471e8
@ -7,8 +7,7 @@
|
||||
|
||||
static double detectBatteryTemp()
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY temps;
|
||||
ffListInit(&temps, sizeof(FFTempValue));
|
||||
FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue));
|
||||
|
||||
ffDetectCoreTemps(FF_TEMP_BATTERY, &temps);
|
||||
|
||||
|
@ -15,8 +15,7 @@ static double getFrequency(const char* propName)
|
||||
|
||||
static double detectCpuTemp(const FFstrbuf* cpuName)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY temps;
|
||||
ffListInit(&temps, sizeof(FFTempValue));
|
||||
FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue));
|
||||
|
||||
if(ffStrbufStartsWithS(cpuName, "Apple M1"))
|
||||
ffDetectCoreTemps(FF_TEMP_CPU_M1X, &temps);
|
||||
|
@ -201,8 +201,7 @@ void ffDetectDisksImpl(FFDiskResult* disks)
|
||||
return;
|
||||
}
|
||||
|
||||
FF_LIST_AUTO_DESTROY devices;
|
||||
ffListInit(&devices, sizeof(FFstrbuf));
|
||||
FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
|
@ -9,8 +9,7 @@
|
||||
|
||||
static double detectGpuTemp(const FFstrbuf* gpuName)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY temps;
|
||||
ffListInit(&temps, sizeof(FFTempValue));
|
||||
FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue));
|
||||
|
||||
if(ffStrbufStartsWithS(gpuName, "Apple M1"))
|
||||
ffDetectCoreTemps(FF_TEMP_GPU_M1X, &temps);
|
||||
|
@ -37,8 +37,7 @@ void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options)
|
||||
return;
|
||||
}
|
||||
|
||||
FFlist filtered;
|
||||
ffListInit(&filtered, sizeof(FFBluetoothDevice*));
|
||||
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*));
|
||||
|
||||
FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices)
|
||||
{
|
||||
@ -59,8 +58,6 @@ void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options)
|
||||
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
|
||||
printDevice(instance, options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index);
|
||||
}
|
||||
|
||||
ffListDestroy(&filtered);
|
||||
}
|
||||
|
||||
void ffInitBluetoothOptions(FFBluetoothOptions* options)
|
||||
|
@ -7,8 +7,8 @@
|
||||
|
||||
void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFBrightnessResult));
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult));
|
||||
|
||||
const char* error = ffDetectBrightness(&result);
|
||||
|
||||
if(error)
|
||||
|
@ -22,8 +22,8 @@ static void printDevice(FFinstance* instance, FFGamepadOptions* options, const F
|
||||
|
||||
void ffPrintGamepad(FFinstance* instance, FFGamepadOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFGamepadDevice));
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice));
|
||||
|
||||
const char* error = ffDetectGamepad(instance, &result);
|
||||
|
||||
if(error)
|
||||
|
@ -55,8 +55,7 @@ static void printIp(FFLocalIpResult* ip)
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance, FFLocalIpOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY results;
|
||||
ffListInit(&results, sizeof(FFLocalIpResult));
|
||||
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult));
|
||||
|
||||
const char* error = ffDetectLocalIps(options, &results);
|
||||
|
||||
|
@ -37,8 +37,8 @@ static void printDevice(FFinstance* instance, FFSoundOptions* options, const FFS
|
||||
|
||||
void ffPrintSound(FFinstance* instance, FFSoundOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFSoundDevice));
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice));
|
||||
|
||||
const char* error = ffDetectSound(instance, &result);
|
||||
|
||||
if(error)
|
||||
@ -47,8 +47,7 @@ void ffPrintSound(FFinstance* instance, FFSoundOptions* options)
|
||||
return;
|
||||
}
|
||||
|
||||
FF_LIST_AUTO_DESTROY filtered;
|
||||
ffListInit(&filtered, sizeof(FFSoundDevice*));
|
||||
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFSoundDevice*));
|
||||
|
||||
FF_LIST_FOR_EACH(FFSoundDevice, device, result)
|
||||
{
|
||||
|
@ -7,8 +7,7 @@
|
||||
|
||||
void ffPrintUsers(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY users;
|
||||
ffListInit(&users, sizeof(FFstrbuf));
|
||||
FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate();
|
||||
|
||||
|
@ -7,8 +7,7 @@
|
||||
|
||||
void ffPrintWifi(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFWifiResult));
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult));
|
||||
|
||||
const char* error = ffDetectWifi(instance, &result);
|
||||
if(error)
|
||||
|
@ -55,11 +55,3 @@ bool ffListPop(FFlist* list, void* result)
|
||||
--list->length;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ffListDestroy(FFlist* list)
|
||||
{
|
||||
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
|
||||
list->capacity = list->length = 0;
|
||||
free(list->data);
|
||||
list->data = NULL;
|
||||
}
|
||||
|
@ -31,12 +31,20 @@ bool ffListShift(FFlist* list, void* result);
|
||||
// Removes the last element, and copy its value to `*result`
|
||||
bool ffListPop(FFlist* list, void* result);
|
||||
|
||||
void ffListDestroy(FFlist* list);
|
||||
|
||||
static inline void ffListInit(FFlist* list, uint32_t elementSize)
|
||||
{
|
||||
assert(elementSize > 0);
|
||||
ffListInitA(list, elementSize, 0);
|
||||
list->elementSize = elementSize;
|
||||
list->capacity = 0;
|
||||
list->length = 0;
|
||||
list->data = NULL;
|
||||
}
|
||||
|
||||
static inline FFlist ffListCreate(uint32_t elementSize)
|
||||
{
|
||||
FFlist result;
|
||||
ffListInit(&result, elementSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline void* ffListGet(const FFlist* list, uint32_t index)
|
||||
@ -50,6 +58,16 @@ static inline void ffListSort(FFlist* list, int(*compar)(const void*, const void
|
||||
qsort(list->data, list->length, list->elementSize, compar);
|
||||
}
|
||||
|
||||
static inline void ffListDestroy(FFlist* list)
|
||||
{
|
||||
if (!list->data) return;
|
||||
|
||||
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
|
||||
list->capacity = list->length = 0;
|
||||
free(list->data);
|
||||
list->data = NULL;
|
||||
}
|
||||
|
||||
#define FF_LIST_FOR_EACH(itemType, itemVarName, listVar) \
|
||||
assert(sizeof(itemType) == (listVar).elementSize); \
|
||||
for(itemType* itemVarName = (itemType*)(listVar).data; \
|
||||
|
@ -108,6 +108,13 @@ int main(void)
|
||||
VERIFY(list.capacity == 0);
|
||||
VERIFY(list.length == 0);
|
||||
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY test = ffListCreate(1);
|
||||
VERIFY(test.elementSize = 1);
|
||||
VERIFY(test.capacity == 0);
|
||||
VERIFY(test.length == 0);
|
||||
}
|
||||
|
||||
//Success
|
||||
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user