mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:24:58 +02:00
FFPlatform: only add a path to data dirs if the path exits
This commit is contained in:
@@ -53,6 +53,11 @@ static inline void* ffListGet(const FFlist* list, uint32_t index)
|
||||
return list->data + (index * list->elementSize);
|
||||
}
|
||||
|
||||
static inline bool ffListContains(const FFlist* list, void* compElement, bool(*compFunc)(const void*, const void*))
|
||||
{
|
||||
return ffListFirstIndexComp(list, compElement, compFunc) != list->length;
|
||||
}
|
||||
|
||||
static inline void ffListSort(FFlist* list, int(*compar)(const void*, const void*))
|
||||
{
|
||||
qsort(list->data, list->length, list->elementSize, compar);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "FFPlatform_private.h"
|
||||
#include "util/stringUtils.h"
|
||||
#include "common/io/io.h"
|
||||
|
||||
void ffPlatformInit(FFPlatform* platform)
|
||||
{
|
||||
@@ -68,55 +69,21 @@ void ffPlatformDestroy(FFPlatform* platform)
|
||||
|
||||
void ffPlatformPathAddAbsolute(FFlist* dirs, const char* path)
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppendS(buffer, path);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_PLATFORM_PATH_UNIQUE(dirs, buffer);
|
||||
if (!ffPathExists(path, FF_PATHTYPE_DIRECTORY))
|
||||
return;
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateS(path);
|
||||
ffStrbufEnsureEndsWithC(&buffer, '/');
|
||||
if (!ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
||||
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
||||
}
|
||||
|
||||
void ffPlatformPathAddHome(FFlist* dirs, const FFPlatform* platform, const char* suffix)
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppend(buffer, &platform->homeDir);
|
||||
ffStrbufAppendS(buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_PLATFORM_PATH_UNIQUE(dirs, buffer);
|
||||
}
|
||||
|
||||
void ffPlatformPathAddEnv(FFlist* dirs, const char* env)
|
||||
{
|
||||
const char* envValue = getenv(env);
|
||||
if(!ffStrSet(envValue))
|
||||
return;
|
||||
|
||||
FFstrbuf value;
|
||||
ffStrbufInitA(&value, 64);
|
||||
ffStrbufAppendS(&value, envValue);
|
||||
|
||||
uint32_t startIndex = 0;
|
||||
while (startIndex < value.length)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const char separator = ';';
|
||||
#else
|
||||
const char separator = ':';
|
||||
#endif
|
||||
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&value, startIndex, separator);
|
||||
value.chars[colonIndex] = '\0';
|
||||
|
||||
if(!ffStrSet(value.chars + startIndex))
|
||||
{
|
||||
startIndex = colonIndex + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPlatformPathAddAbsolute(dirs, value.chars + startIndex);
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&value);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateA(64);
|
||||
ffStrbufAppend(&buffer, &platform->homeDir);
|
||||
ffStrbufAppendS(&buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(&buffer, '/');
|
||||
if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
||||
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
||||
}
|
||||
|
||||
@@ -7,15 +7,7 @@
|
||||
|
||||
void ffPlatformInitImpl(FFPlatform* platform);
|
||||
|
||||
#define FF_PLATFORM_PATH_UNIQUE(list, element) \
|
||||
if(ffListFirstIndexComp(list, element, (bool(*)(const void*, const void*))ffStrbufEqual) < list->length - 1) \
|
||||
{ \
|
||||
ffStrbufDestroy(ffListGet(list, list->length - 1)); \
|
||||
--list->length; \
|
||||
}
|
||||
|
||||
void ffPlatformPathAddAbsolute(FFlist* dirs, const char* path);
|
||||
void ffPlatformPathAddHome(FFlist* dirs, const FFPlatform* platform, const char* suffix);
|
||||
void ffPlatformPathAddEnv(FFlist* dirs, const char* env);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,11 +1,39 @@
|
||||
#include "FFPlatform_private.h"
|
||||
#include "util/stringUtils.h"
|
||||
#include "fastfetch_config.h"
|
||||
#include "common/io/io.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
static void platformPathAddEnv(FFlist* dirs, const char* env)
|
||||
{
|
||||
const char* envValue = getenv(env);
|
||||
if(!ffStrSet(envValue))
|
||||
return;
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY value = ffStrbufCreateA(64);
|
||||
ffStrbufAppendS(&value, envValue);
|
||||
|
||||
uint32_t startIndex = 0;
|
||||
while (startIndex < value.length)
|
||||
{
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&value, startIndex, ':');
|
||||
value.chars[colonIndex] = '\0';
|
||||
|
||||
if(!ffStrSet(value.chars + startIndex))
|
||||
{
|
||||
startIndex = colonIndex + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPlatformPathAddAbsolute(dirs, value.chars + startIndex);
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void getHomeDir(FFPlatform* platform, const struct passwd* pwd)
|
||||
{
|
||||
const char* home = pwd ? pwd->pw_dir : getenv("HOME");
|
||||
@@ -30,7 +58,7 @@ static void getCacheDir(FFPlatform* platform)
|
||||
|
||||
static void getConfigDirs(FFPlatform* platform)
|
||||
{
|
||||
ffPlatformPathAddEnv(&platform->configDirs, "XDG_CONFIG_HOME");
|
||||
platformPathAddEnv(&platform->configDirs, "XDG_CONFIG_HOME");
|
||||
ffPlatformPathAddHome(&platform->configDirs, platform, ".config/");
|
||||
|
||||
#if defined(__APPLE__)
|
||||
@@ -39,7 +67,7 @@ static void getConfigDirs(FFPlatform* platform)
|
||||
#endif
|
||||
|
||||
ffPlatformPathAddHome(&platform->configDirs, platform, "");
|
||||
ffPlatformPathAddEnv(&platform->configDirs, "XDG_CONFIG_DIRS");
|
||||
platformPathAddEnv(&platform->configDirs, "XDG_CONFIG_DIRS");
|
||||
|
||||
#if !defined(__APPLE__)
|
||||
ffPlatformPathAddAbsolute(&platform->configDirs, FASTFETCH_TARGET_DIR_ETC "/xdg/");
|
||||
@@ -51,7 +79,7 @@ static void getConfigDirs(FFPlatform* platform)
|
||||
|
||||
static void getDataDirs(FFPlatform* platform)
|
||||
{
|
||||
ffPlatformPathAddEnv(&platform->dataDirs, "XDG_DATA_HOME");
|
||||
platformPathAddEnv(&platform->dataDirs, "XDG_DATA_HOME");
|
||||
ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/");
|
||||
|
||||
#if defined(__APPLE__)
|
||||
@@ -59,7 +87,7 @@ static void getDataDirs(FFPlatform* platform)
|
||||
#endif
|
||||
|
||||
ffPlatformPathAddHome(&platform->dataDirs, platform, "");
|
||||
ffPlatformPathAddEnv(&platform->dataDirs, "XDG_DATA_DIRS");
|
||||
platformPathAddEnv(&platform->dataDirs, "XDG_DATA_DIRS");
|
||||
ffPlatformPathAddAbsolute(&platform->dataDirs, FASTFETCH_TARGET_DIR_USR "/local/share/");
|
||||
ffPlatformPathAddAbsolute(&platform->dataDirs, FASTFETCH_TARGET_DIR_USR "/share/");
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#include "FFPlatform_private.h"
|
||||
#include "common/io/io.h"
|
||||
#include "util/stringUtils.h"
|
||||
#include "util/windows/unicode.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <shlobj.h>
|
||||
|
||||
static void getHomeDir(FFPlatform* platform)
|
||||
{
|
||||
PWSTR pPath;
|
||||
@@ -38,11 +40,11 @@ static void platformPathAddKnownFolder(FFlist* dirs, REFKNOWNFOLDERID folderId)
|
||||
PWSTR pPath;
|
||||
if(SUCCEEDED(SHGetKnownFolderPath(folderId, 0, NULL, &pPath)))
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInitWS(buffer, pPath);
|
||||
ffStrbufReplaceAllC(buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_PLATFORM_PATH_UNIQUE(dirs, buffer);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateWS(pPath);
|
||||
ffStrbufReplaceAllC(&buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(&buffer, '/');
|
||||
if (!ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
||||
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
||||
}
|
||||
CoTaskMemFree(pPath);
|
||||
}
|
||||
@@ -53,14 +55,18 @@ static void platformPathAddEnvSuffix(FFlist* dirs, const char* env, const char*
|
||||
if(!ffStrSet(value))
|
||||
return;
|
||||
|
||||
FFstrbuf* buffer = ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppendS(buffer, value);
|
||||
ffStrbufReplaceAllC(buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
ffStrbufAppendS(buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_PLATFORM_PATH_UNIQUE(dirs, buffer);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateA(64);
|
||||
ffStrbufAppendS(&buffer, value);
|
||||
ffStrbufReplaceAllC(&buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(&buffer, '/');
|
||||
if (suffix)
|
||||
{
|
||||
ffStrbufAppendS(&buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(&buffer, '/');
|
||||
}
|
||||
|
||||
if (ffPathExists(buffer.chars, FF_PATHTYPE_DIRECTORY) && !ffListContains(dirs, &buffer, (void*) ffStrbufEqual))
|
||||
ffStrbufInitMove((FFstrbuf*) ffListAdd(dirs), &buffer);
|
||||
}
|
||||
|
||||
static void getConfigDirs(FFPlatform* platform)
|
||||
@@ -69,11 +75,12 @@ static void getConfigDirs(FFPlatform* platform)
|
||||
{
|
||||
// We are in MSYS2 / Git Bash
|
||||
platformPathAddEnvSuffix(&platform->configDirs, "HOME", ".config/");
|
||||
platformPathAddEnvSuffix(&platform->configDirs, "HOME", "");
|
||||
platformPathAddEnvSuffix(&platform->configDirs, "HOME", NULL);
|
||||
platformPathAddEnvSuffix(&platform->configDirs, "MINGW_PREFIX", "etc");
|
||||
}
|
||||
|
||||
ffPlatformPathAddHome(&platform->configDirs, platform, ".config/");
|
||||
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_ProgramData);
|
||||
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_RoamingAppData);
|
||||
platformPathAddKnownFolder(&platform->configDirs, &FOLDERID_LocalAppData);
|
||||
ffPlatformPathAddHome(&platform->configDirs, platform, "");
|
||||
@@ -85,10 +92,11 @@ static void getDataDirs(FFPlatform* platform)
|
||||
{
|
||||
// We are in MSYS2 / Git Bash
|
||||
platformPathAddEnvSuffix(&platform->dataDirs, "HOME", ".local/share/");
|
||||
platformPathAddEnvSuffix(&platform->dataDirs, "HOME", "");
|
||||
platformPathAddEnvSuffix(&platform->dataDirs, "HOME", NULL);
|
||||
platformPathAddEnvSuffix(&platform->dataDirs, "MINGW_PREFIX", "share");
|
||||
}
|
||||
ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/");
|
||||
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_ProgramData);
|
||||
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_RoamingAppData);
|
||||
platformPathAddKnownFolder(&platform->dataDirs, &FOLDERID_LocalAppData);
|
||||
ffPlatformPathAddHome(&platform->dataDirs, platform, "");
|
||||
|
||||
@@ -87,6 +87,12 @@ int main(void)
|
||||
n = 999;
|
||||
VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == list.length);
|
||||
|
||||
// ffListContains
|
||||
n = 10;
|
||||
VERIFY(ffListContains(&list, &n, numEqualsAdapter));
|
||||
n = 999;
|
||||
VERIFY(!ffListContains(&list, &n, numEqualsAdapter));
|
||||
|
||||
//shift
|
||||
VERIFY(ffListShift(&list, &n));
|
||||
VERIFY(n == 1);
|
||||
|
||||
Reference in New Issue
Block a user