Global: major FFstrbuf related code refactor

This commit is contained in:
李通洲
2023-04-15 15:29:54 +08:00
parent 2df1305521
commit c7119498a3
76 changed files with 274 additions and 537 deletions
+1 -3
View File
@@ -17,8 +17,7 @@ static void printBattery(FFinstance* instance, FFBatteryOptions* options, Batter
result->status.length > 0 &&
ffStrbufIgnCaseCompS(&result->status, "Unknown") != 0;
FFstrbuf str;
ffStrbufInit(&str);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(result->capacity >= 0)
{
@@ -58,7 +57,6 @@ static void printBattery(FFinstance* instance, FFBatteryOptions* options, Batter
}
ffStrbufPutTo(&str, stdout);
ffStrbufDestroy(&str);
}
else
{
+1 -2
View File
@@ -23,8 +23,7 @@ void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options)
return;
}
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
FF_LIST_FOR_EACH(FFBrightnessResult, item, result)
{
+1 -4
View File
@@ -26,15 +26,12 @@ void ffPrintChassis(FFinstance* instance)
{
ffPrintLogoAndKey(instance, FF_CHASSIS_MODULE_NAME, 0, &instance->config.chassis.key);
FFstrbuf output;
ffStrbufInitCopy(&output, &result.chassisType);
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateCopy(&result.chassisType);
if(result.chassisVersion.length > 0)
ffStrbufAppendF(&output, " (%s)", result.chassisVersion.chars);
ffStrbufPutTo(&output, stdout);
ffStrbufDestroy(&output);
}
else
{
+1 -2
View File
@@ -6,8 +6,7 @@
void ffPrintCommand(FFinstance* instance, FFCommandOptions* options)
{
FF_STRBUF_AUTO_DESTROY result;
ffStrbufInit(&result);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
options->shell.chars,
#ifdef _WIN32
+1 -2
View File
@@ -22,8 +22,7 @@ void ffPrintCPUUsage(FFinstance* instance, FFCPUUsageOptions* options)
{
ffPrintLogoAndKey(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs.key);
FF_STRBUF_AUTO_DESTROY str;
ffStrbufInit(&str);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
ffAppendPercentBar(instance, &str, (uint8_t)percentage, 0, 5, 8);
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
+5 -10
View File
@@ -10,8 +10,7 @@
static void printDisk(FFinstance* instance, FFDiskOptions* options, const FFDisk* disk)
{
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
if(options->moduleArgs.key.length == 0)
{
@@ -24,12 +23,10 @@ static void printDisk(FFinstance* instance, FFDiskOptions* options, const FFDisk
});
}
FF_STRBUF_AUTO_DESTROY usedPretty;
ffStrbufInit(&usedPretty);
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
ffParseSize(disk->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
FF_STRBUF_AUTO_DESTROY totalPretty;
ffStrbufInit(&totalPretty);
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
@@ -38,8 +35,7 @@ static void printDisk(FFinstance* instance, FFDiskOptions* options, const FFDisk
{
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
FF_STRBUF_AUTO_DESTROY str;
ffStrbufInit(&str);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(disk->bytesTotal > 0)
{
@@ -109,8 +105,7 @@ static void printMountpoints(FFinstance* instance, FFDiskOptions* options, const
const char separator = ':';
#endif
FFstrbuf mountpoints;
ffStrbufInitCopy(&mountpoints, &options->folders);
FF_STRBUF_AUTO_DESTROY mountpoints = ffStrbufCreateCopy(&options->folders);
ffStrbufTrim(&mountpoints, separator);
uint32_t startIndex = 0;
+1 -2
View File
@@ -43,8 +43,7 @@ void ffPrintDisplay(FFinstance* instance, FFDisplayOptions* options)
return;
}
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
for(uint32_t i = 0; i < dsResult->displays.length; i++)
{
+1 -2
View File
@@ -16,8 +16,7 @@ static void printGPUResult(FFinstance* instance, FFGPUOptions* options, uint8_t
{
ffPrintLogoAndKey(instance, FF_GPU_MODULE_NAME, index, &options->moduleArgs.key);
FF_STRBUF_AUTO_DESTROY output;
ffStrbufInitA(&output, gpu->vendor.length + 1 + gpu->name.length);
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateA(gpu->vendor.length + 1 + gpu->name.length);
if(gpu->vendor.length > 0 && !ffStrbufStartsWith(&gpu->name, &gpu->vendor))
{
+1 -4
View File
@@ -25,8 +25,7 @@ void ffPrintHost(FFinstance* instance, FFHostOptions* options)
{
ffPrintLogoAndKey(instance, FF_HOST_MODULE_NAME, 0, &options->moduleArgs.key);
FFstrbuf output;
ffStrbufInit(&output);
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreate();
if(host->productName.length > 0)
ffStrbufAppend(&output, &host->productName);
@@ -39,8 +38,6 @@ void ffPrintHost(FFinstance* instance, FFHostOptions* options)
}
ffStrbufPutTo(&output, stdout);
ffStrbufDestroy(&output);
}
else
{
+1 -2
View File
@@ -7,8 +7,7 @@
void ffPrintIcons(FFinstance* instance)
{
FF_STRBUF_AUTO_DESTROY icons;
ffStrbufInit(&icons);
FF_STRBUF_AUTO_DESTROY icons = ffStrbufCreate();
const char* error = ffDetectIcons(instance, &icons);
if(error)
+1 -2
View File
@@ -149,8 +149,7 @@ static const char* printJsonConfig(FFinstance* instance)
if (!ffJsonLoadLibrary(instance))
return "Failed to load json-c library";
FF_STRBUF_AUTO_DESTROY content;
ffStrbufInit(&content);
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
FF_LIST_FOR_EACH(FFstrbuf, filename, instance->state.platform.configDirs)
{
uint32_t oldLength = filename->length;
+1 -2
View File
@@ -7,8 +7,7 @@
void ffPrintLocale(FFinstance* instance, FFLocaleOptions* options)
{
FF_STRBUF_AUTO_DESTROY locale;
ffStrbufInit(&locale);
FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate();
ffDetectLocale(&locale);
if(locale.length == 0)
+1 -2
View File
@@ -87,8 +87,7 @@ void ffPrintLocalIp(FFinstance* instance, FFLocalIpOptions* options)
}
else
{
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
{
+2 -8
View File
@@ -52,8 +52,7 @@ void ffPrintMedia(FFinstance* instance)
return;
}
FFstrbuf songPretty;
ffStrbufInitCopy(&songPretty, &media->song);
FF_STRBUF_AUTO_DESTROY songPretty = ffStrbufCreateCopy(&media->song);
const char* removeStrings[] = {
"(Official Music Video)", "(Official Video)", "(Music Video)", "(Official HD Video)",
"[Official Music Video]", "[Official Video]", "[Music Video]", "[Official HD Video]",
@@ -72,8 +71,7 @@ void ffPrintMedia(FFinstance* instance)
if(instance->config.media.outputFormat.length == 0)
{
//We don't expose artistPretty to the format, as it might be empty (when the think that the artist is already in the song title)
FFstrbuf artistPretty;
ffStrbufInitCopy(&artistPretty, &media->artist);
FF_STRBUF_AUTO_DESTROY artistPretty = ffStrbufCreateCopy(&media->artist);
ffStrbufRemoveIgnCaseEndS(&artistPretty, " - Topic");
ffStrbufRemoveIgnCaseEndS(&artistPretty, "VEVO");
ffStrbufTrimRight(&artistPretty, ' ');
@@ -93,8 +91,6 @@ void ffPrintMedia(FFinstance* instance)
ffStrbufAppendF(&songPretty, " (%s)", media->status.chars);
ffStrbufPutTo(&songPretty, stdout);
ffStrbufDestroy(&artistPretty);
}
else
{
@@ -106,6 +102,4 @@ void ffPrintMedia(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &media->status}
});
}
ffStrbufDestroy(&songPretty);
}
+3 -6
View File
@@ -18,12 +18,10 @@ void ffPrintMemory(FFinstance* instance)
return;
}
FF_STRBUF_AUTO_DESTROY usedPretty;
ffStrbufInit(&usedPretty);
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty);
FF_STRBUF_AUTO_DESTROY totalPretty;
ffStrbufInit(&totalPretty);
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t percentage = storage.bytesTotal == 0
@@ -37,8 +35,7 @@ void ffPrintMemory(FFinstance* instance)
puts("Disabled");
else
{
FF_STRBUF_AUTO_DESTROY str;
ffStrbufInit(&str);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
+1 -2
View File
@@ -107,8 +107,7 @@ void ffPrintOS(FFinstance* instance, FFOSOptions* options)
if(options->moduleArgs.outputFormat.length == 0)
{
FF_STRBUF_AUTO_DESTROY result;
ffStrbufInit(&result);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
buildOutputNixOS(instance, os, &result);
+1 -4
View File
@@ -17,8 +17,7 @@ void ffPrintPlayer(FFinstance* instance)
return;
}
FFstrbuf playerPretty;
ffStrbufInit(&playerPretty);
FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate();
//If we are on a website, prepend the website name
if(
@@ -70,6 +69,4 @@ void ffPrintPlayer(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &media->url}
});
}
ffStrbufDestroy(&playerPretty);
}
+3 -12
View File
@@ -14,13 +14,11 @@ void ffPreparePublicIp(FFinstance* instance)
status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/ip", NULL);
else
{
FFstrbuf host;
ffStrbufInitCopy(&host, &instance->config.publicIpUrl);
FF_STRBUF_AUTO_DESTROY host = ffStrbufCreateCopy(&instance->config.publicIpUrl);
ffStrbufSubstrAfterFirstS(&host, "://");
uint32_t pathStartIndex = ffStrbufFirstIndexC(&host, '/');
FFstrbuf path;
ffStrbufInit(&path);
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
if(pathStartIndex != host.length)
{
ffStrbufAppendNS(&path, pathStartIndex, host.chars + (host.length - pathStartIndex));
@@ -29,9 +27,6 @@ void ffPreparePublicIp(FFinstance* instance)
}
status = ffNetworkingSendHttpRequest(&state, host.chars, path.length == 0 ? "/" : path.chars, NULL);
ffStrbufDestroy(&path);
ffStrbufDestroy(&host);
}
}
@@ -46,15 +41,13 @@ void ffPrintPublicIp(FFinstance* instance)
return;
}
FFstrbuf result;
ffStrbufInitA(&result, 4096);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(4096);
bool success = ffNetworkingRecvHttpResponse(&state, &result, instance->config.publicIpTimeout);
if(success) ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
if(!success || result.length == 0)
{
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to receive the server response");
ffStrbufDestroy(&result);
return;
}
@@ -69,6 +62,4 @@ void ffPrintPublicIp(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
});
}
ffStrbufDestroy(&result);
}
+3 -6
View File
@@ -18,12 +18,10 @@ void ffPrintSwap(FFinstance* instance)
return;
}
FF_STRBUF_AUTO_DESTROY usedPretty;
ffStrbufInit(&usedPretty);
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty);
FF_STRBUF_AUTO_DESTROY totalPretty;
ffStrbufInit(&totalPretty);
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t percentage = storage.bytesTotal == 0
@@ -37,8 +35,7 @@ void ffPrintSwap(FFinstance* instance)
puts("Disabled");
else
{
FF_STRBUF_AUTO_DESTROY str;
ffStrbufInit(&str);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
+3 -11
View File
@@ -7,24 +7,20 @@
void ffPrintUsers(FFinstance* instance)
{
FFlist users;
FF_LIST_AUTO_DESTROY users;
ffListInit(&users, sizeof(FFstrbuf));
FFstrbuf error;
ffStrbufInit(&error);
FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate();
ffDetectUsers(&users, &error);
if(error.length > 0)
{
ffPrintError(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users, "%*s", error.length, error.chars);
ffListDestroy(&users);
ffStrbufDestroy(&error);
return;
}
FFstrbuf result;
ffStrbufInit(&result);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
for(uint32_t i = 0; i < users.length; ++i)
{
if(i > 0)
@@ -34,8 +30,6 @@ void ffPrintUsers(FFinstance* instance)
ffStrbufDestroy(user);
}
ffListDestroy(&users);
if(instance->config.users.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users.key);
@@ -47,6 +41,4 @@ void ffPrintUsers(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &result},
});
}
ffStrbufDestroy(&result);
}
+1 -2
View File
@@ -7,8 +7,7 @@
void ffPrintWallpaper(FFinstance* instance)
{
FF_STRBUF_AUTO_DESTROY wallpaper;
ffStrbufInit(&wallpaper);
FF_STRBUF_AUTO_DESTROY wallpaper = ffStrbufCreate();
const char* error = ffDetectWallpaper(instance, &wallpaper);
if(error)
+2 -8
View File
@@ -10,11 +10,9 @@ static int status = -1;
void ffPrepareWeather(FFinstance* instance)
{
FFstrbuf path;
ffStrbufInitS(&path, "/?format=");
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/?format=");
ffStrbufAppend(&path, &instance->config.weatherOutputFormat);
status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
ffStrbufDestroy(&path);
}
void ffPrintWeather(FFinstance* instance)
@@ -28,15 +26,13 @@ void ffPrintWeather(FFinstance* instance)
return;
}
FFstrbuf result;
ffStrbufInitA(&result, 4096);
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(4096);
bool success = ffNetworkingRecvHttpResponse(&state, &result, instance->config.weatherTimeout);
if (success) ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
if(!success || result.length == 0)
{
ffPrintError(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, "Failed to receive the server response");
ffStrbufDestroy(&result);
return;
}
@@ -51,6 +47,4 @@ void ffPrintWeather(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
});
}
ffStrbufDestroy(&result);
}
+1 -3
View File
@@ -7,8 +7,7 @@
void ffPrintWMTheme(FFinstance* instance)
{
FFstrbuf themeOrError;
ffStrbufInit(&themeOrError);
FF_STRBUF_AUTO_DESTROY themeOrError = ffStrbufCreate();
if(ffDetectWmTheme(instance, &themeOrError))
{
if(instance->config.wmTheme.outputFormat.length == 0)
@@ -27,5 +26,4 @@ void ffPrintWMTheme(FFinstance* instance)
{
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmTheme, "%*s", themeOrError.length, themeOrError.chars);
}
ffStrbufDestroy(&themeOrError);
}