[vlc-devel] [PATCH] demux: json: add json_reader context
Francois Cartegnie
fcvlcdev at free.fr
Wed Oct 14 16:52:52 CEST 2020
---
modules/demux/json/grammar.y | 14 ++++++++------
modules/demux/json/json.h | 11 ++++++++---
modules/demux/json/lexicon.l | 3 ++-
modules/demux/ytdl.c | 13 ++++++++++---
4 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/modules/demux/json/grammar.y b/modules/demux/json/grammar.y
index 3f8eb0a236..92228faf58 100644
--- a/modules/demux/json/grammar.y
+++ b/modules/demux/json/grammar.y
@@ -146,11 +146,13 @@ static void json_append(struct json_object *o, struct json_member m)
%{
-static void yyerror(void *log, void *scanner, struct json_object *result,
+static void yyerror(void *priv, void *scanner, struct json_object *result,
const char *msg)
{
- json_parse_error(log, msg);
- (void) scanner; (void) result;
+ struct json_reader *reader = priv;
+ if(reader->logger)
+ reader->logger(reader->priv, result, msg);
+ (void) scanner;
}
extern int yylex_init_extra(void *, void **);
@@ -213,15 +215,15 @@ value:
%%
-int json_parse(void *opaque, struct json_object *result)
+int json_parse(struct json_reader *reader, struct json_object *result)
{
void *scanner;
- int ret = yylex_init_extra(opaque, &scanner);
+ int ret = yylex_init_extra(reader, &scanner);
if (ret)
return ret;
- ret = yyparse(opaque, scanner, result);
+ ret = yyparse(reader, scanner, result);
yylex_destroy(scanner);
return ret;
}
diff --git a/modules/demux/json/json.h b/modules/demux/json/json.h
index 8274ceb652..bc254693f3 100644
--- a/modules/demux/json/json.h
+++ b/modules/demux/json/json.h
@@ -59,11 +59,16 @@ struct json_member {
struct json_value value;
};
-size_t json_read(void *data, void *buf, size_t max);
-void json_parse_error(void *log, const char *msg);
+struct json_reader {
+ void *priv;
+ ssize_t (*read)(void *, void *, size_t);
+ void (*logger)(void *, const struct json_object *, const char *);
+};
+
char *json_unescape(const char *, size_t);
-int json_parse(void *log, struct json_object *result);
+int json_parse(struct json_reader *, struct json_object *result);
+
void json_free(struct json_object *);
const struct json_value *json_get(const struct json_object *obj,
diff --git a/modules/demux/json/lexicon.l b/modules/demux/json/lexicon.l
index 51780dad49..8e5e2d2d3a 100644
--- a/modules/demux/json/lexicon.l
+++ b/modules/demux/json/lexicon.l
@@ -40,7 +40,8 @@
#define YY_INPUT(buf,result,size) \
{ \
- size_t len = json_read(yyextra, buf, size); \
+ struct json_reader *reader = yyextra;\
+ ssize_t len = reader->read(reader->priv, buf, size); \
result = (len > 0) ? len : YY_NULL; \
}
diff --git a/modules/demux/ytdl.c b/modules/demux/ytdl.c
index 54c722a783..d33a8e9875 100644
--- a/modules/demux/ytdl.c
+++ b/modules/demux/ytdl.c
@@ -43,14 +43,16 @@ struct ytdl_json {
int fd;
};
-void json_parse_error(void *data, const char *msg)
+static void json_parse_error(void *data, const struct json_object *value,
+ const char *msg)
{
struct ytdl_json *sys = data;
vlc_error(sys->logger, "%s", msg);
+ VLC_UNUSED(value);
}
-size_t json_read(void *data, void *buf, size_t size)
+static ssize_t json_read(void *data, void *buf, size_t size)
{
struct ytdl_json *sys = data;
@@ -364,7 +366,12 @@ static int OpenCommon(vlc_object_t *obj)
free(path);
- int val = json_parse(&jsdata, &sys->json);
+ struct json_reader reader = {
+ .priv = &jsdata,
+ .logger = json_parse_error,
+ .read = json_read
+ };
+ int val = json_parse(&reader, &sys->json);
kill(pid, SIGTERM);
vlc_close(jsdata.fd);
--
2.26.2
More information about the vlc-devel
mailing list