Unverified Commit 1d7c15ad by Edward Thomson Committed by GitHub

Merge pull request #4310 from pks-t/pks/common-parser

Common parser interface
parents 46e1dabb 9e66590b
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "array.h"
#include "oid.h"
#include "parse.h"
static const char *git_config_escapes = "ntb\"\\";
static const char *git_config_escaped = "\n\t\b\"\\";
typedef struct config_file {
git_oid checksum;
char *path;
git_array_t(struct config_file) includes;
} git_config_file;
typedef struct {
struct config_file *file;
git_parse_ctx ctx;
} git_config_parser;
typedef int (*git_config_parser_section_cb)(
git_config_parser *parser,
const char *current_section,
const char *line,
size_t line_len,
void *data);
typedef int (*git_config_parser_variable_cb)(
git_config_parser *parser,
const char *current_section,
char *var_name,
char *var_value,
const char *line,
size_t line_len,
void *data);
typedef int (*git_config_parser_comment_cb)(
git_config_parser *parser,
const char *line,
size_t line_len,
void *data);
typedef int (*git_config_parser_eof_cb)(
git_config_parser *parser,
const char *current_section,
void *data);
int git_config_parse(
git_config_parser *parser,
git_config_parser_section_cb on_section,
git_config_parser_variable_cb on_variable,
git_config_parser_comment_cb on_comment,
git_config_parser_eof_cb on_eof,
void *data);
......@@ -83,7 +83,7 @@ int git_diff_from_buffer(
ctx = git_patch_parse_ctx_init(content, content_len, NULL);
GITERR_CHECK_ALLOC(ctx);
while (ctx->remain_len) {
while (ctx->parse_ctx.remain_len) {
if ((error = git_patch_parse(&patch, ctx)) < 0)
break;
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "parse.h"
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len)
{
if (content_len)
ctx->content = content;
else
ctx->content = NULL;
ctx->content_len = content_len;
ctx->remain = ctx->content;
ctx->remain_len = ctx->content_len;
ctx->line = ctx->remain;
ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
ctx->line_num = 1;
return 0;
}
void git_parse_ctx_clear(git_parse_ctx *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}
void git_parse_advance_line(git_parse_ctx *ctx)
{
ctx->line += ctx->line_len;
ctx->remain_len -= ctx->line_len;
ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
ctx->line_num++;
}
void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt)
{
ctx->line += char_cnt;
ctx->remain_len -= char_cnt;
ctx->line_len -= char_cnt;
}
int git_parse_advance_expected(
git_parse_ctx *ctx,
const char *expected,
size_t expected_len)
{
if (ctx->line_len < expected_len)
return -1;
if (memcmp(ctx->line, expected, expected_len) != 0)
return -1;
git_parse_advance_chars(ctx, expected_len);
return 0;
}
int git_parse_advance_ws(git_parse_ctx *ctx)
{
int ret = -1;
while (ctx->line_len > 0 &&
ctx->line[0] != '\n' &&
git__isspace(ctx->line[0])) {
ctx->line++;
ctx->line_len--;
ctx->remain_len--;
ret = 0;
}
return ret;
}
int git_parse_advance_nl(git_parse_ctx *ctx)
{
if (ctx->line_len != 1 || ctx->line[0] != '\n')
return -1;
git_parse_advance_line(ctx);
return 0;
}
int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base)
{
const char *end;
int ret;
if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
return -1;
if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0)
return -1;
git_parse_advance_chars(ctx, (end - ctx->line));
return 0;
}
int git_parse_peek(char *out, git_parse_ctx *ctx, int flags)
{
size_t remain = ctx->line_len;
const char *ptr = ctx->line;
while (remain) {
char c = *ptr;
if ((flags & GIT_PARSE_PEEK_SKIP_WHITESPACE) &&
git__isspace(c)) {
remain--;
ptr++;
continue;
}
*out = c;
return 0;
}
return -1;
}
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
typedef struct {
/* Original content buffer */
const char *content;
size_t content_len;
/* The remaining (unparsed) buffer */
const char *remain;
size_t remain_len;
const char *line;
size_t line_len;
size_t line_num;
} git_parse_ctx;
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);
#define git_parse_err(...) \
( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
#define git_parse_ctx_contains_s(ctx, str) \
git_parse_ctx_contains(ctx, str, sizeof(str) - 1)
GIT_INLINE(bool) git_parse_ctx_contains(
git_parse_ctx *ctx, const char *str, size_t len)
{
return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
}
void git_parse_advance_line(git_parse_ctx *ctx);
void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt);
int git_parse_advance_expected(
git_parse_ctx *ctx,
const char *expected,
size_t expected_len);
#define git_parse_advance_expected_str(ctx, str) \
git_parse_advance_expected(ctx, str, strlen(str))
int git_parse_advance_ws(git_parse_ctx *ctx);
int git_parse_advance_nl(git_parse_ctx *ctx);
int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base);
enum GIT_PARSE_PEEK_FLAGS {
GIT_PARSE_PEEK_SKIP_WHITESPACE = (1 << 0)
};
int git_parse_peek(char *out, git_parse_ctx *ctx, int flags);
......@@ -9,24 +9,15 @@
#include "common.h"
#include "parse.h"
#include "patch.h"
typedef struct {
git_refcount rc;
/* Original content buffer */
const char *content;
size_t content_len;
git_patch_options opts;
/* The remaining (unparsed) buffer */
const char *remain;
size_t remain_len;
const char *line;
size_t line_len;
size_t line_num;
git_parse_ctx parse_ctx;
} git_patch_parse_ctx;
extern git_patch_parse_ctx *git_patch_parse_ctx_init(
......
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