From 8f1262ccb27930b624a9b95d5059cbc0b3f05781 Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Sat, 30 Mar 2024 21:12:37 +0100 Subject: [PATCH] Move parts of config to shared --- buffyboard/config.c | 129 ++----------------------------------- buffyboard/meson.build | 1 + shared/config.c | 118 ++++++++++++++++++++++++++++++++++ shared/config.h | 39 +++++++++++ unl0kr/config.c | 143 ++++------------------------------------- unl0kr/meson.build | 1 + 6 files changed, 178 insertions(+), 253 deletions(-) create mode 100644 shared/config.c create mode 100644 shared/config.h diff --git a/buffyboard/config.c b/buffyboard/config.c index 5f2fd5d..81373aa 100644 --- a/buffyboard/config.c +++ b/buffyboard/config.c @@ -6,12 +6,12 @@ #include "config.h" +#include "../shared/config.h" #include "../shared/log.h" #include "../squeek2lvgl/sq2lv.h" #include "lvgl/lvgl.h" -#include #include #include #include @@ -21,33 +21,6 @@ * Static prototypes */ -/** - * Compares two strings from opaque types. - * - * @param a first string as void pointer - * @param b second string as void pointer - * @return a positive integer if a > b, a negative integer if a < b and 0 otherwise - */ -static int compare_strings(const void* a, const void* b); - -/** - * Checks whether a string ends with a suffix - * - * @param string string to check - * @param suffix suffix to compare to - * @return true if the suffix matches at the end of the string, false otherwise - */ -static bool string_ends_with(const char *string, const char *suffix); - -/** - * Non-recursively searches a directory for configuration files. - * - * @param path folder to search in - * @param found pointer to write found configuration file names into (to be freed by the caller) - * @param num_found pointer to write number of found files into - */ -static void find_files(const char *path, char ***found, int *num_found); - /** * Handle parsing events from INIH. * @@ -59,87 +32,11 @@ static void find_files(const char *path, char ***found, int *num_found); */ static int parsing_handler(void* user_data, const char* section, const char* key, const char* value); -/** - * Attempt to parse a boolean value. - * - * @param value string to parse - * @param result pointer to write result into if parsing is successful - * @return true on success, false otherwise - */ -static bool parse_bool(const char *value, bool *result); - /** * Static functions */ -static int compare_strings(const void* a, const void* b) { - return strcmp(*(const char**)a, *(const char**)b); -} - -static bool string_ends_with(const char *string, const char *suffix) { - if (!string || !suffix || strlen(suffix) > strlen(string)) { - return false; - } - return strncmp(string + strlen(string) - strlen(suffix), suffix, strlen(suffix)) == 0; -} - -static void find_files(const char *path, char ***found, int *num_found) { - /* Initialise output variables */ - *found = NULL; - *num_found = 0; - - /* Count length of directory path */ - const int path_length = strlen(path); - - /* Open directory */ - DIR *d = opendir(path); - if (!d) { - bbx_log(BBX_LOG_LEVEL_WARNING, "Could not read contents of folder %s", path); - return; - } - - /* Loop over directory contents */ - struct dirent *dir; - while ((dir = readdir(d)) != NULL) { - /* Ignore anything except for .conf files */ - if (dir->d_type != DT_REG || !string_ends_with(dir->d_name, ".conf")) { - continue; - } - - /* Grow output array */ - char **tmp = realloc(*found, (*num_found + 1) * sizeof(char *)); - if (!tmp) { - bbx_log(BBX_LOG_LEVEL_ERROR, "Could not reallocate memory for configuration file paths"); - break; - } - *found = tmp; - - /* Extract file name and length */ - char *name = dir->d_name; - int name_length = strlen(name); - - /* Allocate memory for full path */ - char *found_path = malloc(path_length + name_length + 2); /* +1 for path separator and null terminator, respectively */ - if (!found_path) { - bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for configuration file path"); - break; - } - - /* Build full path */ - memcpy(found_path, path, path_length); - found_path[path_length] = '/'; - memcpy(found_path + path_length + 1, dir->d_name, name_length + 1); /* +1 for path separator and null terminator, respectively */ - - /* Store file path */ - (*found)[*num_found] = found_path; - *num_found += 1; - } - - /* Close directory */ - closedir(d); -} - static int parsing_handler(void* user_data, const char* section, const char* key, const char* value) { bb_config_opts *opts = (bb_config_opts *)user_data; @@ -153,17 +50,17 @@ static int parsing_handler(void* user_data, const char* section, const char* key } } else if (strcmp(section, "input") == 0) { if (strcmp(key, "pointer") == 0) { - if (parse_bool(value, &(opts->input.pointer))) { + if (bbx_config_parse_bool(value, &(opts->input.pointer))) { return 1; } } else if (strcmp(key, "touchscreen") == 0) { - if (parse_bool(value, &(opts->input.touchscreen))) { + if (bbx_config_parse_bool(value, &(opts->input.touchscreen))) { return 1; } } } else if (strcmp(section, "quirks") == 0) { if (strcmp(key, "fbdev_force_refresh") == 0) { - if (parse_bool(value, &(opts->quirks.fbdev_force_refresh))) { + if (bbx_config_parse_bool(value, &(opts->quirks.fbdev_force_refresh))) { return 1; } } @@ -173,20 +70,6 @@ static int parsing_handler(void* user_data, const char* section, const char* key return 1; /* Return 1 (true) so that we can use the return value of ini_parse exclusively for file-level errors (e.g. file not found) */ } -static bool parse_bool(const char *value, bool *result) { - if (strcmp(value, "true") == 0) { - *result = true; - return true; - } - - if (strcmp(value, "false") == 0) { - *result = false; - return true; - } - - return false; -} - /** * Public functions @@ -203,10 +86,10 @@ void bb_config_parse_directory(const char *path, bb_config_opts *opts) { /* Find files in directory */ char **found = NULL; int num_found = 0; - find_files(path, &found, &num_found); + bbx_config_find_files(path, &found, &num_found); /* Sort and parse files */ - qsort(found, num_found, sizeof(char *), compare_strings); + qsort(found, num_found, sizeof(char *), bbx_config_compare_strings); bb_config_parse_files((const char **)found, num_found, opts); /* Free memory */ diff --git a/buffyboard/meson.build b/buffyboard/meson.build index dede73f..246bced 100644 --- a/buffyboard/meson.build +++ b/buffyboard/meson.build @@ -24,6 +24,7 @@ buffyboard_sources = [ shared_sources = [ '../shared/cursor/cursor.c', '../shared/fonts/font_32.c', + '../shared/config.c', '../shared/indev.c', '../shared/log.c', '../shared/theme.c', diff --git a/shared/config.c b/shared/config.c new file mode 100644 index 0000000..c2b770d --- /dev/null +++ b/shared/config.c @@ -0,0 +1,118 @@ +/** + * Copyright 2021 Johannes Marbach + * SPDX-License-Identifier: GPL-3.0-or-later + */ + + +#include "config.h" + +#include "log.h" + +#include +#include +#include + + +/** + * Static prototypes + */ + +/** + * Checks whether a string ends with a suffix + * + * @param string string to check + * @param suffix suffix to compare to + * @return true if the suffix matches at the end of the string, false otherwise + */ +static bool string_ends_with(const char *string, const char *suffix); + + +/** + * Static functions + */ + +static bool string_ends_with(const char *string, const char *suffix) { + if (!string || !suffix || strlen(suffix) > strlen(string)) { + return false; + } + return strncmp(string + strlen(string) - strlen(suffix), suffix, strlen(suffix)) == 0; +} + + +/** + * Public functions + */ + +int bbx_config_compare_strings(const void* a, const void* b) { + return strcmp(*(const char**)a, *(const char**)b); +} + +void bbx_config_find_files(const char *path, char ***found, int *num_found) { + /* Initialise output variables */ + *found = NULL; + *num_found = 0; + + /* Count length of directory path */ + const int path_length = strlen(path); + + /* Open directory */ + DIR *d = opendir(path); + if (!d) { + bbx_log(BBX_LOG_LEVEL_WARNING, "Could not read contents of folder %s", path); + return; + } + + /* Loop over directory contents */ + struct dirent *dir; + while ((dir = readdir(d)) != NULL) { + /* Ignore anything except for .conf files */ + if (dir->d_type != DT_REG || !string_ends_with(dir->d_name, ".conf")) { + continue; + } + + /* Grow output array */ + char **tmp = realloc(*found, (*num_found + 1) * sizeof(char *)); + if (!tmp) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not reallocate memory for configuration file paths"); + break; + } + *found = tmp; + + /* Extract file name and length */ + char *name = dir->d_name; + int name_length = strlen(name); + + /* Allocate memory for full path */ + char *found_path = malloc(path_length + name_length + 2); /* +1 for path separator and null terminator, respectively */ + if (!found_path) { + bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for configuration file path"); + break; + } + + /* Build full path */ + memcpy(found_path, path, path_length); + found_path[path_length] = '/'; + memcpy(found_path + path_length + 1, dir->d_name, name_length + 1); /* +1 for path separator and null terminator, respectively */ + + /* Store file path */ + (*found)[*num_found] = found_path; + *num_found += 1; + } + + /* Close directory */ + closedir(d); +} + +bool bbx_config_parse_bool(const char *value, bool *result) { + if (strcmp(value, "true") == 0) { + *result = true; + return true; + } + + if (strcmp(value, "false") == 0) { + *result = false; + return true; + } + + return false; +} diff --git a/shared/config.h b/shared/config.h new file mode 100644 index 0000000..ef67bf3 --- /dev/null +++ b/shared/config.h @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Johannes Marbach + * SPDX-License-Identifier: GPL-3.0-or-later + */ + + +#ifndef BBX_CONFIG_H +#define BBX_CONFIG_H + +#include + +/** + * Compares two strings from opaque types. + * + * @param a first string as void pointer + * @param b second string as void pointer + * @return a positive integer if a > b, a negative integer if a < b and 0 otherwise + */ +int bbx_config_compare_strings(const void* a, const void* b); + +/** + * Non-recursively searches a directory for configuration files. + * + * @param path folder to search in + * @param found pointer to write found configuration file names into (to be freed by the caller) + * @param num_found pointer to write number of found files into + */ +void bbx_config_find_files(const char *path, char ***found, int *num_found); + +/** + * Attempt to parse a boolean value. + * + * @param value string to parse + * @param result pointer to write result into if parsing is successful + * @return true on success, false otherwise + */ +bool bbx_config_parse_bool(const char *value, bool *result); + +#endif /* BBX_CONFIG_H */ diff --git a/unl0kr/config.c b/unl0kr/config.c index 9ac83b6..018a31c 100644 --- a/unl0kr/config.c +++ b/unl0kr/config.c @@ -6,12 +6,12 @@ #include "config.h" +#include "../shared/config.h" #include "../shared/log.h" #include "../squeek2lvgl/sq2lv.h" #include "lvgl/lvgl.h" -#include #include #include #include @@ -21,33 +21,6 @@ * Static prototypes */ -/** - * Compares two strings from opaque types. - * - * @param a first string as void pointer - * @param b second string as void pointer - * @return a positive integer if a > b, a negative integer if a < b and 0 otherwise - */ -static int compare_strings(const void* a, const void* b); - -/** - * Checks whether a string ends with a suffix - * - * @param string string to check - * @param suffix suffix to compare to - * @return true if the suffix matches at the end of the string, false otherwise - */ -static bool string_ends_with(const char *string, const char *suffix); - -/** - * Non-recursively searches a directory for configuration files. - * - * @param path folder to search in - * @param found pointer to write found configuration file names into (to be freed by the caller) - * @param num_found pointer to write number of found files into - */ -static void find_files(const char *path, char ***found, int *num_found); - /** * Handle parsing events from INIH. * @@ -59,93 +32,17 @@ static void find_files(const char *path, char ***found, int *num_found); */ static int parsing_handler(void* user_data, const char* section, const char* key, const char* value); -/** - * Attempt to parse a boolean value. - * - * @param value string to parse - * @param result pointer to write result into if parsing is successful - * @return true on success, false otherwise - */ -static bool parse_bool(const char *value, bool *result); - /** * Static functions */ -static int compare_strings(const void* a, const void* b) { - return strcmp(*(const char**)a, *(const char**)b); -} - -static bool string_ends_with(const char *string, const char *suffix) { - if (!string || !suffix || strlen(suffix) > strlen(string)) { - return false; - } - return strncmp(string + strlen(string) - strlen(suffix), suffix, strlen(suffix)) == 0; -} - -static void find_files(const char *path, char ***found, int *num_found) { - /* Initialise output variables */ - *found = NULL; - *num_found = 0; - - /* Count length of directory path */ - const int path_length = strlen(path); - - /* Open directory */ - DIR *d = opendir(path); - if (!d) { - bbx_log(BBX_LOG_LEVEL_WARNING, "Could not read contents of folder %s", path); - return; - } - - /* Loop over directory contents */ - struct dirent *dir; - while ((dir = readdir(d)) != NULL) { - /* Ignore anything except for .conf files */ - if (dir->d_type != DT_REG || !string_ends_with(dir->d_name, ".conf")) { - continue; - } - - /* Grow output array */ - char **tmp = realloc(*found, (*num_found + 1) * sizeof(char *)); - if (!tmp) { - bbx_log(BBX_LOG_LEVEL_ERROR, "Could not reallocate memory for configuration file paths"); - break; - } - *found = tmp; - - /* Extract file name and length */ - char *name = dir->d_name; - int name_length = strlen(name); - - /* Allocate memory for full path */ - char *found_path = malloc(path_length + name_length + 2); /* +1 for path separator and null terminator, respectively */ - if (!found_path) { - bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for configuration file path"); - break; - } - - /* Build full path */ - memcpy(found_path, path, path_length); - found_path[path_length] = '/'; - memcpy(found_path + path_length + 1, dir->d_name, name_length + 1); /* +1 for path separator and null terminator, respectively */ - - /* Store file path */ - (*found)[*num_found] = found_path; - *num_found += 1; - } - - /* Close directory */ - closedir(d); -} - static int parsing_handler(void* user_data, const char* section, const char* key, const char* value) { ul_config_opts *opts = (ul_config_opts *)user_data; if (strcmp(section, "general") == 0) { if (strcmp(key, "animations") == 0) { - if (parse_bool(value, &(opts->general.animations))) { + if (bbx_config_parse_bool(value, &(opts->general.animations))) { return 1; } } else if (strcmp(key, "backend") == 0) { @@ -161,7 +58,7 @@ static int parsing_handler(void* user_data, const char* section, const char* key } } else if (strcmp(section, "keyboard") == 0) { if (strcmp(key, "autohide") == 0) { - if (parse_bool(value, &(opts->keyboard.autohide))) { + if (bbx_config_parse_bool(value, &(opts->keyboard.autohide))) { return 1; } } else if (strcmp(key, "layout") == 0) { @@ -171,13 +68,13 @@ static int parsing_handler(void* user_data, const char* section, const char* key return 1; } } else if (strcmp(key, "popovers") == 0) { - if (parse_bool(value, &(opts->keyboard.popovers))) { + if (bbx_config_parse_bool(value, &(opts->keyboard.popovers))) { return 1; } } } else if (strcmp(section, "textarea") == 0) { if (strcmp(key, "obscured") == 0) { - if (parse_bool(value, &(opts->textarea.obscured))) { + if (bbx_config_parse_bool(value, &(opts->textarea.obscured))) { return 1; } } else if (strcmp(key, "bullet") == 0) { @@ -203,29 +100,29 @@ static int parsing_handler(void* user_data, const char* section, const char* key } } else if (strcmp(section, "input") == 0) { if (strcmp(key, "keyboard") == 0) { - if (parse_bool(value, &(opts->input.keyboard))) { + if (bbx_config_parse_bool(value, &(opts->input.keyboard))) { return 1; } } else if (strcmp(key, "pointer") == 0) { - if (parse_bool(value, &(opts->input.pointer))) { + if (bbx_config_parse_bool(value, &(opts->input.pointer))) { return 1; } } else if (strcmp(key, "touchscreen") == 0) { - if (parse_bool(value, &(opts->input.touchscreen))) { + if (bbx_config_parse_bool(value, &(opts->input.touchscreen))) { return 1; } } } else if (strcmp(section, "quirks") == 0) { if (strcmp(key, "fbdev_force_refresh") == 0) { - if (parse_bool(value, &(opts->quirks.fbdev_force_refresh))) { + if (bbx_config_parse_bool(value, &(opts->quirks.fbdev_force_refresh))) { return 1; } } else if (strcmp(key, "terminal_prevent_graphics_mode") == 0) { - if (parse_bool(value, &(opts->quirks.terminal_prevent_graphics_mode))) { + if (bbx_config_parse_bool(value, &(opts->quirks.terminal_prevent_graphics_mode))) { return 1; } } else if (strcmp(key, "terminal_allow_keyboard_input") == 0) { - if (parse_bool(value, &(opts->quirks.terminal_allow_keyboard_input))) { + if (bbx_config_parse_bool(value, &(opts->quirks.terminal_allow_keyboard_input))) { return 1; } } @@ -235,20 +132,6 @@ static int parsing_handler(void* user_data, const char* section, const char* key return 1; /* Return 1 (true) so that we can use the return value of ini_parse exclusively for file-level errors (e.g. file not found) */ } -static bool parse_bool(const char *value, bool *result) { - if (strcmp(value, "true") == 0) { - *result = true; - return true; - } - - if (strcmp(value, "false") == 0) { - *result = false; - return true; - } - - return false; -} - /** * Public functions @@ -277,10 +160,10 @@ void ul_config_parse_directory(const char *path, ul_config_opts *opts) { /* Find files in directory */ char **found = NULL; int num_found = 0; - find_files(path, &found, &num_found); + bbx_config_find_files(path, &found, &num_found); /* Sort and parse files */ - qsort(found, num_found, sizeof(char *), compare_strings); + qsort(found, num_found, sizeof(char *), bbx_config_compare_strings); ul_config_parse_files((const char **)found, num_found, opts); /* Free memory */ diff --git a/unl0kr/meson.build b/unl0kr/meson.build index fa7c10d..b6b3b80 100644 --- a/unl0kr/meson.build +++ b/unl0kr/meson.build @@ -24,6 +24,7 @@ unl0kr_sources = [ shared_sources = [ '../shared/cursor/cursor.c', '../shared/fonts/font_32.c', + '../shared/config.c', '../shared/indev.c', '../shared/log.c', '../shared/theme.c',