2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
9 int git_parse_ctx_init(git_parse_ctx
*ctx
, const char *content
, size_t content_len
)
11 if (content
&& content_len
) {
12 ctx
->content
= content
;
13 ctx
->content_len
= content_len
;
19 ctx
->remain
= ctx
->content
;
20 ctx
->remain_len
= ctx
->content_len
;
21 ctx
->line
= ctx
->remain
;
22 ctx
->line_len
= git__linenlen(ctx
->line
, ctx
->remain_len
);
28 void git_parse_ctx_clear(git_parse_ctx
*ctx
)
30 memset(ctx
, 0, sizeof(*ctx
));
34 void git_parse_advance_line(git_parse_ctx
*ctx
)
36 ctx
->line
+= ctx
->line_len
;
37 ctx
->remain_len
-= ctx
->line_len
;
38 ctx
->line_len
= git__linenlen(ctx
->line
, ctx
->remain_len
);
42 void git_parse_advance_chars(git_parse_ctx
*ctx
, size_t char_cnt
)
44 ctx
->line
+= char_cnt
;
45 ctx
->remain_len
-= char_cnt
;
46 ctx
->line_len
-= char_cnt
;
49 int git_parse_advance_expected(
54 if (ctx
->line_len
< expected_len
)
57 if (memcmp(ctx
->line
, expected
, expected_len
) != 0)
60 git_parse_advance_chars(ctx
, expected_len
);
64 int git_parse_advance_ws(git_parse_ctx
*ctx
)
68 while (ctx
->line_len
> 0 &&
69 ctx
->line
[0] != '\n' &&
70 git__isspace(ctx
->line
[0])) {
80 int git_parse_advance_nl(git_parse_ctx
*ctx
)
82 if (ctx
->line_len
!= 1 || ctx
->line
[0] != '\n')
85 git_parse_advance_line(ctx
);
89 int git_parse_advance_digit(int64_t *out
, git_parse_ctx
*ctx
, int base
)
94 if (ctx
->line_len
< 1 || !git__isdigit(ctx
->line
[0]))
97 if ((ret
= git__strntol64(out
, ctx
->line
, ctx
->line_len
, &end
, base
)) < 0)
100 git_parse_advance_chars(ctx
, (end
- ctx
->line
));
104 int git_parse_peek(char *out
, git_parse_ctx
*ctx
, int flags
)
106 size_t remain
= ctx
->line_len
;
107 const char *ptr
= ctx
->line
;
112 if ((flags
& GIT_PARSE_PEEK_SKIP_WHITESPACE
) &&