diff options
author | Thomas Weißschuh <thomas@t-8ch.de> | 2023-02-24 03:04:52 +0000 |
---|---|---|
committer | Dudemanguy <random342@airmail.cc> | 2023-04-29 22:34:54 +0000 |
commit | 6d422b3edc00a36dde5e1b073a07938d98720f3a (patch) | |
tree | b1355c6e076da2b6fe9bb297a87e66abe9c0afe3 /options/parse_configfile.c | |
parent | 7668361685bcd1ab8bfa036c91b731dd86725831 (diff) | |
download | mpv-6d422b3edc00a36dde5e1b073a07938d98720f3a.tar.bz2 mpv-6d422b3edc00a36dde5e1b073a07938d98720f3a.tar.xz |
options: read config file as stream
This aligns the possible sources of config files other loaded data.
For example `--input-conf`.
Diffstat (limited to 'options/parse_configfile.c')
-rw-r--r-- | options/parse_configfile.c | 41 |
1 files changed, 10 insertions, 31 deletions
diff --git a/options/parse_configfile.c b/options/parse_configfile.c index 0058806fdb..edd6be918c 100644 --- a/options/parse_configfile.c +++ b/options/parse_configfile.c @@ -29,6 +29,7 @@ #include "misc/ctype.h" #include "m_option.h" #include "m_config.h" +#include "stream/stream.h" // Skip whitespace and comments (assuming there are no line breaks) static bool skip_ws(bstr *s) @@ -149,51 +150,29 @@ int m_config_parse(m_config_t *config, const char *location, bstr data, return 1; } -static bstr read_file(struct mp_log *log, const char *filename) -{ - FILE *f = fopen(filename, "rb"); - if (!f) { - mp_verbose(log, "Can't open config file: %s\n", mp_strerror(errno)); - return (bstr){0}; - } - char *data = talloc_array(NULL, char, 0); - size_t size = 0; - while (1) { - size_t left = talloc_get_size(data) - size; - if (!left) { - MP_TARRAY_GROW(NULL, data, size + 1); - continue; - } - size_t s = fread(data + size, 1, left, f); - if (!s) { - if (ferror(f)) - mp_err(log, "Error reading config file.\n"); - fclose(f); - MP_TARRAY_APPEND(NULL, data, size, 0); - return (bstr){data, size - 1}; - } - size += s; - } - MP_ASSERT_UNREACHABLE(); -} - // Load options and profiles from a config file. // conffile: path to the config file // initial_section: default section where to add normal options // flags: M_SETOPT_* bits // returns: 1 on success, -1 on error, 0 if file not accessible. -int m_config_parse_config_file(m_config_t *config, const char *conffile, - char *initial_section, int flags) +int m_config_parse_config_file(m_config_t *config, struct mpv_global *global, + const char *conffile, char *initial_section, + int flags) { flags = flags | M_SETOPT_FROM_CONFIG_FILE; MP_VERBOSE(config, "Reading config file %s\n", conffile); - bstr data = read_file(config->log, conffile); + struct stream *s = stream_create(conffile, STREAM_READ | STREAM_ORIGIN_DIRECT, + NULL, global); + if (!s) + return 0; + bstr data = stream_read_complete(s, s, 1000000000); if (!data.start) return 0; int r = m_config_parse(config, conffile, data, initial_section, flags); talloc_free(data.start); + free_stream(s); return r; } |