mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
JsonConfig: refactor to get rid of data-ffjson_*
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
#include "common/library.h"
|
||||
|
||||
typedef struct JSONCData
|
||||
{
|
||||
FF_LIBRARY_SYMBOL(json_tokener_parse)
|
||||
FF_LIBRARY_SYMBOL(json_object_is_type)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_array)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_boolean)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_double)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_int)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string_len)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_object)
|
||||
FF_LIBRARY_SYMBOL(json_object_object_get)
|
||||
FF_LIBRARY_SYMBOL(json_object_put)
|
||||
} JSONCData;
|
||||
|
||||
bool ffJsonConfigParseModuleArgs(JSONCData* data, const char* key, json_object* val, FFModuleArgs* moduleArgs);
|
||||
const char* ffJsonConfigParseEnum(JSONCData* data, json_object* val, int* result, FFKeyValuePair pairs[]);
|
||||
|
||||
// Modified from json_object_object_foreach
|
||||
#define FF_JSON_OBJECT_OBJECT_FOREACH(data, obj, keyVar, valVar) \
|
||||
const char* keyVar = NULL; \
|
||||
json_object* valVar = NULL; \
|
||||
for (struct lh_entry* entry##keyVar = (obj) ? lh_table_head(data->ffjson_object_get_object(obj)) : NULL; \
|
||||
({ \
|
||||
if (entry##keyVar) \
|
||||
{ \
|
||||
keyVar = (const char*) lh_entry_k(entry##keyVar); \
|
||||
valVar = (json_object*) lh_entry_v(entry##keyVar); \
|
||||
}; \
|
||||
entry##keyVar; \
|
||||
}); \
|
||||
entry##keyVar = lh_entry_next(entry##keyVar))
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,139 @@
|
||||
#include "common/json.h"
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
|
||||
#include "common/library.h"
|
||||
#include "common/thread.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
typedef struct FFJsonLibrary
|
||||
{
|
||||
FF_LIBRARY_SYMBOL(json_tokener_parse)
|
||||
FF_LIBRARY_SYMBOL(json_object_is_type)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_array)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_boolean)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_double)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_int)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string_len)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_object)
|
||||
FF_LIBRARY_SYMBOL(json_object_object_get)
|
||||
FF_LIBRARY_SYMBOL(json_object_put)
|
||||
} FFJsonLibrary;
|
||||
|
||||
static const FFJsonLibrary* loadLibSymbols(const FFinstance* instance)
|
||||
{
|
||||
assert(instance);
|
||||
|
||||
static FFJsonLibrary lib;
|
||||
FF_LIBRARY_LOAD(libjsonc, &instance->config.libJSONC, NULL,
|
||||
#ifdef _WIN32
|
||||
"libjson-c-5" FF_LIBRARY_EXTENSION, -1
|
||||
#else
|
||||
"libjson-c" FF_LIBRARY_EXTENSION, 5
|
||||
#endif
|
||||
)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_tokener_parse, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_is_type, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_array, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_boolean, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_double, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_int, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_string_len, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_string, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_get_object, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_object_get, NULL)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(libjsonc, lib, json_object_put, NULL)
|
||||
libjsonc = NULL; // don't dlclose automatically
|
||||
return &lib;
|
||||
}
|
||||
|
||||
const FFJsonLibrary* ffJsonLib = NULL;
|
||||
bool ffJsonLoadLibrary(const FFinstance* instance)
|
||||
{
|
||||
static bool loaded = false;
|
||||
static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER;
|
||||
|
||||
if (!loaded)
|
||||
{
|
||||
ffThreadMutexLock(&mutex);
|
||||
|
||||
if(!loaded)
|
||||
{
|
||||
loaded = true;
|
||||
ffJsonLib = loadLibSymbols(instance);
|
||||
}
|
||||
|
||||
ffThreadMutexUnlock(&mutex);
|
||||
}
|
||||
return !!ffJsonLib;
|
||||
}
|
||||
|
||||
struct json_object *json_tokener_parse(const char *str)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_tokener_parse(str);
|
||||
}
|
||||
|
||||
int json_object_is_type(const json_object *obj, enum json_type type)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_is_type(obj, type);
|
||||
}
|
||||
|
||||
struct array_list *json_object_get_array(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_array(obj);
|
||||
}
|
||||
|
||||
json_bool json_object_get_boolean(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_boolean(obj);
|
||||
}
|
||||
|
||||
double json_object_get_double(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_double(obj);
|
||||
}
|
||||
|
||||
int32_t json_object_get_int(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_int(obj);
|
||||
}
|
||||
|
||||
int json_object_get_string_len(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_string_len(obj);
|
||||
}
|
||||
|
||||
const char *json_object_get_string(json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_string(obj);
|
||||
}
|
||||
|
||||
struct lh_table *json_object_get_object(const json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_get_object(obj);
|
||||
}
|
||||
|
||||
struct json_object *json_object_object_get(const json_object *obj, const char *key)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_object_get(obj, key);
|
||||
}
|
||||
|
||||
int json_object_put(json_object *obj)
|
||||
{
|
||||
assert(ffJsonLib);
|
||||
return ffJsonLib->ffjson_object_put(obj);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
bool ffJsonLoadLibrary(const FFinstance* instance);
|
||||
|
||||
static inline void wrapJsoncFree(json_object** root)
|
||||
{
|
||||
assert(root);
|
||||
if (*root)
|
||||
json_object_put(*root);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,33 +1,33 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/config.h"
|
||||
#include "common/jsonconfig.h"
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
|
||||
bool ffJsonConfigParseModuleArgs(JSONCData* data, const char* key, json_object* val, FFModuleArgs* moduleArgs)
|
||||
bool ffJsonConfigParseModuleArgs(const char* key, json_object* val, FFModuleArgs* moduleArgs)
|
||||
{
|
||||
if(strcasecmp(key, "key") == 0)
|
||||
{
|
||||
ffStrbufSetNS(&moduleArgs->key, (uint32_t) data->ffjson_object_get_string_len(val), data->ffjson_object_get_string(val));
|
||||
ffStrbufSetNS(&moduleArgs->key, (uint32_t) json_object_get_string_len(val), json_object_get_string(val));
|
||||
return true;
|
||||
}
|
||||
else if(strcasecmp(key, "format") == 0)
|
||||
{
|
||||
ffStrbufSetNS(&moduleArgs->outputFormat, (uint32_t) data->ffjson_object_get_string_len(val), data->ffjson_object_get_string(val));
|
||||
ffStrbufSetNS(&moduleArgs->outputFormat, (uint32_t) json_object_get_string_len(val), json_object_get_string(val));
|
||||
return true;
|
||||
}
|
||||
else if(strcasecmp(key, "error") == 0)
|
||||
{
|
||||
ffStrbufSetNS(&moduleArgs->errorFormat, (uint32_t) data->ffjson_object_get_string_len(val), data->ffjson_object_get_string(val));
|
||||
ffStrbufSetNS(&moduleArgs->errorFormat, (uint32_t) json_object_get_string_len(val), json_object_get_string(val));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* ffJsonConfigParseEnum(JSONCData* data, json_object* val, int* result, FFKeyValuePair pairs[])
|
||||
const char* ffJsonConfigParseEnum(json_object* val, int* result, FFKeyValuePair pairs[])
|
||||
{
|
||||
if (data->ffjson_object_is_type(val, json_type_int))
|
||||
if (json_object_is_type(val, json_type_int))
|
||||
{
|
||||
int intVal = data->ffjson_object_get_int(val);
|
||||
int intVal = json_object_get_int(val);
|
||||
|
||||
for (const FFKeyValuePair* pPair = pairs; pPair->key; ++pPair)
|
||||
{
|
||||
@@ -40,9 +40,9 @@ const char* ffJsonConfigParseEnum(JSONCData* data, json_object* val, int* result
|
||||
|
||||
return "Invalid enum integer";
|
||||
}
|
||||
else if (data->ffjson_object_is_type(val, json_type_string))
|
||||
else if (json_object_is_type(val, json_type_string))
|
||||
{
|
||||
const char* strVal = data->ffjson_object_get_string(val);
|
||||
const char* strVal = json_object_get_string(val);
|
||||
for (const FFKeyValuePair* pPair = pairs; pPair->key; ++pPair)
|
||||
{
|
||||
if (strcasecmp(strVal, pPair->key) == 0)
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
|
||||
#include "common/json.h"
|
||||
|
||||
bool ffJsonConfigParseModuleArgs(const char* key, json_object* val, FFModuleArgs* moduleArgs);
|
||||
const char* ffJsonConfigParseEnum(json_object* val, int* result, FFKeyValuePair pairs[]);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user