Commit dbeadf8a by Patrick Steinhardt

config_parse: provide parser init and dispose functions

Right now, all configuration file backends are expected to
directly mess with the configuration parser's internals in order
to set it up. Let's avoid doing that by implementing both a
`git_config_parser_init` and `git_config_parser_dispose` function
to clearly define the interface between configuration backends
and the parser.

Ideally, we would make the `git_config_parser` structure
definition private to its implementation. But as that would
require an additional memory allocation that was not required
before we just live with it being visible to others.
parent 32157526
......@@ -1176,9 +1176,9 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
{
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT;
git_config_parser parser = GIT_CONFIG_PARSER_INIT;
git_filebuf file = GIT_FILEBUF_INIT;
struct write_data write_data;
git_config_parser reader;
int error;
memset(&write_data, 0, sizeof(write_data));
......@@ -1196,8 +1196,8 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
if (error < 0 && error != GIT_ENOTFOUND)
goto done;
reader.path = cfg->file.path;
git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0)
goto done;
ldot = strrchr(key, '.');
name = ldot + 1;
......@@ -1217,7 +1217,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
write_data.preg = preg;
write_data.value = value;
if ((error = git_config_parse(&reader, write_on_section, write_on_variable,
if ((error = git_config_parse(&parser, write_on_section, write_on_variable,
write_on_comment, write_on_eof, &write_data)) < 0)
goto done;
......@@ -1243,7 +1243,7 @@ done:
git_buf_dispose(&buf);
git_buf_dispose(&contents);
git_filebuf_cleanup(&file);
git_parse_ctx_clear(&reader.ctx);
git_config_parser_dispose(&parser);
return error;
}
......@@ -78,20 +78,24 @@ static int read_variable_cb(
static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo)
{
config_memory_backend *memory_backend = (config_memory_backend *) backend;
git_config_parser parser = GIT_PARSE_CTX_INIT;
config_memory_parse_data parse_data;
git_config_parser reader;
int error;
GIT_UNUSED(repo);
if (memory_backend->cfg.size == 0)
return 0;
git_parse_ctx_init(&reader.ctx, memory_backend->cfg.ptr, memory_backend->cfg.size);
reader.path = "in-memory";
if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr,
memory_backend->cfg.size)) < 0)
goto out;
parse_data.entries = memory_backend->entries;
parse_data.level = level;
return git_config_parse(&reader, NULL, read_variable_cb, NULL, NULL, &parse_data);
if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0)
goto out;
out:
git_config_parser_dispose(&parser);
return error;
}
static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out)
......
......@@ -474,6 +474,17 @@ out:
return error;
}
int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen)
{
out->path = path;
return git_parse_ctx_init(&out->ctx, data, datalen);
}
void git_config_parser_dispose(git_config_parser *parser)
{
git_parse_ctx_clear(&parser->ctx);
}
int git_config_parse(
git_config_parser *parser,
git_config_parser_section_cb on_section,
......
......@@ -22,6 +22,8 @@ typedef struct {
git_parse_ctx ctx;
} git_config_parser;
#define GIT_CONFIG_PARSER_INIT { NULL, GIT_PARSE_CTX_INIT }
typedef int (*git_config_parser_section_cb)(
git_config_parser *parser,
const char *current_section,
......@@ -49,6 +51,9 @@ typedef int (*git_config_parser_eof_cb)(
const char *current_section,
void *payload);
int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen);
void git_config_parser_dispose(git_config_parser *parser);
int git_config_parse(
git_config_parser *parser,
git_config_parser_section_cb on_section,
......
......@@ -23,6 +23,8 @@ typedef struct {
size_t line_num;
} git_parse_ctx;
#define GIT_PARSE_CTX_INIT { 0 }
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
void git_parse_ctx_clear(git_parse_ctx *ctx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment