summaryrefslogtreecommitdiffstats
path: root/sub
diff options
context:
space:
mode:
authorUoti Urpala <uau@glyph.nonexistent.invalid>2011-02-25 18:10:00 +0200
committerUoti Urpala <uau@glyph.nonexistent.invalid>2011-03-03 21:39:24 +0200
commit4c117849b343109666c7a25b6fa815ce63d0175f (patch)
treed890c23070556c804304435bf55a7f32cc15dd0d /sub
parent5e0a163886f825983c5bfcd4181e6498b0f3e0f2 (diff)
downloadmpv-4c117849b343109666c7a25b6fa815ce63d0175f.tar.bz2
mpv-4c117849b343109666c7a25b6fa815ce63d0175f.tar.xz
stream.[ch], ass_mp: new stream function for whole-file reads
Add new stream_read_complete() function which reads the complete contents of file. Use that in ass_mp.c which had custom code to do the same.
Diffstat (limited to 'sub')
-rw-r--r--sub/ass_mp.c46
1 files changed, 12 insertions, 34 deletions
diff --git a/sub/ass_mp.c b/sub/ass_mp.c
index 98602ace03..78f607bc7a 100644
--- a/sub/ass_mp.c
+++ b/sub/ass_mp.c
@@ -233,50 +233,28 @@ ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata,
ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
char *charset)
{
- int i;
- char *buf = NULL;
ASS_Track *track;
- size_t sz = 0;
- size_t buf_alloc = 0;
- stream_t *fd;
- fd = open_stream(fname, NULL, NULL);
- if (!fd)
+ struct stream *s = open_stream(fname, NULL, NULL);
+ if (!s)
// Stream code should have printed an error already
return NULL;
- if (fd->end_pos > STREAM_BUFFER_SIZE)
- /* read entire file if size is known */
- buf_alloc = fd->end_pos;
- else
- buf_alloc = 1000;
- for (;;) {
- if (sz > 100000000) {
- mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
- "larger than 100 MB: %s\n", fname);
- sz = 0;
- break;
- }
- buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1));
- buf_alloc = FFMIN(buf_alloc, 100000001);
- buf = realloc(buf, buf_alloc + 1);
- i = stream_read(fd, buf + sz, buf_alloc - sz);
- if (i <= 0)
- break;
- sz += i;
- }
- free_stream(fd);
- if (!sz) {
- free(buf);
+ struct bstr content = stream_read_complete(s, NULL, 100000000, 1);
+ if (content.start == NULL)
+ mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
+ "larger than 100 MB: %s\n", fname);
+ free_stream(s);
+ if (content.len == 0) {
+ talloc_free(content.start);
return NULL;
}
- buf[sz] = 0;
- buf = realloc(buf, sz + 1);
- track = ass_read_memory(library, buf, sz, charset);
+ content.start[content.len] = 0;
+ track = ass_read_memory(library, content.start, content.len, charset);
if (track) {
free(track->name);
track->name = strdup(fname);
}
- free(buf);
+ talloc_free(content.start);
return track;
}