2020-01-31 15:43:42 +01:00
|
|
|
/* Copyright (C) 2010-2020 The RetroArch team
|
2018-05-12 17:56:33 +02:00
|
|
|
*
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
* The following license statement only applies to this file (config_file.c).
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge,
|
|
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2019-01-03 12:48:40 +01:00
|
|
|
#ifdef ORBIS
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <orbisFile.h>
|
|
|
|
#endif
|
2018-05-12 17:56:33 +02:00
|
|
|
#include <retro_miscellaneous.h>
|
|
|
|
#include <compat/strl.h>
|
|
|
|
#include <compat/posix_string.h>
|
|
|
|
#include <compat/fopen_utf8.h>
|
|
|
|
#include <compat/msvc.h>
|
|
|
|
#include <file/config_file.h>
|
|
|
|
#include <file/file_path.h>
|
|
|
|
#include <string/stdstring.h>
|
|
|
|
#include <streams/file_stream.h>
|
2021-04-21 17:23:18 +01:00
|
|
|
#include <array/rhmap.h>
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
#define MAX_INCLUDE_DEPTH 16
|
|
|
|
|
|
|
|
struct config_include_list
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct config_include_list *next;
|
|
|
|
};
|
|
|
|
|
2020-08-25 12:39:28 +02:00
|
|
|
/* Forward declaration */
|
|
|
|
static bool config_file_parse_line(config_file_t *conf,
|
|
|
|
struct config_entry_list *list, char *line, config_file_cb_t *cb);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-08-25 12:39:28 +02:00
|
|
|
static int config_file_sort_compare_func(struct config_entry_list *a,
|
2018-07-16 00:15:49 -04:00
|
|
|
struct config_entry_list *b)
|
|
|
|
{
|
2020-04-27 17:06:35 +01:00
|
|
|
if (a && b)
|
|
|
|
{
|
2020-08-24 19:29:24 +02:00
|
|
|
if (a->key)
|
|
|
|
{
|
|
|
|
if (b->key)
|
|
|
|
return strcasecmp(a->key, b->key);
|
2020-04-27 17:06:35 +01:00
|
|
|
return 1;
|
2020-08-24 19:29:24 +02:00
|
|
|
}
|
2020-04-27 17:06:35 +01:00
|
|
|
else if (b->key)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-07-16 00:15:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* https://stackoverflow.com/questions/7685/merge-sort-a-linked-list */
|
2020-08-25 12:39:28 +02:00
|
|
|
static struct config_entry_list* config_file_merge_sort_linked_list(
|
2019-04-26 06:28:10 +02:00
|
|
|
struct config_entry_list *list, int (*compare)(
|
|
|
|
struct config_entry_list *one,struct config_entry_list *two))
|
2018-07-16 00:15:49 -04:00
|
|
|
{
|
|
|
|
struct config_entry_list
|
|
|
|
*right = list,
|
|
|
|
*temp = list,
|
|
|
|
*last = list,
|
|
|
|
*result = 0,
|
|
|
|
*next = 0,
|
|
|
|
*tail = 0;
|
|
|
|
|
|
|
|
/* Trivial case. */
|
|
|
|
if (!list || !list->next)
|
|
|
|
return list;
|
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
/* Find halfway through the list (by running two pointers,
|
|
|
|
* one at twice the speed of the other). */
|
2018-07-16 00:15:49 -04:00
|
|
|
while (temp && temp->next)
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
last = right;
|
|
|
|
right = right->next;
|
|
|
|
temp = temp->next->next;
|
2018-07-16 00:15:49 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
/* Break the list in two. (prev pointers are broken here,
|
|
|
|
* but we fix later) */
|
2020-06-25 23:16:08 +02:00
|
|
|
last->next = 0;
|
2018-07-16 00:15:49 -04:00
|
|
|
|
|
|
|
/* Recurse on the two smaller lists: */
|
2020-08-25 12:39:28 +02:00
|
|
|
list = config_file_merge_sort_linked_list(list, compare);
|
|
|
|
right = config_file_merge_sort_linked_list(right, compare);
|
2018-07-16 00:15:49 -04:00
|
|
|
|
|
|
|
/* Merge: */
|
|
|
|
while (list || right)
|
|
|
|
{
|
|
|
|
/* Take from empty lists, or compare: */
|
|
|
|
if (!right)
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
next = list;
|
|
|
|
list = list->next;
|
2018-07-16 00:15:49 -04:00
|
|
|
}
|
|
|
|
else if (!list)
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
next = right;
|
2018-07-16 00:15:49 -04:00
|
|
|
right = right->next;
|
|
|
|
}
|
|
|
|
else if (compare(list, right) < 0)
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
next = list;
|
|
|
|
list = list->next;
|
2018-07-16 00:15:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
next = right;
|
2018-07-16 00:15:49 -04:00
|
|
|
right = right->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result)
|
2020-06-25 23:16:08 +02:00
|
|
|
result = next;
|
2018-07-16 00:15:49 -04:00
|
|
|
else
|
2018-07-16 23:01:16 -04:00
|
|
|
tail->next = next;
|
2018-07-16 00:15:49 -04:00
|
|
|
|
2020-06-25 23:16:08 +02:00
|
|
|
tail = next;
|
2018-07-16 00:15:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:30:41 +01:00
|
|
|
/* Searches input string for a comment ('#') entry
|
2020-06-25 10:23:10 +01:00
|
|
|
* > If first character is '#', then entire line is
|
|
|
|
* a comment and may correspond to a directive
|
|
|
|
* (command action - e.g. include sub-config file).
|
|
|
|
* In this case, 'str' is set to NUL and the comment
|
|
|
|
* itself (everything after the '#' character) is
|
|
|
|
* returned
|
|
|
|
* > If a '#' character is found inside a string literal
|
|
|
|
* value, then it does not correspond to a comment and
|
|
|
|
* is ignored. In this case, 'str' is left untouched
|
|
|
|
* and NULL is returned
|
|
|
|
* > If a '#' character is found anywhere else, then the
|
|
|
|
* comment text is a suffix of the input string and
|
|
|
|
* has no programmatic value. In this case, the comment
|
|
|
|
* is removed from the end of 'str' and NULL is returned */
|
2020-08-25 12:39:28 +02:00
|
|
|
static char *config_file_strip_comment(char *str)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-06-24 14:30:41 +01:00
|
|
|
/* Search for a comment (#) character */
|
|
|
|
char *comment = strchr(str, '#');
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-06-24 14:30:41 +01:00
|
|
|
if (comment)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-06-25 10:23:10 +01:00
|
|
|
char *literal_start = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-06-25 10:23:10 +01:00
|
|
|
/* Check whether entire line is a comment
|
|
|
|
* > First character == '#' */
|
|
|
|
if (str == comment)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-06-25 10:23:10 +01:00
|
|
|
/* Set 'str' to NUL and return comment
|
|
|
|
* for processing at a higher level */
|
2020-06-24 14:30:41 +01:00
|
|
|
*str = '\0';
|
|
|
|
return ++comment;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
2020-11-02 21:07:20 -05:00
|
|
|
|
2020-06-25 10:23:10 +01:00
|
|
|
/* Comment character occurs at an offset:
|
|
|
|
* Search for the start of a string literal value */
|
|
|
|
literal_start = strchr(str, '\"');
|
|
|
|
|
|
|
|
/* Check whether string literal start occurs
|
|
|
|
* *before* the comment character */
|
|
|
|
if (literal_start && (literal_start < comment))
|
|
|
|
{
|
|
|
|
/* Search for the end of the string literal
|
|
|
|
* value */
|
|
|
|
char *literal_end = strchr(literal_start + 1, '\"');
|
|
|
|
|
|
|
|
/* Check whether string literal end occurs
|
|
|
|
* *after* the comment character
|
|
|
|
* > If this is the case, ignore the comment
|
|
|
|
* > Leave 'str' untouched and return NULL */
|
|
|
|
if (literal_end && (literal_end > comment))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reach this point, then a comment
|
|
|
|
* exists outside of a string literal
|
|
|
|
* > Trim the entire comment from the end
|
|
|
|
* of 'str' */
|
|
|
|
*comment = '\0';
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:30:41 +01:00
|
|
|
return NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:39:28 +02:00
|
|
|
static char *config_file_extract_value(char *line, bool is_value)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-06-25 15:06:31 +01:00
|
|
|
size_t idx = 0;
|
|
|
|
char *value = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (is_value)
|
|
|
|
{
|
2020-09-27 22:15:32 +02:00
|
|
|
while (ISSPACE((int)*line))
|
2018-05-12 17:56:33 +02:00
|
|
|
line++;
|
|
|
|
|
|
|
|
/* If we don't have an equal sign here,
|
|
|
|
* we've got an invalid string. */
|
|
|
|
if (*line != '=')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:15:32 +02:00
|
|
|
while (ISSPACE((int)*line))
|
2018-05-12 17:56:33 +02:00
|
|
|
line++;
|
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
/* Note: From this point on, an empty value
|
|
|
|
* string is valid - and in this case, strdup("")
|
|
|
|
* will be returned
|
|
|
|
* > If we instead return NULL, the the entry
|
|
|
|
* is ignored completely - which means we cannot
|
|
|
|
* track *changes* in entry value */
|
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* If first character is ("), we have a full string
|
|
|
|
* literal */
|
2018-05-12 17:56:33 +02:00
|
|
|
if (*line == '"')
|
|
|
|
{
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Skip to next character */
|
2018-05-12 17:56:33 +02:00
|
|
|
line++;
|
2020-06-25 15:06:31 +01:00
|
|
|
|
|
|
|
/* If this a ("), then value string is empty */
|
2018-05-12 17:56:33 +02:00
|
|
|
if (*line == '"')
|
2020-04-27 17:06:35 +01:00
|
|
|
return strdup("");
|
2020-06-25 15:06:31 +01:00
|
|
|
|
|
|
|
/* Find the next (") character */
|
|
|
|
while (line[idx] && (line[idx] != '\"'))
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
line[idx] = '\0';
|
|
|
|
value = line;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
2020-06-25 15:06:31 +01:00
|
|
|
/* This is not a string literal - just read
|
|
|
|
* until the next space is found
|
|
|
|
* > Note: Skip this if line is empty */
|
|
|
|
else if (*line != '\0')
|
|
|
|
{
|
|
|
|
/* Find next space character */
|
|
|
|
while (line[idx] && isgraph((int)line[idx]))
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
line[idx] = '\0';
|
|
|
|
value = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value && *value)
|
|
|
|
return strdup(value);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
return strdup("");
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move semantics? */
|
2020-08-25 12:39:28 +02:00
|
|
|
static void config_file_add_child_list(config_file_t *parent, config_file_t *child)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
|
|
|
struct config_entry_list *list = child->entries;
|
2021-04-21 17:23:18 +01:00
|
|
|
bool merge_hash_map = false;
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
if (parent->entries)
|
|
|
|
{
|
|
|
|
struct config_entry_list *head = parent->entries;
|
|
|
|
while (head->next)
|
|
|
|
head = head->next;
|
|
|
|
|
|
|
|
/* set list readonly */
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
list->readonly = true;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
head->next = child->entries;
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
merge_hash_map = true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* set list readonly */
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
list->readonly = true;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
parent->entries = child->entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rebase tail. */
|
|
|
|
if (parent->entries)
|
|
|
|
{
|
|
|
|
struct config_entry_list *head =
|
|
|
|
(struct config_entry_list*)parent->entries;
|
|
|
|
|
|
|
|
while (head->next)
|
|
|
|
head = head->next;
|
|
|
|
parent->tail = head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
parent->tail = NULL;
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
/* Update hash map */
|
|
|
|
if (merge_hash_map)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t cap;
|
|
|
|
|
|
|
|
/* We are merging two lists - if any child entry
|
|
|
|
* (key) is not present in the parent list, add it
|
|
|
|
* to the parent hash map */
|
|
|
|
for (i = 0, cap = RHMAP_CAP(child->entries_map); i != cap; i++)
|
|
|
|
{
|
2021-04-28 16:56:14 +01:00
|
|
|
uint32_t child_hash = RHMAP_KEY(child->entries_map, i);
|
|
|
|
const char *child_key = RHMAP_KEY_STR(child->entries_map, i);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
2021-04-29 12:29:47 +01:00
|
|
|
if (child_hash &&
|
|
|
|
child_key &&
|
|
|
|
!RHMAP_HAS_FULL(parent->entries_map, child_hash, child_key))
|
2021-04-21 17:23:18 +01:00
|
|
|
{
|
2021-04-29 12:29:47 +01:00
|
|
|
struct config_entry_list *entry = child->entries_map[i];
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
if (entry)
|
2021-04-28 16:56:14 +01:00
|
|
|
RHMAP_SET_FULL(parent->entries_map, child_hash, child_key, entry);
|
2021-04-21 17:23:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Child entries map is no longer required,
|
|
|
|
* so free it now */
|
|
|
|
RHMAP_FREE(child->entries_map);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If parent list was originally empty,
|
|
|
|
* take map from child list */
|
|
|
|
RHMAP_FREE(parent->entries_map);
|
|
|
|
parent->entries_map = child->entries_map;
|
|
|
|
child->entries_map = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
child->entries = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 12:14:28 +02:00
|
|
|
static void config_file_get_realpath(char *s, size_t len,
|
|
|
|
char *path, const char *config_path)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!string_is_empty(config_path))
|
|
|
|
fill_pathname_resolve_relative(s, config_path,
|
|
|
|
path, len);
|
|
|
|
#else
|
2021-03-07 09:28:59 +01:00
|
|
|
#if !defined(__PSL1GHT__) && !defined(__PS3__)
|
2020-08-25 12:14:28 +02:00
|
|
|
if (*path == '~')
|
|
|
|
{
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home)
|
|
|
|
{
|
|
|
|
strlcpy(s, home, len);
|
|
|
|
strlcat(s, path + 1, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strlcpy(s, path + 1, len);
|
|
|
|
}
|
|
|
|
else
|
2021-03-07 09:28:59 +01:00
|
|
|
#endif
|
2020-08-25 12:14:28 +02:00
|
|
|
if (!string_is_empty(config_path))
|
|
|
|
fill_pathname_resolve_relative(s, config_path, path, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:39:28 +02:00
|
|
|
static void config_file_add_sub_conf(config_file_t *conf, char *path,
|
2020-08-25 12:29:08 +02:00
|
|
|
char *real_path, size_t len, config_file_cb_t *cb)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
|
|
|
struct config_include_list *head = conf->includes;
|
|
|
|
struct config_include_list *node = (struct config_include_list*)
|
|
|
|
malloc(sizeof(*node));
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
2020-06-25 23:16:08 +02:00
|
|
|
node->next = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
/* Add include list */
|
2020-06-25 23:16:08 +02:00
|
|
|
node->path = strdup(path);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (head)
|
|
|
|
{
|
|
|
|
while (head->next)
|
2020-06-25 23:16:08 +02:00
|
|
|
head = head->next;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-06-25 23:16:08 +02:00
|
|
|
head->next = node;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
conf->includes = node;
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:29:08 +02:00
|
|
|
config_file_get_realpath(real_path, len, path,
|
2020-08-25 12:14:28 +02:00
|
|
|
conf->path);
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:36:26 +02:00
|
|
|
static int config_file_load_internal(
|
2020-08-25 15:29:45 +02:00
|
|
|
struct config_file *conf,
|
2020-08-25 12:39:28 +02:00
|
|
|
const char *path, unsigned depth, config_file_cb_t *cb)
|
|
|
|
{
|
2020-08-25 15:29:45 +02:00
|
|
|
RFILE *file = NULL;
|
2020-08-25 15:34:11 +02:00
|
|
|
char *new_path = strdup(path);
|
|
|
|
if (!new_path)
|
2020-08-25 15:29:45 +02:00
|
|
|
return 1;
|
2020-08-25 12:39:28 +02:00
|
|
|
|
2020-08-25 15:34:11 +02:00
|
|
|
conf->path = new_path;
|
2020-08-25 12:39:28 +02:00
|
|
|
conf->include_depth = depth;
|
|
|
|
file = filestream_open(path,
|
|
|
|
RETRO_VFS_FILE_ACCESS_READ,
|
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
free(conf->path);
|
2020-08-25 15:29:45 +02:00
|
|
|
return 1;
|
2020-08-25 12:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!filestream_eof(file))
|
|
|
|
{
|
|
|
|
char *line = NULL;
|
|
|
|
struct config_entry_list *list = (struct config_entry_list*)
|
|
|
|
malloc(sizeof(*list));
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
{
|
|
|
|
filestream_close(file);
|
2020-08-25 15:29:45 +02:00
|
|
|
return -1;
|
2020-08-25 12:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list->readonly = false;
|
|
|
|
list->key = NULL;
|
|
|
|
list->value = NULL;
|
|
|
|
list->next = NULL;
|
|
|
|
|
|
|
|
line = filestream_getline(file);
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
{
|
|
|
|
free(list);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!string_is_empty(line)
|
|
|
|
&& config_file_parse_line(conf, list, line, cb))
|
|
|
|
{
|
|
|
|
if (conf->entries)
|
|
|
|
conf->tail->next = list;
|
|
|
|
else
|
|
|
|
conf->entries = list;
|
|
|
|
|
|
|
|
conf->tail = list;
|
|
|
|
|
2021-04-21 17:23:18 +01:00
|
|
|
if (list->key)
|
|
|
|
{
|
|
|
|
/* Only add entry to the map if an entry
|
|
|
|
* with the specified value does not
|
|
|
|
* already exist */
|
2021-04-28 16:56:14 +01:00
|
|
|
uint32_t hash = rhmap_hash_string(list->key);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
2021-04-28 16:56:14 +01:00
|
|
|
if (!RHMAP_HAS_FULL(conf->entries_map, hash, list->key))
|
2021-04-21 17:23:18 +01:00
|
|
|
{
|
2021-04-28 16:56:14 +01:00
|
|
|
RHMAP_SET_FULL(conf->entries_map, hash, list->key, list);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
if (cb && list->value)
|
|
|
|
cb->config_file_new_entry_cb(list->key, list->value);
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 12:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
if (list != conf->tail)
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
filestream_close(file);
|
|
|
|
|
2020-08-25 15:29:45 +02:00
|
|
|
return 0;
|
2020-08-25 12:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool config_file_parse_line(config_file_t *conf,
|
2019-04-28 03:12:59 +02:00
|
|
|
struct config_entry_list *list, char *line, config_file_cb_t *cb)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-06-25 15:06:31 +01:00
|
|
|
size_t cur_size = 32;
|
2020-06-24 15:40:42 +02:00
|
|
|
size_t idx = 0;
|
|
|
|
char *key = NULL;
|
|
|
|
char *key_tmp = NULL;
|
2020-06-25 10:23:10 +01:00
|
|
|
/* Remove any comment text */
|
2020-08-25 12:39:28 +02:00
|
|
|
char *comment = config_file_strip_comment(line);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-06-25 10:23:10 +01:00
|
|
|
/* Check whether entire line is a comment */
|
2020-06-24 14:30:41 +01:00
|
|
|
if (comment)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-08-25 17:54:22 +02:00
|
|
|
config_file_t sub_conf;
|
2020-11-02 21:07:20 -05:00
|
|
|
bool include_found = false;
|
|
|
|
bool reference_found = false;
|
2020-08-25 12:29:08 +02:00
|
|
|
char real_path[PATH_MAX_LENGTH];
|
|
|
|
char *path = NULL;
|
|
|
|
char *include_line = NULL;
|
2020-11-01 09:18:40 -05:00
|
|
|
char *reference_line = NULL;
|
2020-06-24 15:40:42 +02:00
|
|
|
|
2020-11-02 21:07:20 -05:00
|
|
|
include_found = string_starts_with_size(comment, "include ",
|
|
|
|
STRLEN_CONST("include "));
|
|
|
|
reference_found = string_starts_with_size(comment, "reference ",
|
|
|
|
STRLEN_CONST("reference "));
|
|
|
|
|
|
|
|
/* All comments except those starting with the include or
|
|
|
|
* reference directive are ignored */
|
|
|
|
if (!include_found && !reference_found)
|
2020-06-24 15:40:42 +02:00
|
|
|
return false;
|
2020-06-24 14:30:41 +01:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
/* Starting a line with an 'include' directive
|
|
|
|
* appends a sub-config file */
|
2020-11-02 21:07:20 -05:00
|
|
|
if (include_found)
|
2020-11-01 09:18:40 -05:00
|
|
|
{
|
|
|
|
include_line = comment + STRLEN_CONST("include ");
|
2020-06-24 14:30:41 +01:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
if (string_is_empty(include_line))
|
|
|
|
return false;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
path = config_file_extract_value(include_line, false);
|
2019-04-26 06:28:10 +02:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
if (!path)
|
|
|
|
return false;
|
2020-06-24 14:30:41 +01:00
|
|
|
|
2020-11-02 21:07:20 -05:00
|
|
|
if ( string_is_empty(path)
|
|
|
|
|| conf->include_depth >= MAX_INCLUDE_DEPTH)
|
2020-11-01 09:18:40 -05:00
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-24 15:40:42 +02:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
real_path[0] = '\0';
|
|
|
|
config_file_add_sub_conf(conf, path,
|
2020-08-25 15:29:45 +02:00
|
|
|
real_path, sizeof(real_path), cb);
|
2020-08-25 12:29:08 +02:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
config_file_initialize(&sub_conf);
|
2020-08-25 15:29:45 +02:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
switch (config_file_load_internal(&sub_conf, real_path,
|
|
|
|
conf->include_depth + 1, cb))
|
|
|
|
{
|
2020-11-02 21:07:20 -05:00
|
|
|
case 0:
|
|
|
|
/* Pilfer internal list. */
|
|
|
|
config_file_add_child_list(conf, &sub_conf);
|
|
|
|
/* fall-through to deinitialize */
|
|
|
|
case -1:
|
|
|
|
config_file_deinitialize(&sub_conf);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
default:
|
|
|
|
break;
|
2020-11-01 09:18:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:07:20 -05:00
|
|
|
/* Starting a line with an 'reference' directive
|
|
|
|
* sets the reference path */
|
|
|
|
if (reference_found)
|
2020-11-01 09:18:40 -05:00
|
|
|
{
|
|
|
|
reference_line = comment + STRLEN_CONST("reference ");
|
|
|
|
|
|
|
|
if (string_is_empty(reference_line))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path = config_file_extract_value(reference_line, false);
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
config_file_set_reference_path(conf, path);
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return false;
|
2020-08-25 18:10:32 +02:00
|
|
|
}
|
2020-08-25 12:29:08 +02:00
|
|
|
|
2020-06-24 15:40:42 +02:00
|
|
|
free(path);
|
|
|
|
return true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Skip to first non-space character */
|
2020-09-27 22:15:32 +02:00
|
|
|
while (ISSPACE((int)*line))
|
2018-05-12 17:56:33 +02:00
|
|
|
line++;
|
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Allocate storage for key */
|
|
|
|
key = (char*)malloc(cur_size + 1);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
2019-04-26 06:28:10 +02:00
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Copy line contents into key until we
|
|
|
|
* reach the next space character */
|
2018-05-12 17:56:33 +02:00
|
|
|
while (isgraph((int)*line))
|
|
|
|
{
|
2020-06-25 15:06:31 +01:00
|
|
|
/* If current key storage is too small,
|
|
|
|
* double its size */
|
2018-05-12 17:56:33 +02:00
|
|
|
if (idx == cur_size)
|
|
|
|
{
|
|
|
|
cur_size *= 2;
|
2019-04-26 06:28:10 +02:00
|
|
|
key_tmp = (char*)realloc(key, cur_size + 1);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (!key_tmp)
|
2019-04-26 06:28:10 +02:00
|
|
|
{
|
|
|
|
free(key);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
key = key_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
key[idx++] = *line++;
|
|
|
|
}
|
|
|
|
key[idx] = '\0';
|
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Add key and value entries to list */
|
|
|
|
list->key = key;
|
2020-08-25 12:39:28 +02:00
|
|
|
list->value = config_file_extract_value(line, true);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-06-25 15:06:31 +01:00
|
|
|
/* An entry without a value is invalid */
|
2018-05-12 17:56:33 +02:00
|
|
|
if (!list->value)
|
|
|
|
{
|
|
|
|
list->key = NULL;
|
2019-04-26 06:28:10 +02:00
|
|
|
free(key);
|
|
|
|
return false;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:38:12 +02:00
|
|
|
static int config_file_from_string_internal(
|
|
|
|
struct config_file *conf,
|
|
|
|
char *from_string,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
char *lines = from_string;
|
|
|
|
char *save_ptr = NULL;
|
|
|
|
char *line = NULL;
|
|
|
|
|
|
|
|
if (!string_is_empty(path))
|
|
|
|
conf->path = strdup(path);
|
|
|
|
if (string_is_empty(lines))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Get first line of config file */
|
|
|
|
line = strtok_r(lines, "\n", &save_ptr);
|
|
|
|
|
|
|
|
while (line)
|
|
|
|
{
|
|
|
|
struct config_entry_list *list = (struct config_entry_list*)
|
|
|
|
malloc(sizeof(*list));
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list->readonly = false;
|
|
|
|
list->key = NULL;
|
|
|
|
list->value = NULL;
|
|
|
|
list->next = NULL;
|
|
|
|
|
|
|
|
/* Parse current line */
|
|
|
|
if (
|
|
|
|
!string_is_empty(line)
|
|
|
|
&& config_file_parse_line(conf, list, line, NULL))
|
|
|
|
{
|
|
|
|
if (conf->entries)
|
|
|
|
conf->tail->next = list;
|
|
|
|
else
|
|
|
|
conf->entries = list;
|
|
|
|
|
|
|
|
conf->tail = list;
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
if (list->key)
|
|
|
|
{
|
|
|
|
/* Only add entry to the map if an entry
|
|
|
|
* with the specified value does not
|
|
|
|
* already exist */
|
2021-04-28 16:56:14 +01:00
|
|
|
uint32_t hash = rhmap_hash_string(list->key);
|
|
|
|
if (!RHMAP_HAS_FULL(conf->entries_map, hash, list->key))
|
|
|
|
RHMAP_SET_FULL(conf->entries_map, hash, list->key, list);
|
2021-04-21 17:23:18 +01:00
|
|
|
}
|
2020-08-25 18:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (list != conf->tail)
|
|
|
|
free(list);
|
|
|
|
|
|
|
|
/* Get next line of config file */
|
|
|
|
line = strtok_r(NULL, "\n", &save_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
void config_file_set_reference_path(config_file_t *conf, char *path)
|
|
|
|
{
|
2020-12-26 21:09:27 -05:00
|
|
|
/* It is expected that the conf has it's path already set */
|
|
|
|
|
|
|
|
char short_path[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
short_path[0] = '\0';
|
|
|
|
|
2020-11-02 21:07:20 -05:00
|
|
|
if (!conf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (conf->reference)
|
2020-11-01 09:18:40 -05:00
|
|
|
{
|
2020-11-02 21:07:20 -05:00
|
|
|
free(conf->reference);
|
|
|
|
conf->reference = NULL;
|
2020-11-01 09:18:40 -05:00
|
|
|
}
|
2020-11-02 21:07:20 -05:00
|
|
|
|
2020-12-26 21:09:27 -05:00
|
|
|
fill_pathname_abbreviated_or_relative(short_path, conf->path, path, sizeof(short_path));
|
|
|
|
|
|
|
|
conf->reference = strdup(short_path);
|
2020-11-01 09:18:40 -05:00
|
|
|
}
|
2020-08-25 18:38:12 +02:00
|
|
|
|
2020-08-25 17:54:22 +02:00
|
|
|
bool config_file_deinitialize(config_file_t *conf)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
|
|
|
struct config_include_list *inc_tmp = NULL;
|
2020-08-24 23:25:57 +02:00
|
|
|
struct config_entry_list *tmp = NULL;
|
|
|
|
if (!conf)
|
2020-08-25 17:54:22 +02:00
|
|
|
return false;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-08-24 23:25:57 +02:00
|
|
|
tmp = conf->entries;
|
2018-05-12 17:56:33 +02:00
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
struct config_entry_list *hold = NULL;
|
|
|
|
if (tmp->key)
|
|
|
|
free(tmp->key);
|
|
|
|
if (tmp->value)
|
|
|
|
free(tmp->value);
|
|
|
|
|
|
|
|
tmp->value = NULL;
|
|
|
|
tmp->key = NULL;
|
|
|
|
|
|
|
|
hold = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
|
|
|
|
if (hold)
|
|
|
|
free(hold);
|
|
|
|
}
|
|
|
|
|
|
|
|
inc_tmp = (struct config_include_list*)conf->includes;
|
|
|
|
while (inc_tmp)
|
|
|
|
{
|
|
|
|
struct config_include_list *hold = NULL;
|
2019-07-13 15:23:28 +02:00
|
|
|
if (inc_tmp->path)
|
|
|
|
free(inc_tmp->path);
|
2018-05-12 17:56:33 +02:00
|
|
|
hold = (struct config_include_list*)inc_tmp;
|
|
|
|
inc_tmp = inc_tmp->next;
|
2019-07-13 15:23:28 +02:00
|
|
|
if (hold)
|
|
|
|
free(hold);
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 21:07:20 -05:00
|
|
|
if (conf->reference)
|
|
|
|
free(conf->reference);
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
if (conf->path)
|
|
|
|
free(conf->path);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
|
|
|
RHMAP_FREE(conf->entries_map);
|
|
|
|
|
2020-08-25 17:54:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_file_free(config_file_t *conf)
|
|
|
|
{
|
|
|
|
if (!config_file_deinitialize(conf))
|
|
|
|
return;
|
2018-05-12 17:56:33 +02:00
|
|
|
free(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_append_file(config_file_t *conf, const char *path)
|
|
|
|
{
|
2019-07-18 16:15:02 +02:00
|
|
|
config_file_t *new_conf = config_file_new_from_path_to_string(path);
|
2021-04-21 17:23:18 +01:00
|
|
|
size_t i;
|
|
|
|
size_t cap;
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
if (!new_conf)
|
|
|
|
return false;
|
|
|
|
|
2021-04-21 17:23:18 +01:00
|
|
|
/* Update hash map */
|
|
|
|
for (i = 0, cap = RHMAP_CAP(new_conf->entries_map); i != cap; i++)
|
|
|
|
{
|
2021-04-29 12:29:47 +01:00
|
|
|
uint32_t new_hash = RHMAP_KEY(new_conf->entries_map, i);
|
|
|
|
const char *new_key = RHMAP_KEY_STR(new_conf->entries_map, i);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
2021-04-29 12:29:47 +01:00
|
|
|
if (new_hash && new_key)
|
|
|
|
{
|
|
|
|
struct config_entry_list *entry = new_conf->entries_map[i];
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
RHMAP_SET_FULL(conf->entries_map, new_hash, new_key, entry);
|
|
|
|
}
|
2021-04-21 17:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
if (new_conf->tail)
|
|
|
|
{
|
|
|
|
new_conf->tail->next = conf->entries;
|
|
|
|
conf->entries = new_conf->entries; /* Pilfer. */
|
|
|
|
new_conf->entries = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_file_free(new_conf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:37:14 +02:00
|
|
|
config_file_t *config_file_new_from_string(char *from_string,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
struct config_file *conf = config_file_new_alloc();
|
|
|
|
|
|
|
|
if (!conf)
|
|
|
|
return NULL;
|
|
|
|
if (config_file_from_string_internal(conf, from_string, path) == -1)
|
|
|
|
{
|
|
|
|
config_file_free(conf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-12 17:56:33 +02:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:03:50 +02:00
|
|
|
config_file_t *config_file_new_from_path_to_string(const char *path)
|
|
|
|
{
|
|
|
|
int64_t length = 0;
|
|
|
|
uint8_t *ret_buf = NULL;
|
|
|
|
config_file_t *conf = NULL;
|
|
|
|
|
2019-07-25 12:25:52 +01:00
|
|
|
if (path_is_valid(path))
|
2019-07-18 12:03:50 +02:00
|
|
|
{
|
2019-07-25 12:25:52 +01:00
|
|
|
if (filestream_read_file(path, (void**)&ret_buf, &length))
|
|
|
|
{
|
2020-06-25 15:06:31 +01:00
|
|
|
/* Note: 'ret_buf' is not used outside this
|
|
|
|
* function - we do not care that it will be
|
|
|
|
* modified by config_file_new_from_string() */
|
2019-07-25 12:25:52 +01:00
|
|
|
if (length >= 0)
|
2020-06-25 15:06:31 +01:00
|
|
|
conf = config_file_new_from_string((char*)ret_buf, path);
|
2020-08-25 18:37:14 +02:00
|
|
|
|
2019-07-25 12:25:52 +01:00
|
|
|
if ((void*)ret_buf)
|
|
|
|
free((void*)ret_buf);
|
|
|
|
}
|
2019-07-18 12:03:50 +02:00
|
|
|
}
|
2020-08-25 18:37:14 +02:00
|
|
|
|
2019-07-18 12:03:50 +02:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
config_file_t *config_file_new_with_callback(
|
|
|
|
const char *path, config_file_cb_t *cb)
|
2018-10-11 03:26:58 +02:00
|
|
|
{
|
2020-08-25 15:29:45 +02:00
|
|
|
int ret = 0;
|
|
|
|
struct config_file *conf = config_file_new_alloc();
|
|
|
|
if (!path || !*path)
|
|
|
|
return conf;
|
2020-08-25 15:36:26 +02:00
|
|
|
ret = config_file_load_internal(conf, path, 0, cb);
|
2020-08-25 15:29:45 +02:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
config_file_free(conf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (ret == 1)
|
|
|
|
{
|
|
|
|
free(conf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return conf;
|
2018-10-11 03:26:58 +02:00
|
|
|
}
|
2019-04-26 06:28:10 +02:00
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
config_file_t *config_file_new(const char *path)
|
|
|
|
{
|
2020-08-25 15:29:45 +02:00
|
|
|
int ret = 0;
|
|
|
|
struct config_file *conf = config_file_new_alloc();
|
|
|
|
if (!path || !*path)
|
|
|
|
return conf;
|
2020-08-25 15:36:26 +02:00
|
|
|
ret = config_file_load_internal(conf, path, 0, NULL);
|
2020-08-25 15:29:45 +02:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
config_file_free(conf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (ret == 1)
|
|
|
|
{
|
|
|
|
free(conf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return conf;
|
2020-08-24 23:25:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 17:54:22 +02:00
|
|
|
void config_file_initialize(struct config_file *conf)
|
|
|
|
{
|
|
|
|
if (!conf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
conf->path = NULL;
|
2021-04-21 17:23:18 +01:00
|
|
|
conf->entries_map = NULL;
|
2020-08-25 17:54:22 +02:00
|
|
|
conf->entries = NULL;
|
|
|
|
conf->tail = NULL;
|
|
|
|
conf->last = NULL;
|
2020-11-01 09:18:40 -05:00
|
|
|
conf->reference = NULL;
|
2020-08-25 17:54:22 +02:00
|
|
|
conf->includes = NULL;
|
|
|
|
conf->include_depth = 0;
|
|
|
|
conf->guaranteed_no_duplicates = false;
|
|
|
|
conf->modified = false;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:25:57 +02:00
|
|
|
config_file_t *config_file_new_alloc(void)
|
|
|
|
{
|
|
|
|
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
|
2020-08-24 22:57:08 +02:00
|
|
|
if (!conf)
|
|
|
|
return NULL;
|
2020-08-25 18:12:28 +02:00
|
|
|
config_file_initialize(conf);
|
2020-08-24 22:57:08 +02:00
|
|
|
return conf;
|
2019-04-22 01:13:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 02:17:37 +02:00
|
|
|
static struct config_entry_list *config_get_entry_internal(
|
2019-04-26 06:28:10 +02:00
|
|
|
const config_file_t *conf,
|
2018-05-12 17:56:33 +02:00
|
|
|
const char *key, struct config_entry_list **prev)
|
|
|
|
{
|
|
|
|
struct config_entry_list *entry = NULL;
|
|
|
|
struct config_entry_list *previous = prev ? *prev : NULL;
|
|
|
|
|
2021-04-28 16:56:14 +01:00
|
|
|
entry = RHMAP_GET_STR(conf->entries_map, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2021-04-21 17:23:18 +01:00
|
|
|
if (entry)
|
|
|
|
return entry;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (prev)
|
2021-04-21 17:23:18 +01:00
|
|
|
{
|
|
|
|
for (entry = conf->entries; entry; entry = entry->next)
|
|
|
|
previous = entry;
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
*prev = previous;
|
2021-04-21 17:23:18 +01:00
|
|
|
}
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:17:37 +02:00
|
|
|
struct config_entry_list *config_get_entry(
|
|
|
|
const config_file_t *conf, const char *key)
|
|
|
|
{
|
2021-04-28 16:56:14 +01:00
|
|
|
return RHMAP_GET_STR(conf->entries_map, key);
|
2020-08-26 02:17:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
bool config_get_double(config_file_t *conf, const char *key, double *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
if (!entry)
|
|
|
|
return false;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
*in = strtod(entry->value, NULL);
|
|
|
|
return true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_float(config_file_t *conf, const char *key, float *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
if (!entry)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* strtof() is C99/POSIX. Just use the more portable kind. */
|
|
|
|
*in = (float)strtod(entry->value, NULL);
|
|
|
|
return true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_int(config_file_t *conf, const char *key, int *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
int val = (int)strtol(entry->value, NULL, 0);
|
|
|
|
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
*in = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-13 16:12:09 -04:00
|
|
|
bool config_get_size_t(config_file_t *conf, const char *key, size_t *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-07-13 16:12:09 -04:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
2018-07-14 00:45:16 +02:00
|
|
|
size_t val = 0;
|
|
|
|
if (sscanf(entry->value, "%" PRI_SIZET, &val) == 1)
|
|
|
|
{
|
2018-07-13 16:12:09 -04:00
|
|
|
*in = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
#if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
|
|
|
|
bool config_get_uint64(config_file_t *conf, const char *key, uint64_t *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
uint64_t val = strtoull(entry->value, NULL, 0);
|
|
|
|
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
*in = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool config_get_uint(config_file_t *conf, const char *key, unsigned *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
unsigned val = (unsigned)strtoul(entry->value, NULL, 0);
|
|
|
|
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
*in = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_hex(config_file_t *conf, const char *key, unsigned *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
unsigned val = (unsigned)strtoul(entry->value, NULL, 16);
|
|
|
|
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
*in = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_char(config_file_t *conf, const char *key, char *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (entry->value[0] && entry->value[1])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*in = *entry->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_string(config_file_t *conf, const char *key, char **str)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
if (!entry || !entry->value)
|
2019-04-26 06:28:10 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*str = strdup(entry->value);
|
|
|
|
return true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_config_path(config_file_t *conf, char *s, size_t len)
|
|
|
|
{
|
|
|
|
if (!conf)
|
|
|
|
return false;
|
|
|
|
return strlcpy(s, conf->path, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_array(config_file_t *conf, const char *key,
|
|
|
|
char *buf, size_t size)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
if (entry)
|
|
|
|
return strlcpy(buf, entry->value, size) < size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_path(config_file_t *conf, const char *key,
|
|
|
|
char *buf, size_t size)
|
|
|
|
{
|
|
|
|
#if defined(RARCH_CONSOLE) || !defined(RARCH_INTERNAL)
|
|
|
|
if (config_get_array(conf, key, buf, size))
|
|
|
|
return true;
|
|
|
|
#else
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
fill_pathname_expand_special(buf, entry->value, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_bool(config_file_t *conf, const char *key, bool *in)
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
const struct config_entry_list *entry = config_get_entry(conf, key);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-08-26 01:37:57 +02:00
|
|
|
if (!entry)
|
|
|
|
return false;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-08-26 01:37:57 +02:00
|
|
|
if (
|
|
|
|
(
|
|
|
|
entry->value[0] == '1'
|
|
|
|
&& entry->value[1] == '\0'
|
|
|
|
)
|
|
|
|
|| string_is_equal(entry->value, "true")
|
|
|
|
)
|
|
|
|
*in = true;
|
|
|
|
else if (
|
|
|
|
(
|
|
|
|
entry->value[0] == '0'
|
|
|
|
&& entry->value[1] == '\0'
|
|
|
|
)
|
|
|
|
|| string_is_equal(entry->value, "false")
|
|
|
|
)
|
|
|
|
*in = false;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_string(config_file_t *conf, const char *key, const char *val)
|
|
|
|
{
|
2020-04-27 17:06:35 +01:00
|
|
|
struct config_entry_list *last = NULL;
|
|
|
|
struct config_entry_list *entry = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
if (!conf || !key || !val)
|
|
|
|
return;
|
|
|
|
|
2020-08-25 15:50:26 +02:00
|
|
|
last = conf->entries;
|
2020-04-27 17:06:35 +01:00
|
|
|
|
2020-08-25 15:50:26 +02:00
|
|
|
if (conf->guaranteed_no_duplicates)
|
2020-08-24 21:25:55 +02:00
|
|
|
{
|
2020-08-25 15:50:26 +02:00
|
|
|
if (conf->last)
|
|
|
|
last = conf->last;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-26 02:17:37 +02:00
|
|
|
entry = config_get_entry_internal(
|
|
|
|
conf, key, &last);
|
2020-08-25 15:50:26 +02:00
|
|
|
if (entry)
|
2020-04-27 17:06:35 +01:00
|
|
|
{
|
2020-08-25 15:50:26 +02:00
|
|
|
/* An entry corresponding to 'key' already exists
|
2020-11-09 11:18:49 +00:00
|
|
|
* > Check whether value is currently set */
|
2020-08-25 15:50:26 +02:00
|
|
|
if (entry->value)
|
|
|
|
{
|
|
|
|
/* Do nothing if value is unchanged */
|
|
|
|
if (string_is_equal(entry->value, val))
|
|
|
|
return;
|
2020-08-24 23:25:57 +02:00
|
|
|
|
2020-08-25 15:50:26 +02:00
|
|
|
/* Value is to be updated
|
|
|
|
* > Free existing */
|
|
|
|
free(entry->value);
|
|
|
|
}
|
|
|
|
|
2020-11-09 11:18:49 +00:00
|
|
|
/* Update value
|
|
|
|
* > Note that once a value is set, it
|
|
|
|
* is no longer considered 'read only' */
|
|
|
|
entry->value = strdup(val);
|
|
|
|
entry->readonly = false;
|
|
|
|
conf->modified = true;
|
2020-08-25 15:50:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-27 17:06:35 +01:00
|
|
|
}
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
/* Entry corresponding to 'key' does not exist
|
|
|
|
* > Create new entry */
|
2020-08-24 23:25:57 +02:00
|
|
|
entry = (struct config_entry_list*)malloc(sizeof(*entry));
|
2018-05-12 17:56:33 +02:00
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry->readonly = false;
|
|
|
|
entry->key = strdup(key);
|
|
|
|
entry->value = strdup(val);
|
|
|
|
entry->next = NULL;
|
2020-04-27 17:06:35 +01:00
|
|
|
conf->modified = true;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (last)
|
|
|
|
last->next = entry;
|
|
|
|
else
|
|
|
|
conf->entries = entry;
|
2018-10-11 03:27:06 +02:00
|
|
|
|
2019-04-26 06:28:10 +02:00
|
|
|
conf->last = entry;
|
2021-04-21 17:23:18 +01:00
|
|
|
|
2021-04-28 16:56:14 +01:00
|
|
|
RHMAP_SET_STR(conf->entries_map, entry->key, entry);
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void config_unset(config_file_t *conf, const char *key)
|
|
|
|
{
|
2020-04-27 17:06:35 +01:00
|
|
|
struct config_entry_list *last = NULL;
|
|
|
|
struct config_entry_list *entry = NULL;
|
|
|
|
|
|
|
|
if (!conf || !key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
last = conf->entries;
|
2020-08-26 02:17:37 +02:00
|
|
|
entry = config_get_entry_internal(conf, key, &last);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
2021-04-28 16:56:14 +01:00
|
|
|
(void)RHMAP_DEL_STR(conf->entries_map, entry->key);
|
2021-04-21 17:23:18 +01:00
|
|
|
|
2020-04-27 17:06:35 +01:00
|
|
|
if (entry->key)
|
|
|
|
free(entry->key);
|
|
|
|
|
|
|
|
if (entry->value)
|
|
|
|
free(entry->value);
|
|
|
|
|
|
|
|
entry->key = NULL;
|
|
|
|
entry->value = NULL;
|
|
|
|
conf->modified = true;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_path(config_file_t *conf, const char *entry, const char *val)
|
|
|
|
{
|
|
|
|
#if defined(RARCH_CONSOLE) || !defined(RARCH_INTERNAL)
|
|
|
|
config_set_string(conf, entry, val);
|
|
|
|
#else
|
|
|
|
char buf[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
fill_pathname_abbreviate_special(buf, val, sizeof(buf));
|
|
|
|
config_set_string(conf, entry, buf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_double(config_file_t *conf, const char *key, double val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[320];
|
2018-05-12 17:56:33 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
snprintf(buf, sizeof(buf), "%f", (float)val);
|
|
|
|
#elif defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
|
|
|
|
snprintf(buf, sizeof(buf), "%lf", val);
|
|
|
|
#else
|
|
|
|
snprintf(buf, sizeof(buf), "%f", (float)val);
|
|
|
|
#endif
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_float(config_file_t *conf, const char *key, float val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[64];
|
2018-05-12 17:56:33 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%f", val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_int(config_file_t *conf, const char *key, int val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[16];
|
2018-05-12 17:56:33 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%d", val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:19:14 -04:00
|
|
|
void config_set_uint(config_file_t *conf, const char *key, unsigned int val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[16];
|
2018-07-25 19:19:14 -04:00
|
|
|
snprintf(buf, sizeof(buf), "%u", val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
void config_set_hex(config_file_t *conf, const char *key, unsigned val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[16];
|
2018-05-12 17:56:33 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%x", val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_uint64(config_file_t *conf, const char *key, uint64_t val)
|
|
|
|
{
|
2020-08-25 20:58:55 +02:00
|
|
|
char buf[32];
|
2018-05-12 17:56:33 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%" PRIu64, val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_char(config_file_t *conf, const char *key, char val)
|
|
|
|
{
|
|
|
|
char buf[2];
|
|
|
|
snprintf(buf, sizeof(buf), "%c", val);
|
|
|
|
config_set_string(conf, key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_set_bool(config_file_t *conf, const char *key, bool val)
|
|
|
|
{
|
|
|
|
config_set_string(conf, key, val ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
2019-01-03 09:34:00 -08:00
|
|
|
bool config_file_write(config_file_t *conf, const char *path, bool sort)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
2020-04-27 17:06:35 +01:00
|
|
|
if (!conf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!conf->modified)
|
|
|
|
return true;
|
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
if (!string_is_empty(path))
|
|
|
|
{
|
2019-01-03 12:48:40 +01:00
|
|
|
#ifdef ORBIS
|
2019-04-26 06:28:10 +02:00
|
|
|
int fd = orbisOpen(path,O_RDWR|O_CREAT,0644);
|
2019-01-08 20:43:03 +01:00
|
|
|
if (fd < 0)
|
2019-01-03 12:48:40 +01:00
|
|
|
return false;
|
|
|
|
config_file_dump_orbis(conf,fd);
|
|
|
|
orbisClose(fd);
|
|
|
|
#else
|
2019-07-13 15:23:28 +02:00
|
|
|
void* buf = NULL;
|
2018-05-12 17:56:33 +02:00
|
|
|
FILE *file = (FILE*)fopen_utf8(path, "wb");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* TODO: this is only useful for a few platforms, find which and add ifdef */
|
2019-10-07 23:45:54 +02:00
|
|
|
#if !defined(PSP)
|
2018-05-12 17:56:33 +02:00
|
|
|
buf = calloc(1, 0x4000);
|
|
|
|
setvbuf(file, (char*)buf, _IOFBF, 0x4000);
|
2018-12-24 16:15:05 +01:00
|
|
|
#endif
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2019-01-03 09:34:00 -08:00
|
|
|
config_file_dump(conf, file, sort);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
if (file != stdout)
|
|
|
|
fclose(file);
|
2019-07-13 15:23:28 +02:00
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2019-01-03 12:48:40 +01:00
|
|
|
#endif
|
2020-04-27 17:06:35 +01:00
|
|
|
|
|
|
|
/* Only update modified flag if config file
|
|
|
|
* is actually written to disk */
|
|
|
|
conf->modified = false;
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
else
|
2019-01-03 09:34:00 -08:00
|
|
|
config_file_dump(conf, stdout, sort);
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:48:40 +01:00
|
|
|
#ifdef ORBIS
|
|
|
|
void config_file_dump_orbis(config_file_t *conf, int fd)
|
|
|
|
{
|
|
|
|
struct config_entry_list *list = NULL;
|
|
|
|
struct config_include_list *includes = conf->includes;
|
2020-11-10 17:05:23 -05:00
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
if (conf->reference)
|
2020-12-26 21:09:27 -05:00
|
|
|
{
|
|
|
|
pathname_make_slashes_portable(conf->reference);
|
2020-11-01 09:18:40 -05:00
|
|
|
fprintf(file, "#reference \"%s\"\n", conf->reference);
|
2020-12-26 21:09:27 -05:00
|
|
|
}
|
2020-11-01 09:18:40 -05:00
|
|
|
|
2018-05-12 17:56:33 +02:00
|
|
|
|
2020-08-25 12:39:28 +02:00
|
|
|
list = config_file_merge_sort_linked_list(
|
|
|
|
(struct config_entry_list*)conf->entries,
|
|
|
|
config_file_sort_compare_func);
|
2019-01-03 12:48:40 +01:00
|
|
|
conf->entries = list;
|
|
|
|
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
if (!list->readonly && list->key)
|
|
|
|
{
|
|
|
|
char newlist[256];
|
2020-08-24 19:39:41 +02:00
|
|
|
snprintf(newlist, sizeof(newlist),
|
|
|
|
"%s = %s\n", list->key, list->value);
|
2019-04-27 04:21:10 +02:00
|
|
|
orbisWrite(fd, newlist, strlen(newlist));
|
2019-01-03 12:48:40 +01:00
|
|
|
}
|
|
|
|
list = list->next;
|
|
|
|
}
|
2020-11-09 11:18:49 +00:00
|
|
|
|
|
|
|
/* Config files are read from the top down - if
|
|
|
|
* duplicate entries are found then the topmost
|
|
|
|
* one in the list takes precedence. This means
|
|
|
|
* '#include' directives must go *after* individual
|
|
|
|
* config entries, otherwise they will override
|
|
|
|
* any custom-set values */
|
|
|
|
while (includes)
|
|
|
|
{
|
|
|
|
char cad[256];
|
|
|
|
snprintf(cad, sizeof(cad),
|
|
|
|
"#include %s\n", includes->path);
|
|
|
|
orbisWrite(fd, cad, strlen(cad));
|
|
|
|
includes = includes->next;
|
|
|
|
}
|
2019-01-03 12:48:40 +01:00
|
|
|
}
|
|
|
|
#endif
|
2019-01-08 20:18:08 +01:00
|
|
|
|
2019-01-03 09:34:00 -08:00
|
|
|
void config_file_dump(config_file_t *conf, FILE *file, bool sort)
|
2018-05-12 17:56:33 +02:00
|
|
|
{
|
|
|
|
struct config_entry_list *list = NULL;
|
|
|
|
struct config_include_list *includes = conf->includes;
|
|
|
|
|
2020-11-01 09:18:40 -05:00
|
|
|
if (conf->reference)
|
2020-12-26 21:09:27 -05:00
|
|
|
{
|
|
|
|
pathname_make_slashes_portable(conf->reference);
|
2020-11-01 09:18:40 -05:00
|
|
|
fprintf(file, "#reference \"%s\"\n", conf->reference);
|
2020-12-26 21:09:27 -05:00
|
|
|
}
|
2020-11-01 09:18:40 -05:00
|
|
|
|
2019-01-03 09:34:00 -08:00
|
|
|
if (sort)
|
2020-08-25 12:39:28 +02:00
|
|
|
list = config_file_merge_sort_linked_list(
|
|
|
|
(struct config_entry_list*)conf->entries,
|
|
|
|
config_file_sort_compare_func);
|
2019-01-03 09:34:00 -08:00
|
|
|
else
|
|
|
|
list = (struct config_entry_list*)conf->entries;
|
|
|
|
|
2018-09-03 13:31:46 -04:00
|
|
|
conf->entries = list;
|
2018-05-12 17:56:33 +02:00
|
|
|
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
if (!list->readonly && list->key)
|
|
|
|
fprintf(file, "%s = \"%s\"\n", list->key, list->value);
|
|
|
|
list = list->next;
|
|
|
|
}
|
2020-11-09 11:18:49 +00:00
|
|
|
|
|
|
|
/* Config files are read from the top down - if
|
|
|
|
* duplicate entries are found then the topmost
|
|
|
|
* one in the list takes precedence. This means
|
|
|
|
* '#include' directives must go *after* individual
|
|
|
|
* config entries, otherwise they will override
|
|
|
|
* any custom-set values */
|
|
|
|
while (includes)
|
|
|
|
{
|
|
|
|
fprintf(file, "#include \"%s\"\n", includes->path);
|
|
|
|
includes = includes->next;
|
|
|
|
}
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool config_entry_exists(config_file_t *conf, const char *entry)
|
|
|
|
{
|
2021-04-28 16:56:14 +01:00
|
|
|
return (bool)RHMAP_HAS_STR(conf->entries_map, entry);
|
2018-05-12 17:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_entry_list_head(config_file_t *conf,
|
|
|
|
struct config_file_entry *entry)
|
|
|
|
{
|
|
|
|
const struct config_entry_list *head = conf->entries;
|
|
|
|
|
|
|
|
if (!head)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
entry->key = head->key;
|
|
|
|
entry->value = head->value;
|
|
|
|
entry->next = head->next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_get_entry_list_next(struct config_file_entry *entry)
|
|
|
|
{
|
|
|
|
const struct config_entry_list *next = entry->next;
|
|
|
|
|
|
|
|
if (!next)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
entry->key = next->key;
|
|
|
|
entry->value = next->value;
|
|
|
|
entry->next = next->next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool config_file_exists(const char *path)
|
|
|
|
{
|
2020-08-25 18:10:32 +02:00
|
|
|
config_file_t conf;
|
|
|
|
config_file_initialize(&conf);
|
|
|
|
if (config_file_load_internal(&conf, path, 0, NULL) == 1)
|
2018-05-12 17:56:33 +02:00
|
|
|
return false;
|
|
|
|
|
2020-08-25 18:10:32 +02:00
|
|
|
config_file_deinitialize(&conf);
|
2018-05-12 17:56:33 +02:00
|
|
|
return true;
|
|
|
|
}
|