Skip to content

Commit

Permalink
player/javascript: use stream_read_file for af_push_file
Browse files Browse the repository at this point in the history
Remove the complicated fopen/realloc logic.
  • Loading branch information
na-na-hi authored and kasper93 committed Oct 20, 2024
1 parent 607997d commit ca7006a
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions player/javascript.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,14 @@ static void af_push_file(js_State *J, const char *fname, int limit, void *af)
return;
}

FILE *f = fopen(filename, "rb");
if (!f)
bstr data = stream_read_file(filename, NULL, jctx(J)->mpctx->global, limit);
if (data.start) {
js_pushlstring(J, data.start, data.len);
} else {
js_error(J, "cannot open file: '%s'", filename);
add_af_file(af, f);

int len = MPMIN(limit, 32 * 1024); // initial allocation, size*2 strategy
int got = 0;
char *s = NULL;
while ((s = talloc_realloc(af, s, char, len))) {
int want = len - got;
int r = fread(s + got, 1, want, f);

if (feof(f) || (len == limit && r == want)) {
js_pushlstring(J, s, got + r);
return;
}
if (r != want)
js_error(J, "cannot read data from file: '%s'", filename);

got = got + r;
len = MPMIN(limit, len * 2);
}

js_error(J, "cannot allocate %d bytes for file: '%s'", len, filename);
talloc_free(data.start);
}

// Safely run af_push_file.
Expand Down

0 comments on commit ca7006a

Please sign in to comment.